solutions:
part 1: no switch
monty_3doors_noswitch <- function(){
doors = c(1,2,3) # had 3 doors
choice1 = sample(doors, 1) # 1st choice: pick one door at random
correct = sample(doors, 1) # the correct box
# Assume When making the second choice, you stick with the original choice and you are RIGHT
# let X be a binary variable that takes the value 1 if the choice is correct and 0 if incorrect
x = 0
x = ifelse(choice1 == correct,1,0) # you stick with the original choice and you are RIGHT
x
}
part 2: switch
monty_3doors_switch = function(){
doors = c(1,2,3) # had 3 doors
choice = sample(doors, 1) # 1st choice: pick one door at random
correct = sample(doors, 1) # the correct box
# When making the second choice, you stick with the original choice and you are WRONG.
# let X be a binary variable that takes the value 1 if the choice is correct and 0 if incorrect
x = 0
x = ifelse(choice != correct,1,0) # you stick with the original choice and you are WRONG.
x
}
part 3: empirical probablility of winning for the two experiments.
no switch: run 1 million simulations and obtain the probability = 0.333826
number_of_successes = replicate(1000000, monty_3doors_noswitch())
probability = sum(number_of_successes)/1000000
probability
switch: run 1 million simulations and obtain the probability = 0.665828
number_of_successes = replicate(1000000, monty_3doors_noswitch())
probability = sum(number_of_successes)/1000000
probability
part 4: Compare your answers with the actual theoretical predictions.
no switch: The theoretical probability of success is
"\\frac{1}{3} = 0.3333"switch: The theoretical probability of success is
"\\frac{2}{3} = 0.6667"
Comments
Leave a comment