The Monty Hall problem
[This article was first published on The Beginner Programmer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The Monty Hall problem is a famous game which was played in the television show “Let’s make a deal”.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The game goes like this:
There are three doors, behind each door there is either a goat or an amazing sportcar. The contestant wins if they guess where the car is. There are in total 2 goats and the car. Therefore the initial chance of choosing the car is 1/3.
The host asks the contestant to pick a door. Once a door has been chose, the host, who knows where the goats and the car are, opens a door behind which there is a goat and asks the contestant if they want to switch door.
Now the question is: should the contestant change door or should they stay? Is there any statistical reason which could justify either choice?
It might not be that immediate to understand, however the optimal strategy is to change door. In this case, the probability of winning the car increases from 1/3 to 2/3. You can check this by analysing the favourable scenarios over the possible scenarios. However, should we stick with this or should we make a simulation to test this statement out? Let’s go for the simulation with R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------------------------------------------------------------------- | |
# Monty Hall game SIMULATION | |
# This function generates 3 random doors where 2 are set equal to 0 (the goats) and | |
# 1 is set equal to 1 (the car) | |
generate_random_3_door <- function() | |
{ | |
sampl = c(0,1) | |
probvec = c(.5,.5) | |
a <- sample(sampl,1,prob=probvec) | |
b <- sample(sampl,1,prob=probvec) | |
c <-sample(sampl,1,prob=probvec) | |
if((a|b|c==1) & (a&b)!=1 & (b&c)!=1 & (c&a)!=1) | |
{ | |
vect <- c(a,b,c) | |
return(vect) | |
}else | |
{ | |
return(generate_random_3_doors()) | |
} | |
} | |
# Library needed to plot the pie chart | |
library(plotrix) | |
# This function simulates n games both changing and not changing door | |
trialswsw <- function(n) | |
{ | |
# We generate our sample matrix | |
first <- c(1,0,0) | |
doors <- matrix(first,ncol=3) | |
for(j in 1:n){ | |
doors <- rbind(doors,generate_random_3_doors()) | |
} | |
#View(doors) | |
# Choices matrix, choice 1: no change, choice 2: change | |
choices <- matrix(ncol=2) | |
for(i in 1:nrow(doors)) | |
{ | |
# Since there's no reason to pick a particular door, we always choose the first | |
choice1 <- doors[i,1] | |
# Now Monty gets rid of one of the remaining wrong doors (either number 2 or 3) | |
if((doors[i,2] == 0) & (doors[i,3] == 0)) | |
{ | |
doors[i,2] <- NA | |
} | |
else if(doors[i,2] == 0) | |
{ | |
doors[i,2] <- NA | |
}else if(doors[i,3] == 0) | |
{ | |
doors[i,3] <- NA | |
} | |
# We switch door, while being careful not to choose the open one! (NA) ;) | |
if(is.na(doors[i,2])){ | |
choice2 <- doors[i,3] | |
}else{ | |
choice2 <- doors[i,2] | |
} | |
# This vector contains the choice | |
choicevec = c(choice1,choice2) | |
# we add each vector (game) to the result matrix | |
choices <- rbind(choices,choicevec) | |
} | |
# A small adjustment, not relevant if n is big enough | |
choices[1,]= c(1,1) | |
# We calculate relative frequencies | |
freqrelchoice1 = sum(choices[,1])/nrow(choices) | |
freqrelchoice2 = sum(choices[,2])/nrow(choices) | |
data = c(freqrelchoice1,freqrelchoice2) | |
labels = c("% win without changing","% win changing") | |
names(data) = labels | |
# View(scelte) | |
pie3D(data,labels=labels,explode=0.1,main="Monty Hall % chance of winning") | |
return(data) | |
} | |
# Let's try our function and go for the simulation | |
generate_random_3_doors() | |
# we test our random doors generator | |
trials(10000) # Please ignore this function which I have integrated in the trialswsw() | |
trialswsw(10000) # We simulate 10000 games |
The simulation confirms that, on the long run, the odds are more favourable if the contestant decides to switch door. Hope this was useful and interesting.
More on the Monty Hall problem: http://en.wikipedia.org/wiki/Monty_Hall_problem
To leave a comment for the author, please follow the link and comment on their blog: The Beginner Programmer.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.