2022-1-21 19:25

The Monty Hall problem

The Monty Hall problem is a probability puzzle originally appeared about five decades ago in a popular American game show hosted by Monty Hall.  As it is written in Wikipedia webpage: https://en.wikipedia.org/wiki/Monty_Hall_problem, the puzzle can be described as follows. “Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, ‘Do you want to pick door No. 2?’ Is it to your advantage to switch your choice?”

Picture is downloaded from https://en.wikipedia.org/wiki/Monty_Hall_problem .

The correct answer is, ‘yes, to switch your choice will increase the probability to win the car.’ More specifically, if you stay with your original choice, you will have 1/3 chance to win the car; if you switch your choice, you will have 2/3 chance to win the car.

Note that in this game, the host should follow the following rules:

Rule 1: The host must always open a door that was not picked by you;

Rule 2: The host must always open a door to reveal a goat and never the door to reveal the car;

Rule 3: The host must always offer you the chance to switch between the originally chosen door and the remaining closed door or doors.

Let us generalize the Monty Hall problem a little bit.  Let N be the number of doors and N ≥ 3. Now let you replay the game.  What would be the probability for you to win the car if you take the option to switch your original choice? The answer is 1/N *(N-1)/(N-2).  For example, if the game is for you to start from choosing one of the five doors, you will have 1/5 * 4/3 = 4/15 chance to win the car if you switch your original choice.  You will only have 1/5 = 3/15 chance to win the car if you stay with your original choice.  It is the host’s action of opening a door that did not having the car before offering you the option to change your mind increased the chance for you to win the car.

Doors-3.neta

# 16 Oct 2021; r code for the Monty Hall 3-door problem

#  and the simulation verification of its generalized solution, assuming

#  the game participant will always change his/her first choice.

# propF: probability (in terms of proportion) function of the generalized solution

propF = function(Nt, Nopen,nn) {

# Nt: total number of the doors in the game setting, Nt>= 3

# Nopen: number of the doors that the game host will open, 0<= Nopen <= Nt-2

# nn: number of simulation runs for calculating the proportion

if(Nopen > (Nt-2)) stop("Nopen must be less than or equal to Nt!")

cumuprize = 0    # cumulated frequency that hits the grand prize

allchoice = 1:Nt

for(i in 1:nn) {

grandprize = sample(allchoice, 1) # door number behind which is a grand prize

choice1 = sample(allchoice, 1)

# This is the participant's first choice behind which door with a grand prize

if(length(allchoice[-c(grandprize, choice1)])==1)

{doorsopen = allchoice[-c(grandprize, choice1)]}

else  {doorsopen = sample(allchoice[-c(grandprize, choice1)],Nopen)}

if(length( allchoice[-c(doorsopen, choice1)]) == 1)

{choice2 = allchoice[-c(doorsopen, choice1)]}

else {choice2 = sample(allchoice[-c(doorsopen, choice1)],1)}

# This is the participant's second choice behind which door with a grand prize

if(choice2==grandprize) cumuprize = cumuprize + 1

}

cumuprize/nn    # resulting proportion that hits the grand prize

}      # end of the propF()

举个例子，游戏设置为：共有15扇门，主持人打开了10扇。以上的R代码这样使用：

nt = 15; nop = 10; n = 5000

out = propF(Nt = nt, Nopen = nop, nn=n); out    # simulation solution

out.theo = (1/(nt - nop - 1))*(1-1/nt); out.theo   # theoretical solution

allsimuProps = NULL

for(i in 1:200) {

out = propF(Nt = nt, Nopen = nop, nn=n)

allsimuProps = c(allsimuProps,out)}

summary(allsimuProps)

hist(allsimuProps, br=10, xlim=c(0,1))

abline(v=out.theo, col=2); abline(v=1/nt, col=3)

Min.                  1st Qu.                Median               Mean                  3rd Qu.                  Max.

0.2148               0.2300                 0.2332                0.2335                0.2374                 0.2530

（注意，因是模拟运算结果，每次运算因随机种子改变的缘故会有些微差别。若想每次得到同样的结果，需取一个固定的随机种子random seed.