PCalc binomial functions
September 18, 2018 at 10:51 PM by Dr. Drang
Over the weekend, I read Federico Viticci’s excellent post on using iOS 12 Shortcuts to access PCalc. I haven’t installed iOS 12 yet (I usually wait a few days to let Apple’s servers cool down), but I expect to use Federico’s advice to make a few PCalc shortcuts when I do. In the meantime, his post inspired me to clean up and finish off a PCalc function that I’d halfwritten some time ago.
The function calculates a probability from the binomial distribution. Imagine there are $N$ independent trials of some event. There are only two possible outcomes of each event, which we’ll call success and failure, although you could give them any names you like. For each trial, the probability of success is $p$. The probability of $n$ successes in those $N$ trials is
$$\left(\genfrac{}{}{0ex}{}{N}{n}\right)\phantom{\rule{0.278em}{0ex}}{p}^{n}\phantom{\rule{0.278em}{0ex}}(1p{)}^{Nn}$$The first term in the formula is the binomial coefficient, which is defined as
$$\left(\genfrac{}{}{0ex}{}{N}{n}\right)=\frac{N!}{n!\phantom{\rule{0.278em}{0ex}}(Nn)!}$$The binomial probability, then, is a function of three inputs, $n$, $N$, and $p$, that yields one output. The PCalc function I wrote to implement the function starts with three numbers on the stack,^{1} say $n=5$, $N=10$, and $p=\mathrm{.5}$,
and returns the answer
Any entries on the stack “above” the three inputs will still be there after the function runs.
The function is called “Binomial PMF.” The PMF stands for probability mass function, which is standard terminology for the function that returns the probability of a random variable equaling a discrete value. The function is defined this way,
which is a lot of steps to enter. You’d probably prefer to download it.
If you look through the function definition, you’ll notice that I’m using up a lot of registers—more than is truly necessary to run out the calculation. I do this for a couple of reasons:
 The registers are there to be used, and by keeping all the intermediate calculations in separate registers, I’m able to make the logic of the function easier to follow.
 I have further plans for this function. I want to use it as the starting point for a more complicated function, the cumulative distribution function, which will need to keep track of some of those intermediate calculations.
We’ll look at the Binomial CDF function in the next post.

Yes, it’s an RPNonly function. RPN is the natural way to handle this many inputs, and because I don’t use PCalc’s algebraic mode, I didn’t see any reason to write the function to work in that mode. ↩