PCalc binomial functions

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 half-written some time ago.

The function calculates a probability from the binomial distribution. Imagine there are NN 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 pp. The probability of nn successes in those NN trials is

(Nn)pn(1p)Nn\binom{N}{n}\; p^n\; (1 - p)^{N-n}

The first term in the formula is the binomial coefficient, which is defined as

(Nn)=N!n!(Nn)!\binom{N}{n} = \frac{N!}{n!\;(N - n)!}

The binomial probability, then, is a function of three inputs, nn, NN, and pp, that yields one output. The PCalc function I wrote to implement the function starts with three numbers on the stack,1 say n=5n = 5, N=10N = 10, and p=.5p = .5,

Binomial input

and returns the answer

Binomial 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,

Binomial PMF PCalc function

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:

  1. 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.
  2. 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.


  1. Yes, it’s an RPN-only 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.