# 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 (or fail to). 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 very 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 could 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 "monty.py"

`````` 1 # Simulating the Monty Hall problem
2 #
3 import random, sys
4
5 if sys.version > '3' : raw_input = input  # Python 3
6
7 initial = raw_input("Initial choice (A,B,C)? ").upper()
8 action  = raw_input("Always Switch/Hold (S,H) ? ").upper()
9 doors   = ["A","B","C"]
10 totWins = 0``````

The program will run on Python 2.7 or above. The function input works differently between Python-2 and Python-3. In Python-3 it is the equivalent of raw_input in Python-2

``````11 print("Let's play 100 times")
12 for i in range(100) :
13     placed = random.choice(doors)  # Prize placed randomly
14     chosen = initial               # Your first choice
15     used   = [chosen,placed]       # Moderator chooses an empty door
16     avail  = [door for door in doors if door not in used]
17     opened = random.choice(avail)
18     if action == "S" :             # You switch to remaining door
19         avail = [door for door in doors if door not in [chosen,opened]]
20         chosen = avail[0]          # should be the only door left
21     youWin = (chosen == placed)
22     if youWin : totWins += 1
23     mesg  = " Prize placed behind %s" % placed
24     mesg += "-You chose %s -Monty opened %s " % (initial,opened)
25     if action == "H" : mesg += " -You held to %s" % chosen
26     else             : mesg += " -You switched to %s" % chosen
27     if youWin : mesg += " - won"
28     print(mesg)
29 print("Total Wins %d/100" % totWins)
30
31``````

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 C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind A-You chose A -Monty opened B  -You held to A - won
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind C-You chose A -Monty opened B  -You held to A
Prize placed behind A-You chose A -Monty opened C  -You held to A - won
Prize placed behind B-You chose A -Monty opened C  -You held to A
Prize placed behind B-You chose A -Monty opened C  -You held to A
Total Wins 31/100``````

So, 31% wins. Had we always switched, that run would have been 69%.

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

See if you can figure out why this is so.

## A Small Hint

Why is the prize never behind the door that Monty opened?

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 computer 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