Created
April 24, 2020 19:07
-
-
Save StewSchrieff/e809fabced4f55db30535576f4a4302c to your computer and use it in GitHub Desktop.
Simulation for Riddler Classic (https://fivethirtyeight.com/features/can-you-beat-the-goat-monty-hall-problem/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stay_value 0.377298 | |
switch_value 0.500682 | |
dtype: float64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import random | |
def create_stage(): | |
# Returns a list of 3 "doors", with either 'prize' or 'goat' as specified in puzzle | |
num_goats = np.random.randint(0, 4) | |
# print(num_goats) | |
doors = [] | |
while num_goats > 0: | |
doors.append('goat') | |
num_goats -= 1 | |
while len(doors) < 3: | |
doors.append('prize') | |
random.shuffle(doors) | |
return doors | |
if __name__ == '__main__': | |
results = [] | |
for i in range(1, 100000): | |
doors = create_stage() | |
# Contestant always chooses door #1 (or rather door 0) | |
contestant_chosen = doors.pop() | |
# print(contestant_chosen) | |
# If monty is able, he will open a door with a goat (remove from the list of remaining doors) | |
if 'goat' in doors: | |
# print(doors) | |
doors.remove('goat') | |
# print(len(doors)) | |
results.append({'contestant': contestant_chosen, 'switch': doors[0]}) | |
# save the contestants door and the door if you were to switch | |
# else: | |
# print('monty cant open another door...') | |
results_df = pd.DataFrame(results) | |
results_df['stay_value'] = results_df['contestant'] == 'prize' | |
results_df['switch_value'] = results_df['switch'] == 'prize' | |
print(results_df.mean(axis=0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment