# Monty Hall in Monte Carlo

The Monty Hall problem, named after the host of the Let’s Make a Deal game show, first appeared in Marilyn vos Savant‘s column in Parade magazine in 1990, although people still vigorously debate the answer, as discussed here and here.

The basic premise is that you must choose one of three doors, one of which contains a prize. Once you have selected a door, Monty Hall reveals the contents of one of the doors you did not select, usually containing something like a goat. He then asks if you would like to switch your choice to the remaining unopened door. The question is, will switching doors increase you chances of winning, decrease your chances, or have no effect at all?

One thing to keep in mind, is that when Monty Hall shows you what’s behind one of the doors you didn’t select, he doesn’t ever reveal the door containing the prize. This fact should change your decision making process.

We can answer the question of which strategy is best by calculating the conditional probabilities of winning given you either switch or stay by performing a Monte Carlo simulation. The conditional probability of winning given you decide to switch doors can be written as `Prob(win | switch)`, and is equal to the probability that you switch and win divided by the probability that you switch,

``Prob(win | switch) = Prob(switch & win)/Prob(switch)``

and the conditional probability of winning given you decide to stay with your original door is equal to the probability you stay and win divided by the probability that you stay.

``Prob(win | stay) = Prob(stay & win)/Prob(stay)``

In this simulation, we will generate a bunch of initial guesses, a bunch of prize locations, and a bunch of switch decisions and then use them to calculate the probabilities. First, load the necessary libraries.

``(use '(incanter core stats charts))``

Set a simulation sample size, generate samples of initial-guesses, prize-doors, and switch decisions with the sample function. The probability that the prize is behind any of the three doors is 1/3, as is the probability that you select a particular door as an initial guess, while the probability that you switch doors is 1/2.

``````(def n 10000)
(def initial-guesses (sample [1 2 3] :size n))
(def prize-doors (sample [1 2 3] :size n))
(def switches (sample [true false] :size n))``````

Define a function that returns one if a switch decision results in winning, a zero otherwise.

``````(defn switch-win? [initial-guess switch prize-door]
(if (and switch (not= initial-guess prize-door)) 1 0))``````

Calculate the joint probability of winning and switching, by mapping the switch-win? function over the generated data, summing the occurrences of winning after switching, and then dividing this number by the total number of samples, n.

``````(def prob-switch-win (/ (sum (map switch-win?
initial-guesses
switches
prize-doors))
n))``````

Calculate the probability of switching doors. Use the indicator and sum functions to calculate the frequency of switches, and then divide by the total number of samples.

``(def prob-switch (/ (sum (indicator true? switches)) n))``

Finally, use the formula above to calculate the conditional probability of winning given a switch.

``(def prob-win-given-switch (/ prob-switch-win prob-switch))``

Now repeat the process to calculate the conditional probability of winning given the decision to stay with your original choice. First, define a function that returns a one if a stay decision results in winning, zero otherwise.

``````(defn stay-win? [initial-guess switch prize-door]
(if (and (not switch) (= initial-guess prize-door)) 1 0))``````

Next, calculate the joint probability of winning and staying,

``````(def prob-stay-win (/ (sum (map stay-win?
initial-guesses
switches
prize-doors))
n))``````

and, calculate the probability of staying with the original door.

``(def prob-stay (/ (sum (indicator false? switches)) n))``

Finally, calculate the conditional probability of winning given you stay with the original door

``(def prob-win-given-stay (/ prob-stay-win prob-stay))``

View the results with the pie-chart function.

``````(view (pie-chart ["Switch" "Stay"]
[prob-win-given-switch prob-win-given-stay]))`````` The conditional probability of winning given you switch doors is approximately 0.66, while the probability of winning given you stay with your original choice is approximately 0.33.

To some this outcome may seem non-intuitive. Here’s why it’s the case: the probability that the prize is behind any of the three doors is 1/3, so once you have selected a door, the probability that the prize is behind a door you didn’t select is 2/3. Since Monty Hall knows which door has the prize, and never selects it as the door to reveal, all of the 2/3 probability belongs to the other unselected door. So switching will give you a 2/3 probability of winning the prize, and sticking with your original decision only gives you the original 1/3 chance.

The complete code for this example can be found here.

### 4 responses to “Monty Hall in Monte Carlo”

1. Pickle Pumpers

That’s completely wrong but don’t feel bad you in some pretty good company.

I’ve written a proof using a simulation instead of the flawed logic behind those charts.

Basically it comes down to the third state. At the third state you try to say that the host can now CHANGE the door he initially picked. Um, NO!

Here’s the proof with a program and source code.

http://picklepumpers.com/wordpress/?p=130

• incanter

That’s correct, the host can’t change the door he initially placed the prize, but he can change the door he reveals based on what you choose. If you choose the door with the prize, he can choose either of the remaining doors. If you choose the door without the prize, he must choose the other door without the prize. That is the key, but since people have debated this for nearly 20 years, I’m sure we won’t convince each other :)

2. Steve Gilardi

Whether your first choice has the prize or not is unaffected by whatever happens after. Your first choice will have the prize 1/3 of the time.

Never switch policy: The probability of getting the prize is the probability that your first choice is right: 1/3

Always switch policy: The probability of getting the prize is the probability that your first choice is wrong: 2/3

3. Joe Gallo

The way I’ve explained it to people is to imagine 10,000 doors, and you pick one, and then the host runs through the other 9,999 except for number 5,521 (of which he says, “uh… i’m just going to skip this one…”).

Do you still think there’s a 50/50 chance you are right, after he’s opened all the doors except the one you chose and door 5521?

:)