# 31.  Monty Hall Problem (Optional)

These are draft notes extracted from subtitles. Feel free to improve them. Contributions are most welcome. Thank you!
Please check the wiki guide for some tips on wiki editing.

Contents

## 01 Door Chance

Let's have some fun.This gentleman you probably don't know. His name is Monty Hall.Starting in 1963, for many years Monty Hall ran a game show called Let's Make a Deal.In the center of the game show was a puzzlethat puzzles statisticians to the present day.In this unit, which is optional in the statistics class,I'll let you solve the Monty Hall problem, and you'll get a chance to program itto verify that the assertion, which is not obvious, is actually correct.Here we go.The essence of the game are three doors.Behind one of those is a beautiful car, but Monty won't tell you where the car is.Instead, all the doors are covered by curtains.The subject of the game is as follows.You get to place a bet. Say you pick door 2.If behind the bet you place you'll win the car. Otherwise, you'll win nothing.So far, so good.Here's the caveat.Obviously, between the two remaining doors there'd be one that doesn't contain the car.Monty is going to be mean. He's going to show you.He's going to lift the curtain from one of the other doors, and there is clearly no car.After now closing the curtain again, he'll ask you do you want to switch.That is, do you want to change your vote to the remaining curtainin the hopes of having an increased chance of winning the car.The reason why this problem is interesting is because when Monty Hall lifted the curtainhe really didn't tell you anything.You knew in advance that one of the two curtains wouldn't contain the car.The mere fact that he lifted the curtain should give you zero informationrelative to where between the remaining doors the car is.All you know now is that door 3 doesn't contain the car,but why should door 1 now be more likely than door 2.Here's the question.What are the probabilities of the car being behind door 1, door 2, and door 3,provided that your initial pick was door 2 and Monty then lifted door 3 to show you there's no car?

## 02 Door Chance Solution

Let's start with door 3, and here the answer is 0.You already saw when the curtain was lifted that there is no car.How about door 1, and that is a really tricky question.For reasons that I'll explain in a minute,it's actually 2/3 or 0.667. It's entirely non-intuitive.Given that this is the case, what do you think is the number for door 2?Of course, this is 1 - 2.3, which gives me 1/3 or 0.333.

## 03 Switch Doors

When I now ask you, with these numbers in mindthat you might believe or not believe should we switch.Yes or no? Should we go from door 2 to door 1 to increase our chances of winning?

## 04 Switch Doors Solution

Of course, the answer is yes.That's an interesting phenomenon.You placed a random bet. You had a third chance.Monty would lift another curtain and it feels like he's giving you absolutely no information,because you know such a curtain exists.He just shows you the obvious.Yet in doing so you will always switch--every single time.You won't even think. You'll blindly switch to the remaining curtain.By doing this, you'll double your chances of winning. Why is this so?

## 05 Truth Table 1

Well, let's build the truth table.For the true location we have three choices,and because this is a truth table, I write them multiple times.We happen to know that any of those has exactly 1/3 probability,and we'll be using this 1/3 in a minute.Now say you guess door number 2.With this guess Monty will show me an empty door.He might show me door 1, 2, or 3.Now I have all the combinations of the true location and the door I'm being shown.Let's talk about the probabilities of each of these entries.If the true location is 1, which has 1/3 chance,Monty hall will not show me location 1--probability 0.He won't show me location number 2, because that's the one I guessed.The only location he'll show to me will be automatically location 3.It's not the true one. It's not the one I guessed.It's the only empty one that remains.The probability of showing 3 given that the true location is 1 is certain,but the true location is 1 only with a 1/3 chance.This specific outcome that the true location is 1 and Monty Hall shows me 3has exactly 1/3 of a chance.Now, but analogy, you can now fill out for me the three probabilitiesthat correspond to the true location being 3 and Monty showing me 1, 2, or 3into this these three things over here.

join

## 06 Truth Table 1 Solution

