September 9


Python Solves the Monty Hall Problem

An explanation of what the Monty Hall problem is. With an attempt to solve it using Python as a statistical analysis tool.

Statistical thinking can be applied to problems where the answer doesn’t isn’t obvious and Python is a great tool for statistical analysis.

If you know enough about the variables, you can run simulations on the different courses of action.

Monty Hall was a game show host when game show’s were a thing. The show was called Let’s Make a Deal. Contestants were presented with a choice of 3 doors. Behind one of these was something valuable, the others having just token items.

The contestants were asked to choose a door. Whatever door they choose, the host would open one of the other two remaining doors and reveal that there was nothing behind that one. The contestants were given a choice between sticking to their original selection or choosing again.

Python Turtle Art

Is there a right solution?

What it feels like when you’re solving this problem, it feels like there is no difference between sticking to or changing your selection. You had a 33% chance of success. It seems to be still the case even after the other door is opened.

But apparently this isn’t the right way to think about this. Once you are presented with new information in the form of the other opened door, the initial variables no longer hold. But they no longer hold only if you change doors.

It seems a bit counter intuitive. An analysis showed that contestants who switched performed better than contestants who stayed put.

Why does this happen?

Because changing your selection after being presented with this new information would make you 2/3 more likely to succeed. Sticking with your choice means you would still have the original 1/3 rate of success.

A more intuitive way of thinking about this problem would be to re frame it with 100 doors. As before you would select one. The host would then open 98 out of the remaining 99 doors and ask you if you wanted to switch. Now it would seem more logical to switch. Switching would make it seem that your chances of winning are more likely.

The Proof

The folks who did the original study probably had to do it the hard way. Either go through hours of footage or go through the scripts to find out what happened.

We don’t have time for those shenanigans. But we have Python. And we know how to run simulations.

We could create a virtual game, run it a few thousand times and compare what happened depending on the choices of the virtual contestants.

How the simulation will work

  1. Each game has a choice of 3 doors a, b and c.
  2. We will randomly choose a door that has the prize behind.
  3. The contestant will also randomly choose a door.
  4. Take away the choice of a door that is not the prize or that was selected by the contestant.
  5. Tally the number of times the contestant got it right changing or sticking to the original selection.

Do this 10,000 times.

All the code can be found in the Github Repository.

The output for the code:

Average wins if you didn't change doors is 32%.
Average wins if you changed doors is 67%.


Which is pretty much what we were expecting. Sticking to your selection gave you a 33% chance of success. Changing selection gave you a 67% chance of success.

Why is this good for business?

This is a really interesting way of solving problems such as these. Framing statistical problems along these lines gives you an edge in your decision making.

Businesses or customers can be modelled in this manner, leading to the most informed decisions being made.

The Python programming language is a useful tool for this class of problem. A similar kind of problem has been solved in a previous post to model sales.

If you made it to the end of this post, thanks for reading. Please let me know what you think. It would be great if we could start a discussion on this topic.



You may also like

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    If you like to know more about posts like these or have a query about financial modelling