[simulations] some improvements

This commit is contained in:
Jan Benda 2019-12-23 19:10:36 +01:00
parent 42da5587f5
commit b5607deecb
2 changed files with 39 additions and 28 deletions

View File

@ -7,12 +7,12 @@
The real power of computers for data analysis is the possibility to The real power of computers for data analysis is the possibility to
run simulations. Experimental data of almost unlimited sample sizes run simulations. Experimental data of almost unlimited sample sizes
can be simulated in no time. This allows to explore basic concepts, can be simulated in no time. This allows to explore basic concepts,
like the ones we introduce in the following chapters, with well the ones we introduce in the following chapters and many more, with
controlled data sets that are free of confounding pecularities of real well controlled data sets that are free of confounding pecularities of
data sets. With simulated data we can also test our own analysis real data sets. With simulated data we can also test our own analysis
functions. More importantly, by means of simulations we can explore functions. More importantly, by means of simulations we can explore
possible outcomes of our planned experiments before we even started possible outcomes of our experiments before we even started the
the experiment or we can explore possible results for regimes that we experiment or we can explore possible results for regimes that we
cannot test experimentally. How dynamical systems, like for example cannot test experimentally. How dynamical systems, like for example
predator-prey interactions or the activity of neurons, evolve in time predator-prey interactions or the activity of neurons, evolve in time
is a central application for simulations. Computers becoming available is a central application for simulations. Computers becoming available
@ -23,31 +23,41 @@ code.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Random numbers} \section{Random numbers}
At the heart of many simulations are random numbers. Pseudo random At the heart of many simulations are random numbers that we get from
number generator XXX. These are numerical algorithms that return \enterm[random number generator]{random number generators}. These are
sequences of numbers that appear to be as random as possible. If we numerical algorithms that return sequences of numbers that appear to
draw random number using, for example, the \code{rand()} function, be as random as possible. If we draw random numbers using, for
then these numbers are indeed uniformly distributed and have a mean of example, the \code{rand()} function, then these numbers are indeed
one half. Subsequent numbers are also independent of each other, uniformly distributed and have a mean of one half. Subsequent numbers
i.e. the autocorrelation function is zero everywhere except at lag are independent of each other, i.e. the autocorrelation function is
zero. However, numerical random number generators have a period, after zero everywhere except at lag zero. However, numerical random number
which they repeat the exact same sequence. This differentiates them generators have a period, after which they repeat the exact same
from truely random numbers and hence they are called \enterm{pseudo sequence of numbers. This differentiates them from truely random
random number generators}. In rare cases this periodicity can induce numbers and hence they are called \enterm[random number
problems in your simulations. Luckily, nowadays the periods of random generator!pseudo]{pseudo random number generators}. In rare cases this
nunmber generators very large, $2^{64}$, $2^{128}$, or even larger. periodicity can induce problems in simulations whenever more random
numbers than the period of the random number generator are
An advantage of pseudo random numbers is that they can be exactly used. Luckily, nowadays the periods of random nunmber generators are
repeated given a defined state or seed of the random number very large, $2^{64}$, $2^{128}$, or even larger.
generator. After defining the state of the generator or setting a
\term{seed} with the \code{rng()} function, the exact same sequence of The pseudo randomness of numerical random number generators also has
an advantage. They allow to repeat exactly the same sequence of
random numbers. After defining the state of the generator or setting a
\enterm{seed} with the \code{rng()} function, a particular sequence of
random numbers is generated by subsequent calls of the random number random numbers is generated by subsequent calls of the random number
generator. This is in particular useful for plots that involve some generator. This way we can not only precisly define the statistics of
random numbers but should look the same whenever the script is run. noise in our simulated data, but we can repeat an experiment with
exactly the same sequence of noise values. This is useful for plots
that involve some random numbers but should look the same whenever the
script is run.
\begin{exercise}{}{} \begin{exercise}{}{}
Generate three times the same sequence of 20 uniformly distributed Generate three times the same sequence of 20 uniformly distributed
numbers using the \code{rand()} and \code{rng()} functions. numbers using the \code{rand()} and \code{rng()} functions.
Generate 10\,000 uniformly distributed random numbers and compute
the correlation coefficient between each number and the next one in
the sequence. This is the serial correlation at lag one.
\end{exercise} \end{exercise}
\begin{figure}[t] \begin{figure}[t]
@ -72,8 +82,8 @@ tigers or firing rate of neurons). Doing so we must specify from which
probability distribution the data should originate from and what are probability distribution the data should originate from and what are
the parameters (mean, standard deviation, shape parameters, etc.) the parameters (mean, standard deviation, shape parameters, etc.)
that distribution. How to illuastrate and quantify univariate data, no that distribution. How to illuastrate and quantify univariate data, no
matter whether they have been actually measured or whether they are matter whether they have been actually measured or whether they have
simulated as described in the following, is described in been simulated as described in the following, is described in
chapter~\ref{descriptivestatisticschapter}. chapter~\ref{descriptivestatisticschapter}.
\subsection{Normally distributed data} \subsection{Normally distributed data}
@ -142,6 +152,7 @@ draw (and plot) random functions (in statistics chapter?)
\section{Dynamical systems} \section{Dynamical systems}
\begin{itemize} \begin{itemize}
\item iterated maps
\item euler forward, odeint \item euler forward, odeint
\item introduce derivatives which are also needed for fitting (move box from there here) \item introduce derivatives which are also needed for fitting (move box from there here)
\item Passive membrane \item Passive membrane

View File

@ -22,7 +22,7 @@ if __name__ == "__main__":
fig = plt.figure(figsize=cm_size(16.0, 6.0)) fig = plt.figure(figsize=cm_size(16.0, 6.0))
spec = gridspec.GridSpec(nrows=1, ncols=2, spec = gridspec.GridSpec(nrows=1, ncols=2,
left=0.10, bottom=0.23, right=0.97, top=0.96, wspace=0.4) left=0.12, bottom=0.23, right=0.97, top=0.96, wspace=0.4)
ax1 = fig.add_subplot(spec[0, 0]) ax1 = fig.add_subplot(spec[0, 0])
show_spines(ax1, 'lb') show_spines(ax1, 'lb')
ax1.plot(xx, yy, colors['red'], lw=2) ax1.plot(xx, yy, colors['red'], lw=2)