[simulations] figure for random numbers
This commit is contained in:
parent
a04f0ec718
commit
42da5587f5
@ -102,6 +102,10 @@ def show_spines(ax, spines):
|
||||
# initialization:
|
||||
plt.xkcd()
|
||||
mpl.rcParams['figure.facecolor'] = 'white'
|
||||
#mpl.rcParams['axes.spines.left'] = True # newer matplotlib only
|
||||
#mpl.rcParams['axes.spines.bottom'] = True
|
||||
#mpl.rcParams['axes.spines.top'] = False
|
||||
#mpl.rcParams['axes.spines.right'] = False
|
||||
mpl.rcParams['xtick.direction'] = 'out'
|
||||
mpl.rcParams['ytick.direction'] = 'out'
|
||||
mpl.rcParams['xtick.major.size'] = 6
|
||||
|
65
simulations/lecture/randomnumbers.py
Normal file
65
simulations/lecture/randomnumbers.py
Normal file
@ -0,0 +1,65 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.gridspec as gridspec
|
||||
from plotstyle import colors, cm_size, show_spines
|
||||
|
||||
if __name__ == "__main__":
|
||||
n = 21
|
||||
nn = 10000
|
||||
maxl = 5
|
||||
|
||||
indices = np.arange(n)
|
||||
rng = np.random.RandomState(19281)
|
||||
x1 = rng.rand(n)
|
||||
rng = np.random.RandomState(35281)
|
||||
x2 = rng.rand(n)
|
||||
rng = np.random.RandomState(35281)
|
||||
x3 = rng.rand(n)
|
||||
|
||||
x = rng.rand(nn)
|
||||
lags = np.arange(-maxl, maxl+1, 1)
|
||||
corrs = [np.corrcoef(x[maxl:-maxl-1-l], x[maxl+l:-maxl-1])[0, 1] for l in lags]
|
||||
|
||||
fig = plt.figure(figsize=cm_size(16.0, 11.0))
|
||||
spec = gridspec.GridSpec(nrows=2, ncols=2,
|
||||
left=0.10, bottom=0.12, right=0.98, top=0.97,
|
||||
wspace=0.4, hspace=0.6)
|
||||
|
||||
ax = fig.add_subplot(spec[0, 0])
|
||||
show_spines(ax, 'lb')
|
||||
ax.plot(indices, x1, c=colors['blue'], lw=1, zorder=10)
|
||||
ax.scatter(indices, x1, c=colors['blue'], edgecolor='white', s=50, zorder=20)
|
||||
ax.set_xlabel('Index')
|
||||
ax.set_ylabel('Random number')
|
||||
ax.set_xlim(-1.0, n+1.0)
|
||||
ax.set_ylim(-0.05, 1.05)
|
||||
|
||||
ax = fig.add_subplot(spec[0, 1])
|
||||
show_spines(ax, 'lb')
|
||||
ax.plot(indices, x2, c=colors['blue'], lw=1, zorder=10)
|
||||
ax.scatter(indices, x2, c=colors['blue'], edgecolor='white', s=50, zorder=20)
|
||||
ax.set_xlabel('Index')
|
||||
ax.set_ylabel('Random number')
|
||||
ax.set_xlim(-1.0, n+1.0)
|
||||
ax.set_ylim(-0.05, 1.05)
|
||||
|
||||
ax = fig.add_subplot(spec[1, 1])
|
||||
show_spines(ax, 'lb')
|
||||
ax.plot(indices, x3, c=colors['blue'], lw=1, zorder=10)
|
||||
ax.scatter(indices, x3, c=colors['blue'], edgecolor='white', s=50, zorder=20)
|
||||
ax.set_xlabel('Index')
|
||||
ax.set_ylabel('Random number')
|
||||
ax.set_xlim(-1.0, n+1.0)
|
||||
ax.set_ylim(-0.05, 1.05)
|
||||
|
||||
ax = fig.add_subplot(spec[1, 0])
|
||||
show_spines(ax, 'lb')
|
||||
ax.plot(lags, corrs, c=colors['red'], lw=1, zorder=10)
|
||||
ax.scatter(lags, corrs, c=colors['red'], edgecolor='white', s=50, zorder=20)
|
||||
ax.set_xlabel('Lag')
|
||||
ax.set_ylabel('Serial correlation')
|
||||
ax.set_xlim(-maxl-0.5, maxl+0.5)
|
||||
ax.set_ylim(-0.05, 1.05)
|
||||
|
||||
fig.savefig("randomnumbers.pdf")
|
||||
plt.close()
|
@ -45,17 +45,25 @@ random numbers is generated by subsequent calls of the random number
|
||||
generator. This is in particular useful for plots that involve some
|
||||
random numbers but should look the same whenever the script is run.
|
||||
|
||||
Figure XXX: three sequences - initial one, second different one with
|
||||
seed, third with same seed. Fourth panel with autocorrelation
|
||||
function.
|
||||
|
||||
\begin{exercise}{}{}
|
||||
Generate three times the same sequence of 20 uniformly distributed
|
||||
numbers using the \code{rand()} and \code{rng()} functions.
|
||||
\end{exercise}
|
||||
|
||||
\begin{figure}[t]
|
||||
\includegraphics[width=1\textwidth]{randomnumbers}
|
||||
\titlecaption{\label{randomnumbersfig} Random numbers.} {Numerical
|
||||
random number generators return sequences of numbers that are as
|
||||
random as possible. The returned values approximate a given
|
||||
probability distribution. Here, for example, a uniform
|
||||
distribution between zero and one (top left). The serial
|
||||
correlation (auto-correlation) of the returned sequences is zero
|
||||
at any lag except for zero (bottom left). However, by setting a
|
||||
seed, defining the state, or spawning the random number generator,
|
||||
the exact same sequence can be generated (right).}
|
||||
\end{figure}
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{Univariate data}
|
||||
The most basic type of simulation is to draw random numbers from a
|
||||
given distribution like, for example, the normal distribution. This
|
||||
|
Reference in New Issue
Block a user