Applying Bayes Theorem: Simulating the Monty Hall Problem with Python

NickDoesData
4 min readJan 10, 2017

--

The Monty Hall problem was first featured on the classic game show “Let’s make a Deal”. In the final segment of the show, contestants were presented with a choice of three different doors. Behind two of the doors would be a goat, and behind the third would be an extravagant prize such as a car.

The contestant begins the game by picking one door. The host, Monty Hall, would then open one of the remaining doors. Monty *never* opens the door which contained the car. The door Monty opens always contains a goat. At this point, the contestant was presented with a decision:

1) Open the originally selected door
2) Switch to the other unopened door

In this post, I will examine the theoretical probability of each selection, and then I will use Python to test and prove the theory.

Examining the Theoretical Probability

After Monty opens the door containing a goat, which of these remaining doors presents a better chance of winning the car?

Before the contestant chooses a door, there is a 1/3 chance they select the door contains a car. After Monty opens a door revealing a goat, there are two doors remaining for a contestant to choose from. One of the two remaining doors contains a car. If this decision is considered in a vacuum, it might appear the contestant has a 50% chance of winning regardless of which door they choose.

Roopam Upadhyay of ucanalytics.com provides the following probability analysis of Bayes’ Theorem as applies to this problem. In his breakdown, it is assumed the contestant has selected Door A.

As previously discussed, there is a 0% chance Monty will open the door which contains the car. In the probability analysis above, this is represented as P(Open B|Car@B). This means the probability the car is behind Door C, or P(Open B|Car@C) is 2/3. By choosing to switch, the contestant is betting the car is behind one of the two doors they did not originally select (2/3), as opposed to the probability the car is behind the originally selected door (1/3).

Using Python to Prove Bayes’ Theorem

To test Bayes’ Theorem on the probability of switching vs. staying, I will use the NumPy (Numerical Python) library to generate random door selections and random door’s containing a car.

The previous code produces random pairs of integers. The first value represents the originally selected door, and the second value is the door which contains the car.

The pair [2,3] represents a scenario in which the contestant has guessed the car is behind Door 2, but the car is actually behind Door 3. We know Monty will never open a door containing a car, so Monty proceeds to open Door 1. If the contestant stays with their original selection (Door 2), they will be sent home with a goat instead of a car.

If a contestant initially selects the winning door, Monty will choose at random between the other two doors. Both doors contain goats, so the door in which he opens is does not matter. This is the only scenario in which a contestant loses by switching doors.

The following function simulates wins and losses for each of the two options. A win is represented with a 1, and a loss with a 0.

This function simulates a single contestant who plays the game. Using Python, we can run a nearly infinite number of simulations to test Bayes’ Theorem. When the simulation was run 100 times, it produced the following results:

While switching doors does not always ensure a win, a contestant is twice as likely to win by switching. As the chart shows, the actual winning percentages become closer to the theoretical probability with each additional simulation.

In the event you find yourself on “Let’s Make a Deal” (currently hosted by Wayne Brady), I highly recommend* you switch doors!

The complete underlying code for this post can be found here.

*Winning results not guaranteed

--

--