Let’s Make a Deal. Three approaches to solving the Monty... | by Alex Muhr | Towards Data Science

Your heart is pumping heavily. You’re excited and nervous, not quite sure what to expect next. You’ve never been on TV before, but you know the game well. Led by the wily and charismatic Monty Hall, ‘Let’s Make a Deal’ is one of the most popular shows on TV these days. You’ve been a regular viewer since 1965 and seen Monty barter with contestants countless times. Once you watched a woman accept $50 in exchange for an envelope which held $1,000. Another time you watched a man turn down $800 to instead keep a box that was revealed to contain only paper towels! Now it’s your turn.
Monty Hall turns to you. Quickly, like a play by play announcer, “Behind one of these three doors, labeled 1,2, and 3, lies a new Corvette; the other two conceal worthless goats. That’s right, absolutely worthless goats. Now there, which door will you have?”
You hear a muted “BAAAAA”, but you’re not sure where it’s coming from.
“I’ll have door 1,” you say with conviction.
“Excellent choice! As you can see, behind door 3 is a worthless goat.” Door 3 is opened to reveal a black and white spotted billy goat. It’s almost cute, though it is in fact absolutely worthless. The idea of a goat in your city apartment is comical, and it’s not like you would know how to sell a goat either.
Monty continues, “Is door 1 your final choice? You may switch to door 2 but there’s no going back. I’d hate to see make the wrong decision here.”
You ponder the situation. Your initial choice had a 1⁄3 probability of being correct, but you’ve been granted new information and door 3 is now out of the running. The car must be behind either door 1 or 2, but which one? You consider how bummed you would feel if you were to switch from door 1 to door 2, only to find the car behind door 1 all along. Perhaps it’s best to stick to the original choice ... or is it?
The Monty Hall problem is a classic brain teaser that tests one’s ability to reason with conditional probabilities. In addition, the premise of the problem adds a psychological edge; many choose to stay with the original door only to avoid the disappointment of switching away from the car. In this article, I will demonstrate 3 methods to determine the optimal strategy, stay or switch.
It’s relatively easy to simulate the Monty Hall game many times over and record how many times the game is won vs lost. To be statistically rigorous, one can treat each attempt at the game as a Bernoulli trial (a random experiment with binary outcome) with probability of success is P. By the law of large numbers, a win rate of 100*P percent is expected if the number of experiments, N, is very large. By recording the outcome of many trials for both the stay and switch strategies, a z-test can be used to determine whether there is a statistically significant difference in win rates.
The python code below defines a function monty_hall that simulates the game. The keyword argument switch controls whether a stay or switch strategy is employed. Further, setting the keyword argument verbose = True allows one to play the game in an interactive manner and choose on the spot whether to stay or switch.
import numpy as npdef monty_hall(chosen_door, switch = False, verbose = False):    '''    Parameters    ----------    chosen_door : Initial choice of door, must be either 1, 2, or 3.    switch : False --> stay strategy             True --> switch strategy.    verbose: False --> no input required             True --> player is asked if they'd like to switch                      doors. overrides switch input.        Returns    ------    1 : if the game is won    0 : if the game is lost    '''        # Correct indexing for chosen door    chosen_door += -1         # Randomly initialize array to represent the 3 doors    # Array contains two 0s for goats and a single 1 for the car        doors = [0, 0, 0]    doors[np.random.randint(0,3)] = 1        # Reveal a door concealing a Goat            revealed_door = np.random.choice([i for i, x in enumerate(doors) if x == 0 and i != chosen_door])    switch_door = list({0, 1, 2} - {chosen_door, revealed_door})[0]        # If verbose == True ask the player if they'd like to switch.        input_string = f"You've chosen door {chosen_door + 1}, an excellent choice! As you can see behind door {revealed_door + 1} is a worthless goat. Is door {chosen_door + 1} your final choice? You may switch to door {switch_door + 1} but there's no going back. Enter y if you'd like to switch."        if verbose == True:        choice = input(input_string)        if choice == 'y':            switch == True        else:            switch == False            # Return the result of the game        if switch == False:        result = doors[chosen_door]    else:        result = doors[switch_door]            if verbose == True:        if result == 0:            print("How unfortunate, you've chosen a worthless goat!")        if result == 1:            print("Congratulations, you've just won a new Corvette!")                return result
Let’s use this function to play the game 100,000 times for both the stay and switch strategies. The results will be saved in the stay and switch lists and the win rate for each strategy will be printed.
i = 0stay = []switch = []while i < 100000:    stay.append(monty_hall(np.random.randint(1, 4)))    switch.append(monty_hall(np.random.randint(1, 4), switch = True))    i += 1    print(f"Stay win rate: {round(100 * np.sum(stay) / 100000, 1)}%")print(f"Switch win rate: {round(100 * np.sum(switch) / 100000, 1)}%")
Stay win rate: 33.3%
Switch win rate: 66.6%
Clearly there is a very large difference between the two strategies. Although a statistical test isn’t really needed to detect a difference here, let’s apply a two-tailed z-test to our data anyway.
from statsmodels.stats.weightstats import ztesttest_statistic, p_value = ztest(stay, switch)print(f"Test Statistic: {round(test_statistic, 1)}")print(f"P-Value: {round(p_value, 5)}")
Test Statistic: -157.5
P-Value: 0.0
The z-test has returned a test statistic of -157.5 and a p-value of 0 (really an infinitesimally small number that our computer has rounded to 0). Therefore the null hypothesis, that the win rate of stay and switch strategies are the same, can be rejected with complete certainty. Switching has been revealed to be the obviously superior strategy!
But why is switching so much better? To bet a better sense let’s look at the problem from a Bayesian point of view.
Bayesian statistics, as compared to frequentist statistics, offers an alternative perspective on cause and effect. Frequentist statistics seek to answer the question, “given my hypothesis, how probable is my data?” On the other hand, Bayesian statistics seek to answer the question, “given my data, how probable is my hypothesis?” The difference is subtle yet powerful.
The cornerstone of Bayesian statistics is Bayes’ Rule, a simple identity that informs how to properly update belief in a hypothesis given new data.
P(H|D) = P(D|H)*P(H) / P(D)
H = The hypothesis, D = New observed data
P(H|D) is the probability of our hypothesis given the new observed data. P(D|H) is the probability of observing the data given that the hypothesis is correct. P(H) is our assessment of the probability of the hypothesis being correct before observing the new data, also known as a “Bayesian prior”. P(D) is the probability of observing the data under any circumstances whether or not the hypothesis is true.
For the Monty Hall problem there are two possible hypotheses: H1) the car is behind the initially chosen door, and H2) the car is not behind the initially chosen door and switching will result in a win. For now let’s consider H1 and see how Baye’s rule can be used to determine the probability of this hypothesis.
First, consider the prior probability of the hypothesis P(H1). Remember that the initial choice is made from 3 options that are all equally likely to be correct. P(H1), the probability of making an initial correct choice, is simply 1/3.
Next, consider the probability of observing the new data given that H1 is correct. If the car is in fact behind the originally chosen door, then the probability of a goat being revealed at either of the other doors is 1/2, as Monty will choose randomly between his two options. P(D|H1) = 1/2.
Finally, consider the probability of observing the new data under any circumstances. Because there are only two mutually exclusive hypotheses possible, P(D) can be determined using the following equation, P(D) = P(D|H1)P(H1) + P(D|H2)P(H2). We already know that P(D|H1) = 1/2 and P(H1) = 1/3. P(H2) = 2/3 and is simply the probability of making an initially incorrect choice. Lastly, P(D|H2) = 1/2, since if our initial door conceals a goat the remaining two doors both have equal probabilities of concealing the second goat. In summation P(D) = 1/2 * 1/3 + 1/2 * 2/3 = 1/2.
Altogether we have: P(H1) = 1/3, P(H2) = 2/3, P(D|H1) = 1/2, P(D|H2) = 1/2, and P(D) = 1/2
These values can now be inserted into Bayes’ rule to determine the probability that the car is behind our initially chosen door.
P(H1|D) = P(D|H1) * P(H1) / P(D) = (1/2 * 1/3) / (1/2) = 1/3
It would appear that the new information has not increased the probability of the initial choice being correct at all!
While we’re at it, let’s also use Bayes’ rule to determine the probability of H2, that the car is not behind our initial choice and that a switch will result in a win.
P(H2|D) = P(D|H2)*P(H2) / P(D) = (1/2 * 2/3) / (1/2) = 2/3
We’ve now used Bayesian reasoning to confirm the results of our Frequentist experiment. Further, the Bayesian probabilities determined for H1 and H2 correspond exactly to the win rates observed! By now it’s becoming clearer as to why switching is so much better than staying, but let’s look at the problem in one last way to really drive the point home.
The Monty Hall problem is certainly deceptive, but it’s not especially complex. By thinking through all the possible outcomes one can conclude that switching is the superior strategy rather quickly.
Consider the circumstances under which the switching strategy will either win or lose. Switching will lose when the initial door conceals the car. When the initial door conceals a goat, Monty will be forced to reveal the second goat, the remaining door will conceal the car, and switching will always win. As the initial choice will only conceal the car in 1 of 3 games, the switching strategy will lose approximately 1 in 3 games and win the other 2. Conversely, staying will only win the 1 in 3 games where the initial choice happens to be correct.
Monty shoots an impatient look, “Any day now buddy. I’m beginning to think I should give that new Corvette to the goats!”
“BAAAAA” the black and white billy goat seems to agree with Monty’s sentiment.
“I’ll ask one last time, would you like to switch to door number 2?”
In a rush of epiphany you recall an article you read on this exact topic. Though you can’t be certain, you understand the odds associated with your choices. Resisting the temptation to stick to your guns, and accepting the possible embarrassment of self inflicted misfortune, you calmly announce “I’ll switch to door 2.”
For an even deeper dive into the Monty Hall problem, its history, psychology, and many variations, check out The Monty Hall Problem by Jason Rosenhouse.