#Cleaning environment
rm(list=ls())
#Setting up the normal scenario of choosing a door
set.seed(1) #to control system randomization
doors = c(1,2,3)
win_counter = 0 #initialization
for(i in 1:10000) #simulating the game 10,000 times to understand probability
{
prize_in_door = sample(doors,size = 1)
my_choice = 1 #we can choose any 1 of the 3 doors, doesn't make a difference
if(my_choice == prize_in_door)
{
win_counter = win_counter + 1
}
}
print(win_counter/10000)
## [1] 0.3424
This shows that winning probability is ~33% when you choose 1 of the 3 doors in a game with one door having the prize. This makes a lot of sense.
If you stick with your choice after the host opens an empty door, your probability of winning will remain ~33% as shown above.
Note: When you chose to be not switch your choice of door, you neglected the extra information provided by the host, which immediately helps you gain advantage in this game
Now, let’s say you choose to switch to the remaining door when one of the doors you didn’t select is opened as it is empty as the host mentioned.
What will be the winning probability if you switch everytime you are offered a choice to do so ?
door_to_reveal = function(doors, prize_in_door, my_choice)
{
if(prize_in_door == my_choice)
{
reveal = sample(doors[-my_choice],size = 1)
}
else
{
reveal = doors[-c(my_choice,prize_in_door)]
}
}
What will be the winning probability if you switch every time you are offered a choice to do so ?
#Calculating probability of winning when you switch
set.seed(1)#to control system randomization
win_counter = 0 #initialization
for(i in 1:10000)
{
prize_in_door = sample(doors, 1)
my_choice = 1
reveal = door_to_reveal(doors,prize_in_door,my_choice) #host reveals a door and gives us the choise to stick or switch our choice
new_choice = doors[-c(my_choice,reveal)]
if(prize_in_door == new_choice)
{
win_counter = win_counter + 1
}
}
print(win_counter/10000)
## [1] 0.6647
So if you always switch when an empty door is shown to you after your initial choice, you will win this game ~67% of the time. Simulations certainly help, don’t they ?
Note: Most people believe that when you are offered a choice, probability is 50/50 to get the correct answer, this can be attributed to them seeing 2 doors remaining and they have to choose 1, but as simulations show, that certainly is not the case. :)