1. LON-CAPA Logo
  2. Help
  3. Log In
 

Browsing resource, all submissions are temporary.


Simulating the Monty Hall Problem

The Monty Hall problem is a brain teaser problem in probability. It's not difficult to solve (it's one of the HW problems in Stat 100), but the result is counter-intuitive. Here's 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, nothing. You pick a door and Monty Hall, the host, who knows what's behind the doors, opens another door which has no car. He says to you, "Do you want to switch to the other door?"

What is the best strategy to maximize the chance of winning the car? There are two arguments.

First argument: it doesn't matter whether or not you switch since we now know the car is either behind the door you've picked or behind the other closed door. The chance of the car behind either door is 50-50.

Second argument: When you first picked a door, the probability that the car is behind the door you picked is 1/3. When Monty Hall opened another door, it hasn't changed anything. The car is still behind whatever door it was and so your chance of picking the right door is still 1/3. So you should switch, because the chance of the car behind one of the other two doors is 2/3 and Monty Hall deliberately opens a door that has no car behind. So switching will increase your probability of winning the car from 1/3 to 2/3.

In this exercise, you are going to simulate this problem using R. We are going to throw in a few twists to the problem. By the end of this exercise, we hope you'll appreciate the subtlety of the problem.

Without loss of generality, we can label the door the player picks as door 1 and the other doors as 2 and 3. The car is equally likely to be behind doors 1, 2 and 3 *. The following function monty1() simulates the game. In the simulation, we assume that the player always switches to the other door after Monty Hall opens a door with no car behind it.

monty1 <- function() {
  doors <- 1:3
  
  # randomly assign the car behind a door
  car <- sample(doors,1)
  
  # Monty Hall chooses a door to open 
  if (car==1) {
    # Case 1: player's chosen door (door 1) has the car behind it. 
    # Monty Hall randomly chooses door 2 or 3 to open
    opened <- sample(2:3,1)
  } else {
    # Case 2: player's chosen door (door 1) has no car behind it. 
    # Monty Hall opens the other door with no car.
    opened <- doors[-c(1,car)]
  }
  
  # Player switches to the other closed door: set player <- 2 if opened is 3 and player <- 3 if opened is 2
  player <- MISSING CODE
  
  # Return TRUE if the player wins the car, FALSE if the player does not win
  player==car
}

a. What could be the missing code that replaces MISSING CODE above to complete this program? (select all that apply)
opened + 1
opened - 1
doors[-c(car,opened)]
5 - opened
doors[-c(1,opened)]

 Tries 0/3

Replace MISSING CODE by one of the correct answers and then simulate playing the game 30,000 times and gather the result using the following command:

RNGversion("3.5.0") 
set.seed(8083407)
win <- replicate(3e4, monty1())

b. Use the simulation result to calculate the probability that the player wins the car. Enter your answer to 4 decimal places.

P(win) ≈

 Tries 0/3

Parts (c)–(e) will show up after you finish part (a).


* In case you don't understand this. Here's a detail explanation. Let's label the doors as A, B, and C. If the player picks door A, we'll relabel door A as door 1, door B as door 2 and door C as door 3. If the player picks door B, we'll relabel door B as door 1, door C as door 2, and door A as door 3. If the player picks door C, we'll relabel door C as door 1, door A as door 2 and door B as door 3. In all cases, we label door 1 as the door the player picks. The car is equally likely to be behind doors A, B, and C. So

P(car behind door A) = P(car behind door B) = P(car behind door C),

but A, B, C are permutations of 1, 2, and 3 after relabelling. So we have

P(car behind door 1) = P(car behind door 2) = P(car behind door 3).

That is to say that the car is equally likely to be behind doors 1, 2 and 3.