Wednesday, February 25, 2015

Monty Hall Problem Simulation

Yesterday evening I was reading an article about Monty Hall Problem (The time everyone corrected the worlds smartest woman). A reader asked this question on Marilyn vos Savant's "Ask Marilyn" column in Parade magazine in 1990:

"Suppose you're on a game show, and 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, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice?"


Source: Wikipedia

Marilyin Vos Savant suggested that the contestant should switch to the other door. Under the standard assumptions, contestants who switch have a 2/3 chance of winning the car, while contestants who stick to their choice have only a 1/3 chance. This problem became famous when nearly 1000 PhD's were unable to comprehend the suggestion provided by Marilyn vos Savant. For her suggestions on switching the door she was criticized harshly. Many people have simulated the problem and found her suggestion to be correct.

For fun I decided to write a simple python code to simulate the Monty Hall Problem.

###################################################################
import numpy as np

# Number of simulations: 100000
nSim = 100000

# Number of doors: nDoor
nDoor = 3

# Function to simulate a game or a trial of Monty Hall problem
def game(nDoor, switch_choice):
    
    # List of all doors
    doors = ['goat'] * nDoor
    
    # Randomly assign a door behind which there is a "Car"
    car = np.random.randint(0, nDoor)
    doors[car] = 'car'
    
    # Contestant makes a random guess
    guess = np.random.randint(0, nDoor)
    
    # Store value of guess
    temp = doors[guess]

    ## Monty Hall opens all but 2 doors:
        # 1. Door with car behind it or the door chosen by contestant
        # 2. One "Goat" door
        
    while len(doors) > 2:
        
# Open a door such that door with car behind it or the door chosen by contestant is never opened
     
        openDoor = np.random.choice(range(len(doors)))
    
        if openDoor == car or openDoor == guess:
            continue
        doors.pop(openDoor)
    
    ## Now there are only 2 unopened doors remaining
    # Contestant switches choice: Boolean - value True or False    
    if switch_choice: # If true contestant switches choice
        
        # Switch the choice to other unopened door, i.e. remove from unopened door list
        closedDoors = list(doors)
        closedDoors.remove(temp)
        
        # Only one unopened door remains, update guess 
        temp = closedDoors.pop()
     
    # Check if guessed door has car behind it or not
    result = (temp == 'car')
    return result

## Simulation
win_switch = 0
win_no_switch = 0

for i in range(nSim):
    switch_trial = game(nDoor, switch_choice = True)
    if switch_trial is True:
        win_switch += 1
    
    no_switch_trial = game(nDoor, switch_choice = False)
    if no_switch_trial is True:
        win_no_switch += 1
    
print 'If you switch door, the probability of winning the car is: %0.2f' % (float(win_switch)/nSim)
print 'If you do not switch, the probability of winning the car is: %0.2f'% (float(win_no_switch)/nSim)

Out: 
If you switch door, the probability of winning the car is: 0.67
If you do not switch, the probability of winning the car is: 0.33

Here is link to Monty Hall Problem Simulation IPython Notebook 

4 comments:

  1. A Classic problem, very nicely simulated! Very cool to see!

    ReplyDelete

  2. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete