This is a follow-up question to Declaring a function to be used by pgfplots.
The MWE
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
[
declare function = {binom(\x,\y) = \x! / \y! / (\x - \y)!;},
declare function = {binompdf(\x,\y,\z) = binom(\x, \z) * \y^\z * (1 - \y)^(\x - \z);}
]
\begin{axis}
[
grid = none,
tick style = {black},
tick label style = {
/pgf/number format/use comma,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill},
scaled ticks = false,
% x axis
xmin = 0, xmax = 0.01,
axis x line = middle, x axis line style = -{Stealth},
xlabel = $p$, xlabel style = {below},
xtick = {0, 0.001, ..., 0.01}, xticklabels = {},
extra x ticks = {0.001},
xticklabel style = {/pgf/number format/precision = 3},
% y axis
ymin = 0, ymax = 1.1,
axis y line = middle, y axis line style = -{Stealth},
ylabel = $P(X \ge 10)$, ylabel style = {left},
ytick = {0, 0.1, ..., 1.1}, yticklabels = {},
extra y ticks = {0.1},
]
\addplot[domain = 0 : 0.01, samples = 100] {0.8};
\addplot[domain = 0 : 0.01, samples = 100] {1 - binompdf(2100, x, 0) - binompdf(2100, x, 1) - binompdf(2100, x, 2) - binompdf(2100, x, 3) - binompdf(2100, x, 4) - binompdf(2100, x, 5) - binompdf(2100, x, 6) - binompdf(2100, x, 7) - binompdf(2100, x, 8) - binompdf(2100, x, 9)};
\end{axis}
\end{tikzpicture}
\end{document}
produces the following result:
There are three problems:
- The calculation is not very accurate (see the Maple output below). This is probably due to the large intermediate result when calculating 2100!.
- LaTeX takes ages to compile the document.
- Writing the second
\addplot
command is quite inconvenient. Is there a way to declare a function for the cumulation?
xintexpr
has extended syntax, can work with varying precision but is currently lacking most everything apart square root you need for math. Thus surely an internal solution would be preferable. It is clear that computing binomial by ratio of factorial is not optimal and presumably roof ot problem. Perhaps make a feature request at pgf to get "partial factorial function"? or it exists already? – user4686 Apr 8 '18 at 9:51