owned this note
owned this note
Published
Linked with GitHub
# Monty Hall Problem
The Monty Hall problem is a brain teaser, in the form of a probability puzzle, loosely based on the American television game show *Let's Make a Deal* and named after its original host, Monty Hall.
- There are 3 doors, behind which are two goats and a car.
- You pick a door (call it door A). You're hoping for the car of course.
- Monty Hall, the game show host, examines the other doors (B & C) and opens one with a goat. (If both doors have goats, he picks randomly.)
## Define a Monty Hall game as a Python function
- **input**: your initial choice [0, 1, 2]; and your decision ['stay', 'switch']
- **outcome**: win or loss
```python
def MHGame(door, d):
print('You select door: %d' %door)
## initialize the setting
out = np.zeros(3)
## generate a door with a car
door0 = random.randint(0, 2)
out[door0] = 1.
## Monty opens a door with a goat
res_door = set([0,1,2]) - set([door0, door])
open_door = random.choice(list(res_door))
print('Monty open the door: %d' %open_door)
if d == 'stay':
final_door = door
print('You decide to stay...')
final_out = out[door]
elif d == 'switch':
print('You decide to switch...')
final_door = list(set([0,1,2]) - set([door, open_door]))[0]
final_out = out[final_door]
else:
print('please input a correct decision: stay or switch')
print('The true door is: %d; your final choice is: %d' %(door0, final_door))
if final_out == 1:
print('You win!')
else:
print('Sorry, You lose!')
return final_out
```
```python
## test your function
MHGame(door=1, d='stay')
```
> You select door: 1
> Monty open the door: 2
> You decide to stay...
> The true door is: 1; your final choice is: 1
> You win!
```python
## test your function
MHGame(door=2, d='switch')
```
> You select door: 2
> Monty open the door: 1
> You decide to switch...
> The true door is: 0; your final choice is: 0
> You win!
## Statistical simulation
- Run `MHGame()` 1000 times with different decisions, to see which one is better
```python
res = {'door': [], 'decision': [], 'out': []}
n_trial = 1000
for i in range(n_trial):
for d in ['switch', 'stay']:
for door in range(3):
out_tmp = MHGame(door=2, d=d)
res['door'].append(door)
res['decision'].append(d)
res['out'].append(out_tmp)
```
```python
res = pd.DataFrame(res)
```
## Analyze the result:
- Compute the conditional probability
- Visualize the results
```python
for d in ['stay', 'switch']:
freq_tmp = res[res['decision'] == d]['out'].mean()
print('The freq of %s to win the game is: %.3f' %(d, freq_tmp))
```
The freq of stay to win the game is: 0.342
The freq of switch to win the game is: 0.656
```python
sum_res = res.groupby(['door', 'decision']).mean().reset_index()
```
| door | decision | out |
| ----------- | ----------- | --- |
|0 | stay | 0.337 |
|0 | switch | 0.662 |
|1 | stay | 0.361 |
|1 | switch | 0.648 |
|2 | stay | 0.328 |
|2 | switch | 0.657 |
### Histgram: decision matters?
```python
import seaborn as sns
sns.set_theme(style="white", palette='pastel')
sns.displot(res, x="out", col="decision", hue='decision', stat="probability")
```

### Histgram: initial door matters?
```python
sns.displot(res, x="out", col="door", hue='door', stat="probability")
```

```python
sns.catplot(data=sum_res, x="door", y="out", hue="decision", kind='bar')
# sns.catplot(data=df, x="age", y="class", kind="box")
```

## Probabilistic interpretation[^1]
[^1]: `latex`([tutorial](https://www.youtube.com/watch?v=zqQM66uAig0)) is required, which is optional (but highly recommended) for our course.
A statistical perspective: we want to make a decision $D$: "stay" (=0) or "switch" (=1), to maximize the probability, that is,
$$
\max_{d = 0, 1} \mathbb{P}\big( Y = 1 \big| D = d \big).
$$
Thus, the primary goal is to compute the conditional probability. Clearly, $Y = 1$ is highly depended on your choice in the first stage, let's denote $X \in \{0, 1\}$ as your initial selection with or w/o car. Then, we have
$$
\mathbb{P}\big( Y = 1 | D =d \big) = \mathbb{P} \big( Y = 1, X = 0 | D =d \big) + \mathbb{P} \big( Y = 1, X = 1 | D =d \big) \\
= \mathbb{P} \big( Y = 1, | X = 0, D =d \big) \mathbb{P}(X = 0) + \mathbb{P} \big( Y = 1 | X=1, D =d \big) \mathbb{P}(X = 1)
$$
where the first equality follows from the relation between marginal and joint probabilities, and the second equality follows Bayes' theorem. Note that
$$
\mathbb{P}(X = 1) = \mathbb{P}(\text{initially select car}) = 1/3, \ \
\mathbb{P}(X = 0) = \mathbb{P}(\text{initially select goat}) = 2/3.
$$
Now, we compare the difference in the conditional probabilities when making different decisions.
- ==D = 'stay'==
$$
\mathbb{P}( Y = 1 | D = 0 \big) = \frac{2}{3} \mathbb{P} \big( Y = 1 | X = 0, D=0 \big) + \frac{1}{3} \mathbb{P} \big( Y = 1 | X=1, D=0 \big) = \frac{1}{3}.
$$
- ==D = 'switch'==
$$
\mathbb{P}( Y = 1 | D = 0 \big) = \frac{2}{3} \mathbb{P} \big( Y = 1 | X = 0, D=1 \big) + \frac{1}{3} \mathbb{P} \big( Y = 1 | X=1, D=1 \big) = \frac{2}{3}.
$$