<jupyter_start><jupyter_text>The Monty Hall Problem See the [Wikipedia entry](https://en.wikipedia.org/wiki/Monty_Hall_problem) for a summary of the problem.(But don't rely on Wikipedia for any so called facts!)  Random door selection Here's some code to pick a door at random.<jupyter_code># Python provides a library called random to generate pseudo-random numbers and do stuff with them.
import random

# The three doors in a list.
doors = ['yellow', 'red', 'blue']

# Pick a random door.
print(random.choice(doors))<jupyter_output>blue<jupyter_text>The pick is meant to give a one third probability to each door. Let's pick 10,000 doors are see if that looks correct.<jupyter_code># 10,000 random doors.
tenthous = [random.choice(doors) for i in range(10000)]

tenthous<jupyter_output><empty_output><jupyter_text>Let's plot it now, and see that each door is picked about a third of the time.<jupyter_code>import seaborn as sns
import matplotlib.pyplot as pl

pl.figure(figsize=(10, 6))
sns.set(style="darkgrid")
ax = sns.countplot(y=tenthous)
pl.show()<jupyter_output><empty_output><jupyter_text>Simulate the game Let's simulate the game now. Let's:1. Pick a door to put the car behind.2. Have the contestant pick a door.3. Have the show host open one of the other doors to reveal a goat.4. Ask the contestant if they want to switch.5. See how often the contestant wins.The question we're looking to answer is whether staying with your original pick makes a difference.<jupyter_code># A function to simulate a game and tell us if the contestant wins.
def simulate(stay=True):
    doors = ['yellow', 'red', 'blue']
    
    # Put the car behind a random door.
    car = random.choice(doors)
    
    # Have the contestant pick a door.
    pick = random.choice(doors)
    
    # Open a door with a goat.
    show = random.choice([door for door in doors if door != car and door != pick])

    # Figure out which door was not opened or picked.
    notopen = [door for door in doors if door != pick and door != show][0]
    
    return (car == [pick, notopen][not stay])<jupyter_output><empty_output><jupyter_text>So, we can simulate a game in which the contestant stays with their original pick by running the following. A return value of True means they won the car, False means they didn't.<jupyter_code>simulate(stay=True)<jupyter_output><empty_output><jupyter_text>Ten thousand times each Let's run the game 10,000 times where the contestant stays, and then 10,000 where they switch. Then we'll see how often they win in each case.<jupyter_code>staying = [simulate(stay=True) for i in range(10000)]<jupyter_output><empty_output><jupyter_text>Let's plot the result of staying.<jupyter_code>ax = sns.countplot(y=staying)
pl.show()<jupyter_output><empty_output><jupyter_text>Looks like when the contestant stays, they win only about a third of the time.<jupyter_code>switching = [simulate(stay=False) for i in range(10000)]
ax = sns.countplot(y=switching)
pl.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0911214953271028, 'fraction_numerical': 0.014686248331108143, 'mean_word_length': 4.876470588235295, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '116769', 'license': 'Apache-2.0', 'n_tokens_mistral': 985, 'n_tokens_neox': 923, 'n_words': 368, 'path': '52446 Fundamentals of Data Analysis/monty-hall.ipynb', 'repo_name': 'MichaelORegan/Lecture-Notebooks', 'size': '2996'}