<- function(pick, switch) {
monty_hall <- FALSE
interactive if(missing(pick)) {
<- TRUE
interactive cat('Pick your door:')
<- LETTERS[menu(c('A', 'B', 'C'))]
pick else {
} if(!pick %in% LETTERS[1:3]) {
stop('pick must be either A, B, or C')
}
}<- c('win', 'lose', 'lose')
doors <- sample(doors) # Shuffle the doors
doors names(doors) <- LETTERS[1:3]
if(doors[pick] == 'win') {
<- sample(names(doors[!names(doors) %in% pick]), size = 1)
show else {
} <- doors[!names(doors) %in% pick] == 'lose'
show <- names(which(show == TRUE))
show
}if(missing(switch)) {
<- TRUE
interactive cat(paste0('Showing door ', show, '. Do you want to switch your choice?'))
switch <- menu(c('yes', 'no')) == 1
}if(switch) {
<- names(doors)[!names(doors) %in% c(show, pick)]
pick
}<- unname(doors[pick] == 'win')
win if(interactive) {
if(win) {
cat('You win!')
else {
} cat('Sorry, you lost.')
}invisible(win)
else {
} return(win)
} }
Simulating Monty Hall’s Problem
I find that when teaching statistics (and probability) it is often helpful to simulate data first in order to get an understanding of the problem. The Monty Hall problem recently came up in a class so I implemented a function to play the game.
The Monty Hall problem results from a game show, Let’s Make a Deal, hosted by Monty Hall. In this game, the player picks one of three doors. Behind one is a car, the other two are goats. After picking a door the player is shown the contents of one of the other two doors, which because the host knows the contents, is a goat. The question to the player: Do you switch your choice?
For more information, be sure to see the Wikipedia article.
Below we implement a function that will simulate a single play of this game. You can play interactively, or if you specify the pick
and switch
parameters this can be looped in order to simulate the results.
We can play a single game:
monty_hall()
Pick your door:
1: A
2: B
3: C
Selection: 2
Showing door A. Do you want to switch your choice?
1: yes
2: no
Selection: 1
You win!
Let’s now simulate 1,000 games. We will use two vectors, mh_switch
and mh_no_switch
, to store the results after switching doors or not, respectively. For each iteration, the initial door pick is randomly selected.
<- 1000
n_games <- logical(n_games)
mh_switch <- logical(n_games)
mh_no_switch for(i in 1:n_games) {
<- sample(LETTERS[1:3], size = 1)
pick <- monty_hall(pick = pick, switch = TRUE)
mh_switch[i] <- monty_hall(pick = pick, switch = FALSE)
mh_no_switch[i] }
The probability of winning if we switch the door is:
mean(mh_switch)
[1] 0.671
The probability of winning if we do not switch the door is:
mean(mh_no_switch)
[1] 0.328
It should be noted that the theoretical probability of winning if you switch is 2/3, and is 1/3 if you don’t switch.