top of page

The Story

Monte Carlo Simulation of the Monty Hall Problem: Does The Math Actually Work?



Introduction

If you ever took a stats class back in high school or college, then you were probably told about the infamous Monty Hall problem. The premise is very simple:

  • The contestant is presented with 3 doors. Behind one of the doors, there is a car, and behind the other two, there is a goat.

  • The contestant picks a door, then the host reveals one of the other two doors.

  • The contestant then has a choice: should she switch doors or stick with what she picked first?

  • You can play the game in this link, try it out for yourself!

You can find the full article here.


Now here is the full code (in Python)


import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns

import random

%matplotlib inline


def generate_doors():

door1 = random.randint(0, 1)

if door1 == 1:

door2 = 0

door3 = 0

door_with_car = 0

elif door1 == 0:

door2 = random.randint(0, 1)

if door2 == 1:

door3 = 0

door_with_car = 1

else:

door3 = 1

door_with_car = 2


all_doors = [door1, door2, door3]

return all_doors, door_with_car


def reveal_door(door_with_car, player_choice):

lst = [0, 1, 2]

try:

lst.remove(door_with_car)

lst.remove(player_choice)

except:

pass

return random.choice(lst)


score = {'player_keep': 0, 'player_switch': 0}


# Setting up the game

all_doors, door_with_car = generate_doors()

print("We start with 3 doors with the door of '1' having the car:", all_doors)


# Player makes a choice of door

player_choice = random.randint(0, 2)

print("Player chooses door #", player_choice + 1)


# Host reveals the door

revealed_door = reveal_door(door_with_car, player_choice)

print("Host reveals door #", revealed_door + 1)


# Remove revealed door from list

del all_doors[revealed_door]

print("Now there are only 2 doors left to choose:", all_doors)


# Player chooses between keeping or switching

player_keep = player_choice

player_switch = [x for x in all_doors if x != player_choice][0]


# Decide who's right

if player_keep == door_with_car:

score['player_keep'] += 1

print("If player chooses to keep, he wins!")

else:

score['player_switch'] += 1

print("If player chooses to switch, he wins!")


score = {'Keep Choice': 0, 'Switch Choice': 0}


for i in range(10000):

# Setting up the game

all_doors, door_with_car = generate_doors()


# Player makes a choice of door

player_choice = random.randint(0, 2)


# Host reveals the door

revealed_door = reveal_door(door_with_car, player_choice)


# Remove revealed door from list

del all_doors[revealed_door]


# Player chooses between keeping or switching

player_keep = player_choice

player_switch = [x for x in all_doors if x != player_choice][0]


# Decide who's right

if player_keep == door_with_car:

score['Keep Choice'] += 1

else:

score['Switch Choice'] += 1



score


pd.DataFrame.from_dict(score, orient='index').plot.bar(legend=False, title="Count of Wins for Each Strategy", figsize=(7, 4));



Wanna learn how to write it yourself? Check out our classes or contact us now, we are here to help :-)













131 views0 comments
bottom of page