## The Problem Statement

From the Riddler:

Now suppose Monty changes the rules. First, he will randomly pick a number of goats to put behind the doors: zero, one, two or three, each with a 25 percent chance. After the number of goats is chosen, they are assigned to the doors at random, and each door has at most one goat. Any doors that don’t have a goat behind them have an identical prize behind them.

At this point, you choose a door. If Monty is able to open another door, revealing a goat, he will do so. But if no other doors have goats behind them, he will tell you that is the case.

It just so happens that when you play, Monty is able to open another door, revealing a goat behind it. Should you stay with your original selection or switch? And what are your chances of winning the prize?

## The Simulation

A single round of this game will consist of selecting the number of goats to place behind the three doors, randomly assigning the goats to the doors (if not 0 or 3), and then randomly selecting a door. Then Monty will either reveal a goat and you will stay or switch, or he will tell you he cannot reveal a goat in which case you would be silly not to switch.

I will have the simulation return two values, both 0 or 1. The first value will be 0 if there are no goats behind the other two doors and a 1 otherwise. The second value will be a 0 if there are no prizes behind the other two doors and a 1 otherwise. (Notice that two 0’s is impossible).

oneMontyRound <- function(){
# First we simulate the number of goats.
goats <- sample(0:3,1)
# Next we set up the number of goats (0's) and prizes (1's)
doors <- c(rep(0,goats),rep(1,3-goats))
# Now randomize them behind the doors
doors <- doors[sample(1:3,3)]
# Now we select a door.
selectDoor <- sample(1:3,1)
# Are there any goats behind the other two doors?
goatLeft <- ifelse(any(doors[-selectDoor]==0),1,0)
# Are there any prizes behind the other two doors?
prizeLeft <- ifelse(any(doors[-selectDoor]==1),1,0)
# Return these values
return(c(goatLeft,prizeLeft))
}

set.seed(20200424)
oneMontyRound()
## [1] 1 1
oneMontyRound()
## [1] 1 0

Notice in the first simulation, after I selected a door, there was both a goat and a prize behind the remaining two doors. In the second simulation, there were only goats behind the remaining doors and no prizes.

For the full simulation, we will only want to keep track of those times in which there is a goat for Monty to show (since that is the condition we are working under) and not those times when there is not one to show. In building the simulation, I will assume that we will switch no matter what and calculate the proportion of wins under that assumption.

## Input N: the number of simulations
MontySim <- function(N){
# The number of times we play and have a goat to show has
# an initial value of 0
play <- 0
## the number of times we win a prize when Monty has a goat to show
## by SWITCHING
win <- 0
## simulate this until play reaches N
while(play<N){
## play a round!
round <- oneMontyRound()
## if there were no goats to show, skip this round and play another
if(round[1]==0){next}
## if there were goats to show, increase the play by 1
## and increase the wins if there was a prize left
else{
play <- play + 1
win <- win + round[2]
}
}
# Finally, return the proportion of wins
return(win/play)
}

set.seed(219)
MontySim(10000)
## [1] 0.4951
MontySim(10000)
## [1] 0.5069

Simulating 10000 times twice, we obtain a 49.51% and 50.69%. It appears that the proportion of wins is 50% assuming we switch every time. Let’s make sense of this mathematically.

## Mathematical Solution

To find the probability in question, we need to find the probability that a goat will be shown along with the compound probability that a goat will be shown AND we win a prize (assuming we switch).

There are exactly six scenarios of this experiment.

1. 0 goats are picked in which case it doesn’t matter which door we select, leaving Monty no goats to reveal and a certainty that we will win a prize.
2. 1 goat is picked and we select the door with the goat behind it, leaving no goats for Monty to show and a certainty that we will win a prize.
3. 1 goat is picked and we select a door without a goat behind it, leaving the goat for Monty to show and a certain chance of getting prize.
4. 2 goats are picked and we select a door with a goat behind it, leaving the only remaining goat for Monty to show and a certain chance of getting the prize.
5. 2 goats are picked and we select the door with the prize behind it, leaving either door for Monty to open and a certain chance of not getting the prize.
6. 3 goats are picked in which case it doesn’t matter which door we select, leaving Monty any door to reveal a goat and a certain chance of not getting the prize.

Each of these have the following probabilities: 1. 0.25 2. (0.25)(1/3) 3. (0.25)(2/3) 4. (0.25)(2/3) 5. (0.25)(1/3) 6. (0.25)

Letting G represent the event that a goat is available for Monty to show, and W the event that we win a prize, we are ultimately after $P(W|G) = \frac{P(G\cap W)}{P(G)}.$

The numerator is the sum of 3 and 4, while the denominator is the sum of 3, 4, 5, and 6. This gives: $P(W|G) = \frac{(0.25)*(2/3)+(0.25)*(2/3)}{(0.25)*(2/3)+(0.25)*(2/3)+(0.25)*(1/3)+(0.25)} = 0.5$

So, under the condition given, it does not matter if we stay or switch!