Solving the Monty-Hall problem with Monte-Carlo Simulation

The Monty Hall problem is a classic probability puzzle that continues to intrigue mathematicians, statisticians, and now, machine learning enthusiasts. Here, we'll use a Monte Carlo simulation for verification.

Imagine you're on a game show facing three doors. Behind one door is a car, and behind the other two, goats. After you select a door, the host, who knows what's behind them, opens another door, revealing a goat. Now, you're given a choice: stick with your initial pick or switch to the other unopened door. What would you do? Intuition might say your odds are 50/50, but the puzzle's solution is far more intriguing: you should always switch.

The reason lies in probability. Your initial choice has a 1/3 chance of being correct. When the host opens a door with a goat, switching doors gives you a 2/3 chance of winning the car, as it essentially consolidates the probability of both remaining doors.

To empirically verify the Monty Hall problem's solution, we use a Monte Carlo simulation, which involves running numerous trials to observe outcomes and estimate probabilities.

The Monte Carlo simulation for the Monty Hall problem is straightforward but powerful. The process is as follows:

  1. Initialize Counters: Set up counters to track the number of wins when sticking with the initial choice and when switching.
  2. Run Trials: For each trial:
  3. Calculate Probabilities: After all trials are completed, calculate the winning probabilities for both sticking and switching by dividing the number of wins by the total number of trials (relative frequency).

Python Code for Monte Carlo Simulation:

import numpy as np

win_switch = 0
win_stick = 0
trials = 100_000  # simulate 100,000 Monte Carlo iterations

for _ in range(trials):
    car_position = np.random.randint(1, 4)
    choice = np.random.randint(1, 4)

    # The host reveals a goat
    revealed = next(x for x in range(1, 4) if x != choice and x != car_position)

    # Switch choice
    switch_choice = next(x for x in range(1, 4) if x != choice and x != revealed)

    win_switch += switch_choice == car_position
    win_stick += choice == car_position

print(f"Winning by switching: {win_switch / trials:.4f}")
print(f"Winning by sticking: {win_stick / trials:.4f}")

Output:

Winning by switching: 0.6656
Winning by sticking: 0.3344

Conclusion:

In conclusion, the Monty Hall problem demonstrates how our intuition about probability can often lead us astray. By employing a Monte Carlo simulation, we empirically verified that switching doors after the host reveals a goat leads to a significantly higher chance of winning the car, with a probability of approximately 2/3. This simulation provides a powerful tool for understanding and validating complex probability problems.

The key takeaways from this tutorial are:

  1. Intuition can be misleading when dealing with probability problems. It's essential to analyze the problem systematically and consider all possible outcomes.
  2. The Monty Hall problem highlights the importance of conditional probability. The host's action of revealing a goat changes the probabilities of the remaining doors, making switching the optimal strategy.
  3. Monte Carlo simulations offer a practical approach to estimating probabilities and verifying solutions to complex problems. By running a large number of trials and observing the outcomes, we can gain insights into the underlying probabilities.