5

Can you please help me with making a 2D Guassian sample with specified means and variances?

I only know how to make a guassian curve :D

\documentclass{article}
\usepackage{paralist,pst-func, pst-plot, pst-math, pstricks-add,pgfplots}
\usetikzlibrary{patterns,matrix,arrows}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[hide axis,clip=false,xmin=-4,xmax=4,xlabel={X},ymin=0,ymax=1] 
    \addplot[color=lime, samples=100] {1/sqrt(2)*exp(-(x+1)^2/1)} ;
    \end{axis}
\end{tikzpicture}
\end{document}

And here is the formula for it: enter image description here

And this would be an example of three normal distributions together: enter image description here

Naji
  • 1,505

1 Answers1

9

For this, you 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 (based on my answer to TikZ: Drawing the same data with scatter plots and parallel coordinates). 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}
Jake
  • 232,450