From 09fa08f73e337c0d4a78b5978fc1cd44bdc62797 Mon Sep 17 00:00:00 2001 From: Roger Labbe Date: Sat, 10 May 2014 11:25:52 -0700 Subject: [PATCH] mv'ed gaussian.py to stats.py The name was horrible, we were using gaussian.gaussian() to compute gaussians. stats.gaussian() is much better. Updated all notebooks and scripts to reflect the change. --- Gaussians.ipynb | 10 ++++++--- Kalman Filters.ipynb | 25 +++++++++++----------- Multidimensional Kalman Filters.ipynb | 30 +++++++++++++-------------- gaussian_internal.py | 10 ++++----- gaussian.py => stats.py | 0 5 files changed, 39 insertions(+), 36 deletions(-) rename gaussian.py => stats.py (100%) diff --git a/Gaussians.ipynb b/Gaussians.ipynb index 16eda12..88516e1 100644 --- a/Gaussians.ipynb +++ b/Gaussians.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:9184f3be6129a4cf39b259921633ec76099fb8ca364fd39a633da1920257669b" + "signature": "sha256:20b4feb883a71cab956a175006257f6c548b074ace2a845bd9dc5c1c1f9b7a91" }, "nbformat": 3, "nbformat_minor": 0, @@ -83,7 +83,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "from gaussian import gaussian\n", + "from stats import gaussian\n", "plot_gaussian(22,4,True,xlabel='$^{\\circ}C$',ylabel=\"Percent\")\n", "\n", "print('Probability of 22 is %.2f' % (gaussian(22,22,4)*100))\n", @@ -103,7 +103,11 @@ "\n", "> *Important*: I will repeat what I wrote at the top of this section: \"A Gaussian...is completely described with two parameters\"\n", "\n", - "The standard notation for a normal distribution for a random variable $X$ is $X \\sim\\ \\mathcal{N}(\\mu,\\sigma^2)$. This means I can express the temperature reading of our thermometer as $$temp = \\mathcal{N}(22,4)$$This is an **extremely important** result. Gaussians allow me to capture an infinite number of possible values with only two numbers! With the values $\\mu=22$ and $\\sigma^2=4$ I can compute the probability of the temperature being $22\\,^{\\circ}C$, of $20\\,^{\\circ}C$, of $87.3429\\,^{\\circ}C$, or any other arbitrary value.\n", + "The standard notation for a normal distribution for a random variable $X$ is $X \\sim\\ \\mathcal{N}(\\mu,\\sigma^2)$. This means I can express the temperature reading of our thermometer as\n", + "\n", + "$$temp = \\mathcal{N}(22,4)$$\n", + "\n", + "This is an **extremely important** result. Gaussians allow me to capture an infinite number of possible values with only two numbers! With the values $\\mu=22$ and $\\sigma^2=4$ I can compute the probability of the temperature being $22\\,^{\\circ}C$, of $20\\,^{\\circ}C$, of $87.3429\\,^{\\circ}C$, or any other arbitrary value.\n", "\n", "###### The Variance\n", "\n", diff --git a/Kalman Filters.ipynb b/Kalman Filters.ipynb index 7144ec8..29aa158 100644 --- a/Kalman Filters.ipynb +++ b/Kalman Filters.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:91af593ec32629bec5fa5a58b7f5ae29e79fc039fe09b20d37c26dca0ac20430" + "signature": "sha256:f5b17ce3aa0755a19e9550b31e0a75c9c255bb570b57e1e26c533de072549821" }, "nbformat": 3, "nbformat_minor": 0, @@ -193,8 +193,8 @@ "cell_type": "code", "collapsed": false, "input": [ - "import gaussian\n", - "gaussian.norm_plot(23, 5)" + "import stats\n", + "stats.norm_plot(23, 5)" ], "language": "python", "metadata": {}, @@ -289,7 +289,6 @@ "cell_type": "code", "collapsed": false, "input": [ - "import gaussian\n", "import numpy as np\n", "\n", "def multiply(mu1, sig1, mu2, sig2):\n", @@ -303,10 +302,10 @@ "m1,s1 = 23, 5\n", "m, s = multiply(m1,s1,m1,s1)\n", "\n", - "ys = [gaussian.gaussian(x,m1,s1) for x in xs]\n", + "ys = [stats.gaussian(x,m1,s1) for x in xs]\n", "p1, = plt.plot (xs,ys)\n", "\n", - "ys = [gaussian.gaussian(x,m,s) for x in xs]\n", + "ys = [stats.gaussian(x,m,s) for x in xs]\n", "p2, = plt.plot (xs,ys)\n", "\n", "plt.legend([p1,p2],['original', 'multiply'])\n", @@ -339,13 +338,13 @@ "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", + "ys = [stats.gaussian(x,m1,s1) for x in xs]\n", "p1, = plt.plot (xs,ys)\n", "\n", - "ys = [gaussian.gaussian(x,m2,s2) for x in xs]\n", + "ys = [stats.gaussian(x,m2,s2) for x in xs]\n", "p2, = plt.plot (xs,ys)\n", "\n", - "ys = [gaussian.gaussian(x,m,s) for x in xs]\n", + "ys = [stats.gaussian(x,m,s) for x in xs]\n", "p3, = plt.plot(xs,ys)\n", "plt.legend([p1,p2,p3],['measure 1', 'measure 2', 'multiply'])\n", "plt.show()" @@ -379,13 +378,13 @@ "m2,s2 = 50, 5\n", "m, s = multiply(m1,s1,m2,s2)\n", "\n", - "ys = [gaussian.gaussian(x,m1,s1) for x in xs]\n", + "ys = [stats.gaussian(x,m1,s1) for x in xs]\n", "p1, = plt.plot (xs,ys)\n", "\n", - "ys = [gaussian.gaussian(x,m2,s2) for x in xs]\n", + "ys = [stats.gaussian(x,m2,s2) for x in xs]\n", "p2, = plt.plot (xs,ys)\n", "\n", - "ys = [gaussian.gaussian(x,m,s) for x in xs]\n", + "ys = [stats.gaussian(x,m,s) for x in xs]\n", "p3, = plt.plot(xs,ys)\n", "plt.legend([p1,p2,p3],['measure 1', 'measure 2', 'multiply'])\n", "plt.show()" @@ -779,7 +778,7 @@ "\n", " voltage = update(voltage[0], voltage[1], movement, movement_error)\n", "\n", - "plt.scatter(range(N), zs,marker='+')\n", + "plt.scatter(range(N), zs, marker='+')\n", "p1, = plt.plot(ps, c='g')\n", "plt.legend([p1], ['filter'], 3)\n", "plt.xlim((0,N));plt.ylim((0,30))\n", diff --git a/Multidimensional Kalman Filters.ipynb b/Multidimensional Kalman Filters.ipynb index 9a25856..1db4a93 100644 --- a/Multidimensional Kalman Filters.ipynb +++ b/Multidimensional Kalman Filters.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:c85d72f3d31f37b3edfae8ecdc7e79438bde16b84b6d6d119c4edfc1239913d8" + "signature": "sha256:fd542341131473089289548f12ba7f78ac88663ce51e586ff357c5fb48c3eb91" }, "nbformat": 3, "nbformat_minor": 0, @@ -81,7 +81,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "from gaussian import gaussian, multivariate_gaussian" + "from stats import gaussian, multivariate_gaussian" ], "language": "python", "metadata": {}, @@ -241,23 +241,23 @@ "cell_type": "code", "collapsed": false, "input": [ - "import gaussian as g\n", + "import stats\n", "\n", "cov = np.array([[2,0],[0,2]])\n", - "e = g.sigma_ellipse (cov, 2, 7)\n", + "e = stats.sigma_ellipse (cov, 2, 7)\n", "plt.subplot(131)\n", - "g.plot_sigma_ellipse(e, '|2 0|\\n|0 2|')\n", + "stats.plot_sigma_ellipse(e, '|2 0|\\n|0 2|')\n", "\n", "\n", "cov = np.array([[2,0],[0,9]])\n", - "e = g.sigma_ellipse (cov, 2, 7)\n", + "e = stats.sigma_ellipse (cov, 2, 7)\n", "plt.subplot(132)\n", - "g.plot_sigma_ellipse(e, '|2 0|\\n|0 9|')\n", + "stats.plot_sigma_ellipse(e, '|2 0|\\n|0 9|')\n", "\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", + "e = stats.sigma_ellipse (cov, 2, 7)\n", + "stats.plot_sigma_ellipse(e,'|2 1.2|\\n|1.2 2|')\n", "plt.show()" ], "language": "python", @@ -351,10 +351,10 @@ "input": [ "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", + "e1 = stats.sigma_ellipses(cov, x=1, y=1, sigma=sigma)\n", + "e2 = stats.sigma_ellipses(cov, x=2, y=2, sigma=sigma)\n", + "e3 = stats.sigma_ellipses(cov, x=3, y=3, sigma=sigma)\n", + "stats.plot_sigma_ellipses([e1, e2, e3], axis_equal=True,x_lim=[0,4],y_lim=[0,15])\n", "plt.ylim([0,11])\n", "plt.show()" ], @@ -380,11 +380,11 @@ "from matplotlib.patches import Ellipse\n", "\n", "cov = np.array([[1,1],[1,1.1]])\n", - "ev = g.sigma_ellipses(cov, x=2, y=2, sigma=sigma)\n", + "ev = stats.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", "plt.figure().gca().add_artist(isct)\n", - "g.plot_sigma_ellipses([e1, e2, e3, ev], axis_equal=True,x_lim=[0,4],y_lim=[0,15])\n", + "stats.plot_sigma_ellipses([e1, e2, e3, ev], axis_equal=True,x_lim=[0,4],y_lim=[0,15])\n", "plt.ylim([0,11])\n", "plt.show()" ], diff --git a/gaussian_internal.py b/gaussian_internal.py index b0c4490..1c33e6c 100644 --- a/gaussian_internal.py +++ b/gaussian_internal.py @@ -9,7 +9,7 @@ import math import matplotlib.pylab as pylab import matplotlib.pyplot as plt import numpy as np -import gaussian as g +import stats def plot_gaussian (mu, variance, mu_line=False, @@ -17,7 +17,7 @@ def plot_gaussian (mu, variance, xlabel=None, ylabel=None): xs = np.arange(mu-variance*2,mu+variance*2,0.1) - ys = [g.gaussian (x, mu, variance)*100 for x in xs] + ys = [stats.gaussian (x, mu, variance)*100 for x in xs] plt.plot (xs, ys) if mu_line: plt.axvline(mu) @@ -34,12 +34,12 @@ def display_stddev_plot(): pylab.rcParams['figure.figsize'] = 12,6 xs = np.arange(10,30,0.1) var = 8; stddev = math.sqrt(var) - p2, = plt.plot (xs,[g.gaussian(x, 20, var) for x in xs]) + p2, = plt.plot (xs,[stats.gaussian(x, 20, var) for x in xs]) x = 20+stddev - y = g.gaussian(x, 20, var) + y = stats.gaussian(x, 20, var) plt.plot ([x,x], [0,y],'g') plt.plot ([20-stddev, 20-stddev], [0,y], 'g') - y = g.gaussian(20,20,var) + y = stats.gaussian(20,20,var) plt.plot ([20,20],[0,y],'b') ax = plt.axes() ax.annotate('68%', xy=(20.3, 0.045)) diff --git a/gaussian.py b/stats.py similarity index 100% rename from gaussian.py rename to stats.py