import random
import seaborn as sns
sns.set(context="poster", style="dark")
import mpld3
def monty_hall():
doors = list(range(3))
car = randint(0,2)
old_choice = randint(0,2)
# reveal a door that is neither the car nor the choice and remove it from doors
for i in range(3):
if i != car and i != old_choice:
reveal = i
[ doors.remove(i) for i in [reveal, old_choice] ]
bayes_choice = doors[0] # can change to randomly selecting a member of the list for len(doors)>3
return car, old_choice, bayes_choice
def mass_monty(num_trials):
stubborn_wins = 0
bayes_wins = 0
stubborn_rate = zeros(num_trials)
bayes_rate = zeros(num_trials)
for i in range(num_trials):
car, old_choice, bayes_choice = monty_hall()
if old_choice == car:
stubborn_wins += 1
else:
bayes_wins += 1
stubborn_rate[i] = stubborn_wins / (i+1)
# print("\n", i+1, " st ", stubborn_wins, " ", stubborn_rate[i])
bayes_rate[i] = bayes_wins / (i+1)
# print(i+1, " bayes ", bayes_wins, " ", bayes_rate[i])
return stubborn_rate, bayes_rate
stubborn_rate, bayes_rate = mass_monty(100)
plot(stubborn_rate, '-', label="stubborn choice")
plot(bayes_rate, '-r', label="bayes choice")
legend(loc="best")
xlabel("trial")
ylabel("success rate")
mpld3.display()