Monty Hall Problem using Python. We have all heard the probability brain... | by Siddhartha Pachhai | Towards Data Science

We have all heard the probability brain teaser for the three door game show. Each contestant guesses whats behind the door, the show host reveals one of the three doors that didn’t have the prize and gives an opportunity to the contestant to switch doors. It is hypothesized and in fact proven using conditional probability that switching doors increases your chances to an amazing 66%. But wait hold on, when I entered the game and had no clue which door had the prize i had a 33% chance of winning and when the host revealed one of the doors with no prizes, doesn’t that mean two doors remain and its a 50–50 chance of winning? Why should I switch? Well if you are the type of person who doesn’t believe what you hear on TV or what you read in a fun facts column of a magazine, but have curiosity in mathematical proofs, here is your answer.
Below are the initial probabilities when you walk in to the game show.
Now comes the tricky part and this is where the mathematics starts getting very confusing. Lets say the contestant chooses the 1st door, now what is the probability that the host will open Door 3, given that the prize is behind either door 1,2 or 3. This is a important concept to understand as this is used later on to derive the probabilities.
The first probability(left) answers the question: if you chose door 1, and the prize was behind 1, what is the probability that the host will reveal door 3? The answer is 50%, the host can either open door 3 or door 2 because both of them don’t have the prizes. But the key concept here is that the probability exists because you started with door 1.
If you choose door 1, and the prize is in door 2(middle), It is highly likely that the host will reveal door 3, because door 1 was already chosen by the contestant and door 2 has the prize so the host can only open the remaining door with no prize which is 3.
Now if the prize was in door 3, there is no chance that the host will open door 3, after the contestant chooses door 1. Like the previous scenario the host will always open door 2, when this is the case. So given we start with door 1, the probability of opening door 3 if door 3 has the prize is 0.
Now that we have understood the reasoning, lets suppose that in this example the host opens door 3. Door 3 doesn’t have the prize, now how sure can we be that door 2 has the prize?
The probabilities are based on Bayes theorem. So far our logic and deduction have created information that we are going to plug in to this equation to obtain probabilities of a conditioned event.
The main concept that should be understood here is the probability statement in the far left. It calculates probability of success for door 2 given that door 3 is revealed as not having prizes and our initial choice was door 1. So the chances of success are 2/3 when we switch! The alternative; if we had remained with door 1 in this scenario, the chances of winning would be as follows.
The same probabilities should follow regardless of what door you start from. The conditions are created in such a way that the given outcomes are always produced.
Now for the fun part. Lets see if this really is true!
%matplotlib inlineimport matplotlibimport numpy as npimport matplotlib.pyplot as pltfrom time import time as TTfrom random import choice as chimport numpy as npac = []tc = []N = []st = TT()for M in range(1,10000): #Outer loop from 1 to 10000    st1 = TT()    score = []    runs = 0    cards = [1,2,3]    for K in range(1,M): # sub loop that simulates 1 to M(outerloop) games        aset = []        host = cards.copy()        hbk = ch(host) #Randomly choose as answer which host knows        aset.append(hbk)        #print("The host knows the answer",hbk)        player = cards.copy()        px = ch(player) # Contestanrs random guess        aset.append(px)        #print ("Players first choice",px)        chance = 0        for i in host: # The computation....host will eliminate P(X|DOOR) = 0            if i not in aset:                chance = i        #print ("The elimination",chance)        #print (player)        player.pop(player.index(chance))        player.pop(player.index(px))        #print ("final answe",player)        if player[0] == hbk:            score.append(1)        else:            score.append(0)        runs = K        #print ("\n\n")    ac.append(np.mean(score))    N.append(M)    en1 = TT()    tc.append(en1-st1)en = TT()    print ("Total time for Loop  ", en - st )
The above code simulates the gameshow 10,000 times, where each itteration is a loop of increasing amounts of games. This process can be done in different ways, but the outcome should be the same but it was interesting to use the loop described above to get a variation of the number of games being played, which does effect the final outcome.
The main logic in this program is that, after contestant picks the first door, the host will eliminate the door that was not picked by the contestant or the door that was the right answer. If the contestants first guess was the correct guess, the door chosen to reveal is a random process. But the way my computation works it will be the last element of the list(1,2,3) given the element is not the correct answer. After doing this, for simulation purposes the contestant will always switch his answers after revealing. So thus going through this simulation we have the following results.
The X-Axis is the number of sub games that were played. The Y-Axis is the average sub-games won. Between 0–100 sub-games, you can see there is a lot of variation but after the 2000 mark and close to 10,000 the average number of sub-games won converges to around 64%-68%(maximum 70). Which supports our initial reasoning of that was derived through Bayes Theorem.
This theory might scale differently if we had more than 3 options or less.