# Monty Hall problem

Wed 19 January 2022The Monty Hall problem is a probability puzzle based on an American television game show whose host was named Monty Hall.

The problem is a paradox because its solution is counterintuitive and can seem absurd. When first purposed and solved many people, including renowned mathematician Paul Erdős, were initial unconvinced with the solution.

The Monty Hall problem is mathematically closely related to the earlier Three Prisoners problem.

# The problem

Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?

# The simple solution

Always switching doors has a 2/3 probability of winning the car, while never switching (staying with the initial choice) has only a
1/3 probability. **Hence, it is always to our advantage to switch doors when prompted by the host.**

Here is an intuitive explanation:

If the contestant initially picks a goat, the contestant will win the car by switching because the other goat can no longer be picked (the host had to reveal its location).

If the contestant initially picks the car (1 out of 3 chances), the contestant will not win the car by switching.

Thus, when always switching doors, winning the car only depends on whether the contestant has initially chosen a goat (2/3 probability).

The fact that the host subsequently reveals a goat in one of the unchosen doors changes nothing about the initial probability.

# Simulation

Lets conduct a simulation of the game to check this solution.

First we will import the `random`

module that will allow us to pick doors at random.

```
import random
```

Then we create a function to simulate a single game of the Monty Hall problem. In this function, if `switch`

is `True`

, the contestant will switch their initially chosen door when offered the chance. The function will return `True`

if the contestant has won the car, otherwise it returns `False`

. Doors are numbered 0 to 2 (3 doors total).

```
def simulate_game(switch=True):
DOORS = [0, 1, 2]
# Choose prize door
car_door = random.choices(DOORS)[0]
# Contestant choses a door
contestant_door = random.choices(DOORS)[0]
# Host opens one door without prize
host_cant_choose = [car_door, contestant_door]
host_options = [x for x in DOORS if x not in host_cant_choose]
host_door = random.choices(host_options)[0]
# If contestant switches doors
if switch:
selected_doors = [contestant_door, host_door]
contestant_door = [x for x in DOORS if x not in selected_doors][0]
# Check if contestant won
if contestant_door == car_door:
return True
return False
```

Now, lets run this simulation multiple times (100 thousand) and calculate the win rate when the contestant switches doors and when the contestant keeps its initially selected door.

```
N = 100_000 # number of simulations to run
# Run N simulations where the contestant never switches doors
results_no_switch = []
for i in range(N):
results_no_switch.append(simulate_game(switch=False))
no_switch_wins = len([x for x in results_no_switch if x is True])
no_switch_wins_rate = no_switch_wins / N * 100
print(f"Never switches: {no_switch_wins} wins out of {N} games ({no_switch_wins_rate:.2f} % win rate).")
# Run N simulations where the contestant always switches doors
results_switch = []
for i in range(N):
results_switch.append(simulate_game(switch=True))
switch_wins = len([x for x in results_switch if x is True])
switch_wins_rate = switch_wins / N * 100
print(f"Always switches: {switch_wins} wins out of {N} games ({switch_wins_rate:.2f} % win rate).")
```

As an output of our simulation we get:

```
Never switches: 33430 wins out of 100000 games (33.43 % win rate).
Always switches: 66785 wins out of 100000 games (66.79 % win rate).
```

Switching doors resulted in an win rate of 66.79 %, very close to the expected value of 2/3 (66.67 %).

We can see the evolution of the simulation as the number of games increases in the plot below. Here 15 simulation of 100 thousand games each was conducted. It is clearly visible that, as the number of simulated games increases the win rate of the switching door strategy tends to 66.67 % (2/3), while the win rate of not switching tends to 33.33 % (1/3).

# References

Wikipedia - Monty Hall problem