R Tutorial: Monty Hall

The Monty Hall problem isn't 50/50 when you switch doors. This tutorial simulates both strategies in R, showing why switching wins more often, and walks through the code for random door selection and modeling the host's reveal.
Chapters
Another well known probability puzzle is Monty hall, which is named after the host of a game show called let's Make a Deal. In this puzzle, a contestant is shown three doors. Behind one of the doors is a prize and the other 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 one. The host Monty hall then opens one of the other doors that does not contain the prize. The contestant has a to stick or switch. To stick means to stay with the original choice here door number one and to switch means to choose the remaining door here door number three. 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 5050 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. 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 one and the prize is behind door number two, then the only possible door that the host can reveal is is door number three. From a coding standpoint, we must exclude doors one and two 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 three. Note that in general, we can reference elements of the door's object by supplying either an explicit numeric index or or a variable name. Here we knew that door number one was the choice and door number two contained the prize. So we could supply the numbers one and two 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 two as it was here. 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 one. But if it were random, we would have to assign it a variable name and then access it using that name. Let's use these concepts to simulate our solution to the model.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







