In: Computer Science
Using Rstudio
# 1. Monty-Hall Three doors
Recall the Monty-Hall game with three doors, discussed in class. Run a simulation to check that the probablility of winning increases to 2/3 if we switch doors at step two.
Set up the experiment two functions "monty_3doors_noswitch" and "monty_3doors_switch" (these functions will have no input values):
```{r}
monty_3doors_noswitch <- function(){
}
monty_3doors_switch <- function(){
}
```
Use your two functions and the replicate function to compute the
empirical probablility of winning for the two experiments.
Compare your answers with the actual theoretical predictions.
```{r}
```
monty3doors_no_switch.r:
monty3doors_no_switch <- function(){
doors = c(1,2,3) # had 3 doors
ch1 = sample(doors, 1) # 1st choice: pick one door at random
crct1 = 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(ch1 == crct1,1,0) # you stick with the original choice
and you are RIGHT
x
}
no_of_Successes = replicate(1000000, monty3doors_no_switch())
prob = sum(no_of_Successes)/1000000
probability = prob
probability
Output Screenshot:
monty_3doors_switch.r:
monty_3doors_switch = function(){
doors = c(1,2,3) # had 3 doors
ch = sample(doors, 1) # 1st choice: pick one door at random
crct = 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(ch != crct,1,0) # you stick with the original choice and
you are WRONG.
x
}
no_of_Successes = replicate(1000000,
monty_3doors_switch())
prob = sum(no_of_Successes)/1000000
probabilty = prob
prob
Output
Screenshot:
Compare your answers with the actual theoretical predictions: