**The Monty Hall Problem Explained**

**Introduction to The Monty Hall Problem**

Check this situation out. You are on a TV show and you're playing a game. The game is simple. You initially have three doors behind which are two goats and a car. You are supposed to win a car by choosing a right door and you have two chances for that. You have to choose one door first and the host has to remove one door excluding the door you're currently choosing and the door with car if it's not the door you have chosen. Then you can either switch to other door or remain with the current one. After your second choice the host removes the remaining one door and gives you whatever is behind the door you're holding till now.

Now the question is: Do you shift your choice to other door or remain choosing the current door?

This ladies and gentlemen is **The Monty Hall Problem.**

Take some minutes and decide. It's a simple probability question. If you're a nerd then you can also try to calculate the probability of winning the car when:

- You change your choice in your second chance
- You remain with your first choice

Write you answer down and let's check if you have a key in your hand or the pooping goat. Let's see what you're made of.

**Solution to The Monty Hall Problem - Monty Hall Problem Proof**

The probability of winning a car is 2/3 when you switch and 1/3 when you don't. So better switch to other door if you want the car or it's also better to remain with your first choice if you are fond of goats.

*Chuckling goats: Meahahahah...

Let's see why this is so, after all, you have started cursing me that I'm wrong and it's same thing choosing the other door or not choosing it because you have 2 doors which contain only one car. So, it's a 50-50 chance of winning.

Well my boys and gals, buckle up to blow your mind.

### Analysis of The Monty Hall Problem

- Logical Analysis of The Monty Hall Problem

*he can't choose the door with car*?

**than**

__door with goat____with car__. So, because you have more chance of choosing goat at first it's more likely to win a car by shifting your choice to other door in the second time.

- When you switch, you win car 2 out of three cases.
- When you don't, you win car 1 out of 3 times.

**Mathematical proof** for The Monty Hall Problem** using Bayes Theorem**

**Mathematical proof**for The Monty Hall Problem**using Bayes Theorem** In order to fully understand Bayes Theorem you must have the basic understanding of **Probability** and **Conditional Probability.**

** **What Bayes Theorem can do is, it can update the already known probability of an event given that a new evidence or incident has happened that had in turn affected this probability.

In other words, if you calculated a probability of an event and after that if some other independent event happened that affected this probability you've just calculated, then you can use Bayes Theorem to update this probability to correct the changes made by this new event.

#### __Statement:__

*P(A | B) = P(B | A) * P(A) / P(B)***Grammar:**

**= Probability of occurrence of event A after event B has already happened**

*P(A | B)***= Probability of occurrence of event B after event A has already happened**

*P(B | A)***= Probability of happening of event B**

*P(B)***= Probability of happening of event A**

*P(A)***P(A | B)**and

**P(B | A)**are conditional probability situations.

**C**represents the hypothesis that door 1 has the car**G**represents an event where the host has shown a door with goat beside it

**P(C)**represents the probability of getting car in one door. This is equal to 1/3.**P(nC)**represents the probability of not getting the car in one door. This is equal to 1-P(C) = 2/3.**P(C | G)**represents the probability of getting car after a goat is revealed. This is to be calculated.**P(G | C)**represents the probability of getting goat after door1 having car is chosen. Because the host cannot reveal door with car even if the contestant didn't choose at first, P(G) = 1.**P(G | nC)**represents the probability of getting goat given that door1, not having car, is chosen. Because the host cannot reveal door with car yet, P(G | nC) = 1.**P(G)**represents the probability of revealing goat by the host.

**P(G)**=

**P(G | C)***

**P(C)**+

**P(G | nC)***

**P(nC)**

**P(C | G)**.

**P(C | G)**=

**P(G | C)***

**P(C)**/

**P(G)**

**= 1 * (1/3)/1**

**P(C | G) = 1/3**

**Simulation for The Monty Hall Problem**

**If you are still skeptical about the analysis then lets simulate some real life tests using computer. We will simulate the cases for this problem in this manner:**

- First we will pick one door among the three just like a contestant.
- Then we will remove a door among the remaining two just like a host.
- Then we will either switch to remaining door or not.
- Finally, we will see if we got a goat or a car.

**Python code for The Monty Hall Problem Simulation**

`#Lets import some packages that we'll use first`

`import numpy as np`

`import matplotlib.pyplot as plt`

`import collections`

```
def montyHall(experiment_number, no_of_doors, change = True):
'''
```

` This function returns total number of wins and losses in `

` experiment_number times of tests in dictionary form`

```
experiment_number(INT) = no of times this test is to be carried out
no_of_doors(INT) = initial no of doors the participant can choose
change(BOOL) = if the participant can change initial choice
```

```
(True = Can)
'''
result = []
not_changing_door_no = no_of_doors
for _ in range(experiment_number):
no_of_doors = not_changing_door_no
#print('\nExperiment starting')
doors = np.arange(1,not_changing_door_no+1)
#print('Door: ',doors)
jackpot_door = np.random.choice(doors)
#print('Money door: ', jackpot_door)
prev_door = 0
while True:
#Choose a door
while True:
chosen = np.random.choice(doors)
#print('Chosen', chosen)
if prev_door != chosen:
#print('Accepted')
break
prev_door = chosen
#Remove a Door
if no_of_doors > 2:
#print('Removing')
while True:
to_remove = np.random.choice(doors)
#print('Choosing to delete', to_remove)
if to_remove != chosen and to_remove != jackpot_door:
break
#print('Accepted', to_remove)
doors = np.delete(doors, to_remove-1)
#print('Updated door: ', doors)
else:
#print('Comparing the last doors')
#print('Jackpot:',jackpot_door,'chosen: ', chosen)
result.append(True) if chosen == jackpot_door else result.append(False)
break
no_of_doors -= 1
#print(result[_])
return result
experiment_number = int(input('Enter the no of test cases to carry: '))
#no_of_doors = int(input('Enter the initial number of doors')) #Because there is a bug for doors > 3 also the probablity is same as with 3 doors
# Simulation will come out exactly same with custom door number as with 3 doors
no_of_doors = 3
change = input('Do you want to switch places after your chioce repeteadly?(Y/N)')
change = True if change.upper() == 'Y' else False
result = montyHall(experiment_number, no_of_doors, change = change)
counted = collections.Counter(result)
#Plot Section
plt.title('Monty Hall Problem Simulation')
plt.bar(['Won','Lost'], [counted[True],counted[False]], color = ['green','red'])
plt.xlabel('Status')
plt.ylabel('No of wins/lose in '+str(experiment_number)+' tests')
plt.tight_layout()
plt.show()
```

### Result of some simulations:

- When contestant have to switch doors in 1000000 tests i.e. 1 Million tests

- When contestant doesn't switch the door for 1000000 tests i.e. 1 Million tests

- When the contestant switches the door for 10000000 i.e. 10 Million times

## 0 Comments