Monte Carlo Methods
Suppose you are given the following question: There are 5 babies in a room, 2 boys and 3 girls. A new baby is born in the room. A nurse picks a baby at random. The baby is a boy. What is the probability the new baby is a boy?
This is a question I stumbled upon in a job interviewed. Now for some this question can be answered with a couple of lines of maths using probability, but for others this may not be the case. This brief page aims to show how Monte Carlo methods can be employed to solve problems where you may not know the maths or be an expert. First I shall go through the mathematical solution (you do not have to understand this) and then I shall cover how Monte Carlo methods can help us.
The Math
Suppose b represent the new baby where M is Male (boy) and F is female (Girl), and n represent the nurse selection. \[\mathbb{P}(b=M|n=M) = ?\] \[Prior Probabilities: \mathbb{P}(b=F) = \mathbb{P}(b=M) = 0.5\] \[\mathbb{P}(n=M|b=M) = \frac{\textrm{num of boys}}{\textrm{num of babies}}= \frac{3}{6} = 0.5 \] \[\mathbb{P}(n=M|b=F) = \frac{\textrm{num of boys}}{\textrm{num of babies}}= \frac{2}{6} = \frac{1}{3}\] \[Bayes Rule: \mathbb{P}(A|B) = \frac{\mathbb{P}(B|A)\mathbb{P}(A)}{\mathbb{P}(B)} \] \[\mathbb{P}(b=M|n=M) = \frac{\mathbb{P}(n=M|b=M)\mathbb{P}(b=M)}{\mathbb{P}(n=M)} \] \[\mathbb{P}(n=M) = \mathbb{P}(n=M|b=M)\times\mathbb{P}(b=M) + \mathbb{P}(n=M|b=F)\times\mathbb{P}(b=F) = \] \[0.5\times0.5 + \frac{1}{3}\times0.5 \] \[\mathbb{P}(b=M|n=M) = \frac{0.5\times0.5}{0.5\times0.5 + \frac{1}{3}\times0.5} = \frac{0.5}{0.5 + \frac{1}{3}} = \frac{3}{5}\] So our answer is 0.6.Monte Carlo Methods
As questions like these get more and more complex, they require more thought and expertise. What if there is a way to solve this problem with no knowledge of statistics? Well this is the solution! Monte Carlo Methods essentially simulate the situation and run enough trial to obtain a robust result. So our pseudo code is as follows for this specific question.
In this code we are sampling the scenario given, and we are only interested in the sample if the nurse selects a boy as this is what the question tells us. And out of these samples we look at how many time was the new baby a boy and this is our result.Code
n = 1,000,000 samples = 0 successfull_selection = 0 successfull_sample = 0 new_baby = '' for i in range n: num_girls = 3 num_boys = 2 if random(0,1) == 1: new_baby = 'b' num_boys += 1 else: new_baby = 'g' num_girls += 1 if 'b' == random.choice(['b','g'], prob = [num_boys/6, num_girls/6]): successfull_selection += 1 if new_baby == 'b': successfull_sample += 1 return successfull_sample/successfull_selection
Results
After running 1,000,000 trials our approximation is 0.0003 off the actual value of 0.6.Is clear how MC can be used as this code requires no knowledge of probability or Bayes theorem however are results are still accurate. We see from the graph however that as the number of samples increase so does the time taken to run the simulation. A further downside to MC is that there is not interpretable results, we do not know what would be the answer say if there we originally 4 girls. Maths can allow us to establish a relationship between the result and other variables but the MC methods does not. Lets look at this further.
Further Investigating
Lets suppose the question is There are 5 babies in a room, 2 boys and an unknown number of girls. A new baby is born in the room. A nurse picks a baby at random. The baby is a boy. What is the probability the new baby is a boy?
Where exactly do we start with this? Lets start with looking at the maths.