Gaussian Sample

Gaussian Sample

We need a way to generate normally distributed random numbers. One way of doing this is to use the Box-Muller transform.

Here’s an example of using PGFPlots for this. I’ve plotted the marginal distributions to show that the numbers are indeed approximately normal:

\documentclass{article}
\usepackage{pgfplots}
% Create a function for generating inverse normally distributed numbers using the Box–Muller transform
\pgfmathdeclarefunction{invgauss}{2}{%
  \pgfmathparse{sqrt(-2*ln(#1))*cos(deg(2*pi*#2))}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis equal image,
    xmin=-2.5,xmax=2.5,
    ymin=-2.5, ymax=2.5,
    enlargelimits=false,
    xtick=data, 
    xticklabel=\empty,
    ytick=data,
    yticklabel=\empty,
    extra x ticks={-2,...,2},
    every extra x tick/.style={
        tick align=outside,
        xticklabel=\pgfmathprintnumber{\tick}
    },
    extra y ticks={-2,...,2},
    every extra y tick/.style={
        tick align=outside,
        yticklabel=\pgfmathprintnumber{\tick}
    }
]
\addplot [only marks, samples=100] ({invgauss(rnd,rnd)},{invgauss(rnd,rnd)});
\end{axis}
\end{tikzpicture}
\end{document}