Thursday, April 15, 2021

Using Simulation to Solve Probability Questions

Professor P runs two experiments: in A, he rolls a die till he gets two consecutive 6s. In B, he rolls a die till he gets a six followed by a 5. After running each experiment 100,000 times, he takes the average number of rolls needed - for A this number is x, for experiment B, this is y. The question is, is x=y? True or False?

The naive or lazy reader usually concludes that the answer is True. But this is not so. Let's think through this. 

In A, if he rolls a 6 and then rolls a different number, he has to start all over. In B, if he rolls a 6 and then rolls another number that is not a 6, he has to start over. But in B, if he rolls two sixes, he doesn't start over, he has another chance to try to roll a 5. This means that y < x in average. But how do we prove this? Mathematically, this can be done using the principle of iterated expectation. A well-written solution is available here. This tells us that the average number of rolls for A is 42, and that for B it is 36. 

In this post though, let's look at how to solve this problem if you are not able to do the slightly involved math. Simulations are a great way to do this. And python makes it simple beyond describing. Let's look at code: 

import random as r; 

def die_roll(): return r.randint(1,6); 

# experiment A
x=[]; # store the results of the simulation
for i in range(100000): 
 t=[die_roll(), die_roll()]; 
 while t[-1]!=t[-2] or t[-1]!=6: t+=[die_roll()];
 x+=[len(t)];

print(sum(x)/float(len(x))); # computer prints 42.06674

#experiment B
x=[];
for i in range(100000):
 t=[die_roll(), die_roll()]; 
 while t[-1]!=5 or t[-2]!=6: t+=[die_roll()];
 x+=[len(t)];

print(sum(x)/float(len(x))); # computer prints 36.08444

As you can see, a relatively lazy 5 minute piece of code can generate the same answers quite comfortably and makes it possible for us to confidently answer questions like this one. 

Why do interviewers ask questions like this in financial services interviews? 
This kind of evolution of a number series (through die rolls here) with the end of the series being defined by the first time a particular score appears similar in many ways, to the evolution of price movements and the pricing of knock-out options. True, the mechanics of option pricing is quite well defined in literature and there are formulas for achieving that, but in an interview setting the interviewer is trying to assess whether the candidate is able to think through issues like this and propose a method to solve this kind of problem. When I conduct interviews (and I have conducted several hundred over the years), I tell the candidate up front I don't care what numeric answer they come up with, I just want  to understand their approach to solving these problems, and what they are thinking.



No comments:

Post a Comment