<jupyter_start><jupyter_text>The Monty Hall problemThe [Monty Hall problem](https://en.wikipedia.org/wiki/Monty_Hall_problem) isa problem in probability, originally posed by [SteveSelvin](https://en.wikipedia.org/wiki/Steve_Selvin), a professor ofBiostatistics at Berkeley.The setup is the following:* You are a contestant on a game show.* The host, [Monty Hall](https://en.wikipedia.org/wiki/Monty_Hall), shows you  three closed and identical doors.* Behind one of the doors, is a car.  Behind the other two doors, there is  a goat.![Monty Hall illustration]({{ site.baseurl }}/images/monty_hall_goat.png)* Assume for a moment you'd rather have a car than a goat.* Monty offers you the choice of any of the three doors.  You chose a door, but  Monty leaves the door closed for now.* Monty tells you he is going to open one of the other doors that has a goat  behind it.  He does, there is a goat behind it.  Call this "the goat reveal".* Now he asks you the following question:  Do you want to stick with your  original choice of door, do you want to change your choice to the remaining  door, or does it make no difference which you chose?It turns out this is a trickier problem than it might first appear.  Among manyothers, a very famous mathematician [PaulErdős](https://en.wikipedia.org/wiki/Paul_Erd%C5%91s), got the answer wrong. Hehad to be convinced with a computer simulation.  That's what we will do now.For the simulation, we will need:* `np.random.choice` \- see [the iteration page](../05/iteration) for an  introduction.* `np.sort` \- see [sorting arrays](sorting_arrays).  The simulationAs ever, we start with the simplest thing we can think of, which is to simulateone trial.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>First we make an array of the things that can be behind the doors.  There are two goats and one car.<jupyter_code>doors = np.array(['car', 'goat', 'goat'])
doors<jupyter_output><empty_output><jupyter_text>Next we shuffle, to simulate the fact that the object behind each door is random, on each trial.<jupyter_code>np.random.shuffle(doors)
doors<jupyter_output><empty_output><jupyter_text>Next we use `np.random.choice` to choose randomly between the three doors.  Wechoose one of 0, 1, or 2 for the first, second and third door, respectively.<jupyter_code>my_door_index = np.random.choice([0, 1, 2])
my_door_index<jupyter_output><empty_output><jupyter_text>We peek behind our selected door to see what we would have got, if we stayed with our selected door.<jupyter_code>stay_result = doors[my_door_index]
stay_result<jupyter_output><empty_output><jupyter_text>Next we replace whatever was behind our chosen door, with the string "mine", to indicate this was the one we chose.<jupyter_code>doors[my_door_index] = 'mine'
doors<jupyter_output><empty_output><jupyter_text>We now have two possibilities.  The two remaining doors could have:1. "car" and "goat" (in either order) or1. "goat" and "goat".We can use `np.sort` to make it more obvious which situation we are in.  Itwill put "car" in the first position, if it's present, then "goat". Last willbe the string "mine" that we put in when we chose our door.<jupyter_code>doors = np.sort(doors)
doors<jupyter_output><empty_output><jupyter_text>When Monty does his goat reveal, our two options above drop to one.* "car" and "goat" become "car"* "goat" and "goat" become "goat"All we need to do then, is to take the first element in the sorted array.  Itwill be "car" if the car was present, otherwise it will be "goat".<jupyter_code>switch_result = doors[0]
switch_result<jupyter_output><empty_output><jupyter_text>Your turn - try many trialsThat's one trial.   Now let's do that 10000 times.  Fill in the code you need from the statements above.<jupyter_code># Make 10000 trials.
n_tries = 10000
# Array of 10000 length 4 (or less) strings, to store results of stay strategy
stay_results = np.zeros(n_tries, dtype='U4')
# 10000 length 4 (or less) strings, to store results of switch strategy
switch_results = np.zeros(n_tries, dtype='U4')
# Use a "for" loop to repeat the indented block 10000 times.
for i in range(n_tries):
    # Same code as above, for one trial
    # Make the doors array
    doors = np.array(['car', 'goat', 'goat'])
    # Shuffle

    # Choose your door at random

    # Get the result from your chosen door

    # Fill your chosen door with 'mine'

    # Sort the doors.  The car will be first if present.

    # Get the result for switch

    # Store the results for stay and switch in their arrays<jupyter_output><empty_output><jupyter_text>Check the proportion of the Stay choices that resulted in a "car".<jupyter_code>np.count_nonzero(stay_results == 'car') / n_tries<jupyter_output><empty_output><jupyter_text>Check the proportion of the Switch choices that resulted in a "car".<jupyter_code>np.count_nonzero(switch_results == 'goat') / n_tries<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08734511476741824, 'fraction_numerical': 0.009750152346130409, 'mean_word_length': 4.939686369119421, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 4, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '489630', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1488, 'n_words': 630, 'path': 'notebooks/06/monty_hall.ipynb', 'repo_name': 'matthew-brett/cfd-uob', 'size': '4923'}