1. Monty Hall
Another well-known probability puzzle is Monty Hall, which is named after the host of a game show called Let's Make a Deal.
2. Choose one of the doors
In this puzzle, a contestant is shown three doors. Behind one of the doors is a prize, and the other two doors contain no prize. Historically, the prize was a car and the non-prizes were goats. The game begins with the contestant picking a door. Suppose that the contestant has chosen Door number 1.
3. One door is revealed
The host, Monty Hall, then opens one of the other doors that does not contain the prize. The contestant has a choice, to stick or switch? To stick means to stay with the original choice, here Door number 1, and to switch means to choose the remaining door, here Door number 3. Which strategy gives the greatest probability of winning the prize, and what are the probabilities with each strategy?
A common misconception is that sticking or switching both give a 50 50 chance of winning since there are only two remaining doors. In this puzzle, we will write code to simulate both strategies to demonstrate the true win probability in each case.
4. Revealing a door with reverse indexing
To solve this problem, we need to handle some R indexing, specifically when determining which door the host should reveal. Consider the doors object, which contains the values 1, 2, and 3, to represent each door.
If the contestant chooses door number 1, and the prize is behind door number 2, then the only possible door that the host can reveal is door number 3.
From a coding standpoint, we must exclude doors 1 and 2 from the possibilities of doors to reveal. We can do this by using a minus index with the c concatenator to remove doors 1 and 2 as possibilities. Then, the value of the reveal is only door number 3.
Note that, in general, we can reference elements of the doors object by supplying either an explicit numeric index or a variable name. Here, we knew that Door number 1 was the choice and Door number 2 contained the prize, so we could supply the numbers 1 and 2 in the reverse indexing.
In our exercises, the prize door will be chosen randomly, so it must be coded by a variable name that contains the value of the prize door, rather than simply with the number 2, as it was here.
5. Revealing a door at random
In the previous slide, the initial choice and the prize door were different, leaving only one remaining door for the host to reveal, since the host will only reveal a door with no prize.
If the initial choice is the same as the prize door, then there are two possible doors for the host to reveal. In this case, it must be random, which can be done using the sample function. What we want to sample is the doors object with the initial choice removed from it using the negative indexing strategy from the previous slide.
Here, the prize and initial choice were Door number 1, but if it were random, we would have to assign it a variable name and then access it using that name.
6. Let's try it!
Let's use these concepts to simulate our solution to the Monty Hall puzzle.