Algorithms - Monty Hall Paradox
bogotobogo.com site search:
Monty Hall Paradox
Since there are numerous descriptions on Monty Hall Problem, I do not want to make another one here.
Actually, I'm not convinced by the explanations/arguments to this day. However, after seeing the simulations, I finally realized we need to switch the door to increase the odds of winning the prize.
Picture from Monty Hall problem - wiki
Monty Hall Paradox in Python
#!/usr/bin/python2 import random class MontyHall: def __init__(self): self.prize_door = random.randint(1,3) self.selected_door = 0 self.opened_door = 0 def select_door(self): self.selected_door = random.randint(1,3) def host_door(self): # host door should not be the selected nor prize door d = random.randint(1,3) while d == self.selected_door or d == self.prize_door: d = random.randint(1,3) self.opened_door = d def switch_door(self): # sum of door numbers = 1+2+3 = 6. self.selected_door = 6 - self.selected_door - self.opened_door def hit(self): if self.selected_door == self.prize_door: return True def run(self, switch=True): self.select_door() self.host_door() if switch: self.switch_door() return self.hit() # hit? # Switch RUNS = 10000 print "%s Monty runs" %(RUNS) hits = 0 for i in range(RUNS): monty = MontyHall() if monty.run(switch=True): hits += 1 print "Switch : %.1f%%" %(100.*hits/RUNS) # Stay hits = 0 for i in range(RUNS): monty = MontyHall() if monty.run(switch=False): hits += 1 print "Stay : %.1f%%" %(100.*hits/RUNS)
Run:
10000 Monty runs Switch : 66.3% Stay : 33.5%
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization