{ "metadata": { "name": "Untitled1" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Gaussian Approximations for various distributions\n", "\n", "For each of these distributions, we compare the pdf or pmf of the original random variable $X$ to a Gaussian random variable $\\mathcal{X}$ with $\\mu=\\langle X\\rangle$ and $\\sigma^2=\\mathrm{Var}(X)$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline\n", "from __future__ import division\n", "rcParams['figure.figsize'] = (10.0, 8.0)\n", "rcParams['font.size'] = 14\n", "from scipy import stats" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We could enter the formula for pdf or pmf for each distribution by hand, but it's more convenient to use the scipy stats module, which has all of these distributions defined\n", "\n", "## Binomial distribution w/parameters $n$ and $\\alpha$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = 3\n", "alpha = 0.4\n", "x_binom = arange(n+1)\n", "x_binom_cont = linspace(-0.5,n+0.5,1000)\n", "p_binom = stats.binom.pmf(x_binom,n,alpha)\n", "mu_binom = n * alpha\n", "sigma_binom = sqrt(n*alpha*(1-alpha))\n", "f_binom_gauss = stats.norm.pdf(x_binom_cont,mu_binom,sigma_binom)\n", "plot(x_binom,p_binom,'k.',label=('Binom(%d,%.2f)' % (n,alpha)));\n", "plot(x_binom_cont,f_binom_gauss,'b-',label=('Gauss(%.2f,%.2f)' % (mu_binom,sigma_binom**2)));\n", "legend(loc='upper right');\n", "xlabel('$x$');\n", "ylabel('$p(x)$ or $f(x)$');\n", "xlim([-0.5,n+0.5]);" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Poisson distribution w/parameter $\\mu$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mu = 1.3\n", "mu_poisson = mu\n", "sigma_poisson = sqrt(mu)\n", "x_poisson = arange(int(mu+5*sigma_poisson))\n", "x_poisson_cont = linspace(-0.5,x_poisson[-1]+0.5,1000)\n", "p_poisson = stats.poisson.pmf(x_poisson,mu)\n", "f_poisson_gauss = stats.norm.pdf(x_poisson_cont,mu_poisson,sigma_poisson)\n", "plot(x_poisson,p_poisson,'k.',label=('Poisson(%.2f)' % mu));\n", "plot(x_poisson_cont,f_poisson_gauss,'b-',label=('Gauss(%.2f,%.2f)' % (mu_poisson,sigma_poisson**2)));\n", "legend(loc='upper right');\n", "xlabel('$x$');\n", "ylabel('$p(x)$ or $f(x)$');\n", "xlim([x_poisson_cont[0],x_poisson_cont[-1]])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Chi-square distribution with $\\nu$ degrees of freedom" ] }, { "cell_type": "code", "collapsed": false, "input": [ "nu = 3\n", "mu_chisq = nu\n", "sigma_chisq = sqrt(2*nu)\n", "x_chisq = linspace(0,mu_chisq+5*sigma_chisq,1000)\n", "f_chisq = stats.chi2.pdf(x_chisq,nu)\n", "f_chisq_gauss = stats.norm.pdf(x_chisq,mu_chisq,sigma_chisq)\n", "plot(x_chisq,f_chisq,'k--',label=('$\\chi^2$(%d)' % nu));\n", "plot(x_chisq,f_chisq_gauss,'b-',label=('Gauss(%.2f,%.2f)' % (mu_chisq,sigma_chisq**2)));\n", "legend(loc='upper right');\n", "xlabel('$x$');\n", "ylabel('$f(x)$');\n", "xlim([x_chisq[0],x_chisq[-1]])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Gamma disribution with parameters $\\alpha$ and $\\beta$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "alpha = 1.2\n", "beta = 2.5\n", "mu_gamma = alpha*beta\n", "sigma_gamma = sqrt(alpha)*beta\n", "x_gamma = linspace(0,mu_gamma+5*sigma_gamma,1000)\n", "f_gamma = stats.gamma.pdf(x_gamma,alpha,scale=beta)\n", "f_gamma_gauss = stats.norm.pdf(x_gamma,mu_gamma,sigma_gamma)\n", "plot(x_gamma,f_gamma,'k--',label=('Gamma(%.2f,%.2f)' % (alpha,beta)));\n", "plot(x_gamma,f_gamma_gauss,'b-',label=('Gauss(%.2f,%.2f)' % (mu_gamma,sigma_gamma**2)));\n", "legend(loc='upper right');\n", "xlabel('$x$');\n", "ylabel('$f(x)$');\n", "xlim([x_gamma[0],x_gamma[-1]]);" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exponential distribution with parameter $\\lambda$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lam = 0.4\n", "mu_exp = 1./lam\n", "sigma_exp = 1./lam\n", "x_exp = linspace(0,mu_exp+5*sigma_exp,1000)\n", "f_exp = stats.expon.pdf(x_exp,scale=1./lam)\n", "f_exp_gauss = stats.norm.pdf(x_exp,mu_exp,sigma_exp)\n", "plot(x_exp,f_exp,'k--',label=('Exp(%.2f)' % lam));\n", "plot(x_exp,f_exp_gauss,'b-',label=('Gauss(%.2f,%.2f)' % (mu_exp,sigma_exp**2)));\n", "legend(loc='upper right');\n", "xlabel('$x$');\n", "ylabel('$f(x)$');\n", "xlim([x_exp[0],x_exp[-1]]);" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }