- Spoiler: switching doors is the recommended strategy.

I recently had a conversation involving the infamous Monty Hall Problem. I was familiar with the problem, as well as its solution, but **I realized I couldn’t explain it very well**. No one else was able to explain it either, so I decided to **write a simulation**. I’d hoped that writing the code would better help me **intuitively understand the problem**.

# The Monty Hall Problem

The premise is loosely based on the television show Let’s Make a Deal. **Monty Hall himself was the show’s original host**. In any event, here’s how the problem is presented:

- There are three doors.
- Behind
*only one*of these doors is a prize. - You select a door.
- Monty then opens one of the two doors
**you didn’t pick**(to show you that the car isn’t behind it). - Monty asks if you would like to pick a different door.
- Do you?

That’s it. That’s the entire Monty Hall Problem. One of the reasons this problem is so much fun to discuss is that *most* people assume each door has an equal win-probability, and that switching won’t impact their odds of winning. Others observe that your initial chances at picking the car are 1/3 (33%) and after Monty eliminates one of the options, your chances improve to 1/2 (50%). Some of those people argue that switching is the best strategy because your odds improved, and others contend that since switching doors provides a 50/50 chance of winning, there’s really no benefit. The reality is, none of these people **really** understand what’s at play.

# Simulation Code

Let’s first break it down with some pseudo code. Then, I’ll offer a simple visual explanation. I start out by establishing a couple of variables to track which method results in the most amount of wins, and an iterative loop is set to run the simulation a fixed number of times.

```
%As the simulation iterates, it counts how many times each
%method wins
results_switch = 0;
results_noswitch = 0;
for i = 1:1000
```

Next, I randomly select a number between 1 and 3 to represent the door number that hides the prize. At the same time, the door-picker person also randomly picks a door.

```
%Establish the door with the prize
car = randi(3,1);
%Make your first door selection
firstpick = randi(3,1);
```

Remember, Monty knows which door is hiding the prize, and he knows which door the contestant picked. To mimic this, an array is created that includes the doors Monty *could* reveal.

```
%Monty reveals the door that doesn't include the one you
%initially picked OR the one with the prize
revealdoor = setdiff(1:3, [firstpick,prize]);
```

That array *could* contain two door numbers, and it *could* contain just one. If the contestant managed to select the door with the prize, Monty would have the option at revealing either of the other doors. On the other hand, if the contestant picked one of the empty doors, Monty’s hands are tied–he can only show the other empty door!

```
%In the event you actually DID pick the prize on the first
%try, Monty has the option of opening one of the two
%remaining doors
if length(revealdoor) ~= 1
eitherdoor = randsample(2,1);
revealdoor = revealdoor(eitherdoor);
end
```

The contestant switches their pick.

```
%Your second pick doesn't include the door Monty just
%revealed, or your initial pick - remember, you're
%switching every time
secondpick = setdiff(1:3, [firstpick,revealdoor]);
```

The code finishes looping through 1000 times, and it tracks to see whether the switch or no-switch approach renders more wins.

```
%Record if the switching method resulted in a win
if secondpick == prize
results_switch = results_switch + 1;
end
%Record if the "stick with it" method resulted in a win
if firstpick == prize
results_noswitch = results_noswitch +1;
end
end
```

# Simulation Results

I ran the simulation several times, and tabulated the data. It’s pretty convincing! Sticking with your first pick *is* the best strategy!

Run No. | Switch Wins | No-Switch Wins |
---|---|---|

1 | 661 | 339 |

2 | 667 | 333 |

3 | 645 | 355 |

4 | 692 | 308 |

5 | 663 | 337 |

# Conclusion and Explanation

Did you see the step in the code where this was obviously going to be the outcome? It became apparent to me at the point in which **Monty decides which door to reveal**.

```
%In the event you actually DID pick the prize on the first
%try, Monty has the option of opening one of the two
%remaining doors
if length(revealdoor) ~= 1
eitherdoor = randsample(2,1);
revealdoor = revealdoor(eitherdoor);
end
```

Let’s assume the contestant is familiar with game, and plans on switching his pick when given the chance. *If* the contestant initially picked the prize-door, Monty can reveal either of the two other doors, and upon switching, the contestant is rewarded with an empty door. On the other hand, if the contestant picks an empty door on the first try, Monty can only open the other empty door! That *guarantees *that when the contestant switches door, they land on the prize-door! It’s an automatic win if the contestant picks an empty door first (2/3 or 66%), and an automatic loss if the contestant picks the prize-door (1/3 33%).

From the opposing standpoint, if the contestant sticks with their initial pick, their odds at picking the prize-door are 1/3 (33%) and they don’t improve at any point. These results are **exactly predicted by the simulation**!

Well, did I explain the Monty Hall Problem well enough? Let me know in the comments below!