PCalc binomial functions—Part 2

Last time, we defined the probability mass function (PMF) for the binomial distribution in PCalc. This time we’ll do (sort of) the binomial’s cumulative distribution function (CDF).

As you recall, the PMF is defined this way:

[\binom{N}{n}\; p^n\; (1 - p)^{N-n}]

and is the probability that there will be [n] successes in [N] independent trials when the probability of success in any given trial is [p]. The CDF is the probability of no more than [n] successes in [N] independent trials when the probability of success in any given trial is [p]. In math, we can write it this way:

[\sum_{k=0}^n \: \binom{N}{k}\; p^k\; (1 - p)^{N-k}]

Basically, we do the PMF calculation for every number of successes from 0 through [n] and we add them together. The binomial CDF appears in lots of practical probability problems, so it would be useful to have it as a PCalc function.

Unfortunately, I don’t think a real binomial CDF function is possible in PCalc because PCalc can’t loop. It has a variety of Skip operations,

Skip operations in PCalc

which work like GOTO statements combined, in all but the first case, with a Boolean test. You might think these could be used to set up loops, but they allow forward skips only, not backward skips, and I can’t think of any way to get a loop going if the flow of operations can’t go backward.1

So we can’t write a function that will calculate the binomial CDF in one call, but we can write one that allows us to quickly run through the summation for each value of [k], accumulating the sum as we go. We’ll call the function “Binomial CDF Step.”

Here’s how it works. Start by putting a zero on the stack as our initial value of the sum. Then add [n], [N], and [p], so the stack looks like this:

Binomial Step 0

After the first call to Binomial CDF Step, the stack will look like this:

Binomial Step 1

What’s happened is we did the PMF calculation for [k=5], added its result to the cumulative sum (which was zero), and reset the stack for another round with [k=4]. Everything is set for a second call to Binomial CDF Step, after which the stack will look like this:

Binomial Step 2

Another call leads to this:

Binomial Step 3

Another gives us this:

Binomial Step 4

And another:

Binomial Step 5

We’re now ready to do the last PMF calculation, with [k=0].2 After this last step, we get this:

Binomial Step 6

Because there are no more calculations to do, the stack has been reduced to just the sum.

While this is a less than ideal way to run through a loop, it goes surprisingly quickly. PCalc helps out by always putting the last function run at the top of the list you choose from after tapping the ƒ(x) button. Would I ever use this to calculate a CDF for [n=50]? No, but it’s quite handy for small values of [n].

The Binomial CDF Step function is defined this way:

Binomial CDF Step

It’s basically the Binomial PMF function from the last post with a bit of business at the end that resets the stack for the next trip through the loop. You can enter it by hand for the full PCalc programming experience, or just download it.


  1. I think James Thomson has written that PCalc’s functions were implemented initially to support unit conversions, where loops aren’t necessary. 

  2. The way the summation sign is written, it’s natural to think of the loop as incrementing k from 0 to n, but addition is commutative, so it’s perfectly correct to decrement k from n down to 0.