### CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual use to large groups and classes! Also, H100 GPUs starting at \$2/hour.

Views: 5
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2204
Kernel: Python 3 (system-wide)

Code to illustrate the Monty Hall problem @author: stan

## Section Three; The Monty Hall Problem

The Monty Hall problem is a probability puzzle named after Monty Hall, the original host of the television game show "Let's Make a Deal." The problem goes like this:

1. You're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1.

2. Now the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then asks if you want to switch your choice to the remaining door, No. 2.

The question posed in the Monty Hall problem is: Is it to your advantage to switch your choice of door?

```import random  #import the library that contains random number generators

```
```#In this cell don't change doors

count = 1000  #How many trials
success = 0   #A counter for the correct door
number_doors = 3 # the three door problem
while count > 0:  # the loop for the trials

door_with_prize = random.randint(1,3)  #pick a door for the prize

chosen_door = random.randint(1,3)  # choose a door

# Monty Hall will choose a door to open
opened_door = random.choice([i for i in range(1, 4) if i != chosen_door and i != door_with_prize])

# switch doors?
switch = "n"
if switch == "y":
chosen_door = random.choice([i for i in range(1, 4) if i != chosen_door and i != opened_door])
# Is the chosen door the one with the prize?
if chosen_door == door_with_prize:
# increment the success counter
success += 1
# decrement the counter and run another trial
count -= 1

print("success = ", success, "of 1000 tries")
```
success = 358 of 1000 tries
```#In this cell, change doors

count = 1000  #How many trials
success = 0   #A counter for the correct door
number_doors = 3 # the three door problem
while count > 0:  # the loop for the trials

door_with_prize = random.randint(1,3)  #pick a door for the prize

chosen_door = random.randint(1,3)  # choose a door

# Monty Hall will choose a door to open
opened_door = random.choice([i for i in range(1, 4) if i != chosen_door and i != door_with_prize])

# Now switch doors. I left the If statment in, in case I wanted to run it by choosing not to switch
switch = "y"
if switch == "y":
chosen_door = random.choice([i for i in range(1, 4) if i != chosen_door and i != opened_door])
# Is the chosen door the one with the prize?
if chosen_door == door_with_prize:
# increment the success counter
success += 1
# decrement the counter and run another trial
count -= 1

print("success = ", success, "of 1000 tries")
```
success = 647 of 1000 tries

The Surprising Solution: The optimal strategy in the Monte Hall problem is to always switch doors after the host reveals a goat. Counterintuitively, switching doubles the participant's chances of winning the prize compared to sticking with the initial choice. This result becomes evident when we consider the probabilities associated with each scenario.

Initially, the participant has a 1/3 chance of selecting the door with the prize, and a 2/3 chance of selecting a door with a goat. When the host reveals one of the doors with a goat, it effectively transfers the remaining 2/3 probability to the other unopened door. Consequently, the door initially chosen by the participant retains its 1/3 probability, while the unopened door now holds a 2/3 probability of concealing the prize. Switching doors thus increases the likelihood of winning.

```
```