Every now and then I decide to overthink a problem I thought I understood and get confused – last week, it was the Monty Hall problem.
For those unfamiliar with the thought experiment, the basic premise is that you are on a game show and are presented with three doors. Behind one of the doors is a car, while behind the other two are goats.
With zero initial information, you make a guess as to which door you think the car is behind (we assume you have enough goats already). Before looking behind your chosen door, the host opens one of the remaining two doors and reveals a goat. The host then asks you if you would like to change your guess. What should you do?
Most people instinctively think that whether you ‘stick’ or ‘switch’ should make no difference to your chances of winning – it should be 50-50 now that only two doors remain. However, probability says otherwise.
I won’t cover the math here (plenty of online resources do this already), but I have written some simple code that proves the same result – choose the other door. The code can be executed and modified in Colab here.
from random import randrange
number_of_guesses = 10000
doors = [0, 1, 2]
num_correct_guesses_stick = 0
num_correct_guesses_switch = 0
for guess_number in range(number_of_guesses):
# door that hides the car
correct_door = randrange(3)
# door that the contestant guesses when there are 3 choices available
contestant_initial_guess = randrange(3)
# doors that the contestant has not picked e.g. 0 and 2 if contestant picked 1
remaining_doors = [door for door in doors if door!= contestant_initial_guess]
# door opened by the host, revealing a goat
remaining_goat_doors = [door for door in remaining_doors if door!= correct_door]
door_opened = remaining_goat_doors[randrange(len(remaining_goat_doors))]
# door picked by contestant if they choose to switch
contestant_guess_switch = [door for door in doors if door not in [contestant_initial_guess, door_opened]][0]
# record whether or not the initial or changed guess is correct
if contestant_initial_guess == correct_door:
num_correct_guesses_stick += 1
if contestant_guess_switch == correct_door:
num_correct_guesses_switch += 1
print("Success rate when stick with initial guess: {:.2f}%".format(100 * num_correct_guesses_stick / number_of_guesses))
print("Success rate when switch guess after reveal: {:.2f}%".format(100 * num_correct_guesses_switch / number_of_guesses))