Scripting to Solve the Monty Hall Problem

Robert Norberg
July 24, 2019

First Things First

You can find these slides here: https://rpubs.com/rnorberg/monty-hall

The Monty Hall Problem

Verify by Simulation

doors <- c(1:3)
correct_door <- sample(doors, 1)
first_guess <- sample(doors, 1)

Two Strategies

“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!

"Stubborn" Strategy

final_guess <- first_guess

"Switch" Strategy

  • The host must reveal what is behind one door after your first guess.
  • He cannot reveal the prize.
  • He cannot reveal what is behind the door you've chosen with your initial guess.
  • If your first guess is wrong, he must reveal the only remaining incorrect door.
can_be_revealed <- doors[doors != correct_door & doors != first_guess]
door_to_reveal <- sample(can_be_revealed, 1)

"Switch" Strategy

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]

Did I Win?

if (final_guess == correct_door) {
  result <- "Winner!"
} else {
  result <- "Sorry :("
}
result
[1] "Sorry :("

Putting it All Together

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 :("
}

Putting it All Together

monty_hall("stubborn")
[1] "Sorry :("
monty_hall("switch")
[1] "Winner!"

The thing about code is...

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")
}

And the winner is...

table(stubborn_results)
stubborn_results
Sorry :(  Winner! 
    6791     3209 
table(switch_results)
switch_results
Sorry :(  Winner! 
    5142     4858 

Interested?

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