no switch:
# Let "n" be the number of doors available
monty_ndoors_noswitch = function(n){
doors = 1:n # Total available doors
choice = sample(doors, 1) # 1st choice: pick one door at random
correct = sample(doors, 1) # the correct box
# After the 1st pick, "n-1" doors are left. "n-2" doors that don't have the prize are opened so that only 2 doors remain closed i.e. the choice made at first and one other door. The correct door with a prize must be one of the two
# if the first pick was correct, any one of the remaining "n-1" doors is left. but if the first choice is wrong, then the door with the prize is left and the others removed.
last_choice = ifelse(choice == correct, sample(doors[doors!= correct], 1), correct)
# let X be a binary variable that takes the value 1 if the choice is correct and 0 if incorrect
x = 0
# you win if you stick with the original choice
x = ifelse(choice == correct ,1,0)
x
}
switch:
monty_ndoors_switch = function(n){
doors = 1:n # Total available doors
choice = sample(doors, 1) # 1st choice: pick one door at random
correct = sample(doors, 1) # the correct box
# After the 1st pick, "n-1" doors are left. "n-2" doors that don't have the prize are opened so that only 2 doors remain closed i.e. the choice made at first and one other door. The correct door with a prize must be one of the two
# if the first pick was correct, any one of the remaining "n-1" doors is left. but if the first choice is wrong, then the door with the prize is left and the others removed.
last_door = ifelse(choice == correct, sample(doors[doors!= correct], 1), correct)
# let X be a binary variable that takes the value 1 if the choice is correct and 0 if incorrect
x = 0
# you win if you shift from the original choice to the other remaining door
x = ifelse(last_door==correct ,1,0)
x
}
part 3:
To verify, let "n = 3" and run "1,000,000" simulations
we expect the probability of winning if you stick with the initial door to be "0.3333".
wins = replicate(1000000, monty_ndoors_noswitch(3))
sum(wins)/1000000
we get "P=0.3333" from the simulation.
we expect the probability of winning if you stick with the initial door to be "0.6667"
wins = replicate(1000000, monty_ndoors_switch(3))
sum(wins)/1000000
we get "P = 0.6669" from the simulation
Therefore, the empirical probabilities are consistent with the theoretical
Comments
Leave a comment