Skip to content
Snippets Groups Projects
Commit bb8feeb9 authored by Ulrich Kerzel's avatar Ulrich Kerzel
Browse files

add notebook for Monty Hall problem

parent b1c6105a
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Monty Hall
In this puzzle, a contestant in a game-show gets to choose between 3 doors.
Behind one of the doors, a prize (the "car") is hidden, the other two doors do not win (the "goat").
The host of the show lets the contestant make an initial choice, then opens one of the doors (not the one the contestant has chosen) and then offers the contestant the choice to switch doors.
Is it beneficial to switch the doors?
%% Cell type:code id: tags:
```
import random
```
%% Cell type:code id: tags:
```
def monty_hall_simulation(num_trials=5000):
stay_wins = 0
switch_wins = 0
for _ in range(num_trials):
# Randomly place the car behind one of the three doors
car_door = # YOUR CODE HERE
# Contestant makes an initial choice randomly
contestant_choice = # YOUR CODE HERE
# Host opens a door
possible_doors = [1, 2, 3]
# Host cannot open the door with the car or the contestant's choice
doors_host_can_open = [# YOUR CODE HERE]
host_opened_door = # YOUR CODE HERE
# Determine the other door that the contestant can switch to
remaining_door = [# YOUR CODE HERE][0]
# If the contestant stays with their initial choice
if contestant_choice == car_door:
stay_wins += 1
# If the contestant switches to the remaining door
elif remaining_door == car_door:
switch_wins += 1
stay_win_rate = stay_wins / num_trials
switch_win_rate = switch_wins / num_trials
return stay_win_rate, switch_win_rate
```
%% Cell type:code id: tags:
```
num_trials = 5000
stay_win_rate, switch_win_rate = monty_hall_simulation(num_trials=num_trials)
print(f"After {num_trials} trials:")
print(f"Winning by staying with the initial choice: \t {stay_win_rate*100:.2f}%")
print(f"Winning by switching to the other door: \t {switch_win_rate*100:.2f}%")
```
%% Output
After 5000 trials:
Winning by staying with the initial choice: 32.60%
Winning by switching to the other door: 67.40%
%% Cell type:markdown id: tags:
# Monty Hall
In this puzzle, a contestant in a game-show gets to choose between 3 doors.
Behind one of the doors, a prize (the "car") is hidden, the other two doors do not win (the "goat").
The host of the show lets the contestant make an initial choice, then opens one of the doors (not the one the contestant has chosen) and then offers the contestant the choice to switch doors.
Is it beneficial to switch the doors?
%% Cell type:code id: tags:
```
import random
```
%% Cell type:code id: tags:
```
def monty_hall_simulation(num_trials=5000):
stay_wins = 0
switch_wins = 0
for _ in range(num_trials):
# Randomly place the car behind one of the three doors
car_door = random.randint(1, 3)
# Contestant makes an initial choice randomly
contestant_choice = random.randint(1, 3)
# Host opens a door
possible_doors = [1, 2, 3]
# Host cannot open the door with the car or the contestant's choice
doors_host_can_open = [door for door in possible_doors
if
door != car_door and
door != contestant_choice]
host_opened_door = random.choice(doors_host_can_open)
# Determine the other door that the contestant can switch to
remaining_door = [door for door in possible_doors
if
door != contestant_choice and
door != host_opened_door][0]
# If the contestant stays with their initial choice
if contestant_choice == car_door:
stay_wins += 1
# If the contestant switches to the remaining door
elif remaining_door == car_door:
switch_wins += 1
stay_win_rate = stay_wins / num_trials
switch_win_rate = switch_wins / num_trials
return stay_win_rate, switch_win_rate
```
%% Cell type:code id: tags:
```
num_trials = 5000
stay_win_rate, switch_win_rate = monty_hall_simulation(num_trials=num_trials)
print(f"After {num_trials} trials:")
print(f"Winning by staying with the initial choice: \t {stay_win_rate*100:.2f}%")
print(f"Winning by switching to the other door: \t {switch_win_rate*100:.2f}%")
```
%% Output
After 5000 trials:
Winning by staying with the initial choice: 32.60%
Winning by switching to the other door: 67.40%
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment