The Monty Hall Problem

See the Wikipedia entry for a summary of the problem.

Random door selection

Here's some code to pick a door at random.

In [1]:
# Python provides a library called random to generate pseudo-random numbers and do stuff with them.
import random

# The three doors in a list.
doors = ['red', 'green', 'blue']

# Pick a random door.
print(random.choice(doors))
red

The pick is meant to give a one third probability to each door. Let's pick 10,000 doors are see if that looks correct.

In [2]:
# 10,000 random doors.
tenthous = [random.choice(doors) for i in range(10000)]

tenthous
Out[2]:
['blue',
 'blue',
 'blue',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'red',
 'red',
 'blue',
 'red',
 'blue',
 'green',
 'green',
 'blue',
 'blue',
 'blue',
 'blue',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'red',
 'green',
 'blue',
 'blue',
 'blue',
 'red',
 'green',
 'green',
 'blue',
 'red',
 'green',
 'red',
 'blue',
 'green',
 'green',
 'blue',
 'blue',
 'blue',
 'green',
 'green',
 'red',
 'blue',
 'green',
 'red',
 'red',
 'blue',
 'green',
 'blue',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'green',
 'green',
 'green',
 'green',
 'blue',
 'red',
 'green',
 'red',
 'green',
 'green',
 'green',
 'red',
 'blue',
 'red',
 'green',
 'green',
 'blue',
 'blue',
 'red',
 'green',
 'green',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'green',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'red',
 'red',
 'green',
 'green',
 'red',
 'blue',
 'green',
 'green',
 'blue',
 'blue',
 'red',
 'red',
 'blue',
 'red',
 'blue',
 'green',
 'blue',
 'blue',
 'green',
 'blue',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'blue',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'green',
 'red',
 'blue',
 'green',
 'green',
 'red',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'red',
 'red',
 'red',
 'green',
 'blue',
 'blue',
 'green',
 'red',
 'red',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'blue',
 'green',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'green',
 'green',
 'green',
 'red',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'blue',
 'blue',
 'blue',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'red',
 'blue',
 'red',
 'red',
 'blue',
 'red',
 'red',
 'blue',
 'red',
 'red',
 'blue',
 'red',
 'green',
 'red',
 'blue',
 'blue',
 'red',
 'red',
 'red',
 'green',
 'blue',
 'green',
 'red',
 'blue',
 'green',
 'green',
 'red',
 'red',
 'red',
 'blue',
 'green',
 'blue',
 'blue',
 'red',
 'green',
 'green',
 'green',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'blue',
 'blue',
 'red',
 'red',
 'red',
 'green',
 'green',
 'blue',
 'blue',
 'blue',
 'red',
 'green',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'red',
 'blue',
 'red',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'blue',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'red',
 'red',
 'green',
 'blue',
 'red',
 'green',
 'green',
 'red',
 'green',
 'red',
 'red',
 'red',
 'blue',
 'red',
 'blue',
 'green',
 'red',
 'blue',
 'green',
 'green',
 'red',
 'red',
 'red',
 'red',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'blue',
 'blue',
 'red',
 'green',
 'green',
 'green',
 'red',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'blue',
 'blue',
 'green',
 'blue',
 'green',
 'blue',
 'blue',
 'blue',
 'blue',
 'red',
 'red',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'red',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'green',
 'green',
 'red',
 'blue',
 'blue',
 'blue',
 'red',
 'red',
 'green',
 'green',
 'green',
 'green',
 'green',
 'green',
 'green',
 'blue',
 'green',
 'green',
 'blue',
 'red',
 'red',
 'green',
 'blue',
 'red',
 'green',
 'red',
 'blue',
 'red',
 'red',
 'green',
 'red',
 'blue',
 'blue',
 'blue',
 'red',
 'red',
 'green',
 'red',
 'blue',
 'green',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'blue',
 'green',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'green',
 'red',
 'red',
 'green',
 'green',
 'red',
 'green',
 'red',
 'green',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'red',
 'green',
 'red',
 'green',
 'green',
 'red',
 'green',
 'green',
 'red',
 'green',
 'red',
 'red',
 'green',
 'blue',
 'blue',
 'red',
 'green',
 'red',
 'red',
 'red',
 'red',
 'blue',
 'blue',
 'red',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'red',
 'red',
 'red',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'blue',
 'red',
 'red',
 'blue',
 'blue',
 'red',
 'red',
 'blue',
 'red',
 'green',
 'red',
 'green',
 'red',
 'red',
 'green',
 'red',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'red',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'red',
 'red',
 'red',
 'blue',
 'red',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'green',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'blue',
 'red',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'red',
 'green',
 'blue',
 'green',
 'green',
 'red',
 'green',
 'blue',
 'red',
 'red',
 'blue',
 'blue',
 'blue',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'green',
 'blue',
 'green',
 'red',
 'blue',
 'green',
 'red',
 'green',
 'blue',
 'green',
 'green',
 'red',
 'blue',
 'green',
 'red',
 'red',
 'red',
 'green',
 'red',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'green',
 'green',
 'red',
 'blue',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'red',
 'red',
 'green',
 'red',
 'green',
 'red',
 'green',
 'red',
 'green',
 'blue',
 'red',
 'red',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'green',
 'red',
 'blue',
 'blue',
 'green',
 'blue',
 'blue',
 'red',
 'red',
 'red',
 'red',
 'blue',
 'red',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'blue',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'red',
 'red',
 'green',
 'green',
 'red',
 'green',
 'green',
 'green',
 'green',
 'blue',
 'blue',
 'blue',
 'green',
 'red',
 'blue',
 'red',
 'green',
 'red',
 'red',
 'green',
 'green',
 'blue',
 'red',
 'red',
 'red',
 'blue',
 'green',
 'green',
 'red',
 'red',
 'green',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'green',
 'green',
 'red',
 'green',
 'green',
 'blue',
 'green',
 'blue',
 'blue',
 'blue',
 'red',
 'red',
 'green',
 'blue',
 'blue',
 'blue',
 'blue',
 'green',
 'red',
 'blue',
 'blue',
 'red',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'blue',
 'green',
 'blue',
 'blue',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'red',
 'red',
 'green',
 'red',
 'red',
 'red',
 'red',
 'green',
 'red',
 'green',
 'red',
 'green',
 'green',
 'green',
 'blue',
 'green',
 'blue',
 'red',
 'red',
 'blue',
 'red',
 'red',
 'green',
 'blue',
 'red',
 'red',
 'red',
 'green',
 'blue',
 'red',
 'red',
 'red',
 'green',
 'blue',
 'green',
 'red',
 'blue',
 'blue',
 'red',
 'red',
 'blue',
 'blue',
 'green',
 'green',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 'red',
 'blue',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'red',
 'blue',
 'green',
 'blue',
 'red',
 'red',
 'green',
 'green',
 'green',
 'blue',
 'red',
 'red',
 'green',
 'green',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'green',
 'red',
 'green',
 'red',
 'green',
 'green',
 'red',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'red',
 'blue',
 'red',
 'green',
 'green',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'blue',
 'blue',
 'blue',
 'red',
 'red',
 'green',
 'red',
 'green',
 'blue',
 'green',
 'red',
 'green',
 'blue',
 'blue',
 'green',
 'green',
 'green',
 'blue',
 'green',
 'green',
 'green',
 'green',
 'blue',
 'blue',
 'red',
 'red',
 'red',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'green',
 'green',
 'red',
 'red',
 'blue',
 'blue',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'green',
 'blue',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'green',
 'red',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'red',
 'blue',
 'red',
 'red',
 'blue',
 'green',
 'green',
 'green',
 'blue',
 'blue',
 'green',
 'blue',
 'red',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'red',
 'green',
 'red',
 'red',
 'blue',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'green',
 'blue',
 'red',
 'green',
 'blue',
 'green',
 'red',
 'green',
 'red',
 'green',
 'green',
 'green',
 'green',
 'red',
 'green',
 'red',
 'blue',
 'red',
 'red',
 'green',
 'red',
 'green',
 'blue',
 'blue',
 'blue',
 'red',
 'green',
 'red',
 'red',
 'red',
 'green',
 'red',
 'green',
 'blue',
 'green',
 'green',
 'blue',
 'green',
 'blue',
 'green',
 'blue',
 'red',
 'green',
 'blue',
 'red',
 'red',
 'blue',
 'green',
 'red',
 'red',
 'green',
 'red',
 'red',
 'red',
 'red',
 'green',
 'green',
 'blue',
 'red',
 'red',
 'red',
 'blue',
 'blue',
 'blue',
 'blue',
 'red',
 'blue',
 'red',
 'blue',
 'red',
 'green',
 'blue',
 'red',
 'blue',
 'green',
 'blue',
 'green',
 'red',
 'green',
 'green',
 'red',
 'blue',
 'red',
 'green',
 'green',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'red',
 'blue',
 'blue',
 'blue',
 'green',
 ...]

