I subscribe to a newsletter by Kevin Markham at Data School. The Tuesday Tip #18 (2023.05.30) had something a little different. Here’s what it contained.

This is a “special edition” of Tuesday Tips!
Instead of the usual format, I’m going to pose a coding question and ask YOU to write the solution!


Code Challenge #1: Monty Hall problem

There’s a classic probability puzzle based on the TV game show “Let’s Make a Deal” and named after its host, Monty Hall. Here’s the puzzle:

You are a contestant on a game show. In front of you are three closed doors. Behind one of the doors is a car, and behind the other two doors are goats. Your goal is to pick the door with the car.

The host asks you to choose a door. You tell the host your choice. Instead of telling you whether your choice was correct, the host (who knows which door contains the car) opens one of the two doors you didn’t choose and reveals a goat.

You now have the opportunity to keep your original choice or switch your choice to the door that is still closed. Which should you choose?

For example, let’s pretend that you started by choosing door #1. The host opens door #3 to reveal that it contains a goat. Should you keep your original choice of door #1 or switch your choice to door #2?

Use simulations for problem-solving

One of the “superpowers” of being able to write code is that you can use simulations in order to solve problems like these! In this challenge, I want you to write Python code to simulate this problem.

Specifically, I want you to simulate that you are a contestant on this show 1000 times. Each time, you pick a random door as your first choice, let the host open a door that reveals a goat, and then switch your choice to the door that the host didn’t open. With that strategy (known as the “always switch” strategy), how often do you win the car?

Here are a couple of details that I want to be clear about:

  • Before starting each game, the host randomly selects which door contains the car.
  • After you make your initial choice, the host always opens one door that contains a goat, and it will always be a door that you did not initially choose. (If the host has two options for which door to open to reveal a goat, he will randomly select which of those two doors to open.)
  • After the host opens a door that contains a goat, you will always be given the option to switch your choice to the door that is still closed, and you will always accept that option.

Apologies for that lengthy quote. I wasn’t going to submit a solution; but I thought I’d give it a try.

The Long Way

Seems pretty simple. Some variables, a loop, a few calls to the Random module’s methods, if a win increment a counter. Print result. Let’s see what a naive approach can do. Note: I wrote the code to allow for the possibility of more than 3 doors as I thought I might see how things work out with 4 doors. And most surprising of all, I only had one small bug that was easily and quickly fixed.

I decided to keep track of the wins if the contestant didn’t switch doors. Just for comparison in the more than 3 door cases.

# Python version of Monte Carlo simulation of Monty Hall problem
#
# - 3 doors, 1 car, 2 goats
# - Doors for each selected at random.
# - User selects door
# - Monty uncovers door with goat
# - User sticks with choice or chooses the other door
# - Which is more successful approach?

import random as rand

N_DR = 3
N_SIM = 1000
behind = ['goat' for _ in range(N_DR - 1)]
behind.append('car')
ALL_DR = set(list(range(N_DR)))
w_stk = 0   # wins always sticking to first choice
w_chg = 0   # wins when always switching

for i in range(N_SIM):
  # select door for car (well and goats)
  rand.shuffle(behind)
  # user picks a door, randint includes both terminal values in selection
  # so adjust accordingly as 3 is not a valid choice
  u_ch = rand.randint(0, N_DR-1)

  # determine all goat doors not selected by user, host picks one
  g_drs = [j for j in range(N_DR) if behind[j] == 'goat' and j != u_ch]
  h_ch = rand.choice(g_drs)

  # user changes doors, use sets to facilitate the arithmetic
  # but can't pass set to rand.choice()
  u_drs = list(ALL_DR - {u_ch, h_ch})
  u_ch_2 = rand.choice(u_drs)

  # did changing doors result in winning the car?
  if behind[u_ch_2] == 'car':
    w_chg += 1
  elif behind[u_ch] == 'car':
    w_stk += 1

  if i < 5:
    if i == 0:
      print()
    print(f"doors: {behind}, user 1st choice: {u_ch + 1}, host choice: {h_ch + 1}, user 2nd choice: {u_ch_2 + 1}, change wins: {w_chg}")

print(f"\nprobability of winning with always change strategy is {w_chg/N_SIM:0.1%}")
print(f"probability of winning with always stick with first choice strategy is {w_stk/N_SIM:0.1%}")

Sample Output

I am using a conda environment from another experiment.

(fin-3.11) PS R:\learn\py_play\misc> python monty_hall_monte_carlo.py

