From ef22d7e62a41318419ed36628b9b95e825f82108 Mon Sep 17 00:00:00 2001 From: Roger Labbe Date: Wed, 7 May 2014 13:33:10 -0700 Subject: [PATCH] Removed use of --pylab inline, as I mistakenly thought that was the best way to get matplot lib to be inline. Also, imported print_function from future to try to be 3.X compliant. --- Gaussians.ipynb | 38 +++-- Kalman Filters.ipynb | 194 +++++++++++--------------- Multidimensional Kalman Filters.ipynb | 112 +++++++-------- histogram_filter.ipynb | 143 ++++++++----------- 4 files changed, 215 insertions(+), 272 deletions(-) diff --git a/Gaussians.ipynb b/Gaussians.ipynb index 7bb2dfc..38c379b 100644 --- a/Gaussians.ipynb +++ b/Gaussians.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:7c1d5ebfcacf654b0862f976d6da64ba34c3743b23833736a6b3e7efb53ba119" + "signature": "sha256:167e76515c3a54031a76bf820666d9a7a52a0f919f127a68ea60c5e5e7821e63" }, "nbformat": 3, "nbformat_minor": 0, @@ -48,24 +48,24 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", "\n", - "xs = arange(0,10,0.1)\n", + "xs = np.arange(0,10,0.1)\n", "ys = [gaussian (x, 5, 3) for x in xs]\n", - "plot (xs, ys)\n", - "show()" + "plt.plot (xs, ys)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -103,7 +103,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "xs = arange(16,30,0.1)\n", + "xs = np.arange(16,30,0.1)\n", "ys = [gaussian (x,23,1) for x in xs]\n", "plt.plot (xs,ys, 'r')\n", "plt.axvline(23); plt.axvline(24) \n", @@ -111,8 +111,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -140,8 +139,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -177,18 +175,17 @@ " return math.exp (-0.5 * (x-mu)**2 / sigma) / math.sqrt(2.*math.pi*sigma)\n", "\n", "def plt_g (mu,variance):\n", - " xs = arange(0,10,0.15)\n", + " xs = np.arange(0,10,0.15)\n", " ys = [gaussian (x, mu,variance) for x in xs]\n", - " plot (xs, ys)\n", - " ylim((0,1))\n", - " show()\n", + " plt.plot (xs, ys)\n", + " plt.ylim((0,1))\n", + " plt.show()\n", "\n", "interact (plt_g, mu=(0,10), variance=(0.2,4.5))" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -219,8 +216,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] } ], "metadata": {} diff --git a/Kalman Filters.ipynb b/Kalman Filters.ipynb index 692ef17..a670b9c 100644 --- a/Kalman Filters.ipynb +++ b/Kalman Filters.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:62f7253d6ad9a6031738dd1335cf418d484588da5b86c0e2b98b648ae1f20f38" + "signature": "sha256:6ea7b0e562518e41af4e09c3d07bfdc49bd67a20f9d28c17a5b5a053444de18c" }, "nbformat": 3, "nbformat_minor": 0, @@ -50,8 +50,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -65,13 +64,13 @@ "cell_type": "code", "collapsed": false, "input": [ + "from __future__ import print_function\n", "for i in range(20):\n", - " print (\"%.4f\" % random.randn())," + " print(\"%.4f\" % random.randn(),end='\\t')" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -87,20 +86,20 @@ "collapsed": false, "input": [ "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", "\n", "dog = DogSensor (noise=0.0)\n", "xs = []\n", "for i in range(10):\n", " x = dog.sense()\n", " xs.append(x)\n", - " print(\"%.4f\" % x),\n", - "plot(xs)\n", - "show()" + " print(\"%.4f\" % x, end=' '),\n", + "plt.plot(xs)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -122,21 +121,20 @@ " for i in range(100):\n", " x = dog.sense()\n", " xs.append(x)\n", - " p1, = plot(xs, c='b')\n", - " p2, = plot([0,99],[1,100], 'r--')\n", - " xlabel('time')\n", - " ylabel('pos')\n", - " ylim([0,100])\n", - " title('noise = ' + str(noise_scale))\n", - " legend([p1, p2], ['sensor', 'actual'], loc=2)\n", - " show()\n", + " p1, = plt.plot(xs, c='b')\n", + " p2, = plt.plot([0,99],[1,100], 'r--')\n", + " plt.xlabel('time')\n", + " plt.ylabel('pos')\n", + " plt.ylim([0,100])\n", + " plt.title('noise = ' + str(noise_scale))\n", + " plt.legend([p1, p2], ['sensor', 'actual'], loc=2)\n", + " plt.show()\n", " \n", "test_sensor(4.0)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -159,8 +157,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "code", @@ -170,8 +167,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -198,8 +194,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -222,13 +217,12 @@ "for i in xs:\n", " ys.append(dog.sense())\n", " \n", - "plot(xs,ys)\n", - "show()" + "plt.plot(xs,ys)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -277,12 +271,13 @@ "collapsed": false, "input": [ "import gaussian\n", + "import numpy as np\n", + "\n", "def multiply(mu1, sig1, mu2, sig2):\n", " m = (sig1*mu2 + sig2*mu1) / (sig1+sig2)\n", " s = 1. / (1./sig1 + 1./ sig2)\n", " return (m,s)\n", "\n", - "\n", "xs = np.arange(16, 30, 0.1)\n", "\n", "\n", @@ -290,18 +285,17 @@ "m, s = multiply(m1,s1,m1,s1)\n", "\n", "ys = [gaussian.gaussian(x,m1,s1) for x in xs]\n", - "p1, =plot (xs,ys)\n", + "p1, = plt.plot (xs,ys)\n", "\n", "ys = [gaussian.gaussian(x,m,s) for x in xs]\n", - "p2, = plot (xs,ys)\n", + "p2, = plt.plot (xs,ys)\n", "\n", - "legend([p1,p2],['original', 'multiply'])\n", - "show()" + "plt.legend([p1,p2],['original', 'multiply'])\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -318,26 +312,24 @@ "input": [ "xs = np.arange(16, 30, 0.1)\n", "\n", - "\n", "m1,s1 = 23, 5\n", "m2,s2 = 25, 5\n", "m, s = multiply(m1,s1,m2,s2)\n", "\n", "ys = [gaussian.gaussian(x,m1,s1) for x in xs]\n", - "p1, = plot (xs,ys)\n", + "p1, = plt.plot (xs,ys)\n", "\n", "ys = [gaussian.gaussian(x,m2,s2) for x in xs]\n", - "p2, = plot (xs,ys)\n", + "p2, = plt.plot (xs,ys)\n", "\n", "ys = [gaussian.gaussian(x,m,s) for x in xs]\n", - "p3, = plot(xs,ys)\n", - "legend([p1,p2,p3],['measure 1', 'measure 2', 'multiply'])\n", - "show()" + "p3, = plt.plot(xs,ys)\n", + "plt.legend([p1,p2,p3],['measure 1', 'measure 2', 'multiply'])\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -372,8 +364,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -391,8 +382,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -412,13 +402,11 @@ "pos,s = 2, 5\n", "for i in range(20):\n", " pos,s = sense(pos, s, dog.sense(), 5)\n", - " print 'time:', i, 'position = ', \"%.3f\" % pos, 'variance = ', \"%.3f\" % s\n", - "\n" + " print('time:', i, '\\tposition =', \"%.3f\" % pos, '\\tvariance =', \"%.3f\" % s)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -476,8 +464,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -503,7 +490,7 @@ "\n", "for i in range(10):\n", " pos = update(pos[0], pos[1], movement, movement_error)\n", - " print 'UPDATE:', \"%.4f\" %pos[0], \", %.4f\" %pos[1]\n", + " print('UPDATE: %.4f,\\t%.4f' % (pos[0], pos[1]))\n", " \n", " Z = dog.sense()\n", " zs.append(Z)\n", @@ -511,18 +498,17 @@ " pos = sense(pos[0], pos[1], Z, sensor_error)\n", " ps.append(pos[0])\n", " \n", - " print 'SENSE:', \"%.4f\" %pos[0], \", %.4f\" %pos[1]\n", - " print\n", + " print('SENSE: %.4f,\\t%.4f' % (pos[0], pos[1]))\n", + " print()\n", " \n", - "p1, = plot(zs,c='r', linestyle='dashed')\n", - "p2, = plot(ps, c='b')\n", - "legend([p1,p2], ['measurement', 'filter'], 2)\n", - "show()" + "p1, = plt.plot(zs,c='r', linestyle='dashed')\n", + "p2, = plt.plot(ps, c='b')\n", + "plt.legend([p1,p2], ['measurement', 'filter'], 2)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -623,15 +609,14 @@ " ps.append(pos[0])\n", "\n", "\n", - "p1, = plot(zs,c='r', linestyle='dashed')\n", - "p2, = plot(ps, c='b')\n", - "legend([p1,p2], ['measurement', 'filter'], 2)\n", - "show()" + "p1, = plt.plot(zs,c='r', linestyle='dashed')\n", + "p2, = plt.plot(ps, c='b')\n", + "plt.legend([p1,p2], ['measurement', 'filter'], 2)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -665,15 +650,14 @@ " ps.append(pos[0])\n", "\n", "\n", - "p1, = plot(zs,c='r', linestyle='dashed')\n", - "p2, = plot(ps, c='b')\n", - "legend([p1,p2], ['measurement', 'filter'], 2)\n", - "show()" + "p1, = plt.plot(zs,c='r', linestyle='dashed')\n", + "p2, = plt.plot(ps, c='b')\n", + "plt.legend([p1,p2], ['measurement', 'filter'], 2)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -707,15 +691,14 @@ " ps.append(pos[0])\n", "\n", "\n", - "p1, = plot(zs,c='r', linestyle='dashed')\n", - "p2, = plot(ps, c='b')\n", - "legend([p1,p2], ['measurement', 'filter'], 2)\n", - "show()" + "p1, = plt.plot(zs,c='r', linestyle='dashed')\n", + "p2, = plt.plot(ps, c='b')\n", + "plt.legend([p1,p2], ['measurement', 'filter'], 2)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -750,15 +733,14 @@ "\n", " pos = update (pos[0], pos[1], movement, movement_error)\n", "\n", - "p1, = plot(zs,c='r', linestyle='dashed')\n", - "p2, = plot(ps, c='b')\n", - "legend([p1,p2], ['measurement', 'filter'], 2)\n", - "show()" + "p1, = plt.plot(zs,c='r', linestyle='dashed')\n", + "p2, = plt.plot(ps, c='b')\n", + "plt.legend([p1,p2], ['measurement', 'filter'], 2)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -801,15 +783,14 @@ "\n", " pos = update(pos[0], pos[1], movement, movement_error)\n", "\n", - "p1, = plot(zs,c='r', linestyle='dashed')\n", - "p2, = plot(ps, c='b')\n", - "legend([p1,p2], ['measurement', 'filter'], 2)\n", - "show()" + "p1, = plt.plot(zs,c='r', linestyle='dashed')\n", + "p2, = plt.plot(ps, c='b')\n", + "plt.legend([p1,p2], ['measurement', 'filter'], 2)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -829,18 +810,14 @@ ] }, { - "cell_type": "code", - "collapsed": false, - "input": [ + "cell_type": "markdown", + "metadata": {}, + "source": [ "author notes:\n", " clean up the code - same stuff duplicated over and over - write a 'clean implemntation' at the end.\n", " \n", " " - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": "" + ] }, { "cell_type": "code", @@ -864,10 +841,10 @@ " ps.append(pos[0])\n", "\n", "\n", - "p1, = plot(zs,c='r', linestyle='dashed')\n", - "p2, = plot(ps, c='b')\n", - "legend([p1,p2], ['measurement', 'filter'], 2)\n", - "show()" + "p1, = plt.plot(zs,c='r', linestyle='dashed')\n", + "p2, = plt.plot(ps, c='b')\n", + "plt.legend([p1,p2], ['measurement', 'filter'], 2)\n", + "plt.show()" ], "language": "python", "metadata": {}, @@ -886,8 +863,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] } ], "metadata": {} diff --git a/Multidimensional Kalman Filters.ipynb b/Multidimensional Kalman Filters.ipynb index 906a70c..b38824a 100644 --- a/Multidimensional Kalman Filters.ipynb +++ b/Multidimensional Kalman Filters.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:43e6b1c0a6a87cad97442c35e4be82ce79c07ba2a6b41e95e576e4371959d448" + "signature": "sha256:3b71546948dd21caccb388a6147d443538b451fdb64f56494861aa34958fb04a" }, "nbformat": 3, "nbformat_minor": 0, @@ -85,8 +85,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -107,8 +106,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -125,8 +123,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -143,8 +140,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -157,12 +153,11 @@ "cell_type": "code", "collapsed": false, "input": [ - "print multivariate_gaussian(x,mu,cov)" + "print(multivariate_gaussian(x,mu,cov))" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -175,13 +170,14 @@ "cell_type": "code", "collapsed": false, "input": [ + "from __future__ import print_function\n", + "\n", "x = np.array([2,7])\n", - "print multivariate_gaussian(x,mu,cov)" + "print(multivariate_gaussian(x,mu,cov))" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -195,12 +191,16 @@ "collapsed": false, "input": [ "%matplotlib inline\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.pylab as pylab\n", + "from mpl_toolkits.mplot3d import Axes3D\n", + "import numpy as np\n", + "\n", "pylab.rcParams['figure.figsize'] = 12,6\n", "\n", - "from mpl_toolkits.mplot3d import Axes3D\n", - "\n", - "xs, ys = arange(-8, 13, .75), arange(-8, 20, .75)\n", - "xv, yv = meshgrid (xs, ys)\n", + "xs, ys = np.arange(-8, 13, .75), np.arange(-8, 20, .75)\n", + "xv, yv = np.meshgrid (xs, ys)\n", "\n", "zs = np.array([multivariate_gaussian(np.array([x,y]),mu,cov) \n", " for x,y in zip(np.ravel(xv), np.ravel(yv))])\n", @@ -208,13 +208,12 @@ "\n", "ax = plt.figure().add_subplot(111, projection='3d')\n", "ax.plot_wireframe(xv, yv, zv)\n", - "show()\n", + "plt.show()\n", "pylab.rcParams['figure.figsize'] = 6,4" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -232,28 +231,28 @@ "import gaussian as g\n", "pylab.rcParams['figure.figsize'] = 12,4\n", "\n", - "cov = array([[2,0],[0,2]])\n", + "cov = np.array([[2,0],[0,2]])\n", "e = g.sigma_ellipse (cov, 2, 7)\n", - "subplot(131)\n", + "plt.subplot(131)\n", "g.plot_sigma_ellipse(e, '|2 0|\\n|0 2|')\n", "\n", "\n", - "cov = array([[2,0],[0,9]])\n", + "cov = np.array([[2,0],[0,9]])\n", "e = g.sigma_ellipse (cov, 2, 7)\n", - "subplot(132)\n", + "plt.subplot(132)\n", "g.plot_sigma_ellipse(e, '|2 0|\\n|0 9|')\n", "\n", - "subplot(133)\n", - "cov = array([[2,1.2],[1.2,3]])\n", + "plt.subplot(133)\n", + "cov = np.array([[2,1.2],[1.2,3]])\n", "e = g.sigma_ellipse (cov, 2, 7)\n", "g.plot_sigma_ellipse(e,'|2 1.2|\\n|1.2 2|')\n", - "show()\n", + "plt.show()\n", + "\n", "pylab.rcParams['figure.figsize'] = 6,4" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -299,12 +298,14 @@ "cell_type": "code", "collapsed": false, "input": [ - "scatter ([1,2,3],[1,2,3]);xlim([0,4]);ylim([0,4]);show()" + "plt.scatter ([1,2,3],[1,2,3])\n", + "plt.xlim([0,4])\n", + "plt.ylim([0,4])\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -317,15 +318,15 @@ "cell_type": "code", "collapsed": false, "input": [ - "scatter ([1,2,3],[1,2,3]);xlim([0,5]);ylim([0,5])\n", - "plot([0,5],[0,5],'r')\n", - "scatter ([4], [4], c='g', marker='s',s=200)\n", - "show()" + "plt.scatter ([1,2,3],[1,2,3])\n", + "plt.xlim([0,5]); plt.ylim([0,5])\n", + "plt.plot([0,5],[0,5],'r')\n", + "plt.scatter ([4], [4], c='g', marker='s',s=200)\n", + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -338,19 +339,18 @@ "cell_type": "code", "collapsed": false, "input": [ - "cov = array([[0.003,0], [0,12]])\n", - "sigma = sigma=[0.5,1.,1.5,2]\n", + "cov = np.array([[0.003,0], [0,12]])\n", + "sigma=[0.5,1.,1.5,2]\n", "e1 = g.sigma_ellipses(cov, x=1, y=1, sigma=sigma)\n", "e2 = g.sigma_ellipses(cov, x=2, y=2, sigma=sigma)\n", "e3 = g.sigma_ellipses(cov, x=3, y=3, sigma=sigma)\n", "g.plot_sigma_ellipses([e1, e2, e3], axis_equal=True,x_lim=[0,4],y_lim=[0,15])\n", "plt.ylim([0,11])\n", - "show()" + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -369,7 +369,7 @@ "input": [ "from matplotlib.patches import Ellipse\n", "\n", - "cov = array([[1,1],[1,1.1]])\n", + "cov = np.array([[1,1],[1,1.1]])\n", "ev = g.sigma_ellipses(cov, x=2, y=2, sigma=sigma)\n", "\n", "isct = Ellipse(xy=(2,2), width=.2, height=1.2, edgecolor='r', fc='None', lw=4)\n", @@ -380,8 +380,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -413,22 +412,20 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ - "cov = array([[7,4],[4,7.]])\n", - "mu = array([0,0])\n", - "x = array([0,0])\n", - "print multivariate_gaussian(x,mu,cov)" + "cov = np.array([[7,4],[4,7.]])\n", + "mu = np.array([0,0])\n", + "x = np.array([0,0])\n", + "print(multivariate_gaussian(x,mu,cov))" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "code", @@ -443,8 +440,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] } ], "metadata": {} diff --git a/histogram_filter.ipynb b/histogram_filter.ipynb index 9cdcf45..f04f6b2 100644 --- a/histogram_filter.ipynb +++ b/histogram_filter.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:d0bfbbe322bb5a6c6b2c484f5f8da49814c5c8d8ec1120813d02a9511ddf94a1" + "signature": "sha256:35f19121bea1b617984e84b0fa7dca20a612d6302af68a65f9d7d24120786c2d" }, "nbformat": 3, "nbformat_minor": 0, @@ -38,12 +38,13 @@ "cell_type": "code", "collapsed": false, "input": [ - "pos = array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])" + "import numpy as np\n", + "\n", + "pos = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -56,12 +57,11 @@ "cell_type": "code", "collapsed": false, "input": [ - "hallway = array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])" + "hallway = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -75,8 +75,9 @@ "collapsed": false, "input": [ "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", "\n", - "pos = array([0.333, 0.333, 0., 0., 0., 0., 0., 0., 0.333, 0.])\n", + "pos = np.array([0.333, 0.333, 0., 0., 0., 0., 0., 0., 0.333, 0.])\n", "\n", "def bar_plot(pos):\n", " ax = plt.figure().gca()\n", @@ -89,8 +90,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -110,8 +110,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -132,13 +131,12 @@ "cell_type": "code", "collapsed": false, "input": [ - "pos = array([0,1,0,0,0,0,0,0,0,0])\n", + "pos = np.array([0,1,0,0,0,0,0,0,0,0])\n", "print pos" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -168,7 +166,7 @@ "collapsed": false, "input": [ "def measure (pos, measure, p_hit, p_miss):\n", - " q = array(pos, dtype=float)\n", + " q = np.array(pos, dtype=float)\n", " for i in range(len(hallway)):\n", " if hallway[i] == measure:\n", " q[i] = pos[i] * p_hit\n", @@ -176,7 +174,7 @@ " q[i] = pos[i] * p_miss\n", " return q\n", "\n", - "pos = array([0.2]*10)\n", + "pos = np.array([0.2]*10)\n", "reading = 1 # 1 is 'door'\n", "pos = measure (pos, 1, .6, .2)\n", "\n", @@ -185,8 +183,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -205,7 +202,7 @@ " p[i] = p[i] / s\n", " \n", "def sense (pos, measure, p_hit, p_miss):\n", - " q = array(pos, dtype=float)\n", + " q = np.array(pos, dtype=float)\n", " for i in range(len(hallway)):\n", " if hallway[i] == measure:\n", " q[i] = pos[i] * p_hit\n", @@ -215,7 +212,7 @@ " return q\n", "\n", "\n", - "pos = array([0.2]*10)\n", + "pos = np.array([0.2]*10)\n", "reading = 1 # 1 is 'door'\n", "pos = sense (pos, 1, .6, .2)\n", "\n", @@ -225,8 +222,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -260,20 +256,19 @@ " is to the left\n", " \"\"\"\n", " n = len(pos)\n", - " result = array(pos, dtype=float)\n", + " result = np.array(pos, dtype=float)\n", " for i in range(n):\n", " result[i] = pos[(i-move) % n]\n", " return result\n", " \n", - "pos = array([.4, .1, .2, .3])\n", + "pos = np.array([.4, .1, .2, .3])\n", "print 'pos before update =', pos\n", "pos = perfect_update(pos, 1)\n", "print 'pos after update =', pos\n" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -296,7 +291,7 @@ "input": [ "def update (pos, move, p_correct, p_under, p_over):\n", " n = len(pos)\n", - " result = array(pos, dtype=float)\n", + " result = np.array(pos, dtype=float)\n", " for i in range(n):\n", " result[i] = \\\n", " pos[(i-move) % n] * p_correct + \\\n", @@ -304,14 +299,13 @@ " pos[(i-move+1) % n] * p_under \n", " return result\n", "\n", - "p = array([0,0,0,1,0,0,0,0])\n", + "p = np.array([0,0,0,1,0,0,0,0])\n", "res = update (p, 2, .8, .1, .1)\n", "print res" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -324,14 +318,13 @@ "cell_type": "code", "collapsed": false, "input": [ - "p = array([0, 0, .4, .6, 0, 0, 0, 0])\n", + "p = np.array([0, 0, .4, .6, 0, 0, 0, 0])\n", "res = update (p, 2, .8, .1, .1)\n", "print res" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -350,8 +343,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -372,8 +364,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -394,8 +385,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -416,19 +406,16 @@ "cell_type": "code", "collapsed": false, "input": [ - "p = array([.1]*10)\n", - "p = sense (p, 1, .6, .2)\n", - "print p\n", - "p = update (p, 1, .8, .1, .1)\n", - "print p\n", - "bar_plot(p)\n", - "\n", - " " + "p = np.array([.1]*10)\n", + "p = sense(p, 1, .6, .2)\n", + "print(p)\n", + "p = update(p, 1, .8, .1, .1)\n", + "print(p)\n", + "bar_plot(p)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -441,14 +428,13 @@ "cell_type": "code", "collapsed": false, "input": [ - "p = sense (p, 1, .6, .2)\n", - "print p\n", + "p = sense(p, 1, .6, .2)\n", + "print(p)\n", "bar_plot(p)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -461,14 +447,13 @@ "cell_type": "code", "collapsed": false, "input": [ - "p = update (p, 1, .8, .1, .1)\n", - "p = sense (p, 0, .6, .2)\n", - "bar_plot (p)" + "p = update(p, 1, .8, .1, .1)\n", + "p = sense(p, 0, .6, .2)\n", + "bar_plot(p)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -481,14 +466,13 @@ "cell_type": "code", "collapsed": false, "input": [ - "p = update (p, 1, .8, .1, .1)\n", - "p = sense (p, 0, .6, .2)\n", - "bar_plot (p)" + "p = update(p, 1, .8, .1, .1)\n", + "p = sense(p, 0, .6, .2)\n", + "bar_plot(p)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -513,19 +497,18 @@ "collapsed": false, "input": [ "hallway = [1,0,1,0,0,1,0,1,0,0]\n", - "pos = array([.1]*10)\n", + "pos = np.array([.1]*10)\n", "measurements = [1,0,1,0,0]\n", "\n", "for m in measurements:\n", - " pos = sense (pos, m, .6, .2)\n", - " pos = update (pos, 1, .8, .1, .1)\n", + " pos = sense(pos, m, .6, .2)\n", + " pos = update(pos, 1, .8, .1, .1)\n", "bar_plot(pos)\n", "print pos" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -538,14 +521,13 @@ "cell_type": "code", "collapsed": false, "input": [ - "pos = sense (pos, m, .6, .2)\n", - "pos = update (pos, 1, .8, .1, .1)\n", + "pos = sense(pos, m, .6, .2)\n", + "pos = update(pos, 1, .8, .1, .1)\n", "bar_plot(pos)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -561,14 +543,13 @@ "measurements = [0,1,0,1,0,0]\n", "\n", "for m in measurements:\n", - " pos = sense (pos, m, .6, .2)\n", - " pos = update (pos, 1, .8, .1, .1)\n", + " pos = sense(pos, m, .6, .2)\n", + " pos = update(pos, 1, .8, .1, .1)\n", "bar_plot(pos)" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown", @@ -606,11 +587,6 @@ "**If you can understand this chapter you will be able to understand and implement Kalman filters** I cannot stress this enough. If anything is murky, go back and reread this chapter and play with the code. the rest of this book will build on the algorithms that we use here. If you don't intuitively understand why this histogram filter works, and can at least work through the math, you will have little success with the rest of the material. However, if you grasp the fundamental insight - multiplying probabilities when we measure, and shifting probabilities when we update leads to a converging solution - then you understand everything important you need to grasp the Kalman filter. " ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - }, { "cell_type": "code", "collapsed": false, @@ -624,8 +600,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": "" + "outputs": [] }, { "cell_type": "markdown",