rapid habits

my quest to find the next little thing.
i'm mustafa paksoy and i write code for a living. this is my tumblog where i post little discoveries about computing and life.
Apr 14
Permalink

Monty Hall paradox: see for yourself

The Monty Hall paradox/problem goes something like this:

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?
The common answer is that you have 2/3 chance of finding the car if you switch.

The thing that’s always bugged me about this common phrasing (and the accompanying solution) is the assumption that the host always opens a door, revealing a goat. This isn’t exactly evident from the problem definition; we only know that the host knowingly revealed a goat. The host could have been doing so maliciously; revealing a goat only when the competitor chooses a car the first time around in hopes to lure her away from the prize. And I’m not just reading too much into the puzzle; we need to phrase this problem as a repeatable scenario in order to have any notion of probabilistic strategy. The hosts behavioral pattern (random, predetermined or whatever) is a key component to this repeatable scenario.

Still, the switching strategy does yield a higher probability of scoring a sweet ride, when we know that the host will always reveal a goat. Just to put a stake through the heart of my doubt about Monty Pyth Hall’s true benevolence, I wrote something in Python to settle this once and for all. The results are not deterministic because I don’t seed the random number generator with a constant before execution, but the manual runs all yielded values within reasonable ranges of one another. The empirical results are as the common probabilistic solution suggests:

Iterations Stay wins Switch wins
Count 1,000,000 332,765 667,235
Percent 100 33.28 66.72

Here’s the source, try it out if you don’t believe me:

#!/usr/bin/python
# Mustafa Paksoy, April 08
# Usage: ./monty.py [iterations] [debug (optional)]
# Empirically evaluate the outcome of the Monty Hall problem.
from random import choice
from sys import argv

def safeRem(list,num):
  if num in list:
    list.remove(num)

correct_stay=0
correct_switch=0
tries=int(argv[1])
debug=len(argv)>2
for i in range(tries):
  car=choice(range(3))
  pick=choice(range(3))
  others=range(3)
  safeRem(others, car)
  safeRem(others, pick)
  monty=choice(others)
  others=range(3)
  others.remove(monty)
  others.remove(pick)
  newpick=others[0]
  if debug: print 'car', car, 'pick', pick, 'monty', monty, 'newpick', newpick,
  if newpick == car:
    correct_switch+=1
    if debug: print 'switch'
  elif pick == car:
    correct_stay+=1
    if debug: print 'stay'
  else:
    raise ValueError("you've got a bug")

print 'tries:', tries
print 'stay correct:', correct_stay, float(correct_stay)/tries
print 'switch correct:', correct_switch, float(correct_switch)/tries