Game Show Problem
The Game-Show Problem, or Monty Hall Problem according to Wikipedia.
- Choose one of three doors, one of which has the desired prize.
- The game show host always opens one of the remaining two doors which does not have the prize behind it.
- The host then asks the player if they would like to switch doors.
- The player chooses whether to switch doors.
- The host assigns a prize to the player.
What is the optimal strategy for the player?
Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
Play the Game
# One door has the prize "P" and two do not have the prize "N".
doors = pd.DataFrame(data=['P','N','N'], columns=['prizes'])
runs = 100000
wins = []
for i in range(runs):
# 1. Player selects a Door.
doors = doors.sample(frac=1).reset_index(drop=True)
door = doors.sample(n=1)
# 2. Host removes a door that is neither the players door nor has the prize.
remove = doors[(doors.index.isin(door.index) == False)
& (doors['prizes'] == 'N')].sample(n=1)
# 3. Choose randomly whether to switch doors or not.
switch = np.random.choice([0,1])
# 4. Assign the a prize.
if switch == 1:
prize = doors.drop(door.index).drop(remove.index).values
elif switch == 0:
prize = door.values
# 5. If door has the prize, then mark as a win.
if prize == "P":
wins.append([1, switch])
else:
wins.append([0, switch])
Calculate Results
The table below shows that switching doors gives a player a 66% chance of winning while a strategy of not switching doors gives the player only a 33% chance of winning the prize.
df = pd.DataFrame(data=wins, columns=['result','switch'])
Calculate the probability of winning if the player switches doors.
df[df['switch'] == 1]['result'].sum() / df[df['switch'] == 1]['result'].count()
0.669946919544343
Calculate the probability of winning if the player never switches doors.
df[df['switch'] == 0]['result'].sum() / df[df['switch'] == 0]['result'].count()
0.33433268275015593