{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"from datascience import *\n",
"path_data = '../../../assets/data/'\n",
"import matplotlib\n",
"matplotlib.use('Agg')\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plots\n",
"plots.style.use('fivethirtyeight')\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Monty Hall Problem\n",
"This [problem](https://en.wikipedia.org/wiki/Monty_Hall_problem) has flummoxed many people over the years, [mathematicians included](https://web.archive.org/web/20140413131827/http://www.decisionsciences.org/DecisionLine/Vol30/30_1/vazs30_1.pdf). Let's see if we can work it out by simulation.\n",
"\n",
"The setting is derived from a television game show called \"Let's Make a Deal\". Monty Hall hosted this show in the 1960's, and it has since led to a number of spin-offs. An exciting part of the show was that while the contestants had the chance to win great prizes, they might instead end up with \"zonks\" that were less desirable. This is the basis for what is now known as *the Monty Hall problem*.\n",
"\n",
"The setting is a game show in which the contestant is faced with three closed doors. Behind one of the doors is a fancy car, and behind each of the other two there is a goat. The contestant doesn't know where the car is, and has to attempt to find it under the following rules.\n",
"\n",
"- The contestant makes an initial choice, but that door isn't opened.\n",
"- At least one of the other two doors must have a goat behind it. Monty opens one of these doors to reveal a goat, displayed in all its glory in [Wikipedia](https://en.wikipedia.org/wiki/Monty_Hall_problem):\n",
"\n",
"![Monty Hall goat](../../../images/monty_hall_goat.png)\n",
"\n",
"- There are two doors left, one of which was the contestant's original choice. One of the doors has the car behind it, and the other one has a goat. The contestant now gets to choose which of the two doors to open.\n",
"\n",
"The contestant has a decision to make. Which door should she choose to open, if she wants the car? Should she stick with her initial choice, or switch to the other door? That is the Monty Hall problem."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Solution\n",
"\n",
"In any problem involving chances, the assumptions about randomness are important. It's reasonable to assume that there is a 1/3 chance that the contestant's initial choice is the door that has the car behind it. \n",
"\n",
"The solution to the problem is quite straightforward under this assumption, though the straightforward solution doesn't convince everyone. Here it is anyway.\n",
"\n",
"- The chance that the car is behind the originally chosen door is 1/3.\n",
"- The car is behind either the originally chosen door or the door that remains. It can't be anywhere else.\n",
"- Therefore, the chance that the car is behind the door that remains is 2/3.\n",
"- Therefore, the contestant should switch.\n",
"\n",
"That's it. End of story. \n",
"\n",
"Not convinced? Then let's simulate the game and see how the results turn out."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simulation ##\n",
"The simulation will be more complex that those we have done so far. Let's break it down.\n",
"\n",
"**Step 1: What to Simulate**\n",
"\n",
"For each play we will simulate what's behind all three doors:\n",
"- the one the contestant first picks\n",
"- the one that Monty opens\n",
"- the remaining door\n",
"\n",
"So we will be keeping track of three quantitites, not just one."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 2: Simulating One Play**\n",
"\n",
"As is often the case in simulating a game, the bulk of the work consists of simulating one play of the game. This involves several pieces.\n",
"\n",
"**The goats:** We start by setting up an array `goats` that contains unimaginative names for the two goats. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"goats = make_array('first goat', 'second goat')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To help Monty conduct the game, we are going to have to identify which goat is selected and which one is revealed behind the open door. The function `other_goat` takes one goat and returns the other."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def other_goat(x):\n",
" if x == 'first goat':\n",
" return 'second goat'\n",
" elif x == 'second goat':\n",
" return 'first goat'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('second goat', 'first goat', None)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"other_goat('first goat'), other_goat('second goat'), other_goat('watermelon')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The string `watermelon` is not the name of one of the goats, so when `watermelon` is the input then `other_goat` does nothing."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The options:** The array `hidden_behind_doors` contains the three things that are\n",
"behind the doors."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['first goat', 'second goat', 'car'], dtype='\n",
" \n",
" \n",
" Guess Revealed Remaining\n",
" \n",
" \n",
" \n",
" \n",
" first goat second goat car \n",
" \n",
" \n",
" first goat second goat car \n",
" \n",
" \n",
" car first goat second goat\n",
" \n",
" \n",
"\n",
"

... (9997 rows omitted)

"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"games.show(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualization\n",
"\n",
"To see whether the contestant should stick with her original choice or switch, let's see how frequently the car is behind each of her two options.\n",
"\n",
"It is no surprise that the three doors appear about equally often as the contestant's original guess."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"

"
],
"text/plain": [
"Remaining | count\n",
"car | 6681\n",
"first goat | 1676\n",
"second goat | 1643"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"remaining_door = games.group('Remaining')\n",
"remaining_door"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As our earlier solution said, the car is behind the remaining door two-thirds of the time, to a pretty good approximation. The contestant is twice as likely to get the car if she switches than if she sticks with her original choice.\n",
"\n",
"To see this graphically, we can join the two tables above and draw overlaid bar charts."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"