Monty Hall problem

Lady Tasting Data
3 min readAug 18, 2023

--

Application of Bayes’ theorem & Simulations with R

Monty Hall problem: Which door would you choose?

Monty Hall problem is one of the well-known brain teasers in statistics which is fun to think about. It is named after Monty Hall, who was the host of popular American TV show “Let’s Make a Deal”. In the simplest form, we can describe the problem as following:

  • There are three doors (A, B, C) behind one of which there is a prize.
  • You choose one door (let’s say Door A).
  • Monty Hall opens one of the remaining doors (let’s say Door B), and you see no prize behind it.
  • Once you have learned that the prize is not behind Door B, would you still stick to your first decision (Door A), or would you change your mind and switch to the other door (Door C)?

Which door has a higher probability of having the prize behind, given that the prize is not behind Door B?

Let’s code this problem using R:

# If decision=="change", change the decision; otherwise don't change the decision
monty_hall <- function(decision,doors=c("A","B","C")) {
door_with_prize=sample(doors,1)
choice=sample(doors,1)
rm.door=sample(setdiff(doors,c(door_with_prize,choice)),1) # the door that Monty opens
new_doors=doors[-which(doors==rm.door)] # remaining doors
if (decision=="change") {
new_choice=sample(setdiff(new_doors,choice),1)
} else {
new_choice=choice
}
win=(new_choice==door_with_prize) # 1 if win prize; 0 otherwise
return(win)
}

Now, let’s simulate 1000 cases where we change our decision and save the results (1 (win) or 0 (not win)) for each of them:

result_change=matrix(NA,1000,1)
for (i in 1:1000) {
result_change[i]=monty_hall("change")
}

Let’s also simulate 1000 cases where we don’t change our first decision and save the results:

result_same=matrix(NA,1000,1)
for (i in 1:1000) {
result_same[i]=monty_hall("same")
}

Now, let’s compare the frequencies of winning the prize in these two scenarios:

# Frequency of winning when decision is changed
print(mean(result_change))
[1] 0.663

# Frequency of winning when decision is kept the same
print(mean(result_same))
[1] 0.321

# Number of 'winning' cases when decision is changed (out of 1000 cases))
print(sum(result_change))
[1] 663

# Number of 'winning' cases when decision is kept the same (out of 1000 cases)
print(sum(result_same))
[1] 321

As we can see, the frequency of winning the prize when we change our decision is higher (0.66) than the frequency when we stick to our first decision (0.32). More precisely, we end up winning the prize in 663 of the 1000 simulations when we changed our decision; and in 321 of the 1000 simulations when we didn’t change our decision.

From the simulation results, we are convinced that changing our decision would be a better move. Let’s also think about how we can calculate the theoretical probabilities of winning the prize in two scenarios. This is a nice application of Bayes’ theorem.

Let’s say we first chose Door A. With no additional information about the doors, the probability of the prize being behind any of the doors is 1/3.

So, for Door A:

P(A=1) = 1/3 (Probability of prize being behind Door A)

P(A=0) = 2/3 (Probability of prize not being behind Door A)

Let’s say Monty Hall opened Door B. Now we know that

P(B=1) = 0 and P(B=0) = 1.

Now, what is the probability of winning if we change our decision; i.e., the probability of prize being behind Door C?

As we now know that the prize is not behind Door B, we can calculate two probabilities for Door C, conditioned on the possible cases of Door A.

If the prize is behind Door A, then P(C=1|A=1) = 0.

If not, then P(C=1|A=0) = 1.

Therefore,

P(C=1) = P(C=1|A=0) * P(A=0) + P(C=1|A=1) * P(A=1)

P(C=1) = 1 * 2/3 + 0 * 1/3

P(C=1) = 2/3 (probability of prize being behind door C)

Similarly,

P(C=0) = P(C=0|A=0) * P(A=0) + P(C=0|A=1) * P(A=1)

P(C=0) = 0 * 2/3 + 1* 1/3

P(C=0) = 1/3 (probability of prize being behind door A)

Hence, if we switch our decision to Door C, our probability of winning the prize (2/3) would be higher.

Note that how close these probabilities are to the frequencies we had obtained by simulation previously. This is not coincidence, and we would expect that the frequencies we obtained by simulations would converge to the theoretical probabilities as the number of simulations is increased.

--

--

Lady Tasting Data

I write about statistics and data science, and occasionally about other topics which I find interesting.