Yesterday evening I was reading an article about Monty Hall Problem (The time everyone corrected the worlds smartest woman). A reader asked this question on Marilyn vos Savant's "Ask Marilyn" column in Parade magazine in 1990:
"Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?"
Marilyin Vos Savant suggested that the contestant should switch to the other door. Under the standard assumptions, contestants who switch have a 2/3 chance of winning the car, while contestants who stick to their choice have only a 1/3 chance. This problem became famous when nearly 1000 PhD's were unable to comprehend the suggestion provided by Marilyn vos Savant. For her suggestions on switching the door she was criticized harshly. Many people have simulated the problem and found her suggestion to be correct.
For fun I decided to write a simple python code to simulate the Monty Hall Problem.
###################################################################
import numpy as np
# Number of simulations: 100000
nSim = 100000
# Number of doors: nDoor
nDoor = 3
# Function to simulate a game or a trial of Monty Hall problem
def game(nDoor, switch_choice):
# List of all doors
doors = ['goat'] * nDoor
# Randomly assign a door behind which there is a "Car"
car = np.random.randint(0, nDoor)
doors[car] = 'car'
# Contestant makes a random guess
guess = np.random.randint(0, nDoor)
# Store value of guess
temp = doors[guess]
## Monty Hall opens all but 2 doors:
# 1. Door with car behind it or the door chosen by contestant
# 2. One "Goat" door
while len(doors) > 2:
# Open a door such that door with car behind it or the door chosen by contestant is never opened
openDoor = np.random.choice(range(len(doors)))
if openDoor == car or openDoor == guess:
continue
doors.pop(openDoor)
## Now there are only 2 unopened doors remaining
# Contestant switches choice: Boolean - value True or False
if switch_choice: # If true contestant switches choice
# Switch the choice to other unopened door, i.e. remove from unopened door list
closedDoors = list(doors)
closedDoors.remove(temp)
# Only one unopened door remains, update guess
temp = closedDoors.pop()
# Check if guessed door has car behind it or not
result = (temp == 'car')
return result
## Simulation
win_switch = 0
win_no_switch = 0
for i in range(nSim):
switch_trial = game(nDoor, switch_choice = True)
if switch_trial is True:
win_switch += 1
no_switch_trial = game(nDoor, switch_choice = False)
if no_switch_trial is True:
win_no_switch += 1
print 'If you switch door, the probability of winning the car is: %0.2f' % (float(win_switch)/nSim)
print 'If you do not switch, the probability of winning the car is: %0.2f'% (float(win_no_switch)/nSim)
Out:
If you switch door, the probability of winning the car is: 0.67
If you do not switch, the probability of winning the car is: 0.33
Here is link to Monty Hall Problem Simulation IPython Notebook
"Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?"
Source: Wikipedia
Marilyin Vos Savant suggested that the contestant should switch to the other door. Under the standard assumptions, contestants who switch have a 2/3 chance of winning the car, while contestants who stick to their choice have only a 1/3 chance. This problem became famous when nearly 1000 PhD's were unable to comprehend the suggestion provided by Marilyn vos Savant. For her suggestions on switching the door she was criticized harshly. Many people have simulated the problem and found her suggestion to be correct.
For fun I decided to write a simple python code to simulate the Monty Hall Problem.
###################################################################
import numpy as np
# Number of simulations: 100000
nSim = 100000
# Number of doors: nDoor
nDoor = 3
# Function to simulate a game or a trial of Monty Hall problem
def game(nDoor, switch_choice):
# List of all doors
doors = ['goat'] * nDoor
# Randomly assign a door behind which there is a "Car"
car = np.random.randint(0, nDoor)
doors[car] = 'car'
# Contestant makes a random guess
guess = np.random.randint(0, nDoor)
# Store value of guess
temp = doors[guess]
## Monty Hall opens all but 2 doors:
# 1. Door with car behind it or the door chosen by contestant
# 2. One "Goat" door
while len(doors) > 2:
# Open a door such that door with car behind it or the door chosen by contestant is never opened
openDoor = np.random.choice(range(len(doors)))
if openDoor == car or openDoor == guess:
continue
doors.pop(openDoor)
## Now there are only 2 unopened doors remaining
# Contestant switches choice: Boolean - value True or False
if switch_choice: # If true contestant switches choice
# Switch the choice to other unopened door, i.e. remove from unopened door list
closedDoors = list(doors)
closedDoors.remove(temp)
# Only one unopened door remains, update guess
temp = closedDoors.pop()
# Check if guessed door has car behind it or not
result = (temp == 'car')
return result
## Simulation
win_switch = 0
win_no_switch = 0
for i in range(nSim):
switch_trial = game(nDoor, switch_choice = True)
if switch_trial is True:
win_switch += 1
no_switch_trial = game(nDoor, switch_choice = False)
if no_switch_trial is True:
win_no_switch += 1
print 'If you switch door, the probability of winning the car is: %0.2f' % (float(win_switch)/nSim)
print 'If you do not switch, the probability of winning the car is: %0.2f'% (float(win_no_switch)/nSim)
Out:
If you switch door, the probability of winning the car is: 0.67
If you do not switch, the probability of winning the car is: 0.33
Here is link to Monty Hall Problem Simulation IPython Notebook