<jupyter_start><jupyter_text>Monty Hall gameStep 1: What to Simulate  Step 2: Simulating one play   Step 3: Simulating many plays  Step 4: Visualizing results<jupyter_code># Step 1: What to Simulate

# we need to keep track of what is behind each of the doors
# the one the contestant first picks: guessed
# the one that Monty opens: revealed
# the remaining door: remaining
# first, let us build a function that, given one goat, returns the other goat
goats = ['goat one', 'goat two']    
def other_goat(x):
    if x == 'goat one':
        return 'goat two'
    if x == 'goat two':
        return 'goat one'
other_goat(x='goat one')
other_goat('goat two')
outcome = other_goat('goat one')
outcome
# to make a random choice of an element in a list, use np.random.choice
import numpy as np
np.random.choice(goats)
# Step 2: Simulating one play 

# there are three things to keep track of
# guess
# revealed
# remaining

hidden_behind_doors = ['car', 'goat one', 'goat two']
guessed = np.random.choice(hidden_behind_doors)

print(guessed)

# there are two cases to consider
if 'goat' in guessed:
    revealed = other_goat(guessed)
    print('guess: ', guessed, 'revealed: ', revealed)

if guessed == 'car':
    revealed = np.random.choice(goats)
    print('guess: ', guessed, 'revealed: ', revealed)
# now we wrap all this in a function

def monty_hall_game(x, y):
    '''
    this function simulates one game
    
    Arguments
    x: list of what is behind the doors
    y: list of the less desirable prizes
    
    Returns
    [guessed, revealed, returned]
    '''
    
    guessed = np.random.choice(x)
    
    if 'goat' in guessed:
        revealed = other_goat(guessed)
        remaining = 'car'
        
        return [guessed, revealed, remaining]
    
    if guessed == 'car':
        revealed = np.random.choice(y)
        remaining = other_goat(revealed)
        
        return [guessed, revealed, remaining]
monty_hall_game(hidden_behind_doors, goats)
monty_hall_game(hidden_behind_doors, goats)
monty_hall_game(hidden_behind_doors, goats)
# Step 3: Simulating many plays
number_of_plays = 10000

results = []
for i in range(number_of_plays):
    results.append(monty_hall_game(hidden_behind_doors, goats))
# turn the list into an array in order to use more flexible indexing
results_arr = np.array(results)

results_arr[:, 0] == 'car'
# summing over booleans
sum(results_arr[:, 0] == 'car')
# doing the counting with lists (several lines)
counts = []
for hidden in hidden_behind_doors:
    count = sum(results_arr[:, 0] == hidden)
    counts.append(count)
print(counts)
# doing the counting with list comprehension (one lines)


# introducing list comprehension
values = []
for i in range(10):
    two_i = 2*i
    values.append(two_i)
print(values)
values = [2*i for i in range(10)]
print(values)
[sum(results_arr[:, 0] == hidden) for hidden in hidden_behind_doors]
# wrapping it all in a function

def simulation(number_of_plays):
    results = []
    hidden_behind_doors = ['car', 'goat one', 'goat two']
    goats = ['goat one', 'goat two']

    for i in range(number_of_plays):
        results.append(monty_hall_game(hidden_behind_doors, goats))

    results_arr = np.array(results)
    
    # taking advatage of boolean representation as 0 and 1
    # and using list comprehension
    guessed_counts = [sum(results_arr[:, 0] == hidden) for hidden in hidden_behind_doors]
    remaining_counts = [sum(results_arr[:, 2] == hidden) for hidden in hidden_behind_doors]
    
    return guessed_counts, remaining_counts
guessed_counts, remaining_counts = simulation(10000)
#Step 4: Visualizing results
# we will use the library matplotlib to make plots

import matplotlib.pyplot as plt

plt.plot(guessed_counts)
plt.plot(remaining_counts)
# here is a bar plots with appropriate labels etc.

x = np.arange(len(hidden_behind_doors))  # the label x-axis locations
width = 0.35  # the width of the bars

plt.figure()
plt.bar(x - width/2, guessed_counts, width, label='original door')
plt.bar(x + width/2, remaining_counts, width, label='remaining door')

# Add some text for labels, title and custom x-axis tick labels, etc.
plt.ylabel('count')
plt.title('simulated monty hall games')
plt.xticks(ticks=x, labels=hidden_behind_doors)
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10153702841173731, 'fraction_numerical': 0.008616674429436423, 'mean_word_length': 3.994186046511628, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '650787', 'license': 'MIT', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1385, 'n_words': 462, 'path': 'Day2/monty_hall_game_complete.ipynb', 'repo_name': 'nikparth/mathtools-bootcamp2019', 'size': '4294'}