Let's plot it now, and see that each door is picked about a third of the time.

In [3]:
import seaborn as sns
import matplotlib.pyplot as pl

pl.figure(figsize=(10, 6))
sns.set(style="darkgrid")
ax = sns.countplot(y=tenthous)
pl.show()

Simulate the game

Let's simulate the game now. Let's:

  1. Pick a door to put the car behind.
  2. Have the contestant pick a door.
  3. Have the show host open one of the other doors to reveal a goat.
  4. Ask the contestant if they want to switch.
  5. See how often the contestant wins.

The question we're looking to answer is whether staying with your original pick makes a difference.

In [4]:
# A function to simulate a game and tell us if the contestant wins.
def simulate(stay=True):
    doors = ['red', 'green', 'blue']
    
    # Put the car behind a random door.
    car = random.choice(doors)
    
    # Have the contestant pick a door.
    pick = random.choice(doors)
    
    # Open a door with a goat.
    show = random.choice([door for door in doors if door != car and door != pick])

    # Figure out which door was not opened or picked.
    notopen = [door for door in doors if door != pick and door != show][0]
    
    return (car == [pick, notopen][not stay])

So, we can simulate a game in which the contestant stays with their original pick by running the following. A return value of True means they won the car, False means they didn't.

In [5]:
simulate(stay=True)
Out[5]:
True

Ten thousand times each

Let's run the game 10,000 times where the contestant stays, and then 10,000 where they switch. Then we'll see how often they win in each case.

In [6]:
staying = [simulate(stay=True) for i in range(10000)]

Let's plot the result of staying.

In [7]:
ax = sns.countplot(y=staying)
pl.show()

Looks like when the contestant stays, they win only about a third of the time.

In [8]:
switching = [simulate(stay=False) for i in range(10000)]
In [9]:
ax = sns.countplot(y=switching)
pl.show()

Looks like you win two thirds of the time if you switch.

End