doors: ['car', 'goat', 'goat'], user 1st choice: 2, host choice: 3, user 2nd choice: 1, change wins: 1
doors: ['goat', 'goat', 'car'], user 1st choice: 2, host choice: 1, user 2nd choice: 3, change wins: 2
doors: ['car', 'goat', 'goat'], user 1st choice: 2, host choice: 3, user 2nd choice: 1, change wins: 3
doors: ['goat', 'car', 'goat'], user 1st choice: 1, host choice: 3, user 2nd choice: 2, change wins: 4
doors: ['goat', 'goat', 'car'], user 1st choice: 3, host choice: 2, user 2nd choice: 1, change wins: 4

probability of winning with always change strategy is 66.8%
probability of winning with always stick with first choice strategy is 33.2%

Not the result I would have expected. Now, some one who actually understands probability, would likely have worked that out without the simulation. Of course, for any given contestant… But, likely still best to switch doors.

How About More Doors

(fin-3.11) PS R:\learn\py_play\misc> python monty_hall_monte_carlo.py

probability of winning with always change strategy is 41.0%
probability of winning with always stick with first choice strategy is 22.5%

probability of winning with always change strategy is 36.1%
probability of winning with always stick with first choice strategy is 23.8%

probability of winning with always change strategy is 37.3%
probability of winning with always stick with first choice strategy is 22.7%

probability of winning with always change strategy is 37.7%
probability of winning with always stick with first choice strategy is 25.2%

probability of winning with always change strategy is 36.5%
probability of winning with always stick with first choice strategy is 26.3%

probability of winning with always change strategy is 38.4%
probability of winning with always stick with first choice strategy is 25.3%

probability of winning with always change strategy is 37.6%
probability of winning with always stick with first choice strategy is 26.6%

probability of winning with always change strategy is 35.2%
probability of winning with always stick with first choice strategy is 25.1%

probability of winning with always change strategy is 38.5%
probability of winning with always stick with first choice strategy is 23.6%

probability of winning with always change strategy is 35.9%
probability of winning with always stick with first choice strategy is 26.6%

Still a slightly better chance if one always switches to one of the other unopened doors.

And, when I tried 5 doors that also seemed to be the case. But the difference was narrower.

Rethink of 3 Door Case

Once I started playing around, I realized that the 3 door case didn’t really need all that code. Which should produce a result quicker and with fewer computer resources. Let’s consider how that would be possible.

Okay, 3 doors. Contestant picks one. It is either the car or a goat.

Monty opens a door with a goat behind it. That means the car is either behind the door the contestant picked or the other unopended door. Which means we can determine if switching wins by simply checking what is behind the door the contestant first selected. No need to execute all those other lines of code. Something like the following. I am not going to include the variables in the code listing.

for i in range(N_SIM):
  rand.shuffle(behind)
  u_ch = rand.randint(0, 2)

  # only info we really need is whether or not car behind user selected door
  # no need to write and run code for all the other steps
  # if car not behind player selected door, since door host opens will always be a goat, switching will win
  # but if more than 3 doors, will need to simulate all the steps
  if behind[u_ch] == 'car':
    w_stk += 1
  else:
    w_chg += 1

print(f"\nprobability of winning with always change strategy is {w_chg/N_SIM:0.1%}")
print(f"probability of winning with always stick with first choice strategy is {w_stk/N_SIM:0.1%}")

And running the above 10 times we get the following.

(fin-3.11) PS R:\learn\py_play\misc> python monty_hall_monte_carlo.py

probability of winning with always change strategy is 68.8%
probability of winning with always stick with first choice strategy is 31.2%

probability of winning with always change strategy is 65.2%
probability of winning with always stick with first choice strategy is 34.8%

probability of winning with always change strategy is 67.7%
probability of winning with always stick with first choice strategy is 32.3%

probability of winning with always change strategy is 66.4%
probability of winning with always stick with first choice strategy is 33.6%

probability of winning with always change strategy is 69.6%
probability of winning with always stick with first choice strategy is 30.4%

probability of winning with always change strategy is 65.7%
probability of winning with always stick with first choice strategy is 34.3%

probability of winning with always change strategy is 67.3%
probability of winning with always stick with first choice strategy is 32.7%

probability of winning with always change strategy is 64.7%
probability of winning with always stick with first choice strategy is 35.3%

probability of winning with always change strategy is 67.1%
probability of winning with always stick with first choice strategy is 32.9%

probability of winning with always change strategy is 65.3%
probability of winning with always stick with first choice strategy is 34.7%

Which looks pretty consistent with the full simulation approach.

Done

The post is short, but it does cover the subject well enough I believe.

By the way, the always change strategy has an actual win rate of 66.6% using probability theory.

Also, I favour the refactored code. A thoughtful solution over extra workload.

Until next time, remember a little thinking often saves a bunch of work.