Tackling the Monty Hall Problem with a Monte Carlo Simulation in Python

You probably know what the Monty Hall problem is, so I will skip its description and direct those unfamiliar with the problem to the Monty Hall problem’s Wikipedia page. This post is about a way to use a Monte Carlo simulation to convince yourself or another that you should indeed switch doors when playing the game. First, it should be noted that a simple application of Bayes Theorem can illustrate why you should switch doors. Also, you can simply talk through how the game plays out under the two main strategies (always switching vs. never switching) to discover which strategy is superior–for example, consider that when you always switch, and the car is behind door 3, you will win when you select doors 1 and 2 but will not win when you select door 3 (win rate of 2/3).

In any case, a third way to discover the optimal game-playing strategy is a Monte Carlo simulation. Such simulations are useful in a variety of contexts (from nuclear physics to economics) and can be simple enough to program up in under an hour. For the Monty Hall problem, a simulation can do the following and be successful:

  1. Ask the user to choose a switching strategy.
  2. For a large number (N) of iterations, artificially play the game using the user-selected strategy, and random numbers to determine the game’s outcome:
    1. Place the car behind a random door, call this door A.
    2. Make the game-show contestant choose a random door, call this door B (it’s possible that B=A).
    3. Make the game-show host reveal a random door that is not A and is not B, call this door C. In other words, we keep assigning random doors to C until it’s true that C!=A, and C!=B.
    4. If the strategy is to always switch, then make the game-show contestant choose a new door D such that D!=B and D!=C.
    5. If the strategy is to never switch, set D=B.
    6. If D==A, add 1 to the number of successes.
  3. Calculate the strategy’s win rate as the number of successes divided by N.

I wrote a program that carries out such a simulation and reveals which strategy is optimal for the Monty Hall problem. You can find that code here: https://github.com/bbartoldson/examples/blob/master/Monty%20Hall%20Simulation/Monty_Hall_Simulation.py. Note that the Monte Carlo method has 1/sqrt(N) convergence. This means that you can increase N from 10,000 to 1,000,000, a factor of 100, and the error will be reduced to 10% of its prior value. This is a relatively slow convergence rate as far as numerical methods go. So you can increase N if you want a more precise result, but remember that the program takes time to run, and this time is at least linearly related to N.

If you needed this post to convince you to always switch, you are not alone. According to the Monty Hall problem Wikipedia page linked above, the famously prolific mathematician Paul Erdős didn’t believe the solution to the Monty Hall problem was “always switch” until he saw a simulation like the one I just described.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s