As part of my ongoing attempt to learn the

R language, I decided to try to generate a data set to analyze in R using R itself. I wrote a little R script which generates simulated data for a thousand trials of the "Monty Hall" problem. The script is

here, along with other

R examples.

If you source the script, a data frame

**MontyHall** will be populated. The data is re-generated each time you source the script, so if you want to capture a single run, you should export the data and work from that (a saved example run is

here, and contains the data I used in this post).

*In the classical Monty Hall problem, a game show host, named Monty Hall, presents a contestant with three (3) doors. Behind one of the doors is a valuable prize, traditionally a car, while behind the others are dud prizes, traditionally goats. A game proceeds with the contestant selecting a door and keeping the prize that lies behind it (hoping for the actual prize, not a dud). However, instead of opening the selected door, Monty opens one of the other two doors, revealing a goat, and asks if the contestant would like to switch and select the remaining door instead. The problem asks which choice has the higher probability of revealing the actual prize, or whether both options have the same probability of achieving the hoped-for outcome.*

In this simulation, we have the contestant choosing to switch half of the time, hopefully generating enough cases so that the experimental probabilities (from the simulation) match up with the theoretical probabilities that we can come up with by analyzing the problem. (You can increase the number of trials by modifying the script).

The simulation gives us a data set that looks like this.

Our simulation has the probability of switching set to 1/2, as if we were basing our decision on the flip of a fair coin.

We can see that our simulation is pretty close:

We would expect that the probability of the contestant selecting the winning door right away would be 1/3, and this is what we see in the simulation.

All that this tells us is that R is generating random numbers in a reasonable enough way for our purposes. What makes the Monty Hall problem interesting are the probabilities of winning when the contestant switches doors, compared to when they don't switch.

If the contestant does not switch, their chance of winning is the same as their chance of selecting the prize right away, which is just 1/3.

This is not the result that some people expect (some argue that the chance of winning when staying with the original selection should be 1/2), but the simulation agrees:

When the contestant makes their initial selection , they have either won or lost. Switching amounts to trading a win for a loss: if the contestant has picked the winner, they have now switched away from it; if they have picked a looser (one of the goats), when Monty opens the other door (revealing the other goat), switching will cause them to win. So, switching amounts to trading a win for a loss or a loss for a win. Because the probability of losing on the initial door selection is 2/3, it is a good idea to switch: the first choice was likely a goat.

And the simulation agrees:

What about if we pursue the strategy of the imaginary contestant in our simulation, and flip a coin to decide whether we switch doors or stay with the original choice? The law of total probability tells us that we have a probability of winning equal to 1/2.

And in the simulation we come pretty close:

Monty *n*

While writing up the R simulation, I wondered what would happen if the Monty Hall problem was generalized to

*n* doors so that Monty could keep on offering you more chances to switch. There are likely many ways of doing this, so I picked what I think is a simple generalization:

Suppose that the contestant has *n* doors to choose from where *n* > 2. There is still only one (1) car (and *n-1* goats). After a door is selected, Monty will always open a remaining door and provide the contestant with a chance to switch doors, as long as there remain unopened doors that have not been previously selected by the contestant. Monty will not open doors that were previously chosen, nor will he give the contestant the opportunity to switch back to a previously chosen door.

I've put a short write up of this on

*ShareLatex* here. At least in the way that I've framed the problem, there is a nice formula for the probability of winning on

*n* doors if you switch

*k* times:

Or if you prefer a direct formula instead of recursive:

If you work out some values using your preferred version of the formula, you should get results that agree with these:

From this, or from the formulas themselves, you can see that the probability of winning keeps increasing as long as you keep switching doors as long as Monty gives you the option to.

I did not simulate the

*n* > 3 case using R, but I did write a general simulation using Java (a listing for this is included in the write up

here), and found that the simulation results agreed the the theoretical probabilities pretty closely (I have not carried out a detailed analysis of the results).