I've recently been brushing up on my statistics by reading Principles of Statistics by M.G. Bulmer. I came across a problem and since I wrote some code to check my answer, I figured I'd post it with a short discussion about the answer.

First, the question:

In a certain survey of the work of chemical research workers, it was found, on the basis of extensive data, that on average each man required no fume cupboard for 60 per cent of his time, one cupboard for 30 per cent and two cupboards for 10 per cent; three or more were never required. If a group of four chemists worked independently of one another, how many fume cupboards should be availabe in order to provode adequate facilities for at least 95 per cent of the time?

My line of thinking to solve this was to find every combination of the 4 chemists needing 0, 1, or 2 cupboards, the probability of each of those combinations happening, and finally summing up the probability of all the hoods needed.

For example, out of the 81 different possible combinations of cupboards required (3 * 3 * 3* 3, with the 3 coming from 0, 1, or 2 hoods needed), there is only one way where 0 hoods are needed in total and this is where all 4 chemists need no cupboards. Following this, there are 4 ways to have 1 hood total be required, with each of the chemists exclusively requiring a cupboard and the other three needing none (1, 0, 0, 0 & 0, 1, 0, 0 & 0, 0, 1, 0 & 0, 0, 0, 1).

So having one cupboard covers the probability of needing no cupbards amongst the 4 PLUS the probability of needing 1 cupboard amongst the 4.

I first did this problem long-handed, figuring out the probability of 0, 1, 2, and so on cupboards until I got to a sum that had a probability > 0.95. I was making a simple arithmetic error (as usual) and my answer was not matching up with what was in the back of the book, so I thought I would write a simple program to calculate the answer since I was confident in what I was trying to do, but was just having trouble multiplying and adding.

Here's the Python script I wrote (note: you need Python >= 2.6 as I use itertools.product to recreate all the combinations of cupboards needed).

The output is the summing of each of the probability of needing 0 cupboards + 1 cupboard + 2 cupboards and so on. The line with the probablity greater than 0.95 is the answer. In this the case, the answer was 4, which would cover the chemists' needs 95.85 percent of the time.

```
from collections import defaultdict
import itertools
probs = {
'0': 0.6,
'1': 0.3,
'2': 0.1
}
trials = itertools.product('012', repeat=4)
totals = defaultdict(float)
for trial in trials:
#how many hoods needed in this trial
trial_sum = sum(map(lambda x: int(x), trial))
#figure probability of exact trial occurring
total_prob = 1.0
for item in trial:
total_prob *= probs[item]
#add probability of trial to total prob for this number of hoods needed
totals[trial_sum] += total_prob
#print out all probabilities
keys = totals.keys()
keys.sort()
running_prob = 0.0
for i in keys:
running_prob += totals[i]
print i, running_prob * 100
```