# ## 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 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

We will try to understand this problem first intuitively and then through mathematics.

### Logical Analysis of The Monty Hall Problem

At first, you have 3 doors to choose, of which two have goat and one has car. Here, suppose the car is behind door1 and you choose door2. Now, there is no way the host will remove the door behind which is a car i.e. door1 because it's a rule, so it's obvious that he'll choose door3.

Now what do you conclude from this statement that he can't choose the door with car?

Obviously you should switch to other door because the car is behind it since the host left it. And it's    exactly what you should do.

But you don't know which door has the car for real. Maybe it's behind the door you've chosen at first. That will be a huge mistake to switch to other door. That's where basic probability comes in action.

Remember that this is a logical analysis so not much Math here.

First of all there is 2/3 probability that you will choose a door with goat behind it. So, logic says, you have more chance of choosing the door with goat than 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.
In other words, if you have more chance to choose goat first, you have more chance to win a car if you switch the door just because you have already chosen goat. Simple as that.

You can also get clear understanding of the winning cases from this decision tree taken from Brilliant:

You can clearly see from this diagram that:
• 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

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:
P(A | B) = Probability of occurrence of event A after event B has already happened
P(B | A) = Probability of occurrence of event B after event A has already happened
P(B) = Probability of happening of event B
P(A) = Probability of happening of event A

Here, P(A | B) and P(B | A) are conditional probability situations.

Lets define two cases or hypothesis first:
• 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
Now,
• 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)
= 1 * 1/3 + 1 * 2/3
= 1

What we want to calculate is the probability of getting car given that the host has removed a door i.e. P(C | G).

Let's use the formula:
P(C | G)   = P(G | C) * P(C) / P(G)
= 1 * (1/3)/1
P(C | G)   = 1/3

As, we didn't expect, the probability of getting car after a door is removed is 1/3. It's because the door removed doesn't matter in this system and the car is now either in 1st door or the remaining door i.e. 2 out of three doors.

## 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.
We will do this game for large number of times and see if switching favors or not switching.

Lets use Python to code for the simulation because its more readable. The code in here is not limited to only 3 doors. You can use any amount of doors to choose from, but its all the same doing this with 3 doors or with a thousand.

If you're interested then here is a coding walkthrough to watch in 16x speed.

Python code for The Monty Hall Problem Simulation

Here's a GitHub link to this code if you don't want to read in this funky looking style.

`#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

We can see that in 1000000 i.e. 1 million times more than 600k times the contestant won the car and about 300k times he lost it. Which is, he/she has probability of winning the car for about 2/3 when he/she switches the door.
• When contestant doesn't switch the door for 1000000 tests i.e. 1 Million tests

Here, we can see the result is exactly inverted. The contestant lost for more than 600k times and won just above 300k times. The probability of winning dropped from 2/3 to 1/3 when the contestant didn't switch the door.

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

The result again is same as the first simulation. The contestant won for more than 6.5e6 i.e. 6500000 or 6.5 Million times and lost for about 3.5 Million times. The same probability difference as theorized by our Mathematics.

## Conclusion to all this explanation

What we learn from here is if you somehow ended up in such game shows it's better to switch the door if you want a car. Of course if you want to cuddle with goats you better stick with your first choice.

So, how did you like this approach? Please comment down below if you have any thoughts, criticism or anything to say. Share this article to those who still bully you saying it's 50-50 chance. Those guys really need some help.

Also you can message me using the form if you want me to write on other explanatory topics.
Happy Learning!