The answer is 1/3, 0, 0.I guess you can write this as 0.33.The analogy is exactly as before.Clearly he won't show me door number 2, because that's the one I placed by bet on.Three he won't show me because it actually contains the car.So he shows me 1. With absolutely certainty, he'll show me 1.Yet, this case is only 1/3 likely,because it's only 1/3 likely that the car is behind door number 3. Gives us 1/3.

## 07 Truth Table 2

Now comes the tricky part, and I want you to answer it for me.What is the joint probability that the true location is 2 and Monty chooses to show me 1?What is the joint probability for the location be 2 and Monty shows me 2?Two and three, clearly, the true location in this case is 2and I've picked 2, so in this specific situation Monty has a chance to choose fromthe two other doors and say he picks them at 50% chance randomly.

## 08 Truth Table 2 Solution

Here is the answer--a 6, 0, and a 6.The way the 6 is calculated is by a product.We are in this position the 2 is the true answer with 1/3 chance.When 2 is the correct answer, he'll never pick 2,because he'll just show me that my guess is correct, so he'll pick 1 or 3.Each of those he picks with half chance.As a result, for the joint of these two things to occur,you multiple 1/3 by 1/2 and get 1/6, and the same over here.This is the truth table for any combination of the true location and what Monty shows me,provided that I pick door number 2.

## 09 Truth Table 3

Now let's say Monty picks a door and he picks door number 3as indicated by these bars over here.Put differently, because Monty picks door number 3all the other cases of picking door number 1 or 2now have 0 probability.Let me just cross them out.The only events that remain in the truth table are this one, this one, and this one.Here the true location of the car is door 1, door 2, and door 3.Unfortunately, these numbers don't add up to 1.They are a 1/3, 1/6, and 0.I want you to tell me what the true posterior probability isfor the true location to be 1, 2, or 3.Please write those numbers down over here.

## 10 Truth Table 3 Solution

The answer is 2.3 or 0.667, 1/3, and 0.This is a mere matter of normalization.If you add 1/3 and 1/6, there are 2/6 and 1/6. We get 3/6 which is 1/2.These two things over here add up to 1/5.To make them probabilities, we have to multiply everything by 2,and we get those numbers of here.What this suggests is by simple logic of the truth table door 1 has no a 2/3 chance of being correct.Whereas door 2 has only a total of 1/3 probability of being correct.Door 3, of course, because that's the one that Monty Hall lifted, has 0 chance of being correct.That's actually a proof. There's an eaanothersier version of the proof, which is more intuitive.It says before you saw the empty curtainthe chances were 1/3 each.Once you see the empty curtain, you learn nothing.The chance of the bet you placed is still 1/3.Therefore, the chance that the other door must have gone to 2/3.This is actually correct, but when I read this first, I didn't think it was plausible.I really had to go through the truth table. Let's now go and program it.

Note:
It's important to remember the following from the start:

