​The Monty Hall problem became famous after Marilyn vos Savant published her answer that switching doors is the best strategy. She was right, to the surprise of many contemporary mathematicians. Now, this problem is a classic puzzle taught in statistics courses. The first time I saw the Monty Hall problem was in my AP Statistics class, and I remember how counterintuitive the solution seemed to me.

I thought I'd have some fun by simulating the Monty Hall problem in R and writing up a mathematical explanation for the solution.

R Simulation

​I coded up this function to simulate playing the Monty Hall game n times. The function compares the strategy of always switching doors to the strategy of never switching doors, then graphs the results.

If you want to play around with this function, copy and paste it into R.

R function for Monty Hall problem

Note that this function uses the graphical package ggplot2. If you have not installed that package in R, use the following code before copying and pasting the above:

Code to install ggplot2 and dplyr in R

Here is an example of the Monty Hall simulation function in action.

Using the Monty Hall function to simulate playing the Monty Hall game 100,000 times

​The simulation results? When you always stick with your original door choice, you win about 33% of the time. result relativefrequency0 loss 0.666251 win 0.33375

Plot of simulation results in 100,000 runs when the player stays with the original door

​When you always switch doors, you win about 67% of the time. result relativefrequency0 loss 0.331921 win 0.66808

Plot of simulation results in 100,000 runs when the player switches doors

​The simulation shows that switching doors gives you a higher chance of winning.​

Mathematical Explanation

Let's first explore an intuitive explanation of the Monty Hall problem. Suppose you decide to always switch doors. You pick a door randomly.

If a car is behind your first door, Monty will open one of the doors with a goat. Since you decided to always switch doors, you'll switch to the door with a goat - and lose.

If a goat is behind your first door, Monty will not open the door with the car. Instead, Monty will open the remaining door that contains a goat. Since you decided to switch doors, you'll switch to the door with a car - and win.

Of the three possible doors you can pick at the start, there is one way you can start with a car and switch to a goat - hence losing. There are two ways that you can start with a goat and switch to a car - hence winning. So, switching doors gives you a 2/3 chance of winning.​​

Figure: Tree diagram for the Monty Hall problem

A tree diagram showing all possible outcomes when you start by picking door 1

Strategy: Not switchingIf you do not switch doors after Monty reveals door 3, the only way you can win is if the car is behind your original door: door 1. We'll calculate \[P(C_{1} | M_{3})\], the probability that the car is behind door 1 given that Monty reveals door 3.

Strategy: SwitchingIf you do switch doors after Monty reveals door 3, you will end up with whatever is behind door 2. You win if the car is behind door 2. We'll calculate \[P(C_{2} | M_{3})\], the probability that the car is behind door 2 given that Monty reveals door 3. The calculations follow the same pattern as in the first case.

Results

The mathematical explanation and simulation results match. The probability of winning game if you switch doors is 2/3, while the probability of winning if you stay with your original choice is 1/3. Hence, switching is the better strategy.​

I just happened upon this blog by misclicking, and then I saw the data part, and then got curious. I like how you write code :'D I have tons of students (2 years of TAing) who write if statements that are unreadable, and/or functions that make..just about....no sense. The only one thing I would recommend is perhaps renaming your switch variable to switchDoor for readability purposes, but otherwise your code looks magical 😍 Also, I've never coded in R, but it looks a lot like python, but with brace brackets, which is rather interesting.

Reply

Lilian

12/19/2017 06:54:18 am

Thanks, Nat! Haha, switchDoor would've been clearer than switch. Great to hear that R looks like Python, since I'll most likely pick up Python at some point. I've happened upon a few people's blogs with Python code, and the lack of brackets always throws me for a second!