Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
4  
Since this is not a math question site, you should supply the math related to what you want to graph, and explain what parts you are having difficulty with. –  Peter Grill Apr 9 '13 at 18:15
1  
Hi Naji. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  Claudio Fiandrino Apr 9 '13 at 18:20
    
formula is on wikipedia: en.wikipedia.org/wiki/Normal_distribution –  long tom Apr 9 '13 at 18:21
    
@PeterGrill, well I thought the normal distribution is famous enough to not to express it here. –  Naji Apr 9 '13 at 18:22

1 Answer 1

up vote 6 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.