PCalc binomial functions—Part 2
September 20, 2018 at 11:28 PM by Dr. Drang
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:
and is the probability that there will be successes in independent trials when the probability of success in any given trial is . The CDF is the probability of no more than successes in independent trials when the probability of success in any given trial is . In math, we can write it this way:
Basically, we do the PMF calculation for every number of successes from 0 through 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,
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 , 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 , , and , so the stack looks like this:
After the first call to Binomial CDF Step, the stack will look like this:
What’s happened is we did the PMF calculation for , added its result to the cumulative sum (which was zero), and reset the stack for another round with . Everything is set for a second call to Binomial CDF Step, after which the stack will look like this:
Another call leads to this:
Another gives us this:
And another:
We’re now ready to do the last PMF calculation, with .2 After this last step, we get this:
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 ? No, but it’s quite handy for small values of .
The Binomial CDF Step function is defined this way:
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.
-
I think James Thomson has written that PCalc’s functions were implemented initially to support unit conversions, where loops aren’t necessary. ↩
-
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. ↩