7

My intention is to draw the probability function of a binomial distribution with trials = 20 and probability = 0,4. Then i wanna add the curve of an approximate gaussian curve in the same plot. How can I add the gaussian curve?

\documentclass[a4paper,12pt]{scrartcl}
    \usepackage[ngerman]{babel}
    \usepackage[latin1]{inputenc}
    \usepackage[cmyk]{xcolor}
    \usepackage{multirow}
    \usepackage{pgf,tikz}
    \usepackage{amssymb}
    \usepackage{amsmath}
    \usepackage[format=default,font=footnotesize,labelfont=bf]{caption}
    \usepackage[dvipdfm]{geometry}
    \usepackage{tabulary}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.7}
    \usepackage{subfigure}
    \usepackage{float}
    \usetikzlibrary{arrows}
    \usetikzlibrary{shapes}
    \usepackage[thmmarks, amsmath, thref]{ntheorem}
    \begin{document}
    \begin{figure}[H]
    \centering
    \begin{tikzpicture}[declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}]
      \begin{axis}[width=9cm,ymin=0, xmin=-0.5, xmax=17, axis lines=left,xlabel={\scriptsize\scriptsize$k$}, ylabel={\scriptsize$f(x)$}, x label style={at={(axis description cs:1,0)},anchor=west},
        y label style={at={(axis description cs:0,1)},rotate = -90, anchor=south}, ,
        samples at={0,...,16},
        yticklabel style={font=\scriptsize,
            /pgf/number format/fixed,
            /pgf/number format/fixed zerofill,
            /pgf/number format/precision=2},
        ybar=0pt, bar width=1, bar shift=0pt, xticklabel style={font=\scriptsize},xticklabel style={font=\scriptsize}]
      \addplot [fill=gray!25] {binom(x,20,0.4)}; 
      \end{axis}
      \end{tikzpicture}
    \end{figure}
    \end{document} 
percusse
  • 157,807
dave
  • 71
  • Does this help: http://tex.stackexchange.com/a/11371/32374 – darthbith Dec 26 '14 at 22:50
  • If you add something like \addplot[smooth, domain=0:16] {0.45/sqrt(2*pi)*exp(-0.2*(\x-8)^2/2)}; to your plot it is approximated but do you have a specific approximation in mind? – percusse Dec 27 '14 at 01:09
  • subfigure is obsolete and ought not be used. Do you really need and want to override KOMA's page layout and other formatting niceties? – cfr Jan 05 '16 at 22:58

2 Answers2

4

Here's one way of doing it using TikZ/PGFPLOTS.

Output

Binomial and Normal Distribution Plot

Code

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
    % Define probability distribution functions
    declare function={
        binom(\n,\p) = \n!/(x!*(\n-x)!)*\p^x*(1-\p)^(\n-x);
        normal(\m,\s) = 1/(\s*sqrt(2*pi))*exp(-((x-\m)^2)/(2*\s^2));
    },
    % Plotting options
    title=Binomial \& Normal Distribution,
    xlabel={$x$},
    ylabel={$f(x)$},
    ymin=0,
    ymax=1,
    samples at={0,...,20},
    xtick style={draw=none},
    yticklabel style={
        /pgf/number format/fixed,
        /pgf/number format/fixed zerofill,
        /pgf/number format/precision=2,
    },   
]

% Plot Binomial Distribution
\addplot [ybar=0pt,bar width=1pt,fill=black] {binom(20,0.4)};  
\addlegendentry{$B(20,0.4)$}

% Plot Normal Distribution (1)
\addplot [smooth,red,thick] {normal(8,3)}; 
\addlegendentry{$\mathcal{N}(8,3^2)$}

% Plot Normal Distribution (2)
\addplot [smooth,blue,thick] {normal(8,1)};
\addlegendentry{$\mathcal{N}(8,1^2)$}
\end{axis}
\end{tikzpicture}

\end{document}
2

Here is a solution using R, embedded in LaTeX source, and compiled using knitr.

\documentclass[border=.25in]{standalone}
\begin{document}
<<echo=FALSE>>=
n=20
p=0.4
q=0.6
x=0:20
y=dbinom(x,n,p)
plot(x,y,type="h",lwd=2,col="red",ylim=c(0,1),main="Binomial & Normal Distribution",ylab="f(x)")
mu=n*p
s=sqrt(n*p*q)
xx=seq(0,20,length=400)
yy=dnorm(xx,mu,3)
lines(xx,yy,lwd=2,col="red")
zz=dnorm(xx,mu,1)
lines(xx,zz,lwd=2,col="blue")
@
\end{document}

enter image description here