# Simulating the Monty Hall Problem

Posted on

The previous post considered a generalized version of the Monty Hall problem. Here I want to consider a slightly different version. It goes like this.

Monty shows you N doors; N is at least 3 or the game doesn't work. Behind one of the doors is a prize, behind the remaining doors there is nothing. You win if you can guess which door conceals the prize.

There is a twist to the game. After you announce your guess Monty opens one door, not the one you chose and not the one concealing the prize. After this reveal Monty offers you the opportunity to switch doors. Should you switch?

In the game as described in the previous post Monty opened all the doors but two, your guess and one other. In this game Monty shows you less: he opens only one door. But you should still switch. Fundamentally not much has changed. There are N doors, so the probability that your initial guess is correct is 1/N. Monty's big reveal again happens with probability 1, so the chance that your initial guess is correct given the reveal remains at 1/N.

The previous post used a slightly abstract argument to reach its conclusion. Here I want to go in the opposite direction. Probabilistic arguments are easy to get wrong. It can be worthwhile to run a simulation to see whether its results agree with the theoretical predictions.

Lets get specific about the game and then simulate it. Let N=5. There are 5 doors, you make your choice. Monty responds by opening one door and showing you that the prize was not behind it. You are allowed to switch from your initial guess to another door if you like. Should you? Yes, you should.

There are four doors still in play, your guess and three more. I claim that the probability that your original guess is correct is 1/5, and consequently the probability that the prize is behind one of the three other doors is 4/5. If you switch, your probability of winning will be 1/3 of 4/5, so 4/15. The odds ratio of switching to staying put is 4/15 to 1/5, which is 4/15 to 3/15, which is 4 to 3. If you switch you should win about 4/3 times more frequently than if you don't. This claim is easily tested.

Below is a short program that simulates the game. The program is written the Wolfram language (Mathematica). I think it should be intelligible to most programmers, even those who not familiar with Mathematica.

```
trial[numDoors_] :=
Module[{doors, firstGuess, prize, revealed, secondGuess},
doors = Range[1, numDoors];
prize = RandomChoice[doors];
firstGuess = RandomChoice[doors];
revealed = RandomChoice[Complement[doors, {firstGuess, prize}]];
secondGuess =
RandomChoice[Complement[doors, {firstGuess, revealed}]] ;
{If[firstGuess == prize, 1, 0], If[secondGuess == prize, 1, 0]}
]
```

Let's read it. The number of doors in our case is 5, so **doors** is the list {1,2,3,4,5}.
Monty sets up the game by randomly picking one of the doors, **prize**, and hiding the prize behind it.
Then you take your **firstGuess**. Then Monty randomly reveals one of the doors, making sure that it is not your first guess or the prize. Then you switch by taking a **secondGuess**, different from your first guess and the revealed door. The trial returns a pair of numbers:

- {1,0} if the first guess was correct.
- {0,1} if the second guess was correct.
- {0,0} if neither was correct. By construction the first and second guess are not equal, so both cannot be correct.

One trial is not enough. We need to run many trials to make decent simulation.

```
simulation[numDoors_, numTrials_] :=
Module[{fg, sg},
{fg, sg} = Fold[Plus, {0, 0}, Table[trial[numDoors], numTrials]];
Print["correct 1st guesses: ", fg];
Print["correct 2nd guesses: ", sg];
Print["ratio 2ndGuess/1stGuess: ", N[sg/fg]]
]
```

The simulation runs a bunch of trials and adds up (pointwise) the pairs returned by each trial. That addition is done by Folding the Plus operator over a list of trial results and an initial value of {0,0}. Functional programming for the win.

Let's run the simulation:

```
With[{numTrials = 500000, numDoors = 5},
simulation[numDoors, numTrials]]
correct 1st guesses: 99853
correct 2nd guesses: 132958
ratio 2ndGuess/1stGuess: 1.33154
```

That ratio agrees nicely with the predicted 4/3.

The version of the game played on the original Let's Make A Deal television program used three doors. In that case the probability of the first guess winning is 1/3. The probability of winning by switching is 2/3, so you should win twice as often if you switch than if you don't. What does the simulation show?

```
With[{numTrials = 500000, numDoors = 3},
simulation[numDoors, numTrials]]
correct 1st guesses: 167043
correct 2nd guesses: 332957
ratio 2ndGuess/1stGuess: 1.99324
```

Again we have excellent agreement with the theoretical odds ratio of 2. The moral is that sometimes it is both simple and easy to test calculations via simulation. I am a complete sucker for probability puzzles, so for me simulations are gold.