{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": [
     "meta",
     "toc_en"
    ]
   },
   "source": [
    "# Numpy random snippets"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Most comments are taken from the Numpy documentation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import directive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tool functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot(data, bins=30):\n",
    "    plt.hist(data, bins)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Discrete distributions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bernoulli distribution "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def bernoulli(p=None, size=1):\n",
    "    return np.random.binomial(n=1, p=p, size=size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "bernoulli(p=0.5, size=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Binomial distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Samples are drawn from a binomial distribution with specified parameters,\n",
    "$n$ trials and $p$ probability of success where $n \\in \\mathbb{N}$\n",
    "and $p$ is in the interval $[0,1]$.\n",
    "\n",
    "The probability density for the binomial distribution is\n",
    "\n",
    "$$P(N) = \\binom{n}{N}p^N(1-p)^{n-N}$$\n",
    "\n",
    "where $n$ is the number of trials, $p$ is the probability\n",
    "of success, and $N$ is the number of successes.\n",
    "\n",
    "When estimating the standard error of a proportion in a population by\n",
    "using a random sample, the normal distribution works well unless the\n",
    "product $p*n <=5$, where $p$ = population proportion estimate, and n =\n",
    "number of samples, in which case the binomial distribution is used\n",
    "instead. For example, a sample of 15 people shows 4 who are left\n",
    "handed, and 11 who are right handed. Then $p = 4/15 = 27%. 0.27*15 = 4$,\n",
    "so the binomial distribution should be used in this case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.binomial(n=10, p=0.5, size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.binomial(n=10, p=0.25, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.binomial(n=10, p=0.5, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.binomial(n=10, p=0.75, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.binomial(n=25, p=0.5, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Hypergeometric distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Samples are drawn from a hypergeometric distribution with specified\n",
    "parameters, ngood (ways to make a good selection), nbad (ways to make\n",
    "a bad selection), and nsample = number of items sampled, which is less\n",
    "than or equal to the sum ngood + nbad.\n",
    "\n",
    "- ngood : Number of ways to make a good selection.  Must be nonnegative.\n",
    "- nbad : Number of ways to make a bad selection.  Must be nonnegative.\n",
    "- nsample : Number of items sampled.  Must be at least 1 and at most ``ngood + nbad``.\n",
    "\n",
    "The probability density for the Hypergeometric distribution is\n",
    "\n",
    "$$P(x) = \\frac{\\binom{m}{n}\\binom{N-m}{n-x}}{\\binom{N}{n}},$$\n",
    "\n",
    "where $0 \\le x \\le m$ and $n+m-N \\le x \\le n$\n",
    "\n",
    "for $P(x)$ the probability of x successes, n = ngood, m = nbad, and\n",
    "N = number of samples.\n",
    "\n",
    "Consider an urn with black and white marbles in it, ngood of them\n",
    "black and nbad are white. If you draw nsample balls without\n",
    "replacement, then the hypergeometric distribution describes the\n",
    "distribution of black balls in the drawn sample.\n",
    "\n",
    "Note that this distribution is very similar to the binomial\n",
    "distribution, except that in this case, samples are drawn without\n",
    "replacement, whereas in the Binomial case samples are drawn with\n",
    "replacement (or the sample space is infinite). As the sample space\n",
    "becomes large, this distribution approaches the binomial.\n",
    "\n",
    "Suppose you have an urn with 15 white and 15 black marbles.\n",
    "If you pull 15 marbles at random, how likely is it that\n",
    "12 or more of them are one color ?\n",
    "\n",
    "    >>> s = np.random.hypergeometric(15, 15, 15, 100000)\n",
    "    >>> sum(s>=12)/100000. + sum(s<=3)/100000.\n",
    "    #   answer = 0.003 ... pretty unlikely!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.hypergeometric(ngood=15, nbad=15, nsample=15, size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.hypergeometric(ngood=15, nbad=15, nsample=15, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Poisson distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Poisson distribution is the limit of the binomial distribution for large N:\n",
    "\n",
    "$$f(k; \\lambda)=\\frac{\\lambda^k e^{-\\lambda}}{k!}$$\n",
    "\n",
    "For events with an expected separation $\\lambda$ the Poisson distribution $f(k; \\lambda)$ describes the probability of $k$ events occurring within the observed interval $\\lambda$.\n",
    "\n",
    "Because the output is limited to the range of the C long type, a ValueError is raised when `lam` is within 10 sigma of the maximum representable value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lambda=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.poisson(lam=1, size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.poisson(lam=1, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lambda=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.poisson(lam=2, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lambda=3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.poisson(lam=3, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lambda=4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.poisson(lam=4, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Lambda=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.poisson(lam=5, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Geometric distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bernoulli trials are experiments with one of two outcomes:\n",
    "success or failure (an example of such an experiment is flipping\n",
    "a coin).  The geometric distribution models the number of trials\n",
    "that must be run in order to achieve success.  It is therefore\n",
    "supported on the positive integers, $k = 1, 2, \\dots$\n",
    "\n",
    "The probability mass function of the geometric distribution is\n",
    "\n",
    "$$f(k) = (1 - p)^{k - 1} p$$\n",
    "\n",
    "where $p$ is the probability of success of an individual trial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.geometric(p=0.5, size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.geometric(p=0.5, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pascal distribution (negative binomial distribution)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Samples are drawn from a negative binomial distribution with specified\n",
    "parameters, $n$ trials and $p$ probability of success where $n$ is an\n",
    "integer > 0 and $p$ is in the interval $[0, 1]$.\n",
    "\n",
    "The probability density for the negative binomial distribution is\n",
    "\n",
    "$$P(N;n,p) = \\binom{N+n-1}{n-1}p^{n}(1-p)^{N},$$\n",
    "\n",
    "where $n-1$ is the number of successes, $p$ is the\n",
    "probability of success, and $N+n-1$ is the number of trials.\n",
    "The negative binomial distribution gives the probability of $n-1$\n",
    "successes and $N$ failures in $N+n-1$ trials, and success on the $(N+n)$th\n",
    "trial.\n",
    "\n",
    "If one throws a die repeatedly until the third time a \"1\" appears,\n",
    "then the probability distribution of the number of non-\"1\"s that\n",
    "appear before the third \"1\" is a negative binomial distribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.negative_binomial(n=1, p=0.1, size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.negative_binomial(n=1, p=0.1, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Uniform distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.choice(range(10), size=100, replace=True, p=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.choice(range(25), size=100000, replace=True, p=None)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Miscellaneous"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.choice(range(10), size=10, replace=False, p=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.choice([1, 2, 3], size=100, replace=True, p=[0.8, 0.1, 0.1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Continuous distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Uniform distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.uniform(high=0.0, low=1.0, size=50)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Normal distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The probability density function of the normal distribution, first\n",
    "derived by De Moivre and 200 years later by both Gauss and Laplace\n",
    "independently, is often called the bell curve because of\n",
    "its characteristic shape (see the example below).\n",
    "\n",
    "The normal distributions occurs often in nature. For example, it\n",
    "describes the commonly occurring distribution of samples influenced\n",
    "by a large number of tiny, random disturbances, each with its own\n",
    "unique distribution.\n",
    "\n",
    "The probability density for the Gaussian distribution is\n",
    "\n",
    "$$p(x) = \\frac{1}{\\sqrt{ 2 \\pi \\sigma^2 }} e^{ - \\frac{ (x - \\mu)^2 } {2 \\sigma^2} },$$\n",
    "\n",
    "where $\\mu$ is the mean and $\\sigma$ the standard\n",
    "deviation. The square of the standard deviation, $\\sigma^2$,\n",
    "is called the variance.\n",
    "\n",
    "The function has its peak at the mean, and its \"spread\" increases with\n",
    "the standard deviation (the function reaches 0.607 times its maximum at\n",
    "$x + \\sigma$ and $x - \\sigma$).\n",
    "This implies that `numpy.random.normal` is more likely to return samples\n",
    "lying close to the mean, rather than those far away."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.normal(loc=0.0, scale=1.0, size=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.normal(loc=0.0, scale=1.0, size=100000)\n",
    "plot(data, bins=np.arange(-5, 6, 0.2))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.normal(loc=2.0, scale=1.0, size=100000)\n",
    "plot(data, bins=np.arange(-5, 6, 0.2))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.normal(loc=0.0, scale=1.5, size=100000)\n",
    "plot(data, bins=np.arange(-5, 6, 0.2))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Log normal distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draw samples from a log-normal distribution with specified mean,\n",
    "standard deviation, and array shape.  Note that the mean and standard\n",
    "deviation are not the values for the distribution itself, but of the\n",
    "underlying normal distribution it is derived from.\n",
    "\n",
    "A variable $x$ has a log-normal distribution if $log(x)$ is normally\n",
    "distributed. The probability density function for the log-normal\n",
    "distribution is:\n",
    "\n",
    "$$p(x) = \\frac{1}{\\sigma x \\sqrt{2\\pi}} e^{(-\\frac{(ln(x)-\\mu)^2}{2\\sigma^2})}$$\n",
    "\n",
    "where $\\mu$ is the mean and $\\sigma$ is the standard\n",
    "deviation of the normally distributed logarithm of the variable.\n",
    "\n",
    "A log-normal distribution results if a random variable is the *product*\n",
    "of a large number of independent, identically-distributed variables in\n",
    "the same way that a normal distribution results if the variable is the\n",
    "*sum* of a large number of independent, identically-distributed\n",
    "variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.lognormal(mean=0.0, sigma=1.0, size=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.lognormal(mean=0.0, sigma=1.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 10, 0.2))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.lognormal(mean=2.0, sigma=1.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 10, 0.2))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.lognormal(mean=0.0, sigma=1.5, size=100000)\n",
    "plot(data, bins=np.arange(0, 10, 0.2))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Power distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Draws samples in $[0, 1]$ from a power distribution with positive exponent $a - 1$ (with $a > 0$).\n",
    "\n",
    "Also known as the power function distribution.\n",
    "\n",
    "The probability density function is\n",
    "\n",
    "$$P(x; a) = ax^{a-1}, 0 \\le x \\le 1, a>0.$$\n",
    "\n",
    "The power function distribution is just the inverse of the Pareto\n",
    "distribution. It may also be seen as a special case of the Beta\n",
    "distribution.\n",
    "\n",
    "It is used, for example, in modeling the over-reporting of insurance\n",
    "claims."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.power(a=1.0, size=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.power(a=0.25, size=100000)\n",
    "plot(data, bins=np.arange(0, 2, 0.05))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.power(a=0.5, size=100000)\n",
    "plot(data, bins=np.arange(0, 2, 0.05))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.power(a=1.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 2, 0.05))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.power(a=2.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 2, 0.05))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.power(a=5.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 2, 0.05))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Beta distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exponential distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Its probability density function is\n",
    "\n",
    "$$f\\left( x; \\frac{1}{\\beta} \\right) = \\frac{1}{\\beta} \\exp \\left( \\frac{-x}{\\beta} \\right)$$\n",
    "\n",
    "for $x > 0$ and 0 elsewhere.\n",
    "\n",
    "$\\beta$ is the scale parameter, which is the inverse of the rate parameter $\\lambda = 1/\\beta$.\n",
    "The rate parameter is an alternative, widely used parameterization of the exponential distribution.\n",
    "\n",
    "The exponential distribution is a continuous analogue of the\n",
    "geometric distribution. It describes many common situations, such as\n",
    "the size of raindrops measured over many rainstorms, or the time\n",
    "between page requests to Wikipedia.\n",
    "\n",
    "The scale parameter, $\\beta = 1/\\lambda$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.exponential(scale=1.0, size=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.exponential(scale=1.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 30, 0.5))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.exponential(scale=2.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 30, 0.5))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.exponential(scale=5.0, size=100000)\n",
    "plot(data, bins=np.arange(0, 30, 0.5))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.exponential(scale=0.5, size=100000)\n",
    "plot(data, bins=np.arange(0, 30, 0.5))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Chi-square distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When `df` independent random variables, each with standard normal\n",
    "distributions (mean=0, variance=1), are squared and summed, the\n",
    "resulting distribution is chi-square.\n",
    "\n",
    "This distribution is often used in hypothesis testing.\n",
    "\n",
    "The variable obtained by summing the squares of `df` independent,\n",
    "standard normally distributed random variables:\n",
    "\n",
    "$$Q = \\sum_{i=0}^{\\mathtt{df}} X^2_i$$\n",
    "\n",
    "is chi-square distributed, denoted\n",
    "\n",
    "$$Q \\sim \\chi^2_k.$$\n",
    "\n",
    "The probability density function of the chi-squared distribution is\n",
    "\n",
    "$$p(x) = \\frac{(1/2)^{k/2}}{\\Gamma(k/2)} x^{k/2 - 1} e^{-x/2},$$\n",
    "\n",
    "where $\\Gamma$ is the gamma function,\n",
    "\n",
    "$$\\Gamma(x) = \\int_0^{-\\infty} t^{x - 1} e^{-t} dt.$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "np.random.chisquare(df=1.0, size=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.chisquare(df=1.0, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.chisquare(df=2.0, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = np.random.chisquare(df=5.0, size=10000)\n",
    "plot(data, bins=range(30))\n",
    "print(\"mean:\", data.mean())\n",
    "print(\"std:\", data.std())"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
