Monty Hall problem - a small simulation in R
By Dirk Duellmann
The Monty Hall problem is an interesting example for how much intuition can mislead us in some statistical contexts. Even more disturbing though is, for how long we are prepared to debate and defend an expected result before actually checking our initial guesses using a simple Monte Carlo simulation.
Here is simple simulation implementation the Monty Hall game show problem:
In the TV show “Let’s Make a Deal” the host Monty Hall would offer to game participant the choice of three doors. One of them was hiding a valuable price (eg a car) - behind the other two doors were only two less desirable goats.
After the participant has choose a door, which is still kept closed, Monty would open one of the other doors showing that it did not hide the price. After that door was open the participant was asked if he/she wanted to maintain their initial choice or rather change their mind and now swap to the other of the two still closed doors.
It turns out, that swapping the initial choice of door is in fact a better strategy in this game, which is considered by many counter-intuitive. The public discussion about this problem in a print magazine revealed a lot about the excitement and sometime rather unscientific culture when debating a “scientific” question via public media. And twitter was not even available.
Here a small Monte Carlo toy in R to compare between the two game strategies. It uses mainly two features:
- the
sample()
function to pick randomly – here from a vector of doors. - vectors indexed with negative indices: evaluate to all but the “subtracted” elements
doors <- 1:3 # 3 doors named 1 to 3
N <- 1000 # we play N times
swap_wins <- 0 # set win counter to 0
for (i in 1:N) {
price <- sample(doors,1) # randomly pick a door for the price
player <- sample(doors,1) # player randomly picks a door
monty <- sample(doors[-c(player,price)],1) # monty picks from remaining doors
# eg price and player doors removed
swap_wins <- (price != player) + swap_wins # did swap strategy win this time?
} # (price was not behind initial choice)
message("fraction of swap wins: f",round(swap_wins/N,3))
The result of around 0.66 confirms that indeed swapping the door after Monty has opened another one is a better strategy than sticking to the initial choice, which amounts only to 1/3 probability of success.
Such a simulation is of course neither the only nor the most elegant way to arrive at this result. But in case of doubt it looks definitely easier to run a quick test than to embark in a public discussions. At the time this involved the exchange of 10,000 reader letters in the US and proportinal excitement in similar thread in Germany. For more details check out the wikipedia article here or a small book about this topic (in German) here.
PS: Did you notice that the simulation program does not actually use the line that determines Monty’s pick? A hint to an analytical solution.