Monty Hall Problem Simulation
Simulation of Monty Hall problem in python
Problem Description :
- You are in a game show. There are 3 closed doors. You are asked to choose one.
- Behind one of them there is a car. The other 2 doors have 2 goats behind them. If we choose the door with the car, the car is ours. We’d like to win the car. The game show host knows behind which door the car is.
- We made a selection.
- Now, the game show host opens the door where a goat is from the 2 unselected doors. Now there are two unopened doors. The host asks us if we want to change our selection to the other unopened door.
- Should we make a switch ? (3 options : Yes, No, Doesn’t matter)
Answer :
Yes. Do switch. (Popular answer is that it Doesn’t matter)
Initially, the probability of winning the car is 33.33%.
If we switch, the probability of us winning a car will become 66.66%. If we don’t switch, the probability of winning will stay 33.33%.
The answer is not 50%. This is because once the host opens a door with the goat, the experiment is no longer a random experiment. If it were a random experiment, the answer would be 50%
Python Simulation :
Below is the python code to simulate the experiment.
def simulate_monty_hall(n_simulations=n_simulations):
# Select the door number where the car is, randomly
car_index = np.random.randint(0,3, n_simulations) # Create multiple Monty Hall Problem simulations.
problems = np.zeros((n_simulations,3))
for i in range(n_simulations):
problems[i][car_index[i]]=1
# See if we won the car
winning = []
for i in problems:
choice_index = np.random.randint(0,3)
if i[choice_index] == 1:
shift_var = 0
else:
shift_var = 1
winning.append(shift_var)
# Average win percentage across all n_simulations
win_percentage = 100*sum(winning)/len(winning)
return win_percentagesimulate_monty_hall(n_simulations)
Since, this is a probability problem, the more number of simulations, the stronger the results are. Here is a plot, when I vary the n_simulations variable, from 1 to 200.
As we can see, as the number of simulations increased, the percentage of winning the car is stabilized around 66.67 (the red line).
In conclusion, always switch the selection. Switching doubles our odds !
When is the answer 50% ?
If the host doesn’t know where the car is and he opens the door with goat and then again he asks if we want to switch, then the answer is 50%. It doesn’t matter if we switch or not.
Refer to the link for detailed explanation.