Chapter 4 Section 1 Exercise 11

This problem as well as Marilyn von Savant’s name popped up a few times recenty, so it got my interest. Below is the R simulation which was suggeste by exercise 11.

Solution

  • The function below randomly assigns one of the doors to hide the car. Two other doors hide goats.
  • We assume contestant always starts with door 1. The other two doors either both have goats or one has a goat and the other has a car.
  • Regardless what is behind door 1, the host opens another door with a goat (either door 2 or 3).
  • Contestant switches his or her choice to remaining door - 2 or 3. Calculate percentage of times the contestant wins.
  • With above assumptions, contestant wins if car is behind doors 2 or 3. Contestant looses if car is behind door 1. (At this point it is clear that the contestant will win two thirds of the time since the car will be behind doors 2 or 3 two thirds of the time.)
# Monty Hall function
montyhallsim <- function(runs) {
  outcome <- rep(0,n)
  cardoor <- sample(1:3,n,replace=TRUE)
  return (sum(cardoor!=1)/n)
}

# Initialize number of runs per each simulation
n <- 1000

# Run a single simulation
montyhallsim(n)
## [1] 0.665

Looks pretty close to 2/3. Let us run more simulations.

# Initialize number of simulations
simcount <- 1000

# Run simulations
winper <- replicate(n, montyhallsim(n))

# Plot results
library(ggplot2)
ggplot()+geom_histogram(aes(winper), binwidth=.003)+
  xlab("Winning Percentage")+ylab("")

summary(winper)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.6200  0.6570  0.6670  0.6671  0.6760  0.7140

We can clearly see after 1000 simulations that the probability of winning a car with this stragegy is 2/3 (the mean for this run is 0.667077.

Notes

Here is an interesting, 5-minute video explaining a little more history behind the Monty Hall paradox.