• Your choice of door is set to be 2. Therefore Monty Hall will never choose door 2 (i.e., the probability Monty Hall choosing door 2 is zero).
• Monty Hall is never going to choose the door where the prize is (i.e., the probability of Monty Hall choosing the true location is zero).
• The first column represents the probability of the prize being behind a certain door (let's call it the probability of event 1).
• The second column represents the probability of Monty Hall choosing a certain door given that the prize is behind a certain door (let's call it the probability of event 2 happening given that event 1 happened, i.e, that's a conditional or posterior probability).
• The third column represents the probability of the prize being behind a certain door and Monty Hall choosing a certain door (the probability of event 1 and 2 happening at the same time), i.e., that's a joint probability.
• Recall that P(A, B) = P(A \mid B) \times P(B) = P(B, A) = P(B \mid A) \times P(A).
 True Location Monty Hall's Door Joint Probability P(\text{True Location = 1}) = \frac{1}{3} P(\text{Monty Hall's Door = 1} \mid \text{True Location = 1}) = 0 P(\text{True Location = 1}, \text{Monty Hall's Door = 1}) = 0 P(\text{True Location = 1}) = \frac{1}{3} P(\text{Monty Hall's Door = 2} \mid \text{True Location = 1}) = 0 P(\text{True Location = 1}, \text{Monty Hall's Door = 2}) = 0 P(\text{True Location = 1}) = \frac{1}{3} P(\text{Monty Hall's Door = 3} \mid \text{True Location = 1}) = 1 P(\text{True Location = 1}, \text{Monty Hall's Door = 3}) = \frac{1}{3} P(\text{True Location = 2}) = \frac{1}{3} P(\text{Monty Hall's Door = 1}\mid \text{True Location = 2}) = \frac{1}{2} P(\text{True Location = 2}, \text{Monty Hall's Door = 1}) = \frac{1}{6} P(\text{True Location = 2}) = \frac{1}{3} P(\text{Monty Hall's Door = 2}\mid \text{True Location = 2}) = 0 P(\text{True Location = 2}, \text{Monty Hall's Door = 2}) = 0 P(\text{True Location = 2}) = \frac{1}{3} P(\text{Monty Hall's Door = 3}\mid \text{True Location = 2}) = \frac{1}{2} P(\text{True Location = 2}, \text{Monty Hall's Door = 3}) = \frac{1}{6} P(\text{True Location = 3}) = \frac{1}{3} P(\text{Monty Hall's Door = 1}\mid \text{True Location = 3}) = 1 P(\text{True Location = 3}, \text{Monty Hall's Door = 1}) = \frac{1}{3} P(\text{True Location = 3}) = \frac{1}{3} P(\text{Monty Hall's Door = 2}\mid \text{True Location = 3}) = 0 P(\text{True Location = 3}, \text{Monty Hall's Door = 2}) = 0 P(\text{True Location = 3}) = \frac{1}{3} P(\text{Monty Hall's Door = 3}\mid \text{True Location = 3}) = 0 P(\text{True Location = 3}, \text{Monty Hall's Door = 3}) = 0

You might have noticed that the normalization step is not really necessary. If you look at the last column from the table:

• If you don't switch (stick to door 2) the probability of yourself winning is P(\text{True Location = 2},\text{Monty Hall's Door = 1}) + P(\text{True Location = 2},\text{Monty Hall's Door = 2}) + P(\text{True Location = 2},\text{Monty Hall's Door = 3}) = \frac{1}{6} + 0 + \frac{1}{6} = \frac{1}{3}

Since Monty Hall chooses first, the only choices you really have are "switch" or "don't switch" . After Monty Hall makes his choice, there is only one door left to switch to. The switch could be either from door 2 to 1 or door 2 to 3 according with Monty Hall's choice, i.e., if I switch, these represents all the possibilities where you would switch and win:

