I'm trying to plot the probability density function of various normal distributions in latex. All have a mean of 0 and the standard deviations are: 0.4339, 0.4931 and 0.3665.
I use this code:
\documentclass[titlepage]{article}
\usepackage[obeyspaces]{url}
\usepackage{caption}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{listings}
\usetikzlibrary{calc}
\lstset{
showstringspaces=false,
commentstyle=\color{red},
keywordstyle=\color{blue},
basicstyle=\small,
breaklines=true
}
\usepackage{amsmath}
\usepackage{subcaption}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}[h]{0.45\linewidth}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:2,samples=50,smooth},
xmin=-1.6,
xmax=1.6,
ymin=0,
xtick={},
%ytick=\empty,
enlargelimits=auto
],
\addplot {gauss(0, 0.4339)};
\addplot {gauss(0, 0.4931)};
\addplot {gauss(0, 0.3665)};
\addlegendimage{empty legend}
\addlegendentry{$x$}
\addlegendentry{$y$}
\addlegendentry{$z$}
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\end{document}
This results in the following picture:
You can see that the y axis goes up to ~1.1. This makes no sense, right? As its a probability it should be below 1. Additionally the integral of the function should have a sum of 1, as I understand it. Thus the values should be much lower in general.
What am I doing wrong, am I misunderstanding something?
Change
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
into
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{exp(-((x-(#1))*((x-(#1)))/(2*(#2)*(#2)))/((#2)*sqrt(2*pi))}%
}
without using the power function ^2
, which actually calls pow(#1,2)
, and using all the brackets: maybe your distribution still have this bug.