Professional Documents
Culture Documents
A problem came up at lunch yesterday: Imagine shaking a bag of fair pennies and dumping them out on a table. How many pennies
do you need to dump out before you have a ninety percent probability of having 100 heads?
A fair penny is one that has an equal chance of coming up heads or tails, that is, a 50% probability of coming up heads.
First, it's clear you need more than 100 pennies, and it's intuitive that you need more than 200.
A collection of pennies obeys the binomial probability distribution. The following is the probability that, out of n pennies, exactly k
come up heads if every penny has a probability p of coming up heads. For a fair penny, p = 1 2.
pk H1 - pLn-k
n!
k! Hn - kL!
In[62]:= fAk_, n_, p_E :=
That formula is not difficult to understand. Imagine numbering the pennies and then writing out every possible outcome: penny 1
heads, all the rest tails; penny 2 heads, all the rest tails; pennies 1 and 2 heads, all the rest tails; and so on. It's feasible to do this by
hand for some small number of pennies.
The number of different outcomes you will write is 2n , and the number that have exactly k heads is n ! k ! Hn - kL !. The outcomes are
Go further. An outcome of k heads is mutually exclusive with an outcome of k+1 heads, and so on. Therefore, the probability of at
least k heads is this:
This is harder than average to reduce, but Mathematica has a great library of special functions and easily figures out that
-k + n k
((1 - p) p Gamma[1 + n]
p
Hypergeometric2F1Regularized[1, k - n, 1 + k, ------])
----- /
-1 + p
Gamma[1 - k + n]
F
p-1
In[76]:= FunctionExpand Hypergeometric2F1RegularizedB1, k - n, 1 + k,
p
OutputForm
Out[76]//OutputForm=
-1 + p
Hypergeometric2F1[1, k - n, 1 + k, ------]
-----
p
------------------------------------------
-----------------------------------------
Gamma[1 + k]
The hypergeometric functions are a beautiful, deep, and rich topic. Wikipedia has a decent page on them: http://en.wikipedia.org/wiki-
/Hypergeometric_function. Ditto the gamma function: http://en.wikipedia.org/wiki/Hypergeometric_function.
Numerical Samples
Two Coins
Check that the probability of one head up when flipping two fair pennies is 1/2 -- there are four possible, independent, mutually
exlusive outcomes, and two of them have 1 head up
F
1
In[67]:= fB1, 2,
2
1
Out[67]=
2
Only one of the four outcomes has both heads up, and it must have probability 1/4:
F
1
In[68]:= fB2, 2,
2
1
Out[68]=
4
F
1
In[69]:= fgB2, 2,
2
1
Out[69]=
4
F
1
In[70]:= fgB1, 2,
2
3
Out[70]=
4
100 Coins
Back to the original problem:
F
1
In[71]:= NfgB100, 150,
2
Out[71]= 0.0000272377
about 2.7 in 100,000. We'd have to throw out our bag around 35,000 times to get one outcome with at least 100 heads up.
F
1
In[72]:= NfgB100, 250,
2
Out[72]= 0.999393
Almost all the time. If we threw out our bag 1,000 times, only once, on average, would we expect NOT to have at least 100 pennies
heads up.
Manual binary search leads us to the final answer:
F
1
In[79]:= NfgB100, 217,
2
Out[79]= 0.889174
F
1
In[73]:= NfgB100, 218,
2
Out[73]= 0.900981
218 is the smallest number of pennies in the bag that give us a 90% probability of yielding 100 heads up.
We can plot the "at-least 100" function for all numbers of pennies in our bag from 150 to 250.
4 probs001.nb
0.8
0.6
Out[74]=
0.4
0.2
We could do lots more analysis, but this straightforward numerical demonstration gives us a quick answer.