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)$.

Solution Q6.7.4

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)$:

importnumpyasnpn,m=500,400q=3ntrials=100errors_per_page=np.random.binomial(m,1/n,(ntrials,n))av_ge_q=np.sum(errors_per_page>=q)/n/ntrialsprint('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/npoisson=1term=1forkinrange(1,q):term*=lam/kpoisson+=termpoisson=1-np.exp(-lam)*poissonprint('Result from Poisson distribution: {:.6f}'.format(poisson))

A sample output is:

Probability of 3 or more misprints on a given pageResult from 100 trials using binomial distribution: 0.048860Result from Poisson distribution: 0.047423