Oct. 14th, 2006

pmb: (Default)
The Monty Hall problem came up recently, and I just wanted to toss my hat into the "providing an explanation" ring. Actually, I want to abdicate that role. Instead of proving this by logic, let's just observe that the smug bastards who say "switching will win 2/3 of the time" are right by simulating 10,000 rounds of the actual game.

import random
random.seed()

num_tests = 10000
stay = 0 # The number of times the "stay" strategy would have won
switch = 0 # The number of times the "switch" strategy would have won

# Test all this crap many many times
for _ in xrange(num_tests):
    # The mystery door gets chosen
    secret = random.choice([1,2,3])

    # The player chooses a door
    choice = random.choice([1,2,3])

    # Monty reveals an open door.

    possible_reveal = {1:None, 2:None, 3:None}

    # But he only reveals a door that has nothing behind AND that you have not 
    # yet chosen
    del possible_reveal[secret]
    if choice in possible_reveal:
        del possible_reveal[choice]

    reveal = random.choice(possible_reveal.keys())

    # Now, if choice == secret, then the "stay" strategy will win
    if choice == secret:
        stay += 1
    # But if the chosen door doesn't contain the secret, then switching will win
    else:
        switch += 1

print "Staying won the prize", stay, "times"
print "Switching won the prize", switch, "times"


Now, we run this code....
$ python mh.py 
Staying won the prize 3335 times
Switching won the prize 6665 times

$ python mh.py 
Staying won the prize 3295 times
Switching won the prize 6705 times

$ python mh.py 
Staying won the prize 3296 times
Switching won the prize 6704 times


And we can see that those smug jerks are almost certainly correct. Now the problem becomes one of reasoning out why, rather than convincing ourselves that a premise is true. This also illustrates what is, in my opinion, an excellent use of computers in mathematical education. Rather than accepting proofs on faith, let's verify them both with logic and with experimental trials. This should allow students to gain some intuition about the problems at hand.

Profile

pmb: (Default)
pmb

October 2009

S M T W T F S
    1 23
45678910
11121314151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 30th, 2025 08:45 pm
Powered by Dreamwidth Studios