Home > Mobile >  Python Monty Hall Simulation didn't give expected answer
Python Monty Hall Simulation didn't give expected answer

Time:09-26

I get the answer 0.5 in the Monty Hall Simulation.

From the textbook: We assume the car was put behind a door by rolling a three-sided die which made all three choices equally likely. Monty knows where the car is, and always opens a door with a goat behind it. Finally, we assume that if Monty has a choice of doors (i.e., the contestant has picked the door with the car behind it),he chooses each door with probability 1/2. Marilyn clearly expected her readers to assume that the game was played in this manner.

Marilyn's answer is 0.66, and I want to simulate this answer, but I got 0.5 and don't know what's wrong with my codes.

n = 1000000
count = 0
for i in range(n):
    doors = [1,2,3]  
    # the inital doors that monty can choose
    monty_choose = [1,2,3]
    # suppose the car is behind door 1
    car = 1   
    # monty cannot choose the door that has car
    monty_choose.remove(car)    
    ichoose = random.choice(doors)  
    if ichoose in monty_choose:
        # monty cannot choose the door i select
        monty_choose.remove(ichoose)  
        monty = random.choice(monty_choose)  
    else:
        monty = random.choice(monty_choose)
    # i cannot choose the door that monty chose  
    doors.remove(monty)   
    s = random.choice(doors)
    if s == car:
        count = count   1 
print(count/n)

CodePudding user response:

doors = [1,2,3]  # total doors
i_choose = [1,2,3] # the inital doors that I can choose
car = 1   # suppose the car is behind door 1
host_choose = [2,3] # the empty doors the host could show
n = 1000000
count = 0
car = 1
for i in range(n):
    # you can randomize car here, but remember to change host_choose accordingly
    i_choice = random.choice(doors) # I choose one door randomly
    if first_choice in host_choose:
        host_choose.remove(first_choice) # the host cannot open the chosen door
    host_choice = random.choice(host_choose) # the host shows that a door is empty
    i_choose.remove(host_choice)
    i_choose.remove(first_choice)
    # the goal is to show that always changing door results in 66% winrate
    i_choice = random.choice(i_choose) # this is actually a one-element list
    if i_choice == car:
        count = count   1
    i_choose = [1,2,3]
    doors = [1,2,3]
    host_choose = [2,3]
print(count/n)

So basically, you're mixing up who and when does the choices:

  1. A picks a door in [1, 2, 3]
  2. B knows where the car is, and reveals an empty door (that A didn't pick)
  3. A now can choose to keep their door or to change it

Your goal is to show that changing door leads to 0.66 probability of getting the car.

CodePudding user response:

Your could workers find until you get to the last bit. You are picking the door at random


 s = random.choice(doors)
    if s == car:
        count = count   1 

When what you want to do is to wish doors. You can do this by simply removing your first choice then indexing the list at 0.

    doors.remove(ichoose)
    
    if doors[0] == car:
        count = count   1 

full code and result

import random

n = 1000000
count = 0
for i in range(n):
    doors = [1,2,3]  
    # the inital doors that monty can choose
    monty_choose = [1,2,3]
    # suppose the car is behind door 1
    car = 1   
    # monty cannot choose the door that has car
    
    monty_choose.remove(car)    
    ichoose = random.choice(doors)  
    if ichoose in monty_choose:
        # monty cannot choose the door i select
        monty_choose.remove(ichoose) 
        monty = random.choice(monty_choose)
    else:
        monty = random.choice(monty_choose)
    # i cannot choose the door that monty chose  
    doors.remove(monty)  
    
    
    doors.remove(ichoose)
    
    if doors[0] == car:
        count = count   1 
        
        
print(count/n)
0.667145

CodePudding user response:

Your code calculates probability of 0.5 simply because s = random.choice(doors) is choosing from car or goat equally.

Your code does not reflect how the Monty Hall problem works.

If the contestant makes a choice and sticks with that choice, then the probability is obviously 0.33. You never allow ichoose to stick with their choice.

The less obvious part is that the contestant can change their choice and then the probability is 0.66. You never allow ichoose to change their choice.

  • Related