{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import numpy.linalg as la" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "A = np.random.randn(5, 3)\n", "b = np.random.randn(5)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve $Ax\\cong b$ using the normal equations:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x1 = ...\n", "x1" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "array([ 0.23982628, -0.26470109, -0.18776136])" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "(Edit this cell for solution.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve $Ax\\cong b$ using the pseudoinverse:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "U, sigma, VT = la.svd(A)\n", "print U\n", "print sigma\n", "print VT" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[-0.40467972 -0.33714129 -0.63422814 0.20634795 0.52701543]\n", " [ 0.42804578 0.47808154 0.02477487 -0.195988 0.74107338]\n", " [-0.06483149 -0.50603238 0.15333028 -0.83497764 0.13795024]\n", " [ 0.80377855 -0.49127211 -0.27409423 0.1695712 -0.09332588]\n", " [-0.0524219 -0.40044568 0.70604748 0.4393941 0.38121514]]\n", "[ 2.93921603 2.18290554 1.39834826]\n", "[[-0.75141123 0.65519253 -0.07812746]\n", " [ 0.62293553 0.74344157 0.24340493]\n", " [-0.21756029 -0.13422882 0.96677306]]\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "Sigma_inv = np.zeros_like(A.T)\n", "Sigma_inv[:3,:3] = np.diag(1/sigma)\n", "Sigma_inv" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "array([[ 0.34022678, 0. , 0. , 0. , 0. ],\n", " [ 0. , 0.45810503, 0. , 0. , 0. ],\n", " [ 0. , 0. , 0.71512943, 0. , 0. ]])" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "pinv = ...\n", "x2 = np.dot(pinv, b)\n", "x2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "array([ 0.23982628, -0.26470109, -0.18776136])" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "(Edit this cell for solution.)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "la.norm(x1-x2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "1.7554167342883506e-16" ] } ], "prompt_number": 8 } ], "metadata": {} } ] }