Suppose an $n$-page book is known to contain $m$ misprints. If the misprints are independent of one another, the probability of a misprint occuring on a particular page is $p=1/n$ and their distribution may be considered to be binomial. Write a short program to conduct a number of trial virtual "printings" of a book with $n=500, m=400$ and determine the probability, $\mathrm{Pr}$, that a single given page will contain misprints.
Compare with the result predicted by the Poisson distribution with rate parameter $\lambda = m/n$, $\mathrm{Pr} = 1 - e^{-\lambda}\left(\frac{\lambda^0}{0!} + \frac{\lambda}{1!}\right)$.
Here is a more general solution to the problem. Draw the distribution of misprints across the book from the binomial distribution using np.random.binomial()
and count up how many pages have more than $q$ misprints on them. To compare with the Poisson distribution, for the number of misprints on a page, $X$, we must calcualte $\mathrm{Pr(X>=q)} = 1 - \mathrm{Pr(X<q)} = 1 - (\mathrm{Pr}(X=0) + \mathrm{Pr}(X=1) + \cdots + \mathrm{Pr}(X=q-1)$:
import numpy as np
n, m = 500, 400
q = 3
ntrials = 100
errors_per_page = np.random.binomial(m, 1/n, (ntrials, n))
av_ge_q = np.sum(errors_per_page>=q) / n / ntrials
print('Probability of {} or more misprints on a given page'.format(q))
print('Result from {} trials using binomial distribution: {:.6f}'
.format(ntrials, av_ge_q))
# Now calculate the same quantity using the Poisson approximation,
# Pr(X>=q) = 1 - exp(-lam)[1 + lam + lam^2/2! + ... + lam^(q-1}/(q-1)!]
lam = m/n
poisson = 1
term = 1
for k in range(1,q):
term *= lam/k
poisson += term
poisson = 1 - np.exp(-lam) * poisson
print('Result from Poisson distribution: {:.6f}'.format(poisson))
A sample output is:
Probability of 3 or more misprints on a given page
Result from 100 trials using binomial distribution: 0.048860
Result from Poisson distribution: 0.047423