Stick or Twist – the Monty Hall Problem

In Season 4 Episode 9 of the comedy show Brooklyn 99, two characters fall out over a debate around a probability problem. The problem is known as the “Monty Hall problem” due to the fact it was made famous on a game show named “Lets Make a Deal”. Initially I was a little confused into which character had the right answer, but with a little thought it became clear. This post will explain the problem, show the calculations behind the answer and run some simulations to prove it.

What is the Monty Hall Problem

Imagine you are on a game show and there are 3 doors in front of you. Only one of the doors contains a prize – but you don’t know which one. You pick a door (in this case Door 1).

The host (Monty Hall) then opens a door showing you there is nothing in it, let’s say the host opens Door 3;

You now have a choice, stick with your door or open the remaining door. Only one contains a prize. So, should you Stick or Twist? and does it matter anyway? – isn’t this a 50/50 split? the prize is either my door or door 2?

Mapping out the probabilities

Let’s make a table with the possible paths through on instance of this game, we will use the example above. but this time with the knowledge that Door 2 is the winning door.

Never Swapping

Let’s look at what happens if we never swap

My Pick:123
Host Opens:331*
Swap:NNN
Win:NoYES!!No
  • If we pick door number 1 and the host shows us door number 3. We lose unless we swap.
  • If we pick door number 2 and stick with it we win
  • If we pick door number three the host cannot open door number 3 – he cannot show us the winning door and HAS to open door number 1. if we stick we lose.

Always Swapping

If we always swap we would have the following;

My Pick:123
Host Opens:331
Swap:YYY
Win:YES!!NoYES!!
  • If we pick door number 1 and the host shows us door number 3. We swap to 2 and win.
  • If we pick door number 2 and swap – we lose as it was the winning door
  • If we pick door number three the host cannot open door number 3 – he cannot show us the winning door and HAS to open door number 1. if we swap to door 2 we win.

Conclusion

it looks like you should always swap. Put simply unless the door you chose from the start is the winner swapping will always win. You have a 1/3 chance of winning without swapping and a 2/3 chance with swapping.

Bayes Theorem

There is a way of calculating the probabilities of this using a formula known as Bayes Theorem. This formula is for calculating the probability of an event with prior knowledge of conditions that might be related to the event.

What Conditions do we know?

We know one condition with certainty – We know the host cannot reveal the winning prize when they open a door.

The theorem is written as follows;

yeah… Nice one Cookie. but this isn’t as bad as it looks. let’s break this down.

Explaning the Notation

  • P(A) signifies the probability of event A, so if event A were a coin toss it would be 50% of the time.
  • P(A|B) is known as conditional probability, meaning there are 2 events. in this example the probability of A occurring Given that B has occurred.

Applying the formular

let’s go back to the three doors, this time I pick door 3 and the host reveals Door 2.

What is P(A|B)?

What we want to know is what is the probability that Door 1 is the winner (we will swap) given the host has shown Door 2 is not the winner.

P(A) is the prior probability that door 1 is the winner without any prior knowledge at all (no one opening any doors). This is 1/3. Therefore P(A) = 1/3

P(B|A) is the probability the host reveals door 2, given our prior knowledge. we have picked door 3, the prize is in door 1 (as we are calculating the probability of door 1 being the winner). The host has no choice but to open door 2. so, the probability is 1. Therefore P(B|A) = 1

P(B) is the probability the host picks door 2 with no prior knowledge. Given the host can’t pick door 3 as we have chosen that, there are 2 doors left to choose from door 1 or door 2. There is no knowledge of where the prize is, so the probability is 50% or 1/2. Therefore P(B) = 1/2

if we map these into the formula we have P(A|B) = (1*1/3)/1/2
which is 1/3 divided by a half
which is 2/3 or 0.666666 (recurring)

P(A|B) = 0.66666 (recurring)

This concurs what we established in the grid earlier, if we swap, we have a two thirds chance of winning. (The probability the door we swap to wins given our knowledge).

Running a Simulation

So, we have established its better to swap than not to. But can we demonstrate that practically. Let’s write some code to simulate this game.

PlayGame Function

Firstly, let’s make a function that runs the game, we want it to return a Boolean signifying if the game is won or lost and we can pass in another Boolean dictating if the gamer will switch or not.

We have randomly selected values for the winning position and our selection;

Note: the upper bound in the C# rand.next() method can never be picked so 1,4 picks a random number between 1 and 3 (I don’t know why they did this!)

The host (Monty) then picks his door, it cannot be the winner or our selection;

We then have a choice to switch. We can only switch to a value that isn’t ours or Monty’s

now we simply evaluate if the position the gamer has is equal to the winning position and return true if they win or false if they dont.

Running the Games

Now we have a function to play the game and return the result we can play lots of games and see if the probabilities calculated are proved in our simulation. We just need a method that runs the games lots of times and keeps a track of the results.

Now we have this let’s run the simulation on a different volume of games changing the willSwap variable at each volume to show the difference in outcome if we swap or not.
Number of gamesSwap?WinsLossesWin %
10N4640%
10Y7370%
100N307030%
100Y653565%
1000N34066034%
1000Y65834265.8%
10,000N3,2926,70832.92%
10,000Y6,6713,32966.71%
1,000,000N333,438666,56233.34%
1,000,000Y666,503333,49766.65%

The outcomes generated from our simulation concur with the probabilities calculated.

Conclusion

What’s nice about the simulation is not only that the values align perfectly with our calculated probabilities, but you can also see how a larger sample greatly aids precision. So, in conclusion if you are ever involved in a game like this, I would recommend swapping. And going back to Brooklyn 99 as much as it pains me to say it the Epic character of Captain Holt was wrong…. I do believe he overcame this argument with a different solution though.

Simulation code is on GitHub: Monty Hall Problem (github.com)