The Monty Hall Problem Solved
By: J.P. Rinfret
Tags: Python, Data Science, Data Visualization, Probability, Monty Hall

Using Python to generate ten-thousand random simulations of the classic game show.

Photo by Mitchell Luo on Unsplash

I was recently watching the movie 21 and came across the famous scene in which the main character Ben, played by Jim Sturgess, explains the classic Monty Hall problem to his MIT professor and soon to be card counting couch Micky Rosa. The scene can be found here.

The Problem

You are a contestant on a game show. On the stage in front of you are three doors. Behind two of the doors are goats, behind the remaining door is a Ferrari.

At the start of the show, the devilishly handsome host asks you to pick a door (let’s say you pick door 2). The host then opens one of the other doors (1 or 3) and, unsurprisingly, reveals a goat. Since the host knows the location of the Ferrari a goat is revealed at this point every single round. The host then asks you if you would like to stay with your original pick (door 2) or switch to the remaining door.

Is it in your best interest to stay with your original guess or to switch? Is the host using reverse psychology to influence your guess (since he knows the location). Do you even care?

The Solution

Like all good probability questions, we should first think about it logically. A lot of people (incorrectly) assume that you have a 50/50 chance of winning because technically you are randomly picking one out of the two remaining doors in the second round (the door you picked and the door the host did not reveal). However, I prefer a more disciplined approach; let’s examine the probabilities of winning for each approach (switch vs. stay) and see if we can get a probability greater than 50% to render the 50/50 argument once and for all useless.

What are the odds of winning at the onset of the game show, if you stayed with your original pick of door 2? Well, since there are two goats and one Ferrari (and I am assuming that walking away with a Ferrari over a goat is considering winning — unless of course paying taxes on a brand new Ferrari does not excite you) the probability of winning is 1/3 and losing is 2/3.

Now what if we switched every single time you were given the chance? In order to win, you would first have to pick a goat and then switch your pick to the Ferrari (if you switched away from the Ferrari you would obviously lose). In other words, if you would were to pick a goat at the onset you would switch to the Ferrari and win (remember the host always eliminates the other goat so you can never switch from one goat to the other goat!). So by this logic, what is the probability of originally picking a goat? That is right, 2/3.

Okay great, we have logically reasoned that the probability of winning if you were to switch is 2/3, which is greater than the probability of winning if you were to stay with your original guess each time (1/3).

Does this logic hold if we were to simulate multiple random environments where we either switch or stay? Let’s write some code to find out.

The Code

First, we will use Numpy to generate the random environments (or trials) and guesses and Matplotlib and Seaborn to visualize the results.

Next, we will create stay and switch containers. These lists will hold 1s and 0s depending on a win or loss, respectively. Also, let’s create our doors while we are at it.

Now for the fun part.

For each “environment”, we will consider ten-thousand of them, we need to randomly place the Ferrari (“ferrari_door”) and randomly assign an original guess for the contestant (“contestant_door”). Note, we do not need to randomly assign the goats because the goats are automatically behind the two doors that the Ferrari is not (“goat_doors”).

Next, we need a list of doors that the host could choose from for his big reveal (“possible_reveal_doors”). We also know that the ferrari_door is not one of the elements of possible_reveal_door (since the host will never reveal the Ferrari’s location). Possible_reveal_doors can be of length 2, option A, or length 1, option B. For option A to occur, the contestant_door must equal the ferrari_door. Since which door the host reveals (“reveal_door”) ultimately doesn’t matter, as long as it is a goat, we will randomly select one of the doors for the host to reveal. For option B to occur, the contestant_door must be one of the original goat_doors (because you cannot switch to a goat since the host will reveal the remaining goat). Since there is only one element in this list, we grab it.

Finally, let’s define a “switch_door,” which represents the remaining door (not the original contestant_door and not the reveal_door). This switch_door is defined for each environment regardless of whether or not our strategy is to switch or stay.

So now let’s focus in on our strategy to win the game show. If we blindly switch every environment and the switch_door equals the ferrari_door, then we win. Each time this happens we will append a 1 to the switch container and a 0 to the stay container. If we blindly stay every environment and the switch_door does not equal the ferrari_door, then we lose. Each time this happens we will append a 0 to the switch container and a 1 to the stay container. Once we cycle through each environment, we will have a switch container with ten-thousand elements and a stay container with ten-thousand elements.

Let’s examine the code for this:

Now we must calculate the probability of winning for each strategy. This is simply (Number of Wins)/(Total Environments). Since every time each strategy won we appended a 1 (and every time it lost we appended a 0) to the respective container, the above numerator equals the sum of each list. So the probability of winning equals sum(container)/len(container) or simply the mean value of each list. That code is below:

As you can see, the probability of Switch winning equals 66.68% (about 2/3) and the probability of Stay winning equals 33.32% (about 1/3)

The Visualization

The below block of code attempts to plot the probability of winning after each consecutive environment. This probability rapidly approaches our theoretical probability of 2/3 for Switch and 1/3 for Stay (less than 500 trials).

The Summary

As you can see, we have simulated an environment where we randomly assigned the Ferrari behind one of three doors, had our contestant randomly select one of the three doors, had the host reveal a door hiding a goat (and never a Ferrari), and then assigned a 0 or 1 to the appropriate container depending on whether or not the switch strategy would have won or the stay strategy would have won. We then did this ten-thousand times and then calculated the probability of winning if we consistently stayed or switched.

So there you have it. If you are disciplined in your approach and switch your guess every single time, your probability of winning rapidly approaches 2/3.

Please visit my GitHub for the full jupyter notebook. As always, I welcome your comments or questions.