The Monty Hall Problem - Probability Index

  • What is the Monty Hall Problem?

When playing a game, the contestant chooses one out of three options. Two out of three options are a flop. After the contestant chooses a door, the host will reveal one of the bad options and asks the contestant if they want to stick with their answer or switch. The goal is to decide whether to switch or stay. The question is does switching help increase your chances of winning?

Setup and Rules of the Game

A host shows three doors. Behind 2 of those doors are $100 while the last door has $10000.

  • Player starts off by picking one door.
  • The host (who knows what’s behind each door) reveals one door that has the $100
  • The player can now choose to stay with their original pick or switch.

Analytical Probability

Let’s look at the probability between switching and staying.

  • If the player chooses to stay, their probability of getting it right is 1/3

  • If the player chooses to switch, their probability of getting it right is 2/3

Simulating the Game Code

set.seed(123)
monty = function (n = 10000) {
  doors = 1:3
  # Replaces one door with the winning door
  jackpot = sample(doors, n, replace = TRUE)
  # Replaces one door with the players pick
  pick = sample(doors, n, replace = TRUE)
  # Simulates the host revealing the WRONG door
  host = sapply(seq_len(n), function(i) {
    sample(setdiff(doors, c(jackpot[i], pick[i])), 1)
  })
  # Determines what happens if the player switches
  switch = mapply(function(p, h) setdiff(doors, c(p, h)), pick, host)
  switch = sapply(switch, sample, 1)
  # Calculate number of wins
  stay_win = mean(pick == jackpot)
  switch_win = mean(switch == jackpot)
  data.frame(
    Strategy = c("Stay", "Switch"),
    WinRate = c(stay_win, switch_win)
  )
}

monty_progress = function(n = 10000) {
  doors = 1:3
  jackpot = sample(doors, n, replace = TRUE)
  pick = sample(doors, n, replace = TRUE)
  host = sapply(seq_len(n), function(i) sample(setdiff(doors, c(jackpot[i], pick[i])), 1))
  switch = mapply(function(p, h) setdiff(doors, c(p, h)), pick, host)
  switch = sapply(switch, sample, 1)
  stay_rate = cumsum(pick == jackpot) / seq_len(n)
  switch_rate = cumsum(switch == jackpot) / seq_len(n)
  data.frame(Game = 1:n, Stay = stay_rate, Switch = switch_rate)
}

Win Rates Bar Plot

Win Rate Over Time

3D Plot