2883 lines
90 KiB
Plaintext
2883 lines
90 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Elements of Probability Theory and Statistical Data Analysis\n",
|
|
"\n",
|
|
"\n",
|
|
"## Domains and probabilities\n",
|
|
"Consider the following simple example, namely the tossing of two dice, resulting in the following possible values"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\{2,3,4,5,6,7,8,9,10,11,12\\}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"These values are called the *domain*. \n",
|
|
"To this domain we have the corresponding *probabilities*"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\{1/36,2/36/,3/36,4/36,5/36,6/36,5/36,4/36,3/36,2/36,1/36\\}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Tossing the dice\n",
|
|
"The numbers in the domain are the outcomes of the physical process of tossing say two dice.\n",
|
|
"We cannot tell beforehand whether the outcome is 3 or 5 or any other number in this domain.\n",
|
|
"This defines the randomness of the outcome, or unexpectedness or any other synonimous word which\n",
|
|
"encompasses the uncertitude of the final outcome. \n",
|
|
"\n",
|
|
"The only thing we can tell beforehand\n",
|
|
"is that say the outcome 2 has a certain probability. \n",
|
|
"If our favorite hobby is to spend an hour every evening throwing dice and \n",
|
|
"registering the sequence of outcomes, we will note that the numbers in the above domain"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\{2,3,4,5,6,7,8,9,10,11,12\\},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"appear in a random order. After 11 throws the results may look like"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\{10,8,6,3,6,9,11,8,12,4,5\\}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Stochastic variables\n",
|
|
"\n",
|
|
"**Random variables are characterized by a domain which contains all possible values that the random value may take. This domain has a corresponding probability distribution function(PDF)**.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Stochastic variables and the main concepts, the discrete case\n",
|
|
"There are two main concepts associated with a stochastic variable. The\n",
|
|
"*domain* is the set $\\mathbb D = \\{x\\}$ of all accessible values\n",
|
|
"the variable can assume, so that $X \\in \\mathbb D$. An example of a\n",
|
|
"discrete domain is the set of six different numbers that we may get by\n",
|
|
"throwing of a dice, $x\\in\\{1,\\,2,\\,3,\\,4,\\,5,\\,6\\}$.\n",
|
|
"\n",
|
|
"The *probability distribution function (PDF)* is a function\n",
|
|
"$p(x)$ on the domain which, in the discrete case, gives us the\n",
|
|
"probability or relative frequency with which these values of $X$\n",
|
|
"occur"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x) = \\mathrm{Prob}(X=x).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Stochastic variables and the main concepts, the continuous case\n",
|
|
"In the continuous case, the PDF does not directly depict the\n",
|
|
"actual probability. Instead we define the probability for the\n",
|
|
"stochastic variable to assume any value on an infinitesimal interval\n",
|
|
"around $x$ to be $p(x)dx$. The continuous function $p(x)$ then gives us\n",
|
|
"the *density* of the probability rather than the probability\n",
|
|
"itself. The probability for a stochastic variable to assume any value\n",
|
|
"on a non-infinitesimal interval $[a,\\,b]$ is then just the integral"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathrm{Prob}(a\\leq X\\leq b) = \\int_a^b p(x)dx.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Qualitatively speaking, a stochastic variable represents the values of\n",
|
|
"numbers chosen as if by chance from some specified PDF so that the\n",
|
|
"selection of a large set of these numbers reproduces this PDF.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## The cumulative probability\n",
|
|
"Of interest to us is the *cumulative probability\n",
|
|
"distribution function* (**CDF**), $P(x)$, which is just the probability\n",
|
|
"for a stochastic variable $X$ to assume any value less than $x$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"P(x)=\\mathrm{Prob(}X\\leq x\\mathrm{)} =\n",
|
|
"\\int_{-\\infty}^x p(x^{\\prime})dx^{\\prime}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The relation between a CDF and its corresponding PDF is then"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x) = \\frac{d}{dx}P(x).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Properties of PDFs\n",
|
|
"\n",
|
|
"There are two properties that all PDFs must satisfy. The first one is\n",
|
|
"positivity (assuming that the PDF is normalized)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"0 \\leq p(x) \\leq 1.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Naturally, it would be nonsensical for any of the values of the domain\n",
|
|
"to occur with a probability greater than $1$ or less than $0$. Also,\n",
|
|
"the PDF must be normalized. That is, all the probabilities must add up\n",
|
|
"to unity. The probability of \"anything\" to happen is always unity. For\n",
|
|
"both discrete and continuous PDFs, this condition is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{align*}\n",
|
|
"\\sum_{x_i\\in\\mathbb D} p(x_i) & = 1,\\\\\n",
|
|
"\\int_{x\\in\\mathbb D} p(x)\\,dx & = 1.\n",
|
|
"\\end{align*}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Important distributions, the uniform distribution\n",
|
|
"The first one\n",
|
|
"is the most basic PDF; namely the uniform distribution"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:unifromPDF\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"p(x) = \\frac{1}{b-a}\\theta(x-a)\\theta(b-x).\n",
|
|
"\\label{eq:unifromPDF} \\tag{1}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"For $a=0$ and $b=1$ we have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{array}{ll}\n",
|
|
"p(x)dx = dx & \\in [0,1].\n",
|
|
"\\end{array}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The latter distribution is used to generate random numbers. For other PDFs, one needs normally a mapping from this distribution to say for example the exponential distribution.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Gaussian distribution\n",
|
|
"The second one is the Gaussian Distribution"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}} \\exp{(-\\frac{(x-\\mu)^2}{2\\sigma^2})},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with mean value $\\mu$ and standard deviation $\\sigma$. If $\\mu=0$ and $\\sigma=1$, it is normally called the **standard normal distribution**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x) = \\frac{1}{\\sqrt{2\\pi}} \\exp{(-\\frac{x^2}{2})},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The following simple Python code plots the above distribution for different values of $\\mu$ and $\\sigma$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%matplotlib inline\n",
|
|
"\n",
|
|
"import numpy as np\n",
|
|
"from math import acos, exp, sqrt\n",
|
|
"from matplotlib import pyplot as plt\n",
|
|
"from matplotlib import rc, rcParams\n",
|
|
"import matplotlib.units as units\n",
|
|
"import matplotlib.ticker as ticker\n",
|
|
"rc('text',usetex=True)\n",
|
|
"rc('font',**{'family':'serif','serif':['Gaussian distribution']})\n",
|
|
"font = {'family' : 'serif',\n",
|
|
" 'color' : 'darkred',\n",
|
|
" 'weight' : 'normal',\n",
|
|
" 'size' : 16,\n",
|
|
" }\n",
|
|
"pi = acos(-1.0)\n",
|
|
"mu0 = 0.0\n",
|
|
"sigma0 = 1.0\n",
|
|
"mu1= 1.0\n",
|
|
"sigma1 = 2.0\n",
|
|
"mu2 = 2.0\n",
|
|
"sigma2 = 4.0\n",
|
|
"\n",
|
|
"x = np.linspace(-20.0, 20.0)\n",
|
|
"v0 = np.exp(-(x*x-2*x*mu0+mu0*mu0)/(2*sigma0*sigma0))/sqrt(2*pi*sigma0*sigma0)\n",
|
|
"v1 = np.exp(-(x*x-2*x*mu1+mu1*mu1)/(2*sigma1*sigma1))/sqrt(2*pi*sigma1*sigma1)\n",
|
|
"v2 = np.exp(-(x*x-2*x*mu2+mu2*mu2)/(2*sigma2*sigma2))/sqrt(2*pi*sigma2*sigma2)\n",
|
|
"plt.plot(x, v0, 'b-', x, v1, 'r-', x, v2, 'g-')\n",
|
|
"plt.title(r'{\\bf Gaussian distributions}', fontsize=20)\n",
|
|
"plt.text(-19, 0.3, r'Parameters: $\\mu = 0$, $\\sigma = 1$', fontdict=font)\n",
|
|
"plt.text(-19, 0.18, r'Parameters: $\\mu = 1$, $\\sigma = 2$', fontdict=font)\n",
|
|
"plt.text(-19, 0.08, r'Parameters: $\\mu = 2$, $\\sigma = 4$', fontdict=font)\n",
|
|
"plt.xlabel(r'$x$',fontsize=20)\n",
|
|
"plt.ylabel(r'$p(x)$ [MeV]',fontsize=20)\n",
|
|
"\n",
|
|
"# Tweak spacing to prevent clipping of ylabel \n",
|
|
"plt.subplots_adjust(left=0.15)\n",
|
|
"plt.savefig('gaussian.pdf', format='pdf')\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Exponential distribution\n",
|
|
"Another important distribution in science is the exponential distribution"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x) = \\alpha\\exp{-(\\alpha x)}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Expectation values\n",
|
|
"Let $h(x)$ be an arbitrary continuous function on the domain of the stochastic\n",
|
|
"variable $X$ whose PDF is $p(x)$. We define the *expectation value*\n",
|
|
"of $h$ with respect to $p$ as follows"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:expectation_value_of_h_wrt_p\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\langle h \\rangle_X \\equiv \\int\\! h(x)p(x)\\,dx\n",
|
|
"\\label{eq:expectation_value_of_h_wrt_p} \\tag{2}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Whenever the PDF is known implicitly, like in this case, we will drop\n",
|
|
"the index $X$ for clarity. \n",
|
|
"A particularly useful class of special expectation values are the\n",
|
|
"*moments*. The $n$-th moment of the PDF $p$ is defined as\n",
|
|
"follows"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x^n \\rangle \\equiv \\int\\! x^n p(x)\\,dx\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Stochastic variables and the main concepts, mean values\n",
|
|
"The zero-th moment $\\langle 1\\rangle$ is just the normalization condition of\n",
|
|
"$p$. The first moment, $\\langle x\\rangle$, is called the *mean* of $p$\n",
|
|
"and often denoted by the letter $\\mu$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x\\rangle = \\mu \\equiv \\int x p(x)dx,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"for a continuous distribution and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x\\rangle = \\mu \\equiv \\sum_{i=1}^N x_i p(x_i),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"for a discrete distribution. \n",
|
|
"Qualitatively it represents the centroid or the average value of the\n",
|
|
"PDF and is therefore simply called the expectation value of $p(x)$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Stochastic variables and the main concepts, central moments, the variance\n",
|
|
"\n",
|
|
"A special version of the moments is the set of *central moments*, the n-th central moment defined as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle (x-\\langle x\\rangle )^n\\rangle \\equiv \\int\\! (x-\\langle x\\rangle)^n p(x)\\,dx\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The zero-th and first central moments are both trivial, equal $1$ and\n",
|
|
"$0$, respectively. But the second central moment, known as the\n",
|
|
"*variance* of $p$, is of particular interest. For the stochastic\n",
|
|
"variable $X$, the variance is denoted as $\\sigma^2_X$ or $\\mathrm{Var}(X)$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{align*}\n",
|
|
"\\sigma^2_X &=\\mathrm{Var}(X) = \\langle (x-\\langle x\\rangle)^2\\rangle =\n",
|
|
"\\int (x-\\langle x\\rangle)^2 p(x)dx\\\\\n",
|
|
"& = \\int\\left(x^2 - 2 x \\langle x\\rangle^{2} +\\langle x\\rangle^2\\right)p(x)dx\\\\\n",
|
|
"& = \\langle x^2\\rangle - 2 \\langle x\\rangle\\langle x\\rangle + \\langle x\\rangle^2\\\\\n",
|
|
"& = \\langle x^2 \\rangle - \\langle x\\rangle^2\n",
|
|
"\\end{align*}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The square root of the variance, $\\sigma =\\sqrt{\\langle (x-\\langle x\\rangle)^2\\rangle}$ is called the \n",
|
|
"**standard deviation** of $p$. It is the RMS (root-mean-square)\n",
|
|
"value of the deviation of the PDF from its mean value, interpreted\n",
|
|
"qualitatively as the \"spread\" of $p$ around its mean.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Probability Distribution Functions\n",
|
|
"\n",
|
|
"The following table collects properties of probability distribution functions.\n",
|
|
"In our notation we reserve the label $p(x)$ for the probability of a certain event,\n",
|
|
"while $P(x)$ is the cumulative probability. \n",
|
|
"\n",
|
|
"\n",
|
|
"<table border=\"1\">\n",
|
|
"<thead>\n",
|
|
"<tr><th align=\"center\"> </th> <th align=\"center\"> Discrete PDF </th> <th align=\"center\"> Continuous PDF </th> </tr>\n",
|
|
"</thead>\n",
|
|
"<tbody>\n",
|
|
"<tr><td align=\"left\"> Domain </td> <td align=\"center\"> $\\left\\{x_1, x_2, x_3, \\dots, x_N\\right\\}$ </td> <td align=\"center\"> $[a,b]$ </td> </tr>\n",
|
|
"<tr><td align=\"left\"> Probability </td> <td align=\"center\"> $p(x_i)$ </td> <td align=\"center\"> $p(x)dx$ </td> </tr>\n",
|
|
"<tr><td align=\"left\"> Cumulative </td> <td align=\"center\"> $P_i=\\sum_{l=1}^ip(x_l)$ </td> <td align=\"center\"> $P(x)=\\int_a^xp(t)dt$ </td> </tr>\n",
|
|
"<tr><td align=\"left\"> Positivity </td> <td align=\"center\"> $0 \\le p(x_i) \\le 1$ </td> <td align=\"center\"> $p(x) \\ge 0$ </td> </tr>\n",
|
|
"<tr><td align=\"left\"> Positivity </td> <td align=\"center\"> $0 \\le P_i \\le 1$ </td> <td align=\"center\"> $0 \\le P(x) \\le 1$ </td> </tr>\n",
|
|
"<tr><td align=\"left\"> Monotonic </td> <td align=\"center\"> $P_i \\ge P_j$ if $x_i \\ge x_j$ </td> <td align=\"center\"> $P(x_i) \\ge P(x_j)$ if $x_i \\ge x_j$ </td> </tr>\n",
|
|
"<tr><td align=\"left\"> Normalization </td> <td align=\"center\"> $P_N=1$ </td> <td align=\"center\"> $P(b)=1$ </td> </tr>\n",
|
|
"</tbody>\n",
|
|
"</table>\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Probability Distribution Functions\n",
|
|
"With a PDF we can compute expectation values of selected quantities such as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x^k\\rangle=\\sum_{i=1}^{N}x_i^kp(x_i),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"if we have a discrete PDF or"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x^k\\rangle=\\int_a^b x^kp(x)dx,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"in the case of a continuous PDF. We have already defined the mean value $\\mu$\n",
|
|
"and the variance $\\sigma^2$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## The three famous Probability Distribution Functions\n",
|
|
"\n",
|
|
"There are at least three PDFs which one may encounter. These are the\n",
|
|
"\n",
|
|
"**Uniform distribution**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x)=\\frac{1}{b-a}\\Theta(x-a)\\Theta(b-x),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"yielding probabilities different from zero in the interval $[a,b]$.\n",
|
|
"\n",
|
|
"**The exponential distribution**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x)=\\alpha \\exp{(-\\alpha x)},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"yielding probabilities different from zero in the interval $[0,\\infty)$ and with mean value"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu = \\int_0^{\\infty}xp(x)dx=\\int_0^{\\infty}x\\alpha \\exp{(-\\alpha x)}dx=\\frac{1}{\\alpha},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with variance"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma^2=\\int_0^{\\infty}x^2p(x)dx-\\mu^2 = \\frac{1}{\\alpha^2}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Probability Distribution Functions, the normal distribution\n",
|
|
"Finally, we have the so-called univariate normal distribution, or just the **normal distribution**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x)=\\frac{1}{b\\sqrt{2\\pi}}\\exp{\\left(-\\frac{(x-a)^2}{2b^2}\\right)}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with probabilities different from zero in the interval $(-\\infty,\\infty)$.\n",
|
|
"The integral $\\int_{-\\infty}^{\\infty}\\exp{\\left(-(x^2\\right)}dx$ appears in many calculations, its value\n",
|
|
"is $\\sqrt{\\pi}$, a result we will need when we compute the mean value and the variance.\n",
|
|
"The mean value is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu = \\int_0^{\\infty}xp(x)dx=\\frac{1}{b\\sqrt{2\\pi}}\\int_{-\\infty}^{\\infty}x \\exp{\\left(-\\frac{(x-a)^2}{2b^2}\\right)}dx,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which becomes with a suitable change of variables"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu =\\frac{1}{b\\sqrt{2\\pi}}\\int_{-\\infty}^{\\infty}b\\sqrt{2}(a+b\\sqrt{2}y)\\exp{-y^2}dy=a.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Probability Distribution Functions, the normal distribution\n",
|
|
"Similarly, the variance becomes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma^2 = \\frac{1}{b\\sqrt{2\\pi}}\\int_{-\\infty}^{\\infty}(x-\\mu)^2 \\exp{\\left(-\\frac{(x-a)^2}{2b^2}\\right)}dx,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and inserting the mean value and performing a variable change we obtain"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma^2 = \\frac{1}{b\\sqrt{2\\pi}}\\int_{-\\infty}^{\\infty}b\\sqrt{2}(b\\sqrt{2}y)^2\\exp{\\left(-y^2\\right)}dy=\n",
|
|
"\\frac{2b^2}{\\sqrt{\\pi}}\\int_{-\\infty}^{\\infty}y^2\\exp{\\left(-y^2\\right)}dy,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and performing a final integration by parts we obtain the well-known result $\\sigma^2=b^2$.\n",
|
|
"It is useful to introduce the standard normal distribution as well, defined by $\\mu=a=0$, viz. a distribution\n",
|
|
"centered around zero and with a variance $\\sigma^2=1$, leading to"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto1\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
" p(x)=\\frac{1}{\\sqrt{2\\pi}}\\exp{\\left(-\\frac{x^2}{2}\\right)}.\n",
|
|
"\\label{_auto1} \\tag{3}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Probability Distribution Functions, the cumulative distribution\n",
|
|
"\n",
|
|
"The exponential and uniform distributions have simple cumulative functions,\n",
|
|
"whereas the normal distribution does not, being proportional to the so-called\n",
|
|
"error function $erf(x)$, given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"P(x) = \\frac{1}{\\sqrt{2\\pi}}\\int_{-\\infty}^x\\exp{\\left(-\\frac{t^2}{2}\\right)}dt,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which is difficult to evaluate in a quick way.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Probability Distribution Functions, other important distribution\n",
|
|
"\n",
|
|
"Some other PDFs which one encounters often in the natural sciences are the binomial distribution"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x) = \\left(\\begin{array}{c} n \\\\ x\\end{array}\\right)y^x(1-y)^{n-x} \\hspace{0.5cm}x=0,1,\\dots,n,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where $y$ is the probability for a specific event, such as the tossing of a coin or moving left or right\n",
|
|
"in case of a random walker. Note that $x$ is a discrete stochastic variable. \n",
|
|
"\n",
|
|
"The sequence of binomial trials is characterized by the following definitions\n",
|
|
"\n",
|
|
" * Every experiment is thought to consist of $N$ independent trials.\n",
|
|
"\n",
|
|
" * In every independent trial one registers if a specific situation happens or not, such as the jump to the left or right of a random walker.\n",
|
|
"\n",
|
|
" * The probability for every outcome in a single trial has the same value, for example the outcome of tossing (either heads or tails) a coin is always $1/2$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Probability Distribution Functions, the binomial distribution\n",
|
|
"\n",
|
|
"In order to compute the mean and variance we need to recall Newton's binomial\n",
|
|
"formula"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"(a+b)^m=\\sum_{n=0}^m \\left(\\begin{array}{c} m \\\\ n\\end{array}\\right)a^nb^{m-n},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which can be used to show that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sum_{x=0}^n\\left(\\begin{array}{c} n \\\\ x\\end{array}\\right)y^x(1-y)^{n-x} = (y+1-y)^n = 1,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"the PDF is normalized to one. \n",
|
|
"The mean value is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu = \\sum_{x=0}^n x\\left(\\begin{array}{c} n \\\\ x\\end{array}\\right)y^x(1-y)^{n-x} =\n",
|
|
"\\sum_{x=0}^n x\\frac{n!}{x!(n-x)!}y^x(1-y)^{n-x},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"resulting in"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu = \n",
|
|
"\\sum_{x=0}^n x\\frac{(n-1)!}{(x-1)!(n-1-(x-1))!}y^{x-1}(1-y)^{n-1-(x-1)},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which we rewrite as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu=ny\\sum_{\\nu=0}^n\\left(\\begin{array}{c} n-1 \\\\ \\nu\\end{array}\\right)y^{\\nu}(1-y)^{n-1-\\nu} =ny(y+1-y)^{n-1}=ny.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The variance is slightly trickier to get. It reads $\\sigma^2=ny(1-y)$. \n",
|
|
"\n",
|
|
"\n",
|
|
"## Probability Distribution Functions, Poisson's distribution\n",
|
|
"\n",
|
|
"Another important distribution with discrete stochastic variables $x$ is \n",
|
|
"the Poisson model, which resembles the exponential distribution and reads"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"p(x) = \\frac{\\lambda^x}{x!} e^{-\\lambda} \\hspace{0.5cm}x=0,1,\\dots,;\\lambda > 0.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"In this case both the mean value and the variance are easier to calculate,"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu = \\sum_{x=0}^{\\infty} x \\frac{\\lambda^x}{x!} e^{-\\lambda} = \\lambda e^{-\\lambda}\\sum_{x=1}^{\\infty}\n",
|
|
"\\frac{\\lambda^{x-1}}{(x-1)!}=\\lambda,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and the variance is $\\sigma^2=\\lambda$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Probability Distribution Functions, Poisson's distribution\n",
|
|
"An example of applications of the Poisson distribution could be the counting\n",
|
|
"of the number of $\\alpha$-particles emitted from a radioactive source in a given time interval.\n",
|
|
"In the limit of $n\\rightarrow \\infty$ and for small probabilities $y$, the binomial distribution\n",
|
|
"approaches the Poisson distribution. Setting $\\lambda = ny$, with $y$ the probability for an event in\n",
|
|
"the binomial distribution we can show that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\lim_{n\\rightarrow \\infty}\\left(\\begin{array}{c} n \\\\ x\\end{array}\\right)y^x(1-y)^{n-x} e^{-\\lambda}=\\sum_{x=1}^{\\infty}\\frac{\\lambda^x}{x!} e^{-\\lambda}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Meet the covariance!\n",
|
|
"An important quantity in a statistical analysis is the so-called covariance. \n",
|
|
"\n",
|
|
"Consider the set $\\{X_i\\}$ of $n$\n",
|
|
"stochastic variables (not necessarily uncorrelated) with the\n",
|
|
"multivariate PDF $P(x_1,\\dots,x_n)$. The *covariance* of two\n",
|
|
"of the stochastic variables, $X_i$ and $X_j$, is defined as follows"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"_auto2\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\mathrm{Cov}(X_i,\\,X_j) = \\langle (x_i-\\langle x_i\\rangle)(x_j-\\langle x_j\\rangle)\\rangle \n",
|
|
"\\label{_auto2} \\tag{4}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:def_covariance\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation} \n",
|
|
"=\\int\\cdots\\int (x_i-\\langle x_i\\rangle)(x_j-\\langle x_j\\rangle)P(x_1,\\dots,x_n)\\,dx_1\\dots dx_n,\n",
|
|
"\\label{eq:def_covariance} \\tag{5}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x_i\\rangle =\n",
|
|
"\\int\\cdots\\int x_i P(x_1,\\dots,x_n)\\,dx_1\\dots dx_n.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Meet the covariance in matrix disguise\n",
|
|
"If we consider the above covariance as a matrix"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"C_{ij} =\\mathrm{Cov}(X_i,\\,X_j),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"then the diagonal elements are just the familiar\n",
|
|
"variances, $C_{ii} = \\mathrm{Cov}(X_i,\\,X_i) = \\mathrm{Var}(X_i)$. It turns out that\n",
|
|
"all the off-diagonal elements are zero if the stochastic variables are\n",
|
|
"uncorrelated.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Covariance"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Importing various packages\n",
|
|
"from math import exp, sqrt\n",
|
|
"from random import random, seed\n",
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"\n",
|
|
"def covariance(x, y, n):\n",
|
|
" sum = 0.0\n",
|
|
" mean_x = np.mean(x)\n",
|
|
" mean_y = np.mean(y)\n",
|
|
" for i in range(0, n):\n",
|
|
" sum += (x[(i)]-mean_x)*(y[i]-mean_y)\n",
|
|
" return sum/n\n",
|
|
"\n",
|
|
"n = 10\n",
|
|
"\n",
|
|
"x=np.random.normal(size=n)\n",
|
|
"y = 4+3*x+np.random.normal(size=n)\n",
|
|
"covxy = covariance(x,y,n)\n",
|
|
"print(covxy)\n",
|
|
"z = np.vstack((x, y))\n",
|
|
"c = np.cov(z.T)\n",
|
|
"\n",
|
|
"print(c)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Meet the covariance, uncorrelated events\n",
|
|
"\n",
|
|
"Consider the stochastic variables $X_i$ and $X_j$, ($i\\neq j$). We have"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\begin{align*}\n",
|
|
"Cov(X_i,\\,X_j) &= \\langle (x_i-\\langle x_i\\rangle)(x_j-\\langle x_j\\rangle)\\rangle\\\\\n",
|
|
"&=\\langle x_i x_j - x_i\\langle x_j\\rangle - \\langle x_i\\rangle x_j + \\langle x_i\\rangle\\langle x_j\\rangle\\rangle\\\\\n",
|
|
"&=\\langle x_i x_j\\rangle - \\langle x_i\\langle x_j\\rangle\\rangle - \\langle \\langle x_i\\rangle x_j \\rangle +\n",
|
|
"\\langle \\langle x_i\\rangle\\langle x_j\\rangle\\rangle \\\\\n",
|
|
"&=\\langle x_i x_j\\rangle - \\langle x_i\\rangle\\langle x_j\\rangle - \\langle x_i\\rangle\\langle x_j\\rangle +\n",
|
|
"\\langle x_i\\rangle\\langle x_j\\rangle \\\\\n",
|
|
"&=\\langle x_i x_j\\rangle - \\langle x_i\\rangle\\langle x_j\\rangle\n",
|
|
"\\end{align*}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"If $X_i$ and $X_j$ are independent (assuming $i \\neq j$), we have that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x_i x_j\\rangle = \\langle x_i\\rangle\\langle x_j\\rangle,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"leading to"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"Cov(X_i, X_j) = 0 \\hspace{0.1cm} (i\\neq j).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Numerical experiments and the covariance\n",
|
|
"\n",
|
|
"Now that we have constructed an idealized mathematical framework, let\n",
|
|
"us try to apply it to empirical observations. Examples of relevant\n",
|
|
"physical phenomena may be spontaneous decays of nuclei, or a purely\n",
|
|
"mathematical set of numbers produced by some deterministic\n",
|
|
"mechanism. It is the latter we will deal with, using so-called pseudo-random\n",
|
|
"number generators. In general our observations will contain only a limited set of\n",
|
|
"observables. We remind the reader that\n",
|
|
"a *stochastic process* is a process that produces sequentially a\n",
|
|
"chain of values"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\{x_1, x_2,\\dots\\,x_k,\\dots\\}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Numerical experiments and the covariance\n",
|
|
"We will call these\n",
|
|
"values our *measurements* and the entire set as our measured\n",
|
|
"*sample*. The action of measuring all the elements of a sample\n",
|
|
"we will call a stochastic *experiment* (since, operationally,\n",
|
|
"they are often associated with results of empirical observation of\n",
|
|
"some physical or mathematical phenomena; precisely an experiment). We\n",
|
|
"assume that these values are distributed according to some \n",
|
|
"PDF $p_X^{\\phantom X}(x)$, where $X$ is just the formal symbol for the\n",
|
|
"stochastic variable whose PDF is $p_X^{\\phantom X}(x)$. Instead of\n",
|
|
"trying to determine the full distribution $p$ we are often only\n",
|
|
"interested in finding the few lowest moments, like the mean\n",
|
|
"$\\mu_X^{\\phantom X}$ and the variance $\\sigma_X^{\\phantom X}$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Numerical experiments and the covariance, actual situations\n",
|
|
"In practical situations however, a sample is always of finite size. Let that\n",
|
|
"size be $n$. The expectation value of a sample $\\alpha$, the **sample mean**, is then defined as follows"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\langle x_{\\alpha} \\rangle \\equiv \\frac{1}{n}\\sum_{k=1}^n x_{\\alpha,k}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The *sample variance* is:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mathrm{Var}(x) \\equiv \\frac{1}{n}\\sum_{k=1}^n (x_{\\alpha,k} - \\langle x_{\\alpha} \\rangle)^2,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with its square root being the *standard deviation of the sample*.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Numerical experiments and the covariance, our observables\n",
|
|
"You can think of the above observables as a set of quantities which define\n",
|
|
"a given experiment. This experiment is then repeated several times, say $m$ times.\n",
|
|
"The total average is then"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:exptmean\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\langle X_m \\rangle= \\frac{1}{m}\\sum_{\\alpha=1}^mx_{\\alpha}=\\frac{1}{mn}\\sum_{\\alpha, k} x_{\\alpha,k},\n",
|
|
"\\label{eq:exptmean} \\tag{6}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where the last sums end at $m$ and $n$.\n",
|
|
"The total variance is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma^2_m= \\frac{1}{mn^2}\\sum_{\\alpha=1}^m(\\langle x_{\\alpha} \\rangle-\\langle X_m \\rangle)^2,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which we rewrite as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:exptvariance\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\sigma^2_m=\\frac{1}{m}\\sum_{\\alpha=1}^m\\sum_{kl=1}^n (x_{\\alpha,k}-\\langle X_m \\rangle)(x_{\\alpha,l}-\\langle X_m \\rangle).\n",
|
|
"\\label{eq:exptvariance} \\tag{7}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Numerical experiments and the covariance, the sample variance\n",
|
|
"\n",
|
|
"We define also the sample variance $\\sigma^2$ of all $mn$ individual experiments as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:sampleexptvariance\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\sigma^2=\\frac{1}{mn}\\sum_{\\alpha=1}^m\\sum_{k=1}^n (x_{\\alpha,k}-\\langle X_m \\rangle)^2.\n",
|
|
"\\label{eq:sampleexptvariance} \\tag{8}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"These quantities, being known experimental values or the results from our calculations, \n",
|
|
"may differ, in some cases\n",
|
|
"significantly, from the similarly named\n",
|
|
"exact values for the mean value $\\mu_X$, the variance $\\mathrm{Var}(X)$\n",
|
|
"and the covariance $\\mathrm{Cov}(X,Y)$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Numerical experiments and the covariance, central limit theorem\n",
|
|
"\n",
|
|
"The central limit theorem states that the PDF $\\tilde{p}(z)$ of\n",
|
|
"the average of $m$ random values corresponding to a PDF $p(x)$ \n",
|
|
"is a normal distribution whose mean is the \n",
|
|
"mean value of the PDF $p(x)$ and whose variance is the variance\n",
|
|
"of the PDF $p(x)$ divided by $m$, the number of values used to compute $z$.\n",
|
|
"\n",
|
|
"The central limit theorem leads then to the well-known expression for the\n",
|
|
"standard deviation, given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma_m=\n",
|
|
"\\frac{\\sigma}{\\sqrt{m}}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"In many cases the above estimate for the standard deviation, in particular if correlations are strong, may be too simplistic. We need therefore a more precise defintion of the error and the variance in our results.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Definition of Correlation Functions and Standard Deviation\n",
|
|
"Our estimate of the true average $\\mu_{X}$ is the sample mean $\\langle X_m \\rangle$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu_{X}^{\\phantom X} \\approx X_m=\\frac{1}{mn}\\sum_{\\alpha=1}^m\\sum_{k=1}^n x_{\\alpha,k}.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We can then use Eq. ([7](#eq:exptvariance))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma^2_m=\\frac{1}{mn^2}\\sum_{\\alpha=1}^m\\sum_{kl=1}^n (x_{\\alpha,k}-\\langle X_m \\rangle)(x_{\\alpha,l}-\\langle X_m \\rangle),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and rewrite it as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma^2_m=\\frac{\\sigma^2}{n}+\\frac{2}{mn^2}\\sum_{\\alpha=1}^m\\sum_{k<l}^n (x_{\\alpha,k}-\\langle X_m \\rangle)(x_{\\alpha,l}-\\langle X_m \\rangle),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where the first term is the sample variance of all $mn$ experiments divided by $n$\n",
|
|
"and the last term is nothing but the covariance which arises when $k\\ne l$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Definition of Correlation Functions and Standard Deviation\n",
|
|
"Our estimate of the true average $\\mu_{X}$ is the sample mean $\\langle X_m \\rangle$\n",
|
|
"\n",
|
|
"If the \n",
|
|
"observables are uncorrelated, then the covariance is zero and we obtain a total variance\n",
|
|
"which agrees with the central limit theorem. Correlations may often be present in our data set, resulting in a non-zero covariance. The first term is normally called the uncorrelated \n",
|
|
"contribution.\n",
|
|
"Computationally the uncorrelated first term is much easier to treat\n",
|
|
"efficiently than the second.\n",
|
|
"We just accumulate separately the values $x^2$ and $x$ for every\n",
|
|
"measurement $x$ we receive. The correlation term, though, has to be\n",
|
|
"calculated at the end of the experiment since we need all the\n",
|
|
"measurements to calculate the cross terms. Therefore, all measurements\n",
|
|
"have to be stored throughout the experiment.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Definition of Correlation Functions and Standard Deviation\n",
|
|
"\n",
|
|
"Let us analyze the problem by splitting up the correlation term into\n",
|
|
"partial sums of the form"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"f_d = \\frac{1}{nm}\\sum_{\\alpha=1}^m\\sum_{k=1}^{n-d}(x_{\\alpha,k}-\\langle X_m \\rangle)(x_{\\alpha,k+d}-\\langle X_m \\rangle),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The correlation term of the total variance can now be rewritten in terms of\n",
|
|
"$f_d$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\frac{2}{mn^2}\\sum_{\\alpha=1}^m\\sum_{k<l}^n (x_{\\alpha,k}-\\langle X_m \\rangle)(x_{\\alpha,l}-\\langle X_m \\rangle)=\n",
|
|
"\\frac{2}{n}\\sum_{d=1}^{n-1} f_d\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Definition of Correlation Functions and Standard Deviation\n",
|
|
"The value of $f_d$ reflects the correlation between measurements\n",
|
|
"separated by the distance $d$ in the samples. Notice that for\n",
|
|
"$d=0$, $f$ is just the sample variance, $\\sigma^2$. If we divide $f_d$\n",
|
|
"by $\\sigma^2$, we arrive at the so called **autocorrelation function**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:autocorrelformal\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\kappa_d = \\frac{f_d}{\\sigma^2}\n",
|
|
"\\label{eq:autocorrelformal} \\tag{9}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which gives us a useful measure of the correlation pair correlation\n",
|
|
"starting always at $1$ for $d=0$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Definition of Correlation Functions and Standard Deviation, sample variance\n",
|
|
"\n",
|
|
"The sample variance of the $mn$ experiments can now be\n",
|
|
"written in terms of the autocorrelation function"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:error_estimate_corr_time\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\sigma_m^2=\\frac{\\sigma^2}{n}+\\frac{2}{n}\\cdot\\sigma^2\\sum_{d=1}^{n-1}\n",
|
|
"\\frac{f_d}{\\sigma^2}=\\left(1+2\\sum_{d=1}^{n-1}\\kappa_d\\right)\\frac{1}{n}\\sigma^2=\\frac{\\tau}{n}\\cdot\\sigma^2\n",
|
|
"\\label{eq:error_estimate_corr_time} \\tag{10}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and we see that $\\sigma_m$ can be expressed in terms of the\n",
|
|
"uncorrelated sample variance times a correction factor $\\tau$ which\n",
|
|
"accounts for the correlation between measurements. We call this\n",
|
|
"correction factor the *autocorrelation time*"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:autocorrelation_time\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"\\tau = 1+2\\sum_{d=1}^{n-1}\\kappa_d\n",
|
|
"\\label{eq:autocorrelation_time} \\tag{11}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- It is closely related to the area under the graph of the -->\n",
|
|
"<!-- autocorrelation function. -->\n",
|
|
"For a correlation free experiment, $\\tau$\n",
|
|
"equals 1.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Definition of Correlation Functions and Standard Deviation\n",
|
|
"From the point of view of\n",
|
|
"Eq. ([10](#eq:error_estimate_corr_time)) we can interpret a sequential\n",
|
|
"correlation as an effective reduction of the number of measurements by\n",
|
|
"a factor $\\tau$. The effective number of measurements becomes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"n_\\mathrm{eff} = \\frac{n}{\\tau}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"To neglect the autocorrelation time $\\tau$ will always cause our\n",
|
|
"simple uncorrelated estimate of $\\sigma_m^2\\approx \\sigma^2/n$ to\n",
|
|
"be less than the true sample error. The estimate of the error will be\n",
|
|
"too \"good\". On the other hand, the calculation of the full\n",
|
|
"autocorrelation time poses an efficiency problem if the set of\n",
|
|
"measurements is very large. The solution to this problem is given by \n",
|
|
"more practically oriented methods like the blocking technique.\n",
|
|
"<!-- add ref here to flybjerg -->\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Code to compute the Covariance matrix and the Covariance"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Importing various packages\n",
|
|
"from math import exp, sqrt\n",
|
|
"from random import random, seed\n",
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"\n",
|
|
"# Sample covariance, note the factor 1/(n-1)\n",
|
|
"def covariance(x, y, n):\n",
|
|
" sum = 0.0\n",
|
|
" mean_x = np.mean(x)\n",
|
|
" mean_y = np.mean(y)\n",
|
|
" for i in range(0, n):\n",
|
|
" sum += (x[(i)]-mean_x)*(y[i]-mean_y)\n",
|
|
" return sum/(n-1.)\n",
|
|
"\n",
|
|
"n = 100\n",
|
|
"x = np.random.normal(size=n)\n",
|
|
"print(np.mean(x))\n",
|
|
"y = 4+3*x+np.random.normal(size=n)\n",
|
|
"print(np.mean(y))\n",
|
|
"z = x**3+np.random.normal(size=n)\n",
|
|
"print(np.mean(z))\n",
|
|
"covxx = covariance(x,x,n)\n",
|
|
"covyy = covariance(y,y,n)\n",
|
|
"covzz = covariance(z,z,n)\n",
|
|
"covxy = covariance(x,y,n)\n",
|
|
"covxz = covariance(x,z,n)\n",
|
|
"covyz = covariance(y,z,n)\n",
|
|
"print(covxx,covyy, covzz)\n",
|
|
"print(covxy,covxz, covyz)\n",
|
|
"w = np.vstack((x, y, z))\n",
|
|
"#print(w)\n",
|
|
"c = np.cov(w)\n",
|
|
"print(c)\n",
|
|
"#eigen = np.zeros(n)\n",
|
|
"Eigvals, Eigvecs = np.linalg.eig(c)\n",
|
|
"print(Eigvals)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Random Numbers\n",
|
|
"\n",
|
|
"Uniform deviates are just random numbers that lie within a specified range\n",
|
|
"(typically 0 to 1), with any one number in the range just as likely as any other. They\n",
|
|
"are, in other words, what you probably think random numbers are. However,\n",
|
|
"we want to distinguish uniform deviates from other sorts of random numbers, for\n",
|
|
"example numbers drawn from a normal (Gaussian) distribution of specified mean\n",
|
|
"and standard deviation. These other sorts of deviates are almost always generated by\n",
|
|
"performing appropriate operations on one or more uniform deviates, as we will see\n",
|
|
"in subsequent sections. So, a reliable source of random uniform deviates, the subject\n",
|
|
"of this section, is an essential building block for any sort of stochastic modeling\n",
|
|
"or Monte Carlo computer work.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random Numbers, better name: pseudo random numbers\n",
|
|
"\n",
|
|
"A disclaimer is however appropriate. It should be fairly obvious that \n",
|
|
"something as deterministic as a computer cannot generate purely random numbers.\n",
|
|
"\n",
|
|
"Numbers generated by any of the standard algorithms are in reality pseudo random\n",
|
|
"numbers, hopefully abiding to the following criteria:\n",
|
|
"\n",
|
|
" * they produce a uniform distribution in the interval [0,1].\n",
|
|
"\n",
|
|
" * correlations between random numbers are negligible\n",
|
|
"\n",
|
|
" * the period before the same sequence of random numbers is repeated is as large as possible and finally\n",
|
|
"\n",
|
|
" * the algorithm should be fast.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG\n",
|
|
" The most common random number generators are based on so-called\n",
|
|
"Linear congruential relations of the type"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"N_i=(aN_{i-1}+c) \\mathrm{MOD} (M),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which yield a number in the interval [0,1] through"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"x_i=N_i/M\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The number \n",
|
|
"$M$ is called the period and it should be as large as possible \n",
|
|
" and \n",
|
|
"$N_0$ is the starting value, or seed. The function $\\mathrm{MOD}$ means the remainder,\n",
|
|
"that is if we were to evaluate $(13)\\mathrm{MOD}(9)$, the outcome is the remainder\n",
|
|
"of the division $13/9$, namely $4$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG and periodic outputs\n",
|
|
"\n",
|
|
"The problem with such generators is that their outputs are periodic;\n",
|
|
"they \n",
|
|
"will start to repeat themselves with a period that is at most $M$. If however\n",
|
|
"the parameters $a$ and $c$ are badly chosen, the period may be even shorter.\n",
|
|
"\n",
|
|
"Consider the following example"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"N_i=(6N_{i-1}+7) \\mathrm{MOD} (5),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with a seed $N_0=2$. This generator produces the sequence\n",
|
|
"$4,1,3,0,2,4,1,3,0,2,...\\dots$, i.e., a sequence with period $5$.\n",
|
|
"However, increasing $M$ may not guarantee a larger period as the following\n",
|
|
"example shows"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"N_i=(27N_{i-1}+11) \\mathrm{MOD} (54),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which still, with $N_0=2$, results in $11,38,11,38,11,38,\\dots$, a period of\n",
|
|
"just $2$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG and its period\n",
|
|
"Typical periods for the random generators provided in the program library \n",
|
|
"are of the order of $\\sim 10^9$ or larger. Other random number generators which have\n",
|
|
"become increasingly popular are so-called shift-register generators.\n",
|
|
"In these generators each successive number depends on many preceding\n",
|
|
"values (rather than the last values as in the linear congruential\n",
|
|
"generator).\n",
|
|
"For example, you could make a shift register generator whose $l$th \n",
|
|
"number is the sum of the $l-i$th and $l-j$th values with modulo $M$,"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"N_l=(aN_{l-i}+cN_{l-j})\\mathrm{MOD}(M).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Random number generator RNG, other examples\n",
|
|
"Such a generator again produces a sequence of pseudorandom numbers\n",
|
|
"but this time with a period much larger than $M$.\n",
|
|
"It is also possible to construct more elaborate algorithms by including\n",
|
|
"more than two past terms in the sum of each iteration.\n",
|
|
"One example is the generator of [Marsaglia and Zaman](http://dl.acm.org/citation.cfm?id=187154)\n",
|
|
"which consists of two congruential relations"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:mz1\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
" N_l=(N_{l-3}-N_{l-1})\\mathrm{MOD}(2^{31}-69),\n",
|
|
"\\label{eq:mz1} \\tag{12}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"followed by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:mz2\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
" N_l=(69069N_{l-1}+1013904243)\\mathrm{MOD}(2^{32}),\n",
|
|
"\\label{eq:mz2} \\tag{13}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which according to the authors has a period larger than $2^{94}$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG, other examples\n",
|
|
"Instead of using modular addition, we could use the bitwise\n",
|
|
"exclusive-OR ($\\oplus$) operation so that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"N_l=(N_{l-i})\\oplus (N_{l-j})\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where the bitwise action of $\\oplus$ means that if $N_{l-i}=N_{l-j}$ the result is\n",
|
|
"$0$ whereas if $N_{l-i}\\ne N_{l-j}$ the result is\n",
|
|
"$1$. As an example, consider the case where $N_{l-i}=6$ and $N_{l-j}=11$. The first\n",
|
|
"one has a bit representation (using 4 bits only) which reads $0110$ whereas the \n",
|
|
"second number is $1011$. Employing the $\\oplus$ operator yields \n",
|
|
"$1101$, or $2^3+2^2+2^0=13$.\n",
|
|
"\n",
|
|
"In Fortran90, the bitwise $\\oplus$ operation is coded through the intrinsic\n",
|
|
"function $\\mathrm{IEOR}(m,n)$ where $m$ and $n$ are the input numbers, while in $C$\n",
|
|
"it is given by $m\\wedge n$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG, RAN0\n",
|
|
"\n",
|
|
"We show here how the linear congruential algorithm can be implemented, namely"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"N_i=(aN_{i-1}) \\mathrm{MOD} (M).\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"However, since $a$ and $N_{i-1}$ are integers and their multiplication \n",
|
|
"could become greater than the standard 32 bit integer, there is a trick via \n",
|
|
"Schrage's algorithm which approximates the multiplication\n",
|
|
"of large integers through the factorization"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"M=aq+r,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where we have defined"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"q=[M/a],\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"r = M\\hspace{0.1cm}\\mathrm{MOD} \\hspace{0.1cm}a.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where the brackets denote integer division. In the code below the numbers \n",
|
|
"$q$ and $r$ are chosen so that $r < q$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG, RAN0\n",
|
|
"\n",
|
|
"To see how this works we note first that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:rntrick1\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"(aN_{i-1}) \\mathrm{MOD} (M)= (aN_{i-1}-[N_{i-1}/q]M)\\mathrm{MOD} (M),\n",
|
|
"\\label{eq:rntrick1} \\tag{14}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"since we can add or subtract any integer multiple of $M$ from $aN_{i-1}$.\n",
|
|
"The last term $[N_{i-1}/q]M\\mathrm{MOD}(M)$ is zero since the integer division \n",
|
|
"$[N_{i-1}/q]$ just yields a constant which is multiplied with $M$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG, RAN0\n",
|
|
"We can now rewrite Eq. ([14](#eq:rntrick1)) as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:rntrick2\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"(aN_{i-1}) \\mathrm{MOD} (M)= (aN_{i-1}-[N_{i-1}/q](aq+r))\\mathrm{MOD} (M),\n",
|
|
"\\label{eq:rntrick2} \\tag{15}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"which results\n",
|
|
"in"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:rntrick3\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"(aN_{i-1}) \\mathrm{MOD} (M)= \\left(a(N_{i-1}-[N_{i-1}/q]q)-[N_{i-1}/q]r)\\right)\\mathrm{MOD} (M),\n",
|
|
"\\label{eq:rntrick3} \\tag{16}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"yielding"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<!-- Equation labels as ordinary links -->\n",
|
|
"<div id=\"eq:rntrick4\"></div>\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\begin{equation}\n",
|
|
"(aN_{i-1}) \\mathrm{MOD} (M)= \\left(a(N_{i-1}\\mathrm{MOD} (q)) -[N_{i-1}/q]r)\\right)\\mathrm{MOD} (M).\n",
|
|
"\\label{eq:rntrick4} \\tag{17}\n",
|
|
"\\end{equation}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Random number generator RNG, RAN0\n",
|
|
"The term $[N_{i-1}/q]r$ is always smaller or equal $N_{i-1}(r/q)$ and with $r < q$ we obtain always a \n",
|
|
"number smaller than $N_{i-1}$, which is smaller than $M$. \n",
|
|
"And since the number $N_{i-1}\\mathrm{MOD} (q)$ is between zero and $q-1$ then\n",
|
|
"$a(N_{i-1}\\mathrm{MOD} (q))< aq$. Combined with our definition of $q=[M/a]$ ensures that \n",
|
|
"this term is also smaller than $M$ meaning that both terms fit into a\n",
|
|
"32-bit signed integer. None of these two terms can be negative, but their difference could.\n",
|
|
"The algorithm below adds $M$ if their difference is negative.\n",
|
|
"Note that the program uses the bitwise $\\oplus$ operator to generate\n",
|
|
"the starting point for each generation of a random number. The period\n",
|
|
"of $ran0$ is $\\sim 2.1\\times 10^{9}$. A special feature of this\n",
|
|
"algorithm is that is should never be called with the initial seed \n",
|
|
"set to $0$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Random number generator RNG, RAN0 code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" /*\n",
|
|
" ** The function\n",
|
|
" ** ran0()\n",
|
|
" ** is an \"Minimal\" random number generator of Park and Miller\n",
|
|
" ** Set or reset the input value\n",
|
|
" ** idum to any integer value (except the unlikely value MASK)\n",
|
|
" ** to initialize the sequence; idum must not be altered between\n",
|
|
" ** calls for sucessive deviates in a sequence.\n",
|
|
" ** The function returns a uniform deviate between 0.0 and 1.0.\n",
|
|
" */\n",
|
|
" double ran0(long &idum)\n",
|
|
" {\n",
|
|
" const int a = 16807, m = 2147483647, q = 127773;\n",
|
|
" const int r = 2836, MASK = 123459876;\n",
|
|
" const double am = 1./m;\n",
|
|
" long k;\n",
|
|
" double ans;\n",
|
|
" idum ^= MASK;\n",
|
|
" k = (*idum)/q;\n",
|
|
" idum = a*(idum - k*q) - r*k;\n",
|
|
" // add m if negative difference\n",
|
|
" if(idum < 0) idum += m;\n",
|
|
" ans=am*(idum);\n",
|
|
" idum ^= MASK;\n",
|
|
" return ans;\n",
|
|
" } // End: function ran0() \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Properties of Selected Random Number Generators\n",
|
|
"\n",
|
|
"As mentioned previously, the underlying PDF for the generation of\n",
|
|
"random numbers is the uniform distribution, meaning that the \n",
|
|
"probability for finding a number $x$ in the interval [0,1] is $p(x)=1$.\n",
|
|
"\n",
|
|
"A random number generator should produce numbers which are uniformly distributed\n",
|
|
"in this interval. The table shows the distribution of $N=10000$ random\n",
|
|
"numbers generated by the functions in the program library.\n",
|
|
"We note in this table that the number of points in the various\n",
|
|
"intervals $0.0-0.1$, $0.1-0.2$ etc are fairly close to $1000$, with some minor\n",
|
|
"deviations. \n",
|
|
"\n",
|
|
"Two additional measures are the standard deviation $\\sigma$ and the mean\n",
|
|
"$\\mu=\\langle x\\rangle$.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Properties of Selected Random Number Generators\n",
|
|
"For the uniform distribution, the mean value $\\mu$ is then"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\mu=\\langle x\\rangle=\\frac{1}{2}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"while the standard deviation is"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma=\\sqrt{\\langle x^2\\rangle-\\mu^2}=\\frac{1}{\\sqrt{12}}=0.2886.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Properties of Selected Random Number Generators\n",
|
|
"The various random number generators produce results which agree rather well with\n",
|
|
"these limiting values. \n",
|
|
"\n",
|
|
"<table border=\"1\">\n",
|
|
"<thead>\n",
|
|
"<tr><th align=\"center\">$x$-bin </th> <th align=\"center\"> ran0 </th> <th align=\"center\"> ran1 </th> <th align=\"center\"> ran2 </th> <th align=\"center\"> ran3 </th> </tr>\n",
|
|
"</thead>\n",
|
|
"<tbody>\n",
|
|
"<tr><td align=\"center\"> 0.0-0.1 </td> <td align=\"right\"> 1013 </td> <td align=\"right\"> 991 </td> <td align=\"right\"> 938 </td> <td align=\"right\"> 1047 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.1-0.2 </td> <td align=\"right\"> 1002 </td> <td align=\"right\"> 1009 </td> <td align=\"right\"> 1040 </td> <td align=\"right\"> 1030 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.2-0.3 </td> <td align=\"right\"> 989 </td> <td align=\"right\"> 999 </td> <td align=\"right\"> 1030 </td> <td align=\"right\"> 993 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.3-0.4 </td> <td align=\"right\"> 939 </td> <td align=\"right\"> 960 </td> <td align=\"right\"> 1023 </td> <td align=\"right\"> 937 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.4-0.5 </td> <td align=\"right\"> 1038 </td> <td align=\"right\"> 1001 </td> <td align=\"right\"> 1002 </td> <td align=\"right\"> 992 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.5-0.6 </td> <td align=\"right\"> 1037 </td> <td align=\"right\"> 1047 </td> <td align=\"right\"> 1009 </td> <td align=\"right\"> 1009 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.6-0.7 </td> <td align=\"right\"> 1005 </td> <td align=\"right\"> 989 </td> <td align=\"right\"> 1003 </td> <td align=\"right\"> 989 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.7-0.8 </td> <td align=\"right\"> 986 </td> <td align=\"right\"> 962 </td> <td align=\"right\"> 985 </td> <td align=\"right\"> 954 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.8-0.9 </td> <td align=\"right\"> 1000 </td> <td align=\"right\"> 1027 </td> <td align=\"right\"> 1009 </td> <td align=\"right\"> 1023 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> 0.9-1.0 </td> <td align=\"right\"> 991 </td> <td align=\"right\"> 1015 </td> <td align=\"right\"> 961 </td> <td align=\"right\"> 1026 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> $\\mu$ </td> <td align=\"right\"> 0.4997 </td> <td align=\"right\"> 0.5018 </td> <td align=\"right\"> 0.4992 </td> <td align=\"right\"> 0.4990 </td> </tr>\n",
|
|
"<tr><td align=\"center\"> $\\sigma$ </td> <td align=\"right\"> 0.2882 </td> <td align=\"right\"> 0.2892 </td> <td align=\"right\"> 0.2861 </td> <td align=\"right\"> 0.2915 </td> </tr>\n",
|
|
"</tbody>\n",
|
|
"</table>\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Simple demonstration of RNGs using python\n",
|
|
"The following simple Python code plots the distribution of the produced random numbers using the linear congruential RNG employed by Python. The trend displayed in the previous table is seen rather clearly."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!/usr/bin/env python\n",
|
|
"import numpy as np\n",
|
|
"import matplotlib.mlab as mlab\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import random\n",
|
|
"\n",
|
|
"# initialize the rng with a seed\n",
|
|
"random.seed() \n",
|
|
"counts = 10000\n",
|
|
"values = np.zeros(counts) \n",
|
|
"for i in range (1, counts, 1):\n",
|
|
" values[i] = random.random()\n",
|
|
"\n",
|
|
"# the histogram of the data\n",
|
|
"n, bins, patches = plt.hist(values, 10, facecolor='green')\n",
|
|
"\n",
|
|
"plt.xlabel('$x$')\n",
|
|
"plt.ylabel('Number of counts')\n",
|
|
"plt.title(r'Test of uniform distribution')\n",
|
|
"plt.axis([0, 1, 0, 1100])\n",
|
|
"plt.grid(True)\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Properties of Selected Random Number Generators\n",
|
|
"Since our random numbers, which are typically generated via a linear congruential algorithm,\n",
|
|
"are never fully independent, we can then define \n",
|
|
"an important test which measures the degree of correlation, namely the so-called \n",
|
|
"auto-correlation function defined previously, see again Eq. ([9](#eq:autocorrelformal)).\n",
|
|
"We rewrite it here as"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"C_k=\\frac{f_d}\n",
|
|
" {\\sigma^2},\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"with $C_0=1$. Recall that \n",
|
|
"$\\sigma^2=\\langle x_i^2\\rangle-\\langle x_i\\rangle^2$ and that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"f_d = \\frac{1}{nm}\\sum_{\\alpha=1}^m\\sum_{k=1}^{n-d}(x_{\\alpha,k}-\\langle X_m \\rangle)(x_{\\alpha,k+d}-\\langle X_m \\rangle),\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The non-vanishing of $C_k$ for $k\\ne 0$ means that the random\n",
|
|
"numbers are not independent. The independence of the random numbers is crucial \n",
|
|
"in the evaluation of other expectation values. If they are not independent, our\n",
|
|
"assumption for approximating $\\sigma_N$ is no longer valid.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Autocorrelation function\n",
|
|
"This program computes the autocorrelation function as discussed in the equation on the previous slide for random numbers generated with the normal distribution $N(0,1)$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Importing various packages\n",
|
|
"from math import exp, sqrt\n",
|
|
"from random import random, seed\n",
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"\n",
|
|
"def autocovariance(x, n, k, mean_x):\n",
|
|
" sum = 0.0\n",
|
|
" for i in range(0, n-k):\n",
|
|
" sum += (x[(i+k)]-mean_x)*(x[i]-mean_x)\n",
|
|
" return sum/n\n",
|
|
"\n",
|
|
"n = 1000\n",
|
|
"x=np.random.normal(size=n)\n",
|
|
"autocor = np.zeros(n)\n",
|
|
"figaxis = np.zeros(n)\n",
|
|
"mean_x=np.mean(x)\n",
|
|
"var_x = np.var(x)\n",
|
|
"print(mean_x, var_x)\n",
|
|
"for i in range (0, n):\n",
|
|
" figaxis[i] = i\n",
|
|
" autocor[i]=(autocovariance(x, n, i, mean_x))/var_x \n",
|
|
"\n",
|
|
"plt.plot(figaxis, autocor, \"r-\")\n",
|
|
"plt.axis([0,n,-0.1, 1.0])\n",
|
|
"plt.xlabel(r'$i$')\n",
|
|
"plt.ylabel(r'$\\gamma_i$')\n",
|
|
"plt.title(r'Autocorrelation function')\n",
|
|
"plt.show()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"As can be seen from the plot, the first point gives back the variance and a value of one. \n",
|
|
"For the remaining values we notice that there are still non-zero values for the auto-correlation function.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Correlation function and which random number generators should I use\n",
|
|
"The program here computes the correlation function for one of the standard functions included with the c++ compiler."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" // This function computes the autocorrelation function for \n",
|
|
" // the standard c++ random number generator\n",
|
|
" \n",
|
|
" #include <fstream>\n",
|
|
" #include <iomanip>\n",
|
|
" #include <iostream>\n",
|
|
" #include <cmath>\n",
|
|
" using namespace std;\n",
|
|
" // output file as global variable\n",
|
|
" ofstream ofile; \n",
|
|
" \n",
|
|
" // Main function begins here \n",
|
|
" int main(int argc, char* argv[])\n",
|
|
" {\n",
|
|
" int n;\n",
|
|
" char *outfilename;\n",
|
|
" \n",
|
|
" cin >> n;\n",
|
|
" double MCint = 0.; double MCintsqr2=0.;\n",
|
|
" double invers_period = 1./RAND_MAX; // initialise the random number generator\n",
|
|
" srand(time(NULL)); // This produces the so-called seed in MC jargon\n",
|
|
" // Compute the variance and the mean value of the uniform distribution\n",
|
|
" // Compute also the specific values x for each cycle in order to be able to\n",
|
|
" // the covariance and the correlation function \n",
|
|
" // Read in output file, abort if there are too few command-line arguments\n",
|
|
" if( argc <= 2 ){\n",
|
|
" cout << \"Bad Usage: \" << argv[0] << \n",
|
|
" \t \" read also output file and number of cycles on same line\" << endl;\n",
|
|
" exit(1);\n",
|
|
" }\n",
|
|
" else{\n",
|
|
" outfilename=argv[1];\n",
|
|
" }\n",
|
|
" ofile.open(outfilename); \n",
|
|
" // Get the number of Monte-Carlo samples\n",
|
|
" n = atoi(argv[2]);\n",
|
|
" double *X; \n",
|
|
" X = new double[n];\n",
|
|
" for (int i = 0; i < n; i++){\n",
|
|
" double x = double(rand())*invers_period; \n",
|
|
" X[i] = x;\n",
|
|
" MCint += x;\n",
|
|
" MCintsqr2 += x*x;\n",
|
|
" }\n",
|
|
" double Mean = MCint/((double) n );\n",
|
|
" MCintsqr2 = MCintsqr2/((double) n );\n",
|
|
" double STDev = sqrt(MCintsqr2-Mean*Mean);\n",
|
|
" double Variance = MCintsqr2-Mean*Mean;\n",
|
|
" // Write mean value and standard deviation \n",
|
|
" cout << \" Standard deviation= \" << STDev << \" Integral = \" << Mean << endl;\n",
|
|
" \n",
|
|
" // Now we compute the autocorrelation function\n",
|
|
" double *autocor; autocor = new double[n];\n",
|
|
" for (int j = 0; j < n; j++){\n",
|
|
" double sum = 0.0;\n",
|
|
" for (int k = 0; k < (n-j); k++){\n",
|
|
" \t sum += (X[k]-Mean)*(X[k+j]-Mean); \n",
|
|
" }\n",
|
|
" autocor[j] = sum/Variance/((double) n );\n",
|
|
" ofile << setiosflags(ios::showpoint | ios::uppercase);\n",
|
|
" ofile << setw(15) << setprecision(8) << j;\n",
|
|
" ofile << setw(15) << setprecision(8) << autocor[j] << endl;\n",
|
|
" }\n",
|
|
" ofile.close(); // close output file\n",
|
|
" return 0;\n",
|
|
" } // end of main program \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Which RNG should I use?\n",
|
|
"* C++ has a class called **random**. The [random class](http://www.cplusplus.com/reference/random/) contains a large selection of RNGs and is highly recommended. Some of these RNGs have very large periods making it thereby very safe to use these RNGs in case one is performing large calculations. In particular, the [Mersenne twister random number engine](http://www.cplusplus.com/reference/random/mersenne_twister_engine/) has a period of $2^{19937}$. \n",
|
|
"\n",
|
|
"* Add RNGs in Python\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## How to use the Mersenne generator\n",
|
|
"The following part of a c++ code (from project 4) sets up the uniform distribution for $x\\in [0,1]$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" /*\n",
|
|
" \n",
|
|
" // You need this \n",
|
|
" #include <random>\n",
|
|
" \n",
|
|
" // Initialize the seed and call the Mersienne algo\n",
|
|
" std::random_device rd;\n",
|
|
" std::mt19937_64 gen(rd());\n",
|
|
" // Set up the uniform distribution for x \\in [[0, 1]\n",
|
|
" std::uniform_real_distribution<double> RandomNumberGenerator(0.0,1.0);\n",
|
|
" \n",
|
|
" // Now use the RNG\n",
|
|
" int ix = (int) (RandomNumberGenerator(gen)*NSpins);\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Why blocking?\n",
|
|
"**Statistical analysis.**\n",
|
|
"\n",
|
|
" * Monte Carlo simulations can be treated as *computer experiments*\n",
|
|
"\n",
|
|
" * The results can be analysed with the same statistical tools as we would use analysing experimental data.\n",
|
|
"\n",
|
|
" * As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.\n",
|
|
"\n",
|
|
"A very good article which explains blocking is H. Flyvbjerg and H. G. Petersen, *Error estimates on averages of correlated data*, [Journal of Chemical Physics 91, 461-466 (1989)](http://scitation.aip.org/content/aip/journal/jcp/91/1/10.1063/1.457480).\n",
|
|
"\n",
|
|
" \n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Why blocking?\n",
|
|
"**Statistical analysis.**\n",
|
|
"\n",
|
|
" * As in other experiments, Monte Carlo experiments have two classes of errors:\n",
|
|
"\n",
|
|
" * Statistical errors\n",
|
|
"\n",
|
|
" * Systematical errors\n",
|
|
"\n",
|
|
"\n",
|
|
" * Statistical errors can be estimated using standard tools from statistics\n",
|
|
"\n",
|
|
" * Systematical errors are method specific and must be treated differently from case to case. (In VMC a common source is the step length or time step in importance sampling)\n",
|
|
"\n",
|
|
" \n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Code to demonstrate the calculation of the autocorrelation function\n",
|
|
"The following code computes the autocorrelation function, the covariance and the standard deviation\n",
|
|
"for standard RNG. \n",
|
|
"The [following file](https://github.com/CompPhysics/ComputationalPhysics2/tree/gh-pages/doc/Programs/LecturePrograms/programs/Blocking/autocorrelation.cpp) gives the code."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" // This function computes the autocorrelation function for \n",
|
|
" // the Mersenne random number generator with a uniform distribution\n",
|
|
" #include <iostream>\n",
|
|
" #include <fstream>\n",
|
|
" #include <iomanip>\n",
|
|
" #include <cstdlib>\n",
|
|
" #include <random>\n",
|
|
" #include <armadillo>\n",
|
|
" #include <string>\n",
|
|
" #include <cmath>\n",
|
|
" using namespace std;\n",
|
|
" using namespace arma;\n",
|
|
" // output file\n",
|
|
" ofstream ofile;\n",
|
|
" \n",
|
|
" // Main function begins here \n",
|
|
" int main(int argc, char* argv[])\n",
|
|
" {\n",
|
|
" int MonteCarloCycles;\n",
|
|
" string filename;\n",
|
|
" if (argc > 1) {\n",
|
|
" filename=argv[1];\n",
|
|
" MonteCarloCycles = atoi(argv[2]);\n",
|
|
" string fileout = filename;\n",
|
|
" string argument = to_string(MonteCarloCycles);\n",
|
|
" fileout.append(argument);\n",
|
|
" ofile.open(fileout);\n",
|
|
" }\n",
|
|
" \n",
|
|
" // Compute the variance and the mean value of the uniform distribution\n",
|
|
" // Compute also the specific values x for each cycle in order to be able to\n",
|
|
" // compute the covariance and the correlation function \n",
|
|
" \n",
|
|
" vec X = zeros<vec>(MonteCarloCycles);\n",
|
|
" double MCint = 0.; double MCintsqr2=0.;\n",
|
|
" std::random_device rd;\n",
|
|
" std::mt19937_64 gen(rd());\n",
|
|
" // Set up the uniform distribution for x \\in [[0, 1]\n",
|
|
" std::uniform_real_distribution<double> RandomNumberGenerator(0.0,1.0);\n",
|
|
" for (int i = 0; i < MonteCarloCycles; i++){\n",
|
|
" double x = RandomNumberGenerator(gen); \n",
|
|
" X(i) = x;\n",
|
|
" MCint += x;\n",
|
|
" MCintsqr2 += x*x;\n",
|
|
" }\n",
|
|
" double Mean = MCint/((double) MonteCarloCycles );\n",
|
|
" MCintsqr2 = MCintsqr2/((double) MonteCarloCycles );\n",
|
|
" double STDev = sqrt(MCintsqr2-Mean*Mean);\n",
|
|
" double Variance = MCintsqr2-Mean*Mean;\n",
|
|
" // Write mean value and variance\n",
|
|
" cout << \" Sample variance= \" << Variance << \" Mean value = \" << Mean << endl;\n",
|
|
" // Now we compute the autocorrelation function\n",
|
|
" vec autocorrelation = zeros<vec>(MonteCarloCycles);\n",
|
|
" for (int j = 0; j < MonteCarloCycles; j++){\n",
|
|
" double sum = 0.0;\n",
|
|
" for (int k = 0; k < (MonteCarloCycles-j); k++){\n",
|
|
" sum += (X(k)-Mean)*(X(k+j)-Mean); \n",
|
|
" }\n",
|
|
" autocorrelation(j) = sum/Variance/((double) MonteCarloCycles );\n",
|
|
" ofile << setiosflags(ios::showpoint | ios::uppercase);\n",
|
|
" ofile << setw(15) << setprecision(8) << j;\n",
|
|
" ofile << setw(15) << setprecision(8) << autocorrelation(j) << endl;\n",
|
|
" }\n",
|
|
" // Now compute the exact covariance using the autocorrelation function\n",
|
|
" double Covariance = 0.0;\n",
|
|
" for (int j = 0; j < MonteCarloCycles; j++){\n",
|
|
" Covariance += autocorrelation(j);\n",
|
|
" }\n",
|
|
" Covariance *= 2.0/((double) MonteCarloCycles);\n",
|
|
" // Compute now the total variance, including the covariance, and obtain the standard deviation\n",
|
|
" double TotalVariance = (Variance/((double) MonteCarloCycles ))+Covariance;\n",
|
|
" cout << \"Covariance =\" << Covariance << \"Totalvariance= \" << TotalVariance << \"Sample Variance/n= \" << (Variance/((double) MonteCarloCycles )) << endl;\n",
|
|
" cout << \" STD from sample variance= \" << sqrt(Variance/((double) MonteCarloCycles )) << \" STD with covariance = \" << sqrt(TotalVariance) << endl;\n",
|
|
" \n",
|
|
" ofile.close(); // close output file\n",
|
|
" return 0;\n",
|
|
" } // end of main program \n",
|
|
" \n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## What is blocking?\n",
|
|
"**Blocking.**\n",
|
|
"\n",
|
|
" * Say that we have a set of samples from a Monte Carlo experiment\n",
|
|
"\n",
|
|
" * Assuming (wrongly) that our samples are uncorrelated our best estimate of the standard deviation of the mean $\\langle \\mathbf{M}\\rangle$ is given by"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma=\\sqrt{\\frac{1}{n}\\left(\\langle \\mathbf{M}^2\\rangle-\\langle \\mathbf{M}\\rangle^2\\right)}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"* If the samples are correlated we can rewrite our results to show that"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\sigma=\\sqrt{\\frac{1+2\\tau/\\Delta t}{n}\\left(\\langle \\mathbf{M}^2\\rangle-\\langle \\mathbf{M}\\rangle^2\\right)}\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"where $\\tau$ is the correlation time (the time between a sample and the next uncorrelated sample) and $\\Delta t$ is time between each sample\n",
|
|
"\n",
|
|
" \n",
|
|
"\n",
|
|
"\n",
|
|
"## What is blocking?\n",
|
|
"**Blocking.**\n",
|
|
"\n",
|
|
" * If $\\Delta t\\gg\\tau$ our first estimate of $\\sigma$ still holds\n",
|
|
"\n",
|
|
" * Much more common that $\\Delta t<\\tau$\n",
|
|
"\n",
|
|
" * In the method of data blocking we divide the sequence of samples into blocks\n",
|
|
"\n",
|
|
" * We then take the mean $\\langle \\mathbf{M}_i\\rangle$ of block $i=1\\ldots n_{blocks}$ to calculate the total mean and variance\n",
|
|
"\n",
|
|
" * The size of each block must be so large that sample $j$ of block $i$ is not correlated with sample $j$ of block $i+1$\n",
|
|
"\n",
|
|
" * The correlation time $\\tau$ would be a good choice\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## What is blocking?\n",
|
|
"**Blocking.**\n",
|
|
"\n",
|
|
" * Problem: We don't know $\\tau$ or it is too expensive to compute\n",
|
|
"\n",
|
|
" * Solution: Make a plot of std. dev. as a function of blocksize\n",
|
|
"\n",
|
|
" * The estimate of std. dev. of correlated data is too low $\\to$ the error will increase with increasing block size until the blocks are uncorrelated, where we reach a plateau\n",
|
|
"\n",
|
|
" * When the std. dev. stops increasing the blocks are uncorrelated\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Implementation\n",
|
|
" * Do a Monte Carlo simulation, storing all samples to file\n",
|
|
"\n",
|
|
" * Do the statistical analysis on this file, independently of your Monte Carlo program\n",
|
|
"\n",
|
|
" * Read the file into an array\n",
|
|
"\n",
|
|
" * Loop over various block sizes\n",
|
|
"\n",
|
|
" * For each block size $n_b$, loop over the array in steps of $n_b$ taking the mean of elements $i n_b,\\ldots,(i+1) n_b$\n",
|
|
"\n",
|
|
" * Take the mean and variance of the resulting array\n",
|
|
"\n",
|
|
" * Write the results for each block size to file for later\n",
|
|
" analysis\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Actual implementation with code, main function\n",
|
|
"When the file gets large, it can be useful to write your data in binary mode instead of ascii characters.\n",
|
|
"The [following python file](https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py) reads data from file with the output from every Monte Carlo cycle."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": false,
|
|
"editable": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Blocking\n",
|
|
" @timeFunction\n",
|
|
" def blocking(self, blockSizeMax = 500):\n",
|
|
" blockSizeMin = 1\n",
|
|
"\n",
|
|
" self.blockSizes = []\n",
|
|
" self.meanVec = []\n",
|
|
" self.varVec = []\n",
|
|
"\n",
|
|
" for i in range(blockSizeMin, blockSizeMax):\n",
|
|
" if(len(self.data) % i != 0):\n",
|
|
" pass#continue\n",
|
|
" blockSize = i\n",
|
|
" meanTempVec = []\n",
|
|
" varTempVec = []\n",
|
|
" startPoint = 0\n",
|
|
" endPoint = blockSize\n",
|
|
"\n",
|
|
" while endPoint <= len(self.data):\n",
|
|
" meanTempVec.append(np.average(self.data[startPoint:endPoint]))\n",
|
|
" startPoint = endPoint\n",
|
|
" endPoint += blockSize\n",
|
|
" mean, var = np.average(meanTempVec), np.var(meanTempVec)/len(meanTempVec)\n",
|
|
" self.meanVec.append(mean)\n",
|
|
" self.varVec.append(var)\n",
|
|
" self.blockSizes.append(blockSize)\n",
|
|
"\n",
|
|
" self.blockingAvg = np.average(self.meanVec[-200:])\n",
|
|
" self.blockingVar = (np.average(self.varVec[-200:]))\n",
|
|
" self.blockingStd = np.sqrt(self.blockingVar)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## The Bootstrap method\n",
|
|
"\n",
|
|
"The Bootstrap resampling method is also very popular. It is very simple:\n",
|
|
"\n",
|
|
"1. Start with your sample of measurements and compute the sample variance and the mean values\n",
|
|
"\n",
|
|
"2. Then start again but pick in a random way the numbers in the sample and recalculate the mean and the sample variance.\n",
|
|
"\n",
|
|
"3. Repeat this $K$ times.\n",
|
|
"\n",
|
|
"It can be shown, see the article by [Efron](https://projecteuclid.org/download/pdf_1/euclid.aos/1176344552)\n",
|
|
"that it produces the correct standard deviation.\n",
|
|
"\n",
|
|
"This method is very useful for small ensembles of data points. \n",
|
|
"\n",
|
|
"\n",
|
|
"## Bootstrapping\n",
|
|
"Given a set of $N$ data, assume that we are interested in some \n",
|
|
"observable $\\theta$ which may be estimated from that set. This observable can also be for example the result of a fit based on all $N$ raw data. \n",
|
|
"Let us call the value of the observable obtained from the original \n",
|
|
"data set $\\hat{\\theta}$. One recreates from the sample repeatedly \n",
|
|
"other samples by choosing randomly $N$ data out of the original set. \n",
|
|
"This costs essentially nothing, since we just recycle the original data set for the building of new sets. \n",
|
|
"\n",
|
|
"\n",
|
|
"## Bootstrapping, recipe\n",
|
|
"Let us assume we have done this $K$ times and thus have $K$ sets of $N$ \n",
|
|
"data values each. \n",
|
|
"Of course some values will enter more than once in the new sets. For each of these sets one computes the observable $\\theta$ resulting in values $\\theta_k$ with $k = 1,...,K$. Then one determines"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"\\tilde{\\theta} = \\frac{1}{K} \\sum_{k=1}^K \\theta_k,\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"and"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"$$\n",
|
|
"sigma^2_{\\tilde{\\theta}} = \\frac{1}{K} \\sum_{k=1}^K \\left(\\theta_k-\\tilde{\\theta}\\right)^2.\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"These are estimators for $\\angle\\theta\\rangle$ and its variance. They are not unbiased and therefore \n",
|
|
"$\\tilde{\\theta}\\neq\\hat{\\theta}$ for finite K. \n",
|
|
"\n",
|
|
"The difference is called bias and gives an idea on how far away the result may be from \n",
|
|
"the true $\\angle\\theta\\rangle$. As final result for the observable one quotes $\\angle\\theta\\rangle = \\tilde{\\theta} \\pm \\sigma_{\\tilde{\\theta}}$ .\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"## Bootstrapping, [code](https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" # Bootstrap\n",
|
|
" @timeFunction\n",
|
|
" def bootstrap(self, nBoots = 1000):\n",
|
|
" bootVec = np.zeros(nBoots)\n",
|
|
" for k in range(0,nBoots):\n",
|
|
" bootVec[k] = np.average(np.random.choice(self.data, len(self.data)))\n",
|
|
" self.bootAvg = np.average(bootVec)\n",
|
|
" self.bootVar = np.var(bootVec)\n",
|
|
" self.bootStd = np.std(bootVec)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Jackknife, [code](https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
" # Jackknife\n",
|
|
" @timeFunction\n",
|
|
" def jackknife(self):\n",
|
|
" jackknVec = np.zeros(len(self.data))\n",
|
|
" for k in range(0,len(self.data)):\n",
|
|
" jackknVec[k] = np.average(np.delete(self.data, k))\n",
|
|
" self.jackknAvg = self.avg - (len(self.data) - 1) * (np.average(jackknVec) - self.avg)\n",
|
|
" self.jackknVar = float(len(self.data) - 1) * np.var(jackknVec)\n",
|
|
" self.jackknStd = np.sqrt(self.jackknVar)\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|