# Game Playing: The Monty Hall Problem

## Background of the Problem

I recently read Daniel Kahneman book "Thinking, Fast and Slow" which I can highly recommend. Kahneman's surprisingly simple psychological experiments have led to equally surprising insights into how we humans really think and solve problems. We have an impulsive intuitive side that leaps at quick answers which just feel right. We also have a slower and more thoughtful mode of thinking can often override quick answers that are wrong. But not without effort and more time.

One of the problems discussed in the book came out of a TV program called "Let's make a deal". This is a somewhat simplified version. There is a panel with three doors and behind one of them a valuable prize has been placed. The player chooses a door and if the prize is behind it the player keeps it. His chances would be one in three that he picked the right door.

But before this door is opened the moderator (formally Monty Hall), opens one of the other doors which always comes up empty. The player is then given the opportunity to switch his choice to the remaining door. Few people do. Most figure that the odds are now 50-50 that their door holds the prize so there is no point in switching. And there is also a strong tendency for people to stay with their initial choice.

But in this case they are wrong. Their odds of getting the prize actually double if they choose to switch doors.

For almost everyone this is very non-intuitive. Imagine a similar game where we draw straws. Assume that there are 2 long straws and one short. You want to draw the short straw. You choose one straw but do not draw it out immediately. Instead, another player draws another straw and it is long. Now you are offered the chance to switch, but again you might figure the odds are now 50-50 that you made the correct choice.

And you would be right. The odds are indeed 50-50.

At this point you have every right to be a bit baffled. With the Monty Hall problem we can prove the correctness mathematically using Baye's theorum which I find unsatisfying, argue our way through which tends only to confuse (me) or simulate the game with a computer program and watch the results. The last option is not at all hard and I found it extremely helpful in convincing myself.

## Simulation of the game

Here is a small Python program that will let you choose a couple of parameters after which the program will play 100 times tracking wins. You choose one of the 3 doors (A,B,C) and whether to always "hold" or "switch" after the moderator has opened an empty door. Click here to download the program

```# Simulating the Monty Hall problem
import random
initial = raw_input("Initial choice (A,B,C)? ").upper()
action  = raw_input("Always Switch/Hold (S,H) ? ").upper()
doors   = ["A","B","C"]
totWins = 0
print "Let's play 100 times"
for i in range(100) :
placed = random.choice(doors)  # Prize placed randomly
chosen = initial               # Your first choice
used   = [chosen,placed]       # Moderator chooses an empty door
avail  = [door for door in doors if door not in used]
opened = random.choice(avail)
if action == "S" :             # You switch to remaining door
avail = [door for door in doors if door not in [chosen,opened]]
chosen = avail[0]          # should be the only door left
youWin = (chosen == placed)
if youWin : totWins += 1
print "  Prize placed behind", placed,
print "-You chose",initial,"-Moderator opened"   ,opened,
if action == "H" : print  "-You held to"    ,chosen,
else             : print  "-You switched to",chosen,
if youWin : print "-You win (wins now",totWins,")",
print
```

Let's play the game where you always first choose door A and never switch

```\$ python monty.py
Initial choice (A,B,C)? a
Always Switch/Hold (S,H) ? h
Let's play 100 times
Prize placed behind A -You chose A -Moderator opened C -You held to A -You win (wins now 1 )
Prize placed behind A -You chose A -Moderator opened C -You held to A -You win (wins now 2 )
Prize placed behind C -You chose A -Moderator opened B -You held to A
Prize placed behind C -You chose A -Moderator opened B -You held to A
Prize placed behind C -You chose A -Moderator opened B -You held to A
Prize placed behind B -You chose A -Moderator opened C -You held to A
Prize placed behind B -You chose A -Moderator opened C -You held to A
Prize placed behind A -You chose A -Moderator opened B -You held to A -You win (wins now 3 )
Prize placed behind C -You chose A -Moderator opened B -You held to A
.....
Prize placed behind C -You chose A -Moderator opened B -You held to A
Prize placed behind A -You chose A -Moderator opened B -You held to A -You win (wins now 32 )
Prize placed behind A -You chose A -Moderator opened C -You held to A -You win (wins now 33 )
Prize placed behind A -You chose A -Moderator opened C -You held to A -You win (wins now 34 )
Prize placed behind B -You chose A -Moderator opened C -You held to A
Prize placed behind C -You chose A -Moderator opened B -You held to A
Prize placed behind B -You chose A -Moderator opened C -You held to A
Prize placed behind A -You chose A -Moderator opened B -You held to A -You win (wins now 35 )
Prize placed behind C -You chose A -Moderator opened B -You held to A
Prize placed behind C -You chose A -Moderator opened B -You held to A
```

Now let's try it again where we always switch.

```\$ python monty.py
Initial choice (A,B,C)? A
Always Switch/Hold (S,H) ? S
Let's play 100 times
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 1 )
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 2 )
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 3 )
Prize placed behind A -You chose A -Moderator opened C -You switched to B
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 4 )
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 5 )
Prize placed behind C -You chose A -Moderator opened B -You switched to C -You win (wins now 6 )
Prize placed behind A -You chose A -Moderator opened C -You switched to B
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 7 )
Prize placed behind A -You chose A -Moderator opened C -You switched to B
....
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 62 )
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 63 )
Prize placed behind A -You chose A -Moderator opened C -You switched to B
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 64 )
Prize placed behind C -You chose A -Moderator opened B -You switched to C -You win (wins now 65 )
Prize placed behind B -You chose A -Moderator opened C -You switched to B -You win (wins now 66 )
Prize placed behind C -You chose A -Moderator opened B -You switched to C -You win (wins now 67 )
Prize placed behind C -You chose A -Moderator opened B -You switched to C -You win (wins now 68 )
Prize placed behind A -You chose A -Moderator opened B -You switched to C
```

Each time you run the program you will, of course, get different results since the prize is being placed randomly.

But you can see that there is very probably a big difference in the odds. And if you look closely you can also see what is going on. See if you can figure it out from the program output.

If you hold to your initial choice, you will win if and only if the prize was placed behind the door of your choice. It doesn't matter in the least which door is opened since you will ignore it. Your chance of winning stays 1 in 3. The chance of losing is 2 in 3.

But if you always switch you reverse these odds. You are betting that your door is a loser (probability 2/3) and that you have been shown the other loser. You now have inside information.

With the drawing of the straws the straw drawn after you have made your initial choice was a random choice. It too had a 1 in 3 chance of being the short straw. So your overall chance of drawing the short straw is (2/3 * 1/2) which is 1/3 whether you switch or not.

If you are still confused, you are in good company. Even the famous mathamatition Paul Erdos was finally only convinced by a simulation like the one above.

There are several changes you can make to the program. If you make it play 1000 times instead of a hundred you will probably see the results more closely match the odds. Also you can make small change in the code so that the moderator opens a door at random. This changes everything!

For more on this there is a very good wikipedia article on the history of this problem.

## Programming Note

The program uses list comprehension to efficiently filter lists easily. If you are not used to using list comprehensions, figuring out the following evaluation should make it clear.

```>>> [door for door in ['A','B','C'] if door not in ['A','C']]
['B']
```

If you have comments or suggestions You can email me at mail me