During my investigation of this nice question by @Sentient, I found a rather bizarre problem: Given the binomial parameters n=15
and p=0.7
, @Sentient’s code produces some probability values greater than two! This can be illustrated with other parameters as well:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
scale=0.88,
declare function={binom(\x,\n,\p)=\n!/(\x!*(\n-\x)!)*\p^\x*(1-\p)^(\n-\x);},
declare function={normd(\x,\n,\p)=binom(\x*\n,\n,\p);},
declare function={normaldensity(\x,\n,\p)=exp(-\n*(\x-\p)^2/(2*\p*(1-\p)))/sqrt(2*pi*\n*\p*(1-\p));}
]
\begin{axis}
\addplot[cyan, domain=0:1, samples=26, smooth]{normd(x, 25, 0.72)};
\addplot[orange, domain=0:1, smooth]{normaldensity(x, 25, 0.72)};
\end{axis}
\end{tikzpicture}\quad
\begin{tikzpicture}[
scale=0.88,
declare function={binom(\x,\n,\p)=\n!/(\x!*(\n-\x)!)*\p^\x*(1-\p)^(\n-\x);},
declare function={normd(\x,\n,\p)=binom(\x*\n,\n,\p);},
declare function={normaldensity(\x,\n,\p)=exp(-\n*(\x-\p)^2/(2*\p*(1-\p)))/sqrt(2*pi*\n*\p*(1-\p));}
]
\begin{axis}
\addplot[cyan, domain=0:1, samples=27, smooth]{normd(x, 26, 0.72)};
\addplot[orange, domain=0:1, smooth]{normaldensity(x, 26, 0.72)};
\end{axis}
\end{tikzpicture}
\end{document}
On the left, I set n=25
and p=0.72
; On the right, I set n=26
and p=0.72
. The orange curves are the Gaussian approximations generated by the function normaldensity
(the formula can be found in my previous answer), which should be close to the cyan curves (generated by normd
, “normalized binomial”) in both graphs.
However, the calculations of binomial probabilities by normd
are way off on the right, producing probability values bigger than 3! On the contrary, the calculations on the left are perfectly fine. I’m sure @marmot has already noticed this problem when writing this excellent answer, because the gamma function approach produces curves very different from @Sentient’s (but are very close to the Gaussian curves).
My question is: Why does this calculation error occur (but only sometimes, e.g., n = 23, 24 or 26
but not for n = 25
), and how can I fix it?
\addplot[red,only marks,samples at={0,1,...,26}](x/26,{binom(x, 26, 0.72)});
. This should produce marks on top of the cyan plot, but it does not. This seems to suggest that there some issues with these large numbers. I had a very similar problem here, where I found that it helps to set the brackets differently, i.e. instead of plotting(huge/large)*small
you may set the brackets in such a way that you arrive at(not so huge/large)*((not so huge)*small)
.n=15, p=0.7
do not work.