[Tex/LaTex] Probability Density Function Diagram

tikz-pgf

I wants to make PDF diagram like that.

enter image description here

currently, i'm here. Can someone please help me out to complete the diagram further. Thanks in advance.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}[
  no markers, domain=-5:5, samples=100,
  axis lines*=left, xlabel=$x$, ylabel=$y$,
  every axis y label/.style={at=(current axis.above origin),anchor=south},
  every axis x label/.style={at=(current axis.right of origin),anchor=west},
  height=5cm, width=12cm,
  xtick={-1,+1}, ytick=\empty,
  enlargelimits=false, clip=false, axis on top,
  grid = major
  ]
  \addplot [fill=black!20, draw=none, domain=-2.5:0] {gauss(+1,1)} \closedcycle ;
  \addplot [fill=black!20, draw=none, domain=0:+2.5] {gauss(-1,1)} \closedcycle ;
  \addplot [very thick,black!50!black] {gauss(-1,1)};
  \addplot [very thick,black!50!black] {gauss(+1,1)};
\draw (-.2,2.5) node[left] {$f_Y(u)$};
\draw (3,-.5) node[below] {$u$};
\draw[->] (0,0) -- (6.2,0) node[right] {};
\draw[->] (0,0) -- (0,5) node[above] {};
\draw [yshift=-0.6cm, latex-latex](axis cs:0,0) -- node [fill=white] {$1.96\sigma$} (axis cs:0,0);
\end{axis}
\end{tikzpicture}
\end{document}

Best Answer

This might help towards your objective. I leave it to you to do the notation across the x-axis, since there is nothing to indicate the size of each interval.

The graph uses the fill between library from pgfplots to produce the filled areas. I defined some variables for the key parameters, which will make it easier for you to customize.

This is the result:

enter image description here

This is the code:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}

\begin{document}

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

    \begin{tikzpicture}

    \def\startx{-5}    % lower end of domain
    \def\endx{5}       % upper end of domain
    \def\camean{-1.0}  % mean of left side distribution
    \def\casigma{1.0}  % sigma for left side distribution
    \def\cbmean{1.0}   % mean for right side distribution
    \def\cbsigma{1.0}  % sigma for right side distribution
    \def\verticala{0.7} % x vale for first delimiter
    \def\verticalb{1.3} % x value for second delimited

    \begin{axis}[
    domain=\startx:\endx,
    samples=101,
    ymax=0.5,
    enlargelimits=false,
    axis x line=middle,
    axis y line=middle,
    xtick={-1,1},
    xticklabels={-1,1},
    ytick={\empty},
    xlabel=$y$,
    height=5cm,
    width=10cm
    ]
    % Draw distributions
    \addplot [name path=a,thin, smooth] {gauss(\camean,\casigma)};
    \addplot [name path=b,thin, smooth] {gauss(\cbmean,\cbsigma)};%    
    % Draw vertical lines:
    \draw [gray, semithick] (\verticala,0) -- (\verticala,0.4);
    \draw [gray, semithick] (\verticalb,0) -- (\verticalb,0.4);
    % create path for x axis:
    \path[name path=axis] (axis cs:\startx,0) -- (axis cs:\endx,0);
    % generate fills:
    \addplot[darkgray]  fill between[of=a and axis,soft clip={domain=\verticala:\verticalb}]; % 
    \addplot[pattern=north east lines]  fill between[of=b and a,
    soft clip={(\verticala,0) rectangle (\verticalb,0.5)}
    ]; 
    % place the labels for each distribution
    \node (a) at (-1,0.45) {$p(y|1)$};
    \node (b) at (1,0.45) {$p(y|0)$};

    \end{axis}

    \end{tikzpicture}
\end{document}