• P(\text{True Location = 1},\text{Monty Hall's Door = 1})

• P(\text{True Location = 1},\text{Monty Hall's Door = 2})

• P(\text{True Location = 1},\text{Monty Hall's Door = 3})

• P(\text{True Location = 3},\text{Monty Hall's Door = 1})

• P(\text{True Location = 3},\text{Monty Hall's Door = 2})

• P(\text{True Location = 3},\text{Monty Hall's Door = 3})

Therefore:

• If you switch doors, the probability of yourself winning is P(\text{True Location = 1},\text{Monty Hall's Door = 1}) + P(\text{True Location = 1},\text{Monty Hall's Door = 2}) + P(\text{True Location = 1},\text{Monty Hall's Door = 3}) + P(\text{True Location = 3},\text{Monty Hall's Door = 1}) + P(\text{True Location = 3},\text{Monty Hall's Door = 2}) + P(\text{True Location = 3},\text{Monty Hall's Door = 3}) = 0 + 0 + \frac{1}{3} + \frac{1}{3} + 0 + 0 = \frac{2}{3}

Note that you double your chance of winning if you switch.

## 11 Simulate

I want you to program this now.I want you to run 1,000 simulationsusing a function simulate that you have to fill in.In this simulation, you count how often you succeed in the variable k.You pick a random door using the function randint.If you give it the parameter 1,3 it picks a number between 1 and 3 at random.You make your own choice using the same function randint,and then you have to emulate Monty.Sometimes it picks it deterministically. Sometimes it picks it stochastically.Write that code and then flip your choice of the remaining door.If that remaining door is correct, implement k.Otherwise, don't increment k.When you run this 1,000 times, your output should be approximately 2/3.This program requirement requires real knowledge of Python.If you're new to all this, feel free to skip it. It is a big challenging.

from random import randint, choice

N = 1000

def simulate(N):
K = 0
### insert your code here ###

print simulate(N)

## 12 Simulate Solution

Here's my solution. First, I use randint to assign the true location of the car.My initial guess, called guess1, is also chosen at random.You see these independent random choices with equal probability.Now there are two possibilities. Either truth is equivalent to guess or it isn't.When truth is equivalent to guess, Monty will pick one of the two other doors.Let's just implement this. I will let Monty pick a random door using randint.Of course, he might have picked the correct door, which contains the car,which is also my guess, and when that's the case I just rerun the random number generatoruntil it finally finds a door that is not the true door or equivalently not the guess.That this pick. If we look carefully, this might run a number of times.But when it returns, he picked one of the remaining doors at 50% chance.In the else clause, the truth and my guess are not the same.So, Monty now has to pick the remaining door with probability 1.How do we get get Monty's choice?Now I'm going to do a little cute trick.I know that the sum of all doors--1 + 2 + 3--equals 6.If I subtract from 6 the truth and my guess, I get the third door.This equation--6 - truth - guess--will give us the number of the door that isn't truthand isn't guess 1.To flip my door, I can now do the same trick I did over here.I pick the one that wasn't my initial guess, which is subtract guess1 from 6,pick the one that isn't Monty. That gives me guess 2.If that guess is the same as the 2 door, I increment k by 1.Then I return the final number over here.This gives me a single simulation.I run through this n times.So, I added a loop for i in range n, move everything to the right a little bit,and then this simulation is run n times,returning my desired ratio of the successes over the total number of experiments.Let's run it.When I run it 1000 times, I get a number like 0.672.That's not exactly 2.3, but obviously there is some randomness involved in the simulation.If I increase N to 10,000, I get a number slightly closer to 0.66.Do it 400,000, I get 0.66748.That is awfully close to 2/3.It's really interesting to see that the switching helps.Put differently, say I use guess1 as my final guess to compare to the truthas opposed to guess2 and run this.I get 0.331.The interesting thing about guess1 is--the reason why it works is--all the code from if truth all the way to here--guess2--is now irrelevant.I can comment all of this out here in the middle, which I've just done,and just retain the assignment of the truth, which is a random number,the assignment of guess1, which is a random number,and then I just check where these two agree.Clearly, the probability must be a 1/3.All this story about showing the open door has no effect on this.That little thing here that I simulated and when I retained the original guessthen all the story about opening the door has no effect on the chances of my guess,prove that the chance is to be 1/3 for the original door.Therefore, switching goes up to 2/3.Isn't that cool?This is the story of Monty Hall and the very famous quiz.

from random import randint, choice

N = 1000

def simulate(N):
K = 0
### insert your code here ###
for i in range(N):
truth = randint(1,3)
guess1 = randint(1,3)
if truth == guess1:
monte = randint(1,3)
while monte == truth:
monte = randint(1,3)
else:
monte = 6 - truth - guess1
guess2 = 6 - guess1 - monte
if guess2 == truth:
K = K + 1
return float(K) / float(N)

print simulate(N)