Published Aug. 12, 2021, 6:45 p.m.
Welcome back. If you've made it this far then we've demonstrated the Monty Hall Problem "effect" but do you understand why it happens? Hopefully, by the end of this video you will.
Remember our original picture of the 3 doors after the reveal? The door highlighted red, we all agreed, had a 33% chance of being the correct door. Monty Hall reveals the 'whammy'. So now what? Well, we should switch.
Think of it this way: the box on the left represents a 33% chance of being correct. The box on the right represents a 66% chance of being correct. Since Monty Hall revealed one door that we know is NOT the correct answer, then the remaining door must have a 67% chance of being the right choice!
So what if we had 100 doors instead of just 3?
If we pick just one door and stick with it our chances should be 1 in 100 or a 1% chance of getting the prize.
Ok. Less talk more python.
First we need to change our total number of choices from 3 to 100. The easiest way to do that (in my opinion) is as follows. Instead of 'A', 'B' and 'C' we will switch to numbers.
# choices = ['A', 'B', 'C']
choices = [i for i in range(100)]
Next we need to establish how many doors Monty Hall will open. We will just set a variable to use when needed:
how_many_reveals = 10
Everything near the top of our loop stays the same. We still establish the correct choice, then the set of wrong choices, then we pick our choice and make sure it is in the list of available choices. Even the list of possible doors to reveal stays the same.
Our first change in the loop comes when we establish what doors to reveal (doors with no prize). We change our 'reveal' variable from being single valued to being a list of doors that Monty Hall opens.
# reveal = sample(reveals, 1)[0]
reveal = sample(reveals, how_many_reveals)
Now, as we move down, we can keep the switch feature as is and move on to the 'remaining_choices'. Here we need to change what is availalbe for us to select from. Again, python makes that pretty easy. Instead of c equaling 'reveal', we can say to exclude a choice if the value is 'in' the list called reveal:
# remaining_choices = [c for c in choices if not(c == my_choice or c == reveal)]
remaining_choices = [c for c in choices if not(c == my_choice or c in reveal)]
Finally, we need only to 'sample' from our list of remaining choices when we decide to switch:
if switch:
new_answer = sample(remaining_choices,1)[0]
else:
new_answer = my_choice
And that should do it. Now we can set our 'how_many_sims' variable to 100 and see what happens: And the first time I did it I got zero correct! Well, zero is close to 1 which is the number of times we expected (even though we are switching). Lets up the number of sims to 1000.
It still runs very fast and now we are seeing a number close to 1%. Lets up it to 10000. Its probably worthwhile to comment out the print statements that slow the program down at this point.
And with 10000 simulations it was 1.2% correct. Slightly higher than 1% which is what we expect. Since Monty Hall opened 10 doors that are the wrong answer, when we switch our chance is now 1 in 89 of being correct instead of 1 in 100. Those are slightly better odds and work out to being close to 1.2%
So now, what happens if Monty Hall opens 98 doors (the max he can open since you picked one and he has to leave at least one for you to pick.)? Lets look at the door picture again and see what we think:
So the small dashed line box represents 1%. The larger dashed line (big box minus the small box) is 99% of the choices. However, we know that all the boxes but one were opened in the big area. Therefore, the chance that the door available for switching is the correct choice is 99%.
Back to the python!
Simply change your 'how_many_reveals' variable from 10 to 98:
how_many_reveals = 98
Run the program again and: Boom. We guess right 99% of the time.
And now for something completely different:
Next time we will explore some more bells and whistles to make the code just that much more fancy.
skip_nextMonty Hall and the Python #6: Name those doors OR Generate any number of unique strings.