[Tex/LaTex] probability density function and cumulative distribution function for normal distribution

pstrickstikz-arrowstikz-pgf

How can I draw the following graph. I know how to draw a bell curve. But the question is how to draw both curve as shown in the figure. Moreover I want to write $P(X\leq x)=0.7$

enter image description here

Other question is how can I make an animation/gif so the concept become visual.

Here is what I have. But it is far from what I want.

\documentclass[border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\DeclareMathOperator{\CDF}{cdf}

\def\cdf(#1)(#2)(#3){0.5*(1+(erf((#1-#2)/(#3*sqrt(2)))))}%

\tikzset{
    declare function={
        normcdf(\x,\m,\s)=1/(1 + exp(-0.07056*((\x-\m)/\s)^3 - 1.5976*(\x-\m)/\s));
    }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
  xlabel=$x$,
  ylabel=$\CDF(x)$,
  grid=major,
  legend entries={gnuplot, Bowling et al},
  legend pos=south east]
  \addplot[smooth, line width=3pt, orange!50] gnuplot{\cdf(x)(0)(2)};
  \addplot [smooth, black] {normcdf(x,0,2)};
\end{axis}
\end{tikzpicture}


\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
  mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
  axis x line*=bottom, % no box around the plot, only x and y axis
  axis y line*=left, % the * suppresses the arrow tips
  enlargelimits=upper] % extend the axes a bit to the right and top
  \addplot {gauss(0,0.5)};
 % \addplot {gauss(1,0.75)};
\end{axis}
\end{tikzpicture}
\end{document}

Best Answer

The canonical way to plot something like this is to use the groupplots library in order to arrange the plots, and the fillbetween library for the fills.

\documentclass[border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}% <- if you have an older installation, try 1.15 or 1.14
\usepgfplotslibrary{groupplots,fillbetween}
\DeclareMathOperator{\CDF}{cdf}
\DeclareMathOperator{\PDF}{pdf}
\begin{document}
\begin{tikzpicture}[declare function={%
        normcdf(\x,\m,\s)=1/(1 + exp(-0.07056*((\x-\m)/\s)^3 - 1.5976*(\x-\m)/\s));
        gauss(\x,\u,\v)=1/(\v*sqrt(2*pi))*exp(-((\x-\u)^2)/(2*\v^2));
    }]
   \begin{groupplot}[group style={group size=1 by 2},
       xmin=-3,xmax=3,ymin=0,
       domain=-3:3,xlabel=$x$,axis lines=middle,axis on top]
    \nextgroupplot[ylabel=$\CDF(x)$,ymax=1.19]
      \addplot[smooth, black,thick] {normcdf(x,0,1)};
      \draw[cyan!30,very thick,stealth-stealth]
       (0.7,0) coordinate (t) -- (0.7,{normcdf(0.7,0,1)});
      \draw[thick,dashed]  (0.7,{normcdf(0.7,0,1)}) -- (0,{normcdf(0.7,0,1)});
      \draw[thick,stealth-]  (0,{normcdf(0.7,0,1)}) -- (-1,{normcdf(0.7,0,1)}) 
      node[circle,fill,inner sep=1.5pt,label=left:{$P$}]{};
    \nextgroupplot[ylabel=$\PDF(x)$,ytick=\empty,ymax=0.6]
      \addplot[smooth, black,thick,name path=gauss] {gauss(x,0,1)};
      \path[name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
      \addplot [cyan!30] fill between [
          of=gauss and B,soft clip={domain=\pgfkeysvalueof{/pgfplots/xmin}:0.7},
      ];
      \draw[thick,stealth-]  (-0.5,{0.5*gauss(-0.5,0,1)}) 
      -- (-1.5,{0.5*gauss(-0.5,0,1)}) node[circle,fill,inner sep=1.5pt,label=left:{$P$}]{};
      \path (0.7,0) coordinate (b);
   \end{groupplot}
 \draw[thick,dashed] (t) -- (b);
\end{tikzpicture}
\end{document}

enter image description here

This can certainly be animated if it is clear which parameter should vary. Assuming you want to vary the horizontal position (0.7 in the example), you could compile the:

\documentclass[tikz,border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}% <- if you have an older installation, try 1.15 or 1.14
\usepgfplotslibrary{groupplots,fillbetween}
\DeclareMathOperator{\CDF}{cdf}
\DeclareMathOperator{\PDF}{pdf}
\begin{document}
\foreach \X in {-2.5,-2.4,...,2.4}
{\begin{tikzpicture}[declare function={%
        normcdf(\x,\m,\s)=1/(1 + exp(-0.07056*((\x-\m)/\s)^3 - 1.5976*(\x-\m)/\s));
        gauss(\x,\u,\v)=1/(\v*sqrt(2*pi))*exp(-((\x-\u)^2)/(2*\v^2));
    }]
   \pgfmathtruncatemacro{\mysign}{sign(\X)}
   \begin{groupplot}[group style={group size=1 by 2},
       xmin=-3,xmax=3,ymin=0,
       domain=-3:3,xlabel=$x$,axis lines=middle,axis on top]
    \nextgroupplot[ylabel=$\CDF(x)$,ymax=1.19]
      \addplot[smooth, black,thick] {normcdf(x,0,1)};
      \draw[cyan!30,very thick,stealth-stealth]
       (\X,0) coordinate (t) -- (\X,{normcdf(\X,0,1)});
      \draw[thick,dashed]  (\X,{normcdf(\X,0,1)}) -- (0,{normcdf(\X,0,1)});
      \ifnum\mysign>0
        \draw[thick,stealth-]  (0,{normcdf(\X,0,1)}) -- (-1,{normcdf(\X,0,1)}) 
        node[circle,fill,inner sep=1.5pt,label=left:{$P$}]{};
      \else
        \draw[thick,stealth-]  (0,{normcdf(\X,0,1)}) -- (1,{normcdf(\X,0,1)}) 
        node[circle,fill,inner sep=1.5pt,label=right:{$P$}]{};
      \fi
    \nextgroupplot[ylabel={},ytick=\empty,ymax=0.6]
      \addplot[smooth, black,thick,name path=gauss] {gauss(x,0,1)};
      \path[name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
      \addplot [cyan!30] fill between [
          of=gauss and B,soft clip={domain=\pgfkeysvalueof{/pgfplots/xmin}:\X},
      ];
      \ifnum\mysign>0
        \draw[thick,stealth-]  ({-1.5+\X/2},{0.5*gauss(-1.5+\X/2,0,1)}) 
        -- (-2,0.4) 
        node[circle,fill,inner sep=1.5pt,label=left:{$P$}]{};
      \else
        \draw[thick,stealth-]  ({-1.5+\X/2},{0.5*gauss(-1.5+\X/2,0,1)}) 
        -- (2,0.4) 
        node[circle,fill,inner sep=1.5pt,label=left:{$P$}]{};
      \fi
      \path (\X,0) coordinate (b);
   \end{groupplot}
 \draw[thick,dashed] (t) -- (b);
\end{tikzpicture}}
\end{document}

and then use

convert -density 300 -delay 44 -loop 0 -alpha remove file.pdf ani.gif

as explained here to get

enter image description here