5

I would like to define binomial distribution function in pgfplots. I my MWE

\documentclass{article}

\usepackage{pgfplots} 

\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
[
  declare function = {
    binom(\n, \k) = \n! / \k! / (\n - \k)!;
    binompdf(\n, \p, \k) = binom(\n, \k) * \p^\k * (1 - \p)^(\n - \k);
    % binomcdf(\n, \p, \k) = sum(\i = 1 ... \k, binompdf(\n, \p, \i));
  }
]
\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 = 1000] {0.8};
\addplot[domain = 0 : 0.01, samples = 1000] {1 - binompdf(2100, x, 0)};
\end{axis}
\end{tikzpicture}

\end{document}

the declation of binompdf() is recognized by the \addplot command. What is the reason for this?

Note: The ultimate goal is to write a function for the cumulative binomial distribution. For now, the y axis label does not correspond to the function that is plotted.

Matthias
  • 1,956
  • declare function is a pgf key, so you need to either prefix by pgf just like number format or take it up to the tikzpicture environment option. – percusse Apr 06 '18 at 14:51
  • @percusse Please see my edited version. I still get the same error. – Matthias Apr 06 '18 at 15:12
  • Well, personally I would think that you should add a new question. My answer solved the original problem. It is generally not considered nice to expand a question after it got answered. –  Apr 06 '18 at 17:09
  • @marmot The problem of the cumulation is already referred to in the original question. However, you may be right that posting another question and cleaing up the first one may be better. – Matthias Apr 06 '18 at 17:18

1 Answers1

3

You should not use variables that have other meanings in your function definitions and, what is more important, listen to @egreg: avoid unnecessary spaces.

enter image description here

\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)};
\end{axis}
\end{tikzpicture}

\end{document}
  • Using the variables \x, \y and z, the functions generally work. -- Which spaces do you refer to? -- What is the second document supposed to mean? Have you included this to demonstrate the \foreach loop? – Matthias Apr 06 '18 at 16:25
  • 1
    What I mean is binompdf(\x,\y,\z) as opposed to binompdf(\x ,\y ,\z ). And sorry, the second example was just an accident (which does not matter because it is after \end{document}) and which I will remove now. –  Apr 06 '18 at 16:31