Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car and 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?
</br> <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Monty_open_door.svg/440px-Monty_open_door.svg.png"/ height="100" width="350">Always change your choice of door because:
* Sticking with your original door: you will only win one out of every three times you play
the game
* Change your selection: you will win two out of every three times you play.
Seems very not intuitive!!!
Few Intuitive aids (link) :
* think the really crucial thing that people overlook is not their first choice,
but the host's choice, and the assumption that the host made sure not to
reveal the car.It is to your advantage to switch if the host is making sure
only to reveal a goat. On the other hand, if the host picked randomly between
doors 2 and 3, and happened to reveal a goat, then there is no advantage to switching.
* Change the problem to 100 door close .
based mostly on link
In [64]:
import random
class MontyHall(object):
def __init__(self,switch_strategy=True):
self.switch_strategy = switch_strategy
def pick_door(self):
return random.randint(1,3)
def run_game(self):
prize_door = pick_door()
participent_door = pick_door()
# host valid door one of the doors participant didnt pick . all the doors together sumed to 6
# so we substract the two door we dont want and pick one of the options
host_door = pick_door()
host_door = random.choice([6 - participent_door - prize_door,
6 - participent_door - host_door])
if self.switch_strategy:
# all the doors together sumed to 6 so to get the new door we need to subscract both
# our first pick and the host pick
participent_door = 6 - participent_door - host_door
return participent_door == prize_door
class simulation(object):
def __init__(self,switch_strategy=True, iterations=10000):
self.iterations = iterations
self.switch_strategy = switch_strategy
self.m = MontyHall(switch_strategy)
self.perc_win = 0
def run_simulation(self):
# self.m.run_game() return indicator if player one win or not
# since bool are ints we can sum the result for total # of wins
wins = sum(self.m.run_game()
for i in range(self.iterations))
losses = self.iterations-wins
self.perc_win = 100.0*wins / (wins+losses)
return self
def show_stats(self):
print("Monty Hall simulation with{} switching with {} iterations".format('' if self.switch_strategy else 'out',
self.iterations))
print("\tprecentage of winning is: {}% \n"
"\tprecentage of lossing is: {}%\n".format(self.perc_win,
100-self.perc_win))
simulation(switch_strategy=True).run_simulation().show_stats()
simulation(switch_strategy=False).run_simulation().show_stats()
In [ ]: