Robert Norberg
July 24, 2019
You can find these slides here: https://rpubs.com/rnorberg/monty-hall
doors <- c(1:3)
correct_door <- sample(doors, 1)
first_guess <- sample(doors, 1)
“Stubborn”
You make a first guess at random and no matter what the host does, you stick to your guns. You're sure he's only trying to bait you!
“Switch”
You pick a door at random, then the host reveals a door, eliminating a wrong answer. Then you switch from your original guess to the remaining door and hope for the best!
final_guess <- first_guess
can_be_revealed <- doors[doors != correct_door & doors != first_guess]
door_to_reveal <- sample(can_be_revealed, 1)
Finally, you switch from your first guess to whichever remaining door that has not been revealed
final_guess <- doors[doors != door_to_reveal & doors != first_guess]
if (final_guess == correct_door) {
result <- "Winner!"
} else {
result <- "Sorry :("
}
result
[1] "Sorry :("
monty_hall <- function(strategy) {
# set things up
doors <- c(1:3)
correct_door <- sample(doors, 1)
first_guess <- sample(doors, 1)
# now the host reveals a door
can_be_revealed <- doors[doors != correct_door & doors != first_guess]
door_to_reveal <- sample(can_be_revealed, 1)
# and offers me to switch
if (strategy == "stubborn") {
final_guess <- first_guess
} else if (strategy == "switch") {
final_guess <- doors[doors != door_to_reveal & doors != first_guess]
}
# did I win?
if (final_guess == correct_door) "Winner!" else "Sorry :("
}
monty_hall("stubborn")
[1] "Sorry :("
monty_hall("switch")
[1] "Winner!"
If I can run a program once, I can run it many times.
stubborn_results <- vector(mode = "character", length = 10000)
for (i in 1:10000) {
stubborn_results[i] <- monty_hall("stubborn")
}
switch_results <- vector(mode = "character", length = 10000)
for (i in 1:10000) {
switch_results[i] <- monty_hall("switch")
}
table(stubborn_results)
stubborn_results
Sorry :( Winner!
6791 3209
table(switch_results)
switch_results
Sorry :( Winner!
5142 4858
If this captured your interest and you're interested in learning R, we recommend dataquest.io's R courses. And we don't just recommend them, we'll pay for them!
Check out rstudioserverpro.bcbsfl.com for details.
Come see my talk at TechCon on Tuesday, July 30 from 2:00 PM to 2:30 PM or Thursday August 1 from 11:00 AM to 11:30 AM (TechCon registration link)
You can find these slides here: https://rpubs.com/rnorberg/monty-hall