2613 lines
92 KiB
HTML
2613 lines
92 KiB
HTML
\
|
|
<!DOCTYPE html>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
|
|
<meta name="description" content="Data Analysis and Machine Learning: Elements of Probability Theory">
|
|
|
|
<title>Data Analysis and Machine Learning: Elements of Probability Theory</title>
|
|
|
|
|
|
|
|
|
|
<script src="http://sagecell.sagemath.org/static/jquery.min.js"></script>
|
|
<script src="http://sagecell.sagemath.org/embedded_sagecell.js"></script>
|
|
<link rel="stylesheet" type="text/css" href="https://sagecell.sagemath.org/static/sagecell_embed.css">
|
|
<script>
|
|
$(function () {
|
|
// Make the div with id 'mycell' a Sage cell
|
|
sagecell.makeSagecell({inputLocation: '#mycell',
|
|
template: sagecell.templates.minimal,
|
|
evalButtonText: 'Activate'});
|
|
// Make *any* div with class 'compute' a Sage cell
|
|
sagecell.makeSagecell({inputLocation: 'div.compute',
|
|
evalButtonText: 'Evaluate'});
|
|
});
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<!-- reveal.js: http://lab.hakim.se/reveal-js/ -->
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
|
|
|
<meta name="apple-mobile-web-app-capable" content="yes" />
|
|
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
|
|
|
|
<link rel="stylesheet" href="reveal.js/css/reveal.css">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/beige.css" id="theme">
|
|
<!--
|
|
<link rel="stylesheet" href="reveal.js/css/reveal.css">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/beige.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/beigesmall.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/solarized.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/serif.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/night.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/moon.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/simple.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/sky.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/darkgray.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/default.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/cbc.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/simula.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/black.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/white.css" id="theme">
|
|
<link rel="stylesheet" href="reveal.js/css/theme/league.css" id="theme">
|
|
-->
|
|
|
|
<!-- For syntax highlighting -->
|
|
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
|
|
|
|
<!-- Printing and PDF exports -->
|
|
<script>
|
|
var link = document.createElement( 'link' );
|
|
link.rel = 'stylesheet';
|
|
link.type = 'text/css';
|
|
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
|
|
document.getElementsByTagName( 'head' )[0].appendChild( link );
|
|
</script>
|
|
|
|
<style type="text/css">
|
|
hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
|
|
p.caption { width: 80%; font-size: 60%; font-style: italic; text-align: left; }
|
|
hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa}
|
|
.reveal .alert-text-small { font-size: 80%; }
|
|
.reveal .alert-text-large { font-size: 130%; }
|
|
.reveal .alert-text-normal { font-size: 90%; }
|
|
.reveal .alert {
|
|
padding:8px 35px 8px 14px; margin-bottom:18px;
|
|
text-shadow:0 1px 0 rgba(255,255,255,0.5);
|
|
border:5px solid #bababa;
|
|
-webkit-border-radius: 14px; -moz-border-radius: 14px;
|
|
border-radius:14px;
|
|
background-position: 10px 10px;
|
|
background-repeat: no-repeat;
|
|
background-size: 38px;
|
|
padding-left: 30px; /* 55px; if icon */
|
|
}
|
|
.reveal .alert-block {padding-top:14px; padding-bottom:14px}
|
|
.reveal .alert-block > p, .alert-block > ul {margin-bottom:1em}
|
|
/*.reveal .alert li {margin-top: 1em}*/
|
|
.reveal .alert-block p+p {margin-top:5px}
|
|
/*.reveal .alert-notice { background-image: url(http://hplgit.github.io/doconce/bundled/html_images/small_gray_notice.png); }
|
|
.reveal .alert-summary { background-image:url(http://hplgit.github.io/doconce/bundled/html_images/small_gray_summary.png); }
|
|
.reveal .alert-warning { background-image: url(http://hplgit.github.io/doconce/bundled/html_images/small_gray_warning.png); }
|
|
.reveal .alert-question {background-image:url(http://hplgit.github.io/doconce/bundled/html_images/small_gray_question.png); } */
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<!-- Styles for table layout of slides -->
|
|
<style type="text/css">
|
|
td.padding {
|
|
padding-top:20px;
|
|
padding-bottom:20px;
|
|
padding-right:50px;
|
|
padding-left:50px;
|
|
}
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
<div class="reveal">
|
|
|
|
<!-- Any section element inside the <div class="slides"> container
|
|
is displayed as a slide -->
|
|
|
|
<div class="slides">
|
|
|
|
|
|
|
|
|
|
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
TeX: {
|
|
equationNumbers: { autoNumber: "none" },
|
|
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
|
|
}
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async
|
|
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<!-- ------------------- main content ---------------------- -->
|
|
|
|
|
|
|
|
<center><h1 style="text-align: center;">Data Analysis and Machine Learning: Elements of Probability Theory</h1></center> <!-- document title -->
|
|
|
|
<p>
|
|
<!-- author(s): Morten Hjorth-Jensen -->
|
|
|
|
<center>
|
|
<b>Morten Hjorth-Jensen</b> [1, 2]
|
|
</center>
|
|
|
|
<p> <br>
|
|
<!-- institution(s) -->
|
|
|
|
<center>[1] <b>Department of Physics, University of Oslo</b></center>
|
|
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
|
<br>
|
|
<p> <br>
|
|
<center><h4>Oct 4, 2017</h4></center> <!-- date -->
|
|
<br>
|
|
<p>
|
|
|
|
<center style="font-size:80%">
|
|
<!-- copyright --> © 1999-2017, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license
|
|
</center>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec0">Domains and probabilities </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Consider the following simple example, namely the tossing of a dice, resulting in the following possible values
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\{2,3,4,5,6,7,8,9,10,11,12\}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
These values are called the <em>domain</em>.
|
|
To this domain we have the corresponding <em>probabilities</em>
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\{1/36,2/36/3/36,4/36,5/36,6/36,5/36,4/36,3/36,2/36,1/36\}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec1">Tossing a dice </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The numbers in the domain are the outcomes of the physical process tossing the dice.
|
|
We cannot tell beforehand whether the outcome is 3 or 5 or any other number in this domain.
|
|
This defines the randomness of the outcome, or unexpectedness or any other synonimous word which
|
|
encompasses the uncertitude of the final outcome.
|
|
|
|
<p>
|
|
The only thing we can tell beforehand
|
|
is that say the outcome 2 has a certain probability.
|
|
If our favorite hobby is to spend an hour every evening throwing dice and
|
|
registering the sequence of outcomes, we will note that the numbers in the above domain
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\{2,3,4,5,6,7,8,9,10,11,12\},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
appear in a random order. After 11 throws the results may look like
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\{10,8,6,3,6,9,11,8,12,4,5\}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec2">Stochastic variables </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
<b>Random variables are characterized by a domain which contains all possible values that the random value may take. This domain has a corresponding PDF</b>.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec3">Stochastic variables and the main concepts, the discrete case </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
There are two main concepts associated with a stochastic variable. The
|
|
<em>domain</em> is the set \( \mathbb D = \{x\} \) of all accessible values
|
|
the variable can assume, so that \( X \in \mathbb D \). An example of a
|
|
discrete domain is the set of six different numbers that we may get by
|
|
throwing of a dice, \( x\in\{1,\,2,\,3,\,4,\,5,\,6\} \).
|
|
|
|
<p>
|
|
The <em>probability distribution function (PDF)</em> is a function
|
|
\( p(x) \) on the domain which, in the discrete case, gives us the
|
|
probability or relative frequency with which these values of \( X \)
|
|
occur
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x) = \mathrm{Prob}(X=x).
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec4">Stochastic variables and the main concepts, the continuous case </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
In the continuous case, the PDF does not directly depict the
|
|
actual probability. Instead we define the probability for the
|
|
stochastic variable to assume any value on an infinitesimal interval
|
|
around \( x \) to be \( p(x)dx \). The continuous function \( p(x) \) then gives us
|
|
the <em>density</em> of the probability rather than the probability
|
|
itself. The probability for a stochastic variable to assume any value
|
|
on a non-infinitesimal interval \( [a,\,b] \) is then just the integral
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mathrm{Prob}(a\leq X\leq b) = \int_a^b p(x)dx.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
Qualitatively speaking, a stochastic variable represents the values of
|
|
numbers chosen as if by chance from some specified PDF so that the
|
|
selection of a large set of these numbers reproduces this PDF.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec5">The cumulative probability </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Of interest to us is the <em>cumulative probability
|
|
distribution function</em> (<b>CDF</b>), \( P(x) \), which is just the probability
|
|
for a stochastic variable \( X \) to assume any value less than \( x \)
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
P(x)=\mathrm{Prob(}X\leq x\mathrm{)} =
|
|
\int_{-\infty}^x p(x^{\prime})dx^{\prime}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
The relation between a CDF and its corresponding PDF is then
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x) = \frac{d}{dx}P(x).
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec6">Properties of PDFs </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
There are two properties that all PDFs must satisfy. The first one is
|
|
positivity (assuming that the PDF is normalized)
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
0 \leq p(x) \leq 1.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
Naturally, it would be nonsensical for any of the values of the domain
|
|
to occur with a probability greater than \( 1 \) or less than \( 0 \). Also,
|
|
the PDF must be normalized. That is, all the probabilities must add up
|
|
to unity. The probability of "anything" to happen is always unity. For
|
|
both discrete and continuous PDFs, this condition is
|
|
<p> <br>
|
|
$$
|
|
\begin{align*}
|
|
\sum_{x_i\in\mathbb D} p(x_i) & = 1,\\
|
|
\int_{x\in\mathbb D} p(x)\,dx & = 1.
|
|
\end{align*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec7">Important distributions, the uniform distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The first one
|
|
is the most basic PDF; namely the uniform distribution
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
p(x) = \frac{1}{b-a}\theta(x-a)\theta(b-x),
|
|
\tag{1}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
with
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\begin{array}{ll}
|
|
\theta(x)=0 & x < 0 \\
|
|
\theta(x)=\frac{1}{b-a} & \in [a,b].
|
|
\end{array}
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
The normal distribution with \( b=1 \) and \( a=0 \) is used to generate random numbers.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec8">Gaussian distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The second one is the Gaussian Distribution
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp{(-\frac{(x-\mu)^2}{2\sigma^2})},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
with mean value \( \mu \) and standard deviation \( \sigma \). If \( \mu=0 \) and \( \sigma=1 \), it is normally called the <b>standard normal distribution</b>
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x) = \frac{1}{\sqrt{2\pi}} \exp{(-\frac{x^2}{2})},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
<p>
|
|
The following simple Python code plots the above distribution for different values of \( \mu \) and \( \sigma \).
|
|
<p>
|
|
|
|
|
|
<div class="compute"><script type="text/x-sage">
|
|
import numpy as np
|
|
from math import acos, exp, sqrt
|
|
from matplotlib import pyplot as plt
|
|
from matplotlib import rc, rcParams
|
|
import matplotlib.units as units
|
|
import matplotlib.ticker as ticker
|
|
rc('text',usetex=True)
|
|
rc('font',**{'family':'serif','serif':['Gaussian distribution']})
|
|
font = {'family' : 'serif',
|
|
'color' : 'darkred',
|
|
'weight' : 'normal',
|
|
'size' : 16,
|
|
}
|
|
pi = acos(-1.0)
|
|
mu0 = 0.0
|
|
sigma0 = 1.0
|
|
mu1= 1.0
|
|
sigma1 = 2.0
|
|
mu2 = 2.0
|
|
sigma2 = 4.0
|
|
|
|
x = np.linspace(-20.0, 20.0)
|
|
v0 = np.exp(-(x*x-2*x*mu0+mu0*mu0)/(2*sigma0*sigma0))/sqrt(2*pi*sigma0*sigma0)
|
|
v1 = np.exp(-(x*x-2*x*mu1+mu1*mu1)/(2*sigma1*sigma1))/sqrt(2*pi*sigma1*sigma1)
|
|
v2 = np.exp(-(x*x-2*x*mu2+mu2*mu2)/(2*sigma2*sigma2))/sqrt(2*pi*sigma2*sigma2)
|
|
plt.plot(x, v0, 'b-', x, v1, 'r-', x, v2, 'g-')
|
|
plt.title(r'{\bf Gaussian distributions}', fontsize=20)
|
|
plt.text(-19, 0.3, r'Parameters: $\mu = 0$, $\sigma = 1$', fontdict=font)
|
|
plt.text(-19, 0.18, r'Parameters: $\mu = 1$, $\sigma = 2$', fontdict=font)
|
|
plt.text(-19, 0.08, r'Parameters: $\mu = 2$, $\sigma = 4$', fontdict=font)
|
|
plt.xlabel(r'$x$',fontsize=20)
|
|
plt.ylabel(r'$p(x)$ [MeV]',fontsize=20)
|
|
|
|
# Tweak spacing to prevent clipping of ylabel
|
|
plt.subplots_adjust(left=0.15)
|
|
plt.savefig('gaussian.pdf', format='pdf')
|
|
plt.show()
|
|
|
|
</script></div>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec9">Exponential distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Another important distribution in science is the exponential distribution
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x) = \alpha\exp{-(\alpha x)}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec10">Expectation values </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Let \( h(x) \) be an arbitrary continuous function on the domain of the stochastic
|
|
variable \( X \) whose PDF is \( p(x) \). We define the <em>expectation value</em>
|
|
of \( h \) with respect to \( p \) as follows
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
\langle h \rangle_X \equiv \int\! h(x)p(x)\,dx
|
|
\tag{2}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
Whenever the PDF is known implicitly, like in this case, we will drop
|
|
the index \( X \) for clarity.
|
|
A particularly useful class of special expectation values are the
|
|
<em>moments</em>. The \( n \)-th moment of the PDF \( p \) is defined as
|
|
follows
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle x^n \rangle \equiv \int\! x^n p(x)\,dx
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec11">Stochastic variables and the main concepts, mean values </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The zero-th moment \( \langle 1\rangle \) is just the normalization condition of
|
|
\( p \). The first moment, \( \langle x\rangle \), is called the <em>mean</em> of \( p \)
|
|
and often denoted by the letter \( \mu \)
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle x\rangle = \mu \equiv \int x p(x)dx,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
for a continuous distribution and
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle x\rangle = \mu \equiv \frac{1}{N}\sum_{i=1}^N x_i p(x_i),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
for a discrete distribution.
|
|
Qualitatively it represents the centroid or the average value of the
|
|
PDF and is therefore simply called the expectation value of \( p(x) \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec12">Stochastic variables and the main concepts, central moments, the variance </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
A special version of the moments is the set of <em>central moments</em>, the n-th central moment defined as
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle (x-\langle x\rangle )^n\rangle \equiv \int\! (x-\langle x\rangle)^n p(x)\,dx
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
The zero-th and first central moments are both trivial, equal \( 1 \) and
|
|
\( 0 \), respectively. But the second central moment, known as the
|
|
<em>variance</em> of \( p \), is of particular interest. For the stochastic
|
|
variable \( X \), the variance is denoted as \( \sigma^2_X \) or \( \mathrm{Var}(X) \)
|
|
<p> <br>
|
|
$$
|
|
\begin{align*}
|
|
\sigma^2_X &=\mathrm{Var}(X) = \langle (x-\langle x\rangle)^2\rangle =
|
|
\int (x-\langle x\rangle)^2 p(x)dx\\
|
|
& = \int\left(x^2 - 2 x \langle x\rangle^{2} +\langle x\rangle^2\right)p(x)dx\\
|
|
& = \langle x^2\rangle\rangle - 2 \langle x\rangle\langle x\rangle + \langle x\rangle^2\\
|
|
& = \langle x^2 \rangle - \langle x\rangle^2
|
|
\end{align*}
|
|
$$
|
|
<p> <br>
|
|
|
|
The square root of the variance, \( \sigma =\sqrt{\langle (x-\langle x\rangle)^2\rangle} \) is called the
|
|
<b>standard deviation</b> of \( p \). It is the RMS (root-mean-square)
|
|
value of the deviation of the PDF from its mean value, interpreted
|
|
qualitatively as the "spread" of \( p \) around its mean.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec13">Probability Distribution Functions </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The following table collects properties of probability distribution functions.
|
|
In our notation we reserve the label \( p(x) \) for the probability of a certain event,
|
|
while \( P(x) \) is the cumulative probability.
|
|
|
|
<p>
|
|
<table border="1">
|
|
<thead>
|
|
<tr><th align="center"> </th> <th align="center"> Discrete PDF </th> <th align="center"> Continuous PDF </th> </tr>
|
|
</thead>
|
|
<tbody>
|
|
<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>
|
|
<tr><td align="left"> Probability </td> <td align="center"> \( p(x_i) \) </td> <td align="center"> \( p(x)dx \) </td> </tr>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<tr><td align="left"> Normalization </td> <td align="center"> \( P_N=1 \) </td> <td align="center"> \( P(b)=1 \) </td> </tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec14">Probability Distribution Functions </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
With a PDF we can compute expectation values of selected quantities such as
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle x^k\rangle=\frac{1}{N}\sum_{i=1}^{N}x_i^kp(x_i),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
if we have a discrete PDF or
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle x^k\rangle=\int_a^b x^kp(x)dx,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
in the case of a continuous PDF. We have already defined the mean value \( \mu \)
|
|
and the variance \( \sigma^2 \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec15">The three famous Probability Distribution Functions </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
There are at least three PDFs which one may encounter. These are the
|
|
|
|
<p>
|
|
<b>Uniform distribution</b>
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x)=\frac{1}{b-a}\Theta(x-a)\Theta(b-x),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
yielding probabilities different from zero in the interval \( [a,b] \).
|
|
|
|
<p>
|
|
<b>The exponential distribution</b>
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x)=\alpha \exp{(-\alpha x)},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
yielding probabilities different from zero in the interval \( [0,\infty) \) and with mean value
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mu = \int_0^{\infty}xp(x)dx=\int_0^{\infty}x\alpha \exp{(-\alpha x)}dx=\frac{1}{\alpha},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
|
|
with variance
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\sigma^2=\int_0^{\infty}x^2p(x)dx-\mu^2 = \frac{1}{\alpha^2}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec16">Probability Distribution Functions, the normal distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Finally, we have the so-called univariate normal distribution, or just the <b>normal distribution</b>
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x)=\frac{1}{b\sqrt{2\pi}}\exp{\left(-\frac{(x-a)^2}{2b^2}\right)}
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
with probabilities different from zero in the interval \( (-\infty,\infty) \).
|
|
The integral \( \int_{-\infty}^{\infty}\exp{\left(-(x^2\right)}dx \) appears in many calculations, its value
|
|
is \( \sqrt{\pi} \), a result we will need when we compute the mean value and the variance.
|
|
The mean value is
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\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,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
which becomes with a suitable change of variables
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mu =\frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}b\sqrt{2}(a+b\sqrt{2}y)\exp{-y^2}dy=a.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec17">Probability Distribution Functions, the normal distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Similarly, the variance becomes
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\sigma^2 = \frac{1}{b\sqrt{2\pi}}\int_{-\infty}^{\infty}(x-\mu)^2 \exp{\left(-\frac{(x-a)^2}{2b^2}\right)}dx,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
and inserting the mean value and performing a variable change we obtain
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\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=
|
|
\frac{2b^2}{\sqrt{\pi}}\int_{-\infty}^{\infty}y^2\exp{\left(-y^2\right)}dy,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
and performing a final integration by parts we obtain the well-known result \( \sigma^2=b^2 \).
|
|
It is useful to introduce the standard normal distribution as well, defined by \( \mu=a=0 \), viz. a distribution
|
|
centered around zero and with a variance \( \sigma^2=1 \), leading to
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
p(x)=\frac{1}{\sqrt{2\pi}}\exp{\left(-\frac{x^2}{2}\right)}.
|
|
\tag{3}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec18">Probability Distribution Functions, the cumulative distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The exponential and uniform distributions have simple cumulative functions,
|
|
whereas the normal distribution does not, being proportional to the so-called
|
|
error function \( erf(x) \), given by
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
P(x) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^x\exp{\left(-\frac{t^2}{2}\right)}dt,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
which is difficult to evaluate in a quick way.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec19">Probability Distribution Functions, other important distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Some other PDFs which one encounters often in the natural sciences are the binomial distribution
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
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,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
where \( y \) is the probability for a specific event, such as the tossing of a coin or moving left or right
|
|
in case of a random walker. Note that \( x \) is a discrete stochastic variable.
|
|
|
|
<p>
|
|
The sequence of binomial trials is characterized by the following definitions
|
|
|
|
<ul>
|
|
|
|
<p><li> Every experiment is thought to consist of \( N \) independent trials.</li>
|
|
|
|
<p><li> 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.</li>
|
|
|
|
<p><li> 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 \).</li>
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec20">Probability Distribution Functions, the binomial distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
In order to compute the mean and variance we need to recall Newton's binomial
|
|
formula
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
(a+b)^m=\sum_{n=0}^m \left(\begin{array}{c} m \\ n\end{array}\right)a^nb^{m-n},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
which can be used to show that
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\sum_{x=0}^n\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} = (y+1-y)^n = 1,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
the PDF is normalized to one.
|
|
The mean value is
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mu = \sum_{x=0}^n x\left(\begin{array}{c} n \\ x\end{array}\right)y^x(1-y)^{n-x} =
|
|
\sum_{x=0}^n x\frac{n!}{x!(n-x)!}y^x(1-y)^{n-x},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
resulting in
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mu =
|
|
\sum_{x=0}^n x\frac{(n-1)!}{(x-1)!(n-1-(x-1))!}y^{x-1}(1-y)^{n-1-(x-1)},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
which we rewrite as
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\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.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
|
|
The variance is slightly trickier to get. It reads \( \sigma^2=ny(1-y) \).
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec21">Probability Distribution Functions, Poisson's distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Another important distribution with discrete stochastic variables \( x \) is
|
|
the Poisson model, which resembles the exponential distribution and reads
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
p(x) = \frac{\lambda^x}{x!} e^{-\lambda} \hspace{0.5cm}x=0,1,\dots,;\lambda > 0.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
In this case both the mean value and the variance are easier to calculate,
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mu = \sum_{x=0}^{\infty} x \frac{\lambda^x}{x!} e^{-\lambda} = \lambda e^{-\lambda}\sum_{x=1}^{\infty}
|
|
\frac{\lambda^{x-1}}{(x-1)!}=\lambda,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
and the variance is \( \sigma^2=\lambda \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec22">Probability Distribution Functions, Poisson's distribution </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
An example of applications of the Poisson distribution could be the counting
|
|
of the number of \( \alpha \)-particles emitted from a radioactive source in a given time interval.
|
|
In the limit of \( n\rightarrow \infty \) and for small probabilities \( y \), the binomial distribution
|
|
approaches the Poisson distribution. Setting \( \lambda = ny \), with \( y \) the probability for an event in
|
|
the binomial distribution we can show that
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\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}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec23">Meet the covariance! </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
An important quantity in a statistical analysis is the so-called covariance.
|
|
|
|
<p>
|
|
Consider the set \( \{X_i\} \) of \( n \)
|
|
stochastic variables (not necessarily uncorrelated) with the
|
|
multivariate PDF \( P(x_1,\dots,x_n) \). The <em>covariance</em> of two
|
|
of the stochastic variables, \( X_i \) and \( X_j \), is defined as follows
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{align}
|
|
\mathrm{Cov}(X_i,\,X_j) & = \langle (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)\rangle
|
|
\tag{4}\\
|
|
&=\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,
|
|
\tag{5}
|
|
\end{align}
|
|
$$
|
|
<p> <br>
|
|
|
|
with
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle x_i\rangle =
|
|
\int\cdots\int x_i P(x_1,\dots,x_n)\,dx_1\dots dx_n.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec24">Meet the covariance in matrix disguise </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
If we consider the above covariance as a matrix
|
|
<p> <br>
|
|
$$
|
|
C_{ij} =\mathrm{Cov}(X_i,\,X_j),
|
|
$$
|
|
<p> <br>
|
|
|
|
then the diagonal elements are just the familiar
|
|
variances, \( C_{ii} = \mathrm{Cov}(X_i,\,X_i) = \mathrm{Var}(X_i) \). It turns out that
|
|
all the off-diagonal elements are zero if the stochastic variables are
|
|
uncorrelated.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec25">Meet the covariance, uncorrelated events </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
This is easy to show, keeping in mind the linearity of
|
|
the expectation value. Consider the stochastic variables \( X_i \) and
|
|
\( X_j \), (\( i\neq j \))
|
|
<p> <br>
|
|
$$
|
|
\begin{align*}
|
|
\mathrm{Cov}(X_i,\,X_j) &= \langle (x_i-\langle x_i\rangle)(x_j-\langle x_j\rangle)\rangle\\
|
|
&=\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\\
|
|
&=\langle x_i x_j\rangle - \langle x_i\langle x_j\rangle\rangle - \langle \langle x_i\rangle x_j \rangle +
|
|
\langle \langle x_i\rangle\langle x_j\rangle\rangle\\
|
|
&=\langle x_i x_j\rangle - \langle x_i\rangle\langle x_j\rangle - \langle x_i\rangle\langle x_j\rangle +
|
|
\langle x_i\rangle\langle x_j\rangle\\
|
|
&=\langle x_i x_j\rangle - \langle x_i\rangle\langle x_j\rangle
|
|
\end{align*}
|
|
$$
|
|
<p> <br>
|
|
|
|
If \( X_i \) and \( X_j \) are independent, we get
|
|
<p> <br>
|
|
$$
|
|
\langle x_i x_j\rangle =
|
|
\langle x_i\rangle\langle x_j\rangle=\mathrm{Cov}(X_i, X_j) = 0\ \ (i\neq j).
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec26">Numerical experiments and the covariance </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Now that we have constructed an idealized mathematical framework, let
|
|
us try to apply it to empirical observations. Examples of relevant
|
|
physical phenomena may be spontaneous decays of nuclei, or a purely
|
|
mathematical set of numbers produced by some deterministic
|
|
mechanism. It is the latter we will deal with, using so-called pseudo-random
|
|
number generators. In general our observations will contain only a limited set of
|
|
observables. We remind the reader that
|
|
a <em>stochastic process</em> is a process that produces sequentially a
|
|
chain of values
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\{x_1, x_2,\dots\,x_k,\dots\}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec27">Numerical experiments and the covariance </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
We will call these
|
|
values our <em>measurements</em> and the entire set as our measured
|
|
<em>sample</em>. The action of measuring all the elements of a sample
|
|
we will call a stochastic <em>experiment</em> (since, operationally,
|
|
they are often associated with results of empirical observation of
|
|
some physical or mathematical phenomena; precisely an experiment). We
|
|
assume that these values are distributed according to some
|
|
PDF \( p_X^{\phantom X}(x) \), where \( X \) is just the formal symbol for the
|
|
stochastic variable whose PDF is \( p_X^{\phantom X}(x) \). Instead of
|
|
trying to determine the full distribution \( p \) we are often only
|
|
interested in finding the few lowest moments, like the mean
|
|
\( \mu_X^{\phantom X} \) and the variance \( \sigma_X^{\phantom X} \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec28">Numerical experiments and the covariance, actual situations </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
In practical situations however, a sample is always of finite size. Let that
|
|
size be \( n \). The expectation value of a sample \( \alpha \), the <b>sample mean</b>, is then defined as follows
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\langle x_{\alpha} \rangle \equiv \frac{1}{n}\sum_{k=1}^n x_{\alpha,k}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
The <em>sample variance</em> is:
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mathrm{Var}(x) \equiv \frac{1}{n}\sum_{k=1}^n (x_{\alpha,k} - \langle x_{\alpha} \rangle)^2,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
with its square root being the <em>standard deviation of the sample</em>.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec29">Numerical experiments and the covariance, our observables </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
You can think of the above observables as a set of quantities which define
|
|
a given experiment. This experiment is then repeated several times, say \( m \) times.
|
|
The total average is then
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
\langle X_m \rangle= \frac{1}{m}\sum_{\alpha=1}^mx_{\alpha}=\frac{1}{mn}\sum_{\alpha, k} x_{\alpha,k},
|
|
\tag{6}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
where the last sums end at \( m \) and \( n \).
|
|
The total variance is
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\sigma^2_m= \frac{1}{mn^2}\sum_{\alpha=1}^m(\langle x_{\alpha} \rangle-\langle X_m \rangle)^2,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
which we rewrite as
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
\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).
|
|
\tag{7}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec30">Numerical experiments and the covariance, the sample variance </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
We define also the sample variance \( \sigma^2 \) of all \( mn \) individual experiments as
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
\sigma^2=\frac{1}{mn}\sum_{\alpha=1}^m\sum_{k=1}^n (x_{\alpha,k}-\langle X_m \rangle)^2.
|
|
\tag{8}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
<p>
|
|
These quantities, being known experimental values or the results from our calculations,
|
|
may differ, in some cases
|
|
significantly, from the similarly named
|
|
exact values for the mean value \( \mu_X \), the variance \( \mathrm{Var}(X) \)
|
|
and the covariance \( \mathrm{Cov}(X,Y) \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec31">Numerical experiments and the covariance, central limit theorem </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The central limit theorem states that the PDF \( \tilde{p}(z) \) of
|
|
the average of \( m \) random values corresponding to a PDF \( p(x) \)
|
|
is a normal distribution whose mean is the
|
|
mean value of the PDF \( p(x) \) and whose variance is the variance
|
|
of the PDF \( p(x) \) divided by \( m \), the number of values used to compute \( z \).
|
|
|
|
<p>
|
|
The central limit theorem leads then to the well-known expression for the
|
|
standard deviation, given by
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\sigma_m=
|
|
\frac{\sigma}{\sqrt{m}}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
<p>
|
|
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.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec32">Definition of Correlation Functions and Standard Deviation </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Our estimate of the true average \( \mu_{X} \) is the sample mean \( \langle X_m \rangle \)
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mu_{X}^{\phantom X} \approx X_m=\frac{1}{mn}\sum_{\alpha=1}^m\sum_{k=1}^n x_{\alpha,k}.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
<p>
|
|
We can then use Eq. <a href="#mjx-eqn-7">(7)</a>
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\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),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
and rewrite it as
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\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),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
where the first term is the sample variance of all \( mn \) experiments divided by \( n \)
|
|
and the last term is nothing but the covariance which arises when \( k\ne l \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec33">Definition of Correlation Functions and Standard Deviation </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Our estimate of the true average \( \mu_{X} \) is the sample mean \( \langle X_m \rangle \)
|
|
|
|
<p>
|
|
If the
|
|
observables are uncorrelated, then the covariance is zero and we obtain a total variance
|
|
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
|
|
contribution.
|
|
Computationally the uncorrelated first term is much easier to treat
|
|
efficiently than the second.
|
|
We just accumulate separately the values \( x^2 \) and \( x \) for every
|
|
measurement \( x \) we receive. The correlation term, though, has to be
|
|
calculated at the end of the experiment since we need all the
|
|
measurements to calculate the cross terms. Therefore, all measurements
|
|
have to be stored throughout the experiment.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec34">Definition of Correlation Functions and Standard Deviation </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Let us analyze the problem by splitting up the correlation term into
|
|
partial sums of the form
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
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),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
The correlation term of the total variance can now be rewritten in terms of
|
|
\( f_d \)
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\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)=
|
|
\frac{2}{n}\sum_{d=1}^{n-1} f_d
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec35">Definition of Correlation Functions and Standard Deviation </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The value of \( f_d \) reflects the correlation between measurements
|
|
separated by the distance \( d \) in the samples. Notice that for
|
|
\( d=0 \), \( f \) is just the sample variance, \( \sigma^2 \). If we divide \( f_d \)
|
|
by \( \sigma^2 \), we arrive at the so called <b>autocorrelation function</b>
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
\kappa_d = \frac{f_d}{\sigma^2}
|
|
\tag{9}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
which gives us a useful measure of the correlation pair correlation
|
|
starting always at \( 1 \) for \( d=0 \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec36">Definition of Correlation Functions and Standard Deviation, sample variance </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The sample variance of the \( mn \) experiments can now be
|
|
written in terms of the autocorrelation function
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
\sigma_m^2=\frac{\sigma^2}{n}+\frac{2}{n}\cdot\sigma^2\sum_{d=1}^{n-1}
|
|
\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
|
|
\tag{10}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
and we see that \( \sigma_m \) can be expressed in terms of the
|
|
uncorrelated sample variance times a correction factor \( \tau \) which
|
|
accounts for the correlation between measurements. We call this
|
|
correction factor the <em>autocorrelation time</em>
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
\tau = 1+2\sum_{d=1}^{n-1}\kappa_d
|
|
\tag{11}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
<!-- It is closely related to the area under the graph of the -->
|
|
<!-- autocorrelation function. -->
|
|
For a correlation free experiment, \( \tau \)
|
|
equals 1.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec37">Definition of Correlation Functions and Standard Deviation </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
From the point of view of
|
|
Eq. <a href="#mjx-eqn-10">(10)</a> we can interpret a sequential
|
|
correlation as an effective reduction of the number of measurements by
|
|
a factor \( \tau \). The effective number of measurements becomes
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
n_\mathrm{eff} = \frac{n}{\tau}
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
To neglect the autocorrelation time \( \tau \) will always cause our
|
|
simple uncorrelated estimate of \( \sigma_m^2\approx \sigma^2/n \) to
|
|
be less than the true sample error. The estimate of the error will be
|
|
too "good". On the other hand, the calculation of the full
|
|
autocorrelation time poses an efficiency problem if the set of
|
|
measurements is very large. The solution to this problem is given by
|
|
more practically oriented methods like the blocking technique.
|
|
<!-- add ref here to flybjerg -->
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec38">Random Numbers </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Uniform deviates are just random numbers that lie within a specified range
|
|
(typically 0 to 1), with any one number in the range just as likely as any other. They
|
|
are, in other words, what you probably think random numbers are. However,
|
|
we want to distinguish uniform deviates from other sorts of random numbers, for
|
|
example numbers drawn from a normal (Gaussian) distribution of specified mean
|
|
and standard deviation. These other sorts of deviates are almost always generated by
|
|
performing appropriate operations on one or more uniform deviates, as we will see
|
|
in subsequent sections. So, a reliable source of random uniform deviates, the subject
|
|
of this section, is an essential building block for any sort of stochastic modeling
|
|
or Monte Carlo computer work.
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec39">Random Numbers, better name: pseudo random numbers </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
A disclaimer is however appropriate. It should be fairly obvious that
|
|
something as deterministic as a computer cannot generate purely random numbers.
|
|
|
|
<p>
|
|
Numbers generated by any of the standard algorithms are in reality pseudo random
|
|
numbers, hopefully abiding to the following criteria:
|
|
|
|
<ul>
|
|
|
|
<p><li> they produce a uniform distribution in the interval [0,1].</li>
|
|
|
|
<p><li> correlations between random numbers are negligible</li>
|
|
|
|
<p><li> the period before the same sequence of random numbers is repeated is as large as possible and finally</li>
|
|
|
|
<p><li> the algorithm should be fast.</li>
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec40">Random number generator RNG </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The most common random number generators are based on so-called
|
|
Linear congruential relations of the type
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
N_i=(aN_{i-1}+c) \mathrm{MOD} (M),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
which yield a number in the interval [0,1] through
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
x_i=N_i/M
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
<p>
|
|
The number
|
|
\( M \) is called the period and it should be as large as possible
|
|
and
|
|
\( N_0 \) is the starting value, or seed. The function \( \mathrm{MOD} \) means the remainder,
|
|
that is if we were to evaluate \( (13)\mathrm{MOD}(9) \), the outcome is the remainder
|
|
of the division \( 13/9 \), namely \( 4 \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec41">Random number generator RNG and periodic outputs </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The problem with such generators is that their outputs are periodic;
|
|
they
|
|
will start to repeat themselves with a period that is at most \( M \). If however
|
|
the parameters \( a \) and \( c \) are badly chosen, the period may be even shorter.
|
|
|
|
<p>
|
|
Consider the following example
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
N_i=(6N_{i-1}+7) \mathrm{MOD} (5),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
with a seed \( N_0=2 \). This generator produces the sequence
|
|
\( 4,1,3,0,2,4,1,3,0,2,...\dots \), i.e., a sequence with period \( 5 \).
|
|
However, increasing \( M \) may not guarantee a larger period as the following
|
|
example shows
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
N_i=(27N_{i-1}+11) \mathrm{MOD} (54),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
which still, with \( N_0=2 \), results in \( 11,38,11,38,11,38,\dots \), a period of
|
|
just \( 2 \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec42">Random number generator RNG and its period </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Typical periods for the random generators provided in the program library
|
|
are of the order of \( \sim 10^9 \) or larger. Other random number generators which have
|
|
become increasingly popular are so-called shift-register generators.
|
|
In these generators each successive number depends on many preceding
|
|
values (rather than the last values as in the linear congruential
|
|
generator).
|
|
For example, you could make a shift register generator whose $l$th
|
|
number is the sum of the $l-i$th and $l-j$th values with modulo \( M \),
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
N_l=(aN_{l-i}+cN_{l-j})\mathrm{MOD}(M).
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec43">Random number generator RNG, other examples </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Such a generator again produces a sequence of pseudorandom numbers
|
|
but this time with a period much larger than \( M \).
|
|
It is also possible to construct more elaborate algorithms by including
|
|
more than two past terms in the sum of each iteration.
|
|
One example is the generator of <a href="http://dl.acm.org/citation.cfm?id=187154" target="_blank">Marsaglia and Zaman</a>
|
|
which consists of two congruential relations
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
N_l=(N_{l-3}-N_{l-1})\mathrm{MOD}(2^{31}-69),
|
|
\tag{12}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
followed by
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
N_l=(69069N_{l-1}+1013904243)\mathrm{MOD}(2^{32}),
|
|
\tag{13}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
which according to the authors has a period larger than \( 2^{94} \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec44">Random number generator RNG, other examples </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Instead of using modular addition, we could use the bitwise
|
|
exclusive-OR (\( \oplus \)) operation so that
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
N_l=(N_{l-i})\oplus (N_{l-j})
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
where the bitwise action of \( \oplus \) means that if \( N_{l-i}=N_{l-j} \) the result is
|
|
\( 0 \) whereas if \( N_{l-i}\ne N_{l-j} \) the result is
|
|
\( 1 \). As an example, consider the case where \( N_{l-i}=6 \) and \( N_{l-j}=11 \). The first
|
|
one has a bit representation (using 4 bits only) which reads \( 0110 \) whereas the
|
|
second number is \( 1011 \). Employing the \( \oplus \) operator yields
|
|
\( 1101 \), or \( 2^3+2^2+2^0=13 \).
|
|
|
|
<p>
|
|
In Fortran90, the bitwise \( \oplus \) operation is coded through the intrinsic
|
|
function \( \mathrm{IEOR}(m,n) \) where \( m \) and \( n \) are the input numbers, while in \( C \)
|
|
it is given by \( m\wedge n \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec45">Random number generator RNG, RAN0 </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
We show here how the linear congruential algorithm can be implemented, namely
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
N_i=(aN_{i-1}) \mathrm{MOD} (M).
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
However, since \( a \) and \( N_{i-1} \) are integers and their multiplication
|
|
could become greater than the standard 32 bit integer, there is a trick via
|
|
Schrage's algorithm which approximates the multiplication
|
|
of large integers through the factorization
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
M=aq+r,
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
where we have defined
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
q=[M/a],
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
and
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
r = M\hspace{0.1cm}\mathrm{MOD} \hspace{0.1cm}a.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
where the brackets denote integer division. In the code below the numbers
|
|
\( q \) and \( r \) are chosen so that \( r < q \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec46">Random number generator RNG, RAN0 </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
To see how this works we note first that
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
(aN_{i-1}) \mathrm{MOD} (M)= (aN_{i-1}-[N_{i-1}/q]M)\mathrm{MOD} (M),
|
|
\tag{14}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
since we can add or subtract any integer multiple of \( M \) from \( aN_{i-1} \).
|
|
The last term \( [N_{i-1}/q]M\mathrm{MOD}(M) \) is zero since the integer division
|
|
\( [N_{i-1}/q] \) just yields a constant which is multiplied with \( M \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec47">Random number generator RNG, RAN0 </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
We can now rewrite Eq. <a href="#mjx-eqn-14">(14)</a> as
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
(aN_{i-1}) \mathrm{MOD} (M)= (aN_{i-1}-[N_{i-1}/q](aq+r))\mathrm{MOD} (M),
|
|
\tag{15}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
which results
|
|
in
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
(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),
|
|
\tag{16}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
|
|
yielding
|
|
<p> <br>
|
|
$$
|
|
\begin{equation}
|
|
(aN_{i-1}) \mathrm{MOD} (M)= \left(a(N_{i-1}\mathrm{MOD} (q)) -[N_{i-1}/q]r)\right)\mathrm{MOD} (M).
|
|
\tag{17}
|
|
\end{equation}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec48">Random number generator RNG, RAN0 </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
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
|
|
number smaller than \( N_{i-1} \), which is smaller than \( M \).
|
|
And since the number \( N_{i-1}\mathrm{MOD} (q) \) is between zero and \( q-1 \) then
|
|
\( a(N_{i-1}\mathrm{MOD} (q)) < aq \). Combined with our definition of \( q=[M/a] \) ensures that
|
|
this term is also smaller than \( M \) meaning that both terms fit into a
|
|
32-bit signed integer. None of these two terms can be negative, but their difference could.
|
|
The algorithm below adds \( M \) if their difference is negative.
|
|
Note that the program uses the bitwise \( \oplus \) operator to generate
|
|
the starting point for each generation of a random number. The period
|
|
of \( ran0 \) is \( \sim 2.1\times 10^{9} \). A special feature of this
|
|
algorithm is that is should never be called with the initial seed
|
|
set to \( 0 \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec49">Random number generator RNG, RAN0 code </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
|
|
<!-- code=c++ (!bc cppcod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span> <span style="color: #228B22">/*</span>
|
|
<span style="color: #228B22"> ** The function</span>
|
|
<span style="color: #228B22"> ** ran0()</span>
|
|
<span style="color: #228B22"> ** is an "Minimal" random number generator of Park and Miller</span>
|
|
<span style="color: #228B22"> ** Set or reset the input value</span>
|
|
<span style="color: #228B22"> ** idum to any integer value (except the unlikely value MASK)</span>
|
|
<span style="color: #228B22"> ** to initialize the sequence; idum must not be altered between</span>
|
|
<span style="color: #228B22"> ** calls for sucessive deviates in a sequence.</span>
|
|
<span style="color: #228B22"> ** The function returns a uniform deviate between 0.0 and 1.0.</span>
|
|
<span style="color: #228B22"> */</span>
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> <span style="color: #008b45">ran0</span>(<span style="color: #a7a7a7; font-weight: bold">long</span> &idum)
|
|
{
|
|
<span style="color: #8B008B; font-weight: bold">const</span> <span style="color: #a7a7a7; font-weight: bold">int</span> a = <span style="color: #B452CD">16807</span>, m = <span style="color: #B452CD">2147483647</span>, q = <span style="color: #B452CD">127773</span>;
|
|
<span style="color: #8B008B; font-weight: bold">const</span> <span style="color: #a7a7a7; font-weight: bold">int</span> r = <span style="color: #B452CD">2836</span>, MASK = <span style="color: #B452CD">123459876</span>;
|
|
<span style="color: #8B008B; font-weight: bold">const</span> <span style="color: #a7a7a7; font-weight: bold">double</span> am = <span style="color: #B452CD">1.</span>/m;
|
|
<span style="color: #a7a7a7; font-weight: bold">long</span> k;
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> ans;
|
|
idum ^= MASK;
|
|
k = (*idum)/q;
|
|
idum = a*(idum - k*q) - r*k;
|
|
<span style="color: #228B22">// add m if negative difference</span>
|
|
<span style="color: #8B008B; font-weight: bold">if</span>(idum < <span style="color: #B452CD">0</span>) idum += m;
|
|
ans=am*(idum);
|
|
idum ^= MASK;
|
|
<span style="color: #8B008B; font-weight: bold">return</span> ans;
|
|
} <span style="color: #228B22">// End: function ran0() </span>
|
|
</pre></div>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec50">Properties of Selected Random Number Generators </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
As mentioned previously, the underlying PDF for the generation of
|
|
random numbers is the uniform distribution, meaning that the
|
|
probability for finding a number \( x \) in the interval [0,1] is \( p(x)=1 \).
|
|
|
|
<p>
|
|
A random number generator should produce numbers which are uniformly distributed
|
|
in this interval. The table shows the distribution of \( N=10000 \) random
|
|
numbers generated by the functions in the program library.
|
|
We note in this table that the number of points in the various
|
|
intervals \( 0.0-0.1 \), \( 0.1-0.2 \) etc are fairly close to \( 1000 \), with some minor
|
|
deviations.
|
|
|
|
<p>
|
|
Two additional measures are the standard deviation \( \sigma \) and the mean
|
|
\( \mu=\langle x\rangle \).
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec51">Properties of Selected Random Number Generators </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
For the uniform distribution, the mean value \( \mu \) is then
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\mu=\langle x\rangle=\frac{1}{2}
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
while the standard deviation is
|
|
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
\sigma=\sqrt{\langle x^2\rangle-\mu^2}=\frac{1}{\sqrt{12}}=0.2886.
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec52">Properties of Selected Random Number Generators </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The various random number generators produce results which agree rather well with
|
|
these limiting values.
|
|
|
|
<p>
|
|
<table border="1">
|
|
<thead>
|
|
<tr><td align="center">\( x \)-bin </td> <th align="center"> ran0 </th> <th align="center"> ran1 </th> <th align="center"> ran2 </th> <th align="center"> ran3 </th> </tr>
|
|
</thead>
|
|
<tbody>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
<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>
|
|
</tbody>
|
|
</table>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec53">Simple demonstration of RNGs using python </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
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.
|
|
<p>
|
|
|
|
|
|
<div class="compute"><script type="text/x-sage">
|
|
#!/usr/bin/env python
|
|
import numpy as np
|
|
import matplotlib.mlab as mlab
|
|
import matplotlib.pyplot as plt
|
|
import random
|
|
|
|
# initialize the rng with a seed
|
|
random.seed()
|
|
counts = 10000
|
|
values = np.zeros(counts)
|
|
for i in range (1, counts, 1):
|
|
values[i] = random.random()
|
|
|
|
# the histogram of the data
|
|
n, bins, patches = plt.hist(values, 10, facecolor='green')
|
|
|
|
plt.xlabel('$x$')
|
|
plt.ylabel('Number of counts')
|
|
plt.title(r'Test of uniform distribution')
|
|
plt.axis([0, 1, 0, 1100])
|
|
plt.grid(True)
|
|
plt.show()
|
|
|
|
</script></div>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec54">Properties of Selected Random Number Generators </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
Since our random numbers, which are typically generated via a linear congruential algorithm,
|
|
are never fully independent, we can then define
|
|
an important test which measures the degree of correlation, namely the so-called
|
|
auto-correlation function defined previously, see again Eq. <a href="#mjx-eqn-9">(9)</a>.
|
|
We rewrite it here as
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
C_k=\frac{f_d}
|
|
{\sigma^2},
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
with \( C_0=1 \). Recall that
|
|
\( \sigma^2=\langle x_i^2\rangle-\langle x_i\rangle^2 \) and that
|
|
<p> <br>
|
|
$$
|
|
\begin{equation*}
|
|
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),
|
|
\end{equation*}
|
|
$$
|
|
<p> <br>
|
|
|
|
<p>
|
|
The non-vanishing of \( C_k \) for \( k\ne 0 \) means that the random
|
|
numbers are not independent. The independence of the random numbers is crucial
|
|
in the evaluation of other expectation values. If they are not independent, our
|
|
assumption for approximating \( \sigma_N \) is no longer valid.
|
|
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec55">Correlation function and which random number generators should I use </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The program here computes the correlation function for one of the standard functions included with the c++ compiler.
|
|
<p>
|
|
|
|
<!-- code=c++ (!bc cppcod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22">// This function computes the autocorrelation function for </span>
|
|
<span style="color: #228B22">// the standard c++ random number generator</span>
|
|
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><fstream></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><iomanip></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><iostream></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><cmath></span><span style="color: #1e889b"></span>
|
|
<span style="color: #8B008B; font-weight: bold">using</span> <span style="color: #8B008B; font-weight: bold">namespace</span> std;
|
|
<span style="color: #228B22">// output file as global variable</span>
|
|
ofstream ofile;
|
|
|
|
<span style="color: #228B22">// Main function begins here </span>
|
|
<span style="color: #a7a7a7; font-weight: bold">int</span> <span style="color: #008b45">main</span>(<span style="color: #a7a7a7; font-weight: bold">int</span> argc, <span style="color: #a7a7a7; font-weight: bold">char</span>* argv[])
|
|
{
|
|
<span style="color: #a7a7a7; font-weight: bold">int</span> n;
|
|
<span style="color: #a7a7a7; font-weight: bold">char</span> *outfilename;
|
|
|
|
cin >> n;
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> MCint = <span style="color: #B452CD">0.</span>; <span style="color: #a7a7a7; font-weight: bold">double</span> MCintsqr2=<span style="color: #B452CD">0.</span>;
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> invers_period = <span style="color: #B452CD">1.</span>/RAND_MAX; <span style="color: #228B22">// initialise the random number generator</span>
|
|
srand(time(<span style="color: #658b00">NULL</span>)); <span style="color: #228B22">// This produces the so-called seed in MC jargon</span>
|
|
<span style="color: #228B22">// Compute the variance and the mean value of the uniform distribution</span>
|
|
<span style="color: #228B22">// Compute also the specific values x for each cycle in order to be able to</span>
|
|
<span style="color: #228B22">// the covariance and the correlation function </span>
|
|
<span style="color: #228B22">// Read in output file, abort if there are too few command-line arguments</span>
|
|
<span style="color: #8B008B; font-weight: bold">if</span>( argc <= <span style="color: #B452CD">2</span> ){
|
|
cout << <span style="color: #CD5555">"Bad Usage: "</span> << argv[<span style="color: #B452CD">0</span>] <<
|
|
<span style="color: #CD5555">" read also output file and number of cycles on same line"</span> << endl;
|
|
exit(<span style="color: #B452CD">1</span>);
|
|
}
|
|
<span style="color: #8B008B; font-weight: bold">else</span>{
|
|
outfilename=argv[<span style="color: #B452CD">1</span>];
|
|
}
|
|
ofile.open(outfilename);
|
|
<span style="color: #228B22">// Get the number of Monte-Carlo samples</span>
|
|
n = atoi(argv[<span style="color: #B452CD">2</span>]);
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> *X;
|
|
X = <span style="color: #8B008B; font-weight: bold">new</span> <span style="color: #a7a7a7; font-weight: bold">double</span>[n];
|
|
<span style="color: #8B008B; font-weight: bold">for</span> (<span style="color: #a7a7a7; font-weight: bold">int</span> i = <span style="color: #B452CD">0</span>; i < n; i++){
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> x = <span style="color: #a7a7a7; font-weight: bold">double</span>(rand())*invers_period;
|
|
X[i] = x;
|
|
MCint += x;
|
|
MCintsqr2 += x*x;
|
|
}
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> Mean = MCint/((<span style="color: #a7a7a7; font-weight: bold">double</span>) n );
|
|
MCintsqr2 = MCintsqr2/((<span style="color: #a7a7a7; font-weight: bold">double</span>) n );
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> STDev = sqrt(MCintsqr2-Mean*Mean);
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> Variance = MCintsqr2-Mean*Mean;
|
|
<span style="color: #228B22">// Write mean value and standard deviation </span>
|
|
cout << <span style="color: #CD5555">" Standard deviation= "</span> << STDev << <span style="color: #CD5555">" Integral = "</span> << Mean << endl;
|
|
|
|
<span style="color: #228B22">// Now we compute the autocorrelation function</span>
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> *autocor; autocor = <span style="color: #8B008B; font-weight: bold">new</span> <span style="color: #a7a7a7; font-weight: bold">double</span>[n];
|
|
<span style="color: #8B008B; font-weight: bold">for</span> (<span style="color: #a7a7a7; font-weight: bold">int</span> j = <span style="color: #B452CD">0</span>; j < n; j++){
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> sum = <span style="color: #B452CD">0.0</span>;
|
|
<span style="color: #8B008B; font-weight: bold">for</span> (<span style="color: #a7a7a7; font-weight: bold">int</span> k = <span style="color: #B452CD">0</span>; k < (n-j); k++){
|
|
sum += (X[k]-Mean)*(X[k+j]-Mean);
|
|
}
|
|
autocor[j] = sum/Variance/((<span style="color: #a7a7a7; font-weight: bold">double</span>) n );
|
|
ofile << setiosflags(ios::showpoint | ios::uppercase);
|
|
ofile << setw(<span style="color: #B452CD">15</span>) << setprecision(<span style="color: #B452CD">8</span>) << j;
|
|
ofile << setw(<span style="color: #B452CD">15</span>) << setprecision(<span style="color: #B452CD">8</span>) << autocor[j] << endl;
|
|
}
|
|
ofile.close(); <span style="color: #228B22">// close output file</span>
|
|
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #B452CD">0</span>;
|
|
} <span style="color: #228B22">// end of main program </span>
|
|
</pre></div>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec56">Correlation function and which random number generators should I use </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The following Python code plots the results for the correlation function from the above program.
|
|
<p>
|
|
|
|
|
|
<div class="compute"><script type="text/x-sage">
|
|
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
# Load in data file
|
|
data = np.loadtxt("datafiles/autocor.dat")
|
|
# Make arrays containing x-axis and binding energies as function of A
|
|
x = data[:,0]
|
|
corr = data[:,1]
|
|
plt.plot(x, corr ,'ro')
|
|
plt.axis([0,1000,-0.2, 1.1])
|
|
plt.xlabel(r'$d$')
|
|
plt.ylabel(r'$C_d$')
|
|
plt.title(r'autocorrelation function for RNG')
|
|
plt.savefig('autocorr.pdf')
|
|
plt.show()
|
|
|
|
|
|
</script></div>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h1 id="___sec57">Which RNG should I use? </h1>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<ul>
|
|
<p><li> In the library files lib.cpp and lib.h we have included four popular RNGs taken from the widely used textbook <a href="http://numerical.recipes/" target="_blank">Numerical Recipes</a>. These are called ran0, ran1, ran2 and ran3.</li>
|
|
<p><li> C++ has a class called <b>random</b>. The <a href="http://www.cplusplus.com/reference/random/" target="_blank">random class</a> 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 <a href="http://www.cplusplus.com/reference/random/mersenne_twister_engine/" target="_blank">Mersenne twister random number engine</a> has a period of \( 2^{19937} \).</li>
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec58">How to use the Mersenne generator </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<p>
|
|
The following part of a c++ code (from project 4) sets up the uniform distribution for \( x\in [0,1] \).
|
|
<p>
|
|
|
|
<!-- code=c++ (!bc cppcod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>/*
|
|
|
|
<span style="color: #228B22">// You need this </span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><random></span><span style="color: #1e889b"></span>
|
|
|
|
<span style="color: #228B22">// Initialize the seed and call the Mersienne algo</span>
|
|
std::random_device rd;
|
|
std::mt19937_64 gen(rd());
|
|
<span style="color: #228B22">// Set up the uniform distribution for x \in [[0, 1]</span>
|
|
std::uniform_real_distribution<<span style="color: #a7a7a7; font-weight: bold">double</span>> RandomNumberGenerator(<span style="color: #B452CD">0.0</span>,<span style="color: #B452CD">1.0</span>);
|
|
|
|
<span style="color: #228B22">// Now use the RNG</span>
|
|
<span style="color: #a7a7a7; font-weight: bold">int</span> ix = (<span style="color: #a7a7a7; font-weight: bold">int</span>) (RandomNumberGenerator(gen)*NSpins);
|
|
</pre></div>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec59">Why blocking? </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b>Statistical analysis.</b>
|
|
<ul>
|
|
|
|
<p><li> Monte Carlo simulations can be treated as <em>computer experiments</em></li>
|
|
|
|
<p><li> The results can be analysed with the same statistical tools as we would use analysing experimental data.</li>
|
|
|
|
<p><li> As in all experiments, we are looking for expectation values and an estimate of how accurate they are, i.e., possible sources for errors.</li>
|
|
</ul>
|
|
<p>
|
|
|
|
A very good article which explains blocking is H. Flyvbjerg and H. G. Petersen, <em>Error estimates on averages of correlated data</em>, <a href="http://scitation.aip.org/content/aip/journal/jcp/91/1/10.1063/1.457480" target="_blank">Journal of Chemical Physics 91, 461-466 (1989)</a>.
|
|
|
|
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec60">Why blocking? </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b>Statistical analysis.</b>
|
|
<ul>
|
|
|
|
<p><li> As in other experiments, Monte Carlo experiments have two classes of errors:</li>
|
|
|
|
<ul>
|
|
|
|
<p><li> Statistical errors</li>
|
|
|
|
<p><li> Systematical errors</li>
|
|
</ul>
|
|
<p><li> Statistical errors can be estimated using standard tools from statistics</li>
|
|
|
|
<p><li> 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)</li>
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec61">Code to demonstrate the calculation of the autocorrelation function </h2>
|
|
The following code computes the autocorrelation function, the covariance and the standard deviation
|
|
for standard RNG.
|
|
The <a href="https://github.com/CompPhysics/ComputationalPhysics2/tree/gh-pages/doc/Programs/LecturePrograms/programs/Blocking/autocorrelation.cpp" target="_blank">following file</a> gives the code.
|
|
<p>
|
|
|
|
<!-- code=c++ (!bc cppcod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22">// This function computes the autocorrelation function for </span>
|
|
<span style="color: #228B22">// the Mersenne random number generator with a uniform distribution</span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><iostream></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><fstream></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><iomanip></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><cstdlib></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><random></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><armadillo></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><string></span><span style="color: #1e889b"></span>
|
|
<span style="color: #1e889b">#include</span> <span style="color: #228B22"><cmath></span><span style="color: #1e889b"></span>
|
|
<span style="color: #8B008B; font-weight: bold">using</span> <span style="color: #8B008B; font-weight: bold">namespace</span> std;
|
|
<span style="color: #8B008B; font-weight: bold">using</span> <span style="color: #8B008B; font-weight: bold">namespace</span> arma;
|
|
<span style="color: #228B22">// output file</span>
|
|
ofstream ofile;
|
|
|
|
<span style="color: #228B22">// Main function begins here </span>
|
|
<span style="color: #a7a7a7; font-weight: bold">int</span> <span style="color: #008b45">main</span>(<span style="color: #a7a7a7; font-weight: bold">int</span> argc, <span style="color: #a7a7a7; font-weight: bold">char</span>* argv[])
|
|
{
|
|
<span style="color: #a7a7a7; font-weight: bold">int</span> MonteCarloCycles;
|
|
string filename;
|
|
<span style="color: #8B008B; font-weight: bold">if</span> (argc > <span style="color: #B452CD">1</span>) {
|
|
filename=argv[<span style="color: #B452CD">1</span>];
|
|
MonteCarloCycles = atoi(argv[<span style="color: #B452CD">2</span>]);
|
|
string fileout = filename;
|
|
string argument = to_string(MonteCarloCycles);
|
|
fileout.append(argument);
|
|
ofile.open(fileout);
|
|
}
|
|
|
|
<span style="color: #228B22">// Compute the variance and the mean value of the uniform distribution</span>
|
|
<span style="color: #228B22">// Compute also the specific values x for each cycle in order to be able to</span>
|
|
<span style="color: #228B22">// compute the covariance and the correlation function </span>
|
|
|
|
vec X = zeros<vec>(MonteCarloCycles);
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> MCint = <span style="color: #B452CD">0.</span>; <span style="color: #a7a7a7; font-weight: bold">double</span> MCintsqr2=<span style="color: #B452CD">0.</span>;
|
|
std::random_device rd;
|
|
std::mt19937_64 gen(rd());
|
|
<span style="color: #228B22">// Set up the uniform distribution for x \in [[0, 1]</span>
|
|
std::uniform_real_distribution<<span style="color: #a7a7a7; font-weight: bold">double</span>> RandomNumberGenerator(<span style="color: #B452CD">0.0</span>,<span style="color: #B452CD">1.0</span>);
|
|
<span style="color: #8B008B; font-weight: bold">for</span> (<span style="color: #a7a7a7; font-weight: bold">int</span> i = <span style="color: #B452CD">0</span>; i < MonteCarloCycles; i++){
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> x = RandomNumberGenerator(gen);
|
|
X(i) = x;
|
|
MCint += x;
|
|
MCintsqr2 += x*x;
|
|
}
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> Mean = MCint/((<span style="color: #a7a7a7; font-weight: bold">double</span>) MonteCarloCycles );
|
|
MCintsqr2 = MCintsqr2/((<span style="color: #a7a7a7; font-weight: bold">double</span>) MonteCarloCycles );
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> STDev = sqrt(MCintsqr2-Mean*Mean);
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> Variance = MCintsqr2-Mean*Mean;
|
|
<span style="color: #228B22">// Write mean value and variance</span>
|
|
cout << <span style="color: #CD5555">" Sample variance= "</span> << Variance << <span style="color: #CD5555">" Mean value = "</span> << Mean << endl;
|
|
<span style="color: #228B22">// Now we compute the autocorrelation function</span>
|
|
vec autocorrelation = zeros<vec>(MonteCarloCycles);
|
|
<span style="color: #8B008B; font-weight: bold">for</span> (<span style="color: #a7a7a7; font-weight: bold">int</span> j = <span style="color: #B452CD">0</span>; j < MonteCarloCycles; j++){
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> sum = <span style="color: #B452CD">0.0</span>;
|
|
<span style="color: #8B008B; font-weight: bold">for</span> (<span style="color: #a7a7a7; font-weight: bold">int</span> k = <span style="color: #B452CD">0</span>; k < (MonteCarloCycles-j); k++){
|
|
sum += (X(k)-Mean)*(X(k+j)-Mean);
|
|
}
|
|
autocorrelation(j) = sum/Variance/((<span style="color: #a7a7a7; font-weight: bold">double</span>) MonteCarloCycles );
|
|
ofile << setiosflags(ios::showpoint | ios::uppercase);
|
|
ofile << setw(<span style="color: #B452CD">15</span>) << setprecision(<span style="color: #B452CD">8</span>) << j;
|
|
ofile << setw(<span style="color: #B452CD">15</span>) << setprecision(<span style="color: #B452CD">8</span>) << autocorrelation(j) << endl;
|
|
}
|
|
<span style="color: #228B22">// Now compute the exact covariance using the autocorrelation function</span>
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> Covariance = <span style="color: #B452CD">0.0</span>;
|
|
<span style="color: #8B008B; font-weight: bold">for</span> (<span style="color: #a7a7a7; font-weight: bold">int</span> j = <span style="color: #B452CD">0</span>; j < MonteCarloCycles; j++){
|
|
Covariance += autocorrelation(j);
|
|
}
|
|
Covariance *= <span style="color: #B452CD">2.0</span>/((<span style="color: #a7a7a7; font-weight: bold">double</span>) MonteCarloCycles);
|
|
<span style="color: #228B22">// Compute now the total variance, including the covariance, and obtain the standard deviation</span>
|
|
<span style="color: #a7a7a7; font-weight: bold">double</span> TotalVariance = (Variance/((<span style="color: #a7a7a7; font-weight: bold">double</span>) MonteCarloCycles ))+Covariance;
|
|
cout << <span style="color: #CD5555">"Covariance ="</span> << Covariance << <span style="color: #CD5555">"Totalvariance= "</span> << TotalVariance << <span style="color: #CD5555">"Sample Variance/n= "</span> << (Variance/((<span style="color: #a7a7a7; font-weight: bold">double</span>) MonteCarloCycles )) << endl;
|
|
cout << <span style="color: #CD5555">" STD from sample variance= "</span> << sqrt(Variance/((<span style="color: #a7a7a7; font-weight: bold">double</span>) MonteCarloCycles )) << <span style="color: #CD5555">" STD with covariance = "</span> << sqrt(TotalVariance) << endl;
|
|
|
|
ofile.close(); <span style="color: #228B22">// close output file</span>
|
|
<span style="color: #8B008B; font-weight: bold">return</span> <span style="color: #B452CD">0</span>;
|
|
} <span style="color: #228B22">// end of main program </span>
|
|
</pre></div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec62">What is blocking? </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b>Blocking.</b>
|
|
<ul>
|
|
|
|
<p><li> Say that we have a set of samples from a Monte Carlo experiment</li>
|
|
|
|
<p><li> Assuming (wrongly) that our samples are uncorrelated our best estimate of the standard deviation of the mean \( \langle \mathbf{M}\rangle \) is given by</li>
|
|
</ul>
|
|
<p> <br>
|
|
$$
|
|
\sigma=\sqrt{\frac{1}{n}\left(\langle \mathbf{M}^2\rangle-\langle \mathbf{M}\rangle^2\right)}
|
|
$$
|
|
<p> <br>
|
|
|
|
|
|
<ul>
|
|
|
|
<p><li> If the samples are correlated we can rewrite our results to show that</li>
|
|
</ul>
|
|
<p> <br>
|
|
$$
|
|
\sigma=\sqrt{\frac{1+2\tau/\Delta t}{n}\left(\langle \mathbf{M}^2\rangle-\langle \mathbf{M}\rangle^2\right)}
|
|
$$
|
|
<p> <br>
|
|
|
|
where \( \tau \) is the correlation time (the time between a sample and the next uncorrelated sample) and \( \Delta t \) is time between each sample
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec63">What is blocking? </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b>Blocking.</b>
|
|
<ul>
|
|
|
|
<p><li> If \( \Delta t\gg\tau \) our first estimate of \( \sigma \) still holds</li>
|
|
|
|
<p><li> Much more common that \( \Delta t < \tau \)</li>
|
|
|
|
<p><li> In the method of data blocking we divide the sequence of samples into blocks</li>
|
|
|
|
<p><li> We then take the mean \( \langle \mathbf{M}_i\rangle \) of block \( i=1\ldots n_{blocks} \) to calculate the total mean and variance</li>
|
|
|
|
<p><li> 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 \)</li>
|
|
|
|
<p><li> The correlation time \( \tau \) would be a good choice</li>
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec64">What is blocking? </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b>Blocking.</b>
|
|
<ul>
|
|
|
|
<p><li> Problem: We don't know \( \tau \) or it is too expensive to compute</li>
|
|
|
|
<p><li> Solution: Make a plot of std. dev. as a function of blocksize</li>
|
|
|
|
<p><li> 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</li>
|
|
|
|
<p><li> When the std. dev. stops increasing the blocks are uncorrelated</li>
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec65">Implementation </h2>
|
|
<div class="alert alert-block alert-block alert-text-normal">
|
|
<b></b>
|
|
<ul>
|
|
|
|
<p><li> Do a Monte Carlo simulation, storing all samples to file</li>
|
|
|
|
<p><li> Do the statistical analysis on this file, independently of your Monte Carlo program</li>
|
|
|
|
<p><li> Read the file into an array</li>
|
|
|
|
<p><li> Loop over various block sizes</li>
|
|
|
|
<p><li> 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 \)</li>
|
|
|
|
<p><li> Take the mean and variance of the resulting array</li>
|
|
|
|
<p><li> Write the results for each block size to file for later
|
|
analysis</li>
|
|
</ul>
|
|
</div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec66">Actual implementation with code, main function </h2>
|
|
When the file gets large, it can be useful to write your data in binary mode instead of ascii characters.
|
|
The <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py" target="_blank">following python file</a> reads data from file with the output from every Monte Carlo cycle.
|
|
<p>
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span><span style="color: #228B22"># Blocking</span>
|
|
<span style="color: #707a7c">@timeFunction</span>
|
|
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">blocking</span>(<span style="color: #658b00">self</span>, blockSizeMax = <span style="color: #B452CD">500</span>):
|
|
blockSizeMin = <span style="color: #B452CD">1</span>
|
|
|
|
<span style="color: #658b00">self</span>.blockSizes = []
|
|
<span style="color: #658b00">self</span>.meanVec = []
|
|
<span style="color: #658b00">self</span>.varVec = []
|
|
|
|
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(blockSizeMin, blockSizeMax):
|
|
<span style="color: #8B008B; font-weight: bold">if</span>(<span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.data) % i != <span style="color: #B452CD">0</span>):
|
|
<span style="color: #8B008B; font-weight: bold">pass</span><span style="color: #228B22">#continue</span>
|
|
blockSize = i
|
|
meanTempVec = []
|
|
varTempVec = []
|
|
startPoint = <span style="color: #B452CD">0</span>
|
|
endPoint = blockSize
|
|
|
|
<span style="color: #8B008B; font-weight: bold">while</span> endPoint <= <span style="color: #658b00">len</span>(<span style="color: #658b00">self</span>.data):
|
|
meanTempVec.append(np.average(<span style="color: #658b00">self</span>.data[startPoint:endPoint]))
|
|
startPoint = endPoint
|
|
endPoint += blockSize
|
|
mean, var = np.average(meanTempVec), np.var(meanTempVec)/<span style="color: #658b00">len</span>(meanTempVec)
|
|
<span style="color: #658b00">self</span>.meanVec.append(mean)
|
|
<span style="color: #658b00">self</span>.varVec.append(var)
|
|
<span style="color: #658b00">self</span>.blockSizes.append(blockSize)
|
|
|
|
<span style="color: #658b00">self</span>.blockingAvg = np.average(<span style="color: #658b00">self</span>.meanVec[-<span style="color: #B452CD">200</span>:])
|
|
<span style="color: #658b00">self</span>.blockingVar = (np.average(<span style="color: #658b00">self</span>.varVec[-<span style="color: #B452CD">200</span>:]))
|
|
<span style="color: #658b00">self</span>.blockingStd = np.sqrt(<span style="color: #658b00">self</span>.blockingVar)
|
|
</pre></div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec67">The Bootstrap method </h2>
|
|
|
|
<p>
|
|
The Bootstrap resampling method is also very popular. It is very simple:
|
|
|
|
<ol>
|
|
<p><li> Start with your sample of measurements and compute the sample variance and the mean values</li>
|
|
<p><li> Then start again but pick in a random way the numbers in the sample and recalculate the mean and the sample variance.</li>
|
|
<p><li> Repeat this \( K \) times.</li>
|
|
</ol>
|
|
<p>
|
|
|
|
It can be shown, see the article by <a href="https://projecteuclid.org/download/pdf_1/euclid.aos/1176344552" target="_blank">Efron</a>
|
|
that it produces the correct standard deviation.
|
|
|
|
<p>
|
|
This method is very useful for small ensembles of data points.
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec68">Bootstrapping </h2>
|
|
Given a set of \( N \) data, assume that we are interested in some
|
|
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.
|
|
Let us call the value of the observable obtained from the original
|
|
data set \( \hat{\theta} \). One recreates from the sample repeatedly
|
|
other samples by choosing randomly \( N \) data out of the original set.
|
|
This costs essentially nothing, since we just recycle the original data set for the building of new sets.
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec69">Bootstrapping, recipe </h2>
|
|
Let us assume we have done this \( K \) times and thus have \( K \) sets of \( N \)
|
|
data values each.
|
|
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
|
|
<p> <br>
|
|
$$
|
|
\tilde{\theta} = \frac{1}{K} \sum_{k=1}^K \theta_k,
|
|
$$
|
|
<p> <br>
|
|
|
|
and
|
|
<p> <br>
|
|
$$
|
|
sigma^2_{\tilde{\theta}} = \frac{1}{K} \sum_{k=1}^K \left(\theta_k-\tilde{\theta}\right)^2.
|
|
$$
|
|
<p> <br>
|
|
|
|
<p>
|
|
These are estimators for \( \angle\theta\rangle \) and its variance. They are not unbiased and therefore
|
|
\( \tilde{\theta}\neq\hat{\theta} \) for finite K.
|
|
|
|
<p>
|
|
The difference is called bias and gives an idea on how far away the result may be from
|
|
the true \( \angle\theta\rangle \). As final result for the observable one quotes \( \angle\theta\rangle = \tilde{\theta} \pm \sigma_{\tilde{\theta}} \) .
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec70">Bootstrapping, <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py" target="_blank">code</a> </h2>
|
|
<p>
|
|
|
|
<!-- code=text typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span># Bootstrap
|
|
@timeFunction
|
|
def bootstrap(self, nBoots = 1000):
|
|
bootVec = np.zeros(nBoots)
|
|
for k in range(0,nBoots):
|
|
bootVec[k] = np.average(np.random.choice(self.data, len(self.data)))
|
|
self.bootAvg = np.average(bootVec)
|
|
self.bootVar = np.var(bootVec)
|
|
self.bootStd = np.std(bootVec)
|
|
</pre></div>
|
|
</section>
|
|
|
|
|
|
<section>
|
|
<h2 id="___sec71">Jackknife, <a href="https://github.com/CompPhysics/MachineLearning/blob/master/doc/Programs/Sampling/analysis.py" target="_blank">code</a> </h2>
|
|
<p>
|
|
|
|
<!-- code=text typeset with pygments style "perldoc" -->
|
|
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span># Jackknife
|
|
@timeFunction
|
|
def jackknife(self):
|
|
jackknVec = np.zeros(len(self.data))
|
|
for k in range(0,len(self.data)):
|
|
jackknVec[k] = np.average(np.delete(self.data, k))
|
|
self.jackknAvg = self.avg - (len(self.data) - 1) * (np.average(jackknVec) - self.avg)
|
|
self.jackknVar = float(len(self.data) - 1) * np.var(jackknVec)
|
|
self.jackknStd = np.sqrt(self.jackknVar)
|
|
</pre></div>
|
|
</section>
|
|
|
|
|
|
|
|
</div> <!-- class="slides" -->
|
|
</div> <!-- class="reveal" -->
|
|
|
|
<script src="reveal.js/lib/js/head.min.js"></script>
|
|
<script src="reveal.js/js/reveal.js"></script>
|
|
|
|
<script>
|
|
// Full list of configuration options available here:
|
|
// https://github.com/hakimel/reveal.js#configuration
|
|
Reveal.initialize({
|
|
|
|
// Display navigation controls in the bottom right corner
|
|
controls: true,
|
|
|
|
// Display progress bar (below the horiz. slider)
|
|
progress: true,
|
|
|
|
// Display the page number of the current slide
|
|
slideNumber: true,
|
|
|
|
// Push each slide change to the browser history
|
|
history: false,
|
|
|
|
// Enable keyboard shortcuts for navigation
|
|
keyboard: true,
|
|
|
|
// Enable the slide overview mode
|
|
overview: true,
|
|
|
|
// Vertical centering of slides
|
|
//center: true,
|
|
center: false,
|
|
|
|
// Enables touch navigation on devices with touch input
|
|
touch: true,
|
|
|
|
// Loop the presentation
|
|
loop: false,
|
|
|
|
// Change the presentation direction to be RTL
|
|
rtl: false,
|
|
|
|
// Turns fragments on and off globally
|
|
fragments: true,
|
|
|
|
// Flags if the presentation is running in an embedded mode,
|
|
// i.e. contained within a limited portion of the screen
|
|
embedded: false,
|
|
|
|
// Number of milliseconds between automatically proceeding to the
|
|
// next slide, disabled when set to 0, this value can be overwritten
|
|
// by using a data-autoslide attribute on your slides
|
|
autoSlide: 0,
|
|
|
|
// Stop auto-sliding after user input
|
|
autoSlideStoppable: true,
|
|
|
|
// Enable slide navigation via mouse wheel
|
|
mouseWheel: false,
|
|
|
|
// Hides the address bar on mobile devices
|
|
hideAddressBar: true,
|
|
|
|
// Opens links in an iframe preview overlay
|
|
previewLinks: false,
|
|
|
|
// Transition style
|
|
transition: 'default', // default/cube/page/concave/zoom/linear/fade/none
|
|
|
|
// Transition speed
|
|
transitionSpeed: 'default', // default/fast/slow
|
|
|
|
// Transition style for full page slide backgrounds
|
|
backgroundTransition: 'default', // default/none/slide/concave/convex/zoom
|
|
|
|
// Number of slides away from the current that are visible
|
|
viewDistance: 3,
|
|
|
|
// Parallax background image
|
|
//parallaxBackgroundImage: '', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'"
|
|
|
|
// Parallax background size
|
|
//parallaxBackgroundSize: '' // CSS syntax, e.g. "2100px 900px"
|
|
|
|
theme: Reveal.getQueryHash().theme, // available themes are in reveal.js/css/theme
|
|
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
|
|
|
|
});
|
|
|
|
Reveal.initialize({
|
|
dependencies: [
|
|
// Cross-browser shim that fully implements classList - https://github.com/eligrey/classList.js/
|
|
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
|
|
|
|
// Interpret Markdown in <section> elements
|
|
{ src: 'reveal.js/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
|
|
{ src: 'reveal.js/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
|
|
|
|
// Syntax highlight for <code> elements
|
|
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
|
|
|
|
// Zoom in and out with Alt+click
|
|
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
|
|
|
|
// Speaker notes
|
|
{ src: 'reveal.js/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } },
|
|
|
|
// Remote control your reveal.js presentation using a touch device
|
|
//{ src: 'reveal.js/plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } },
|
|
|
|
// MathJax
|
|
//{ src: 'reveal.js/plugin/math/math.js', async: true }
|
|
]
|
|
});
|
|
|
|
Reveal.initialize({
|
|
|
|
// The "normal" size of the presentation, aspect ratio will be preserved
|
|
// when the presentation is scaled to fit different resolutions. Can be
|
|
// specified using percentage units.
|
|
width: 1170, // original: 960,
|
|
height: 700,
|
|
|
|
// Factor of the display size that should remain empty around the content
|
|
margin: 0.1,
|
|
|
|
// Bounds for smallest/largest possible scale to apply to content
|
|
minScale: 0.2,
|
|
maxScale: 1.0
|
|
|
|
});
|
|
</script>
|
|
|
|
<!-- begin footer logo
|
|
<div style="position: absolute; bottom: 0px; left: 0; margin-left: 0px">
|
|
<img src="somelogo.png">
|
|
</div>
|
|
end footer logo -->
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|