Reputation: 23
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)
Upvotes: 0
Views: 397
Reputation: 152
import random
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 = 10000000
count = 0
car = 1
always_change= True # player strategy
for i in range(n):
# you can randomize car here, but remember to change host_choose accordingly
first_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)
if always_change:
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
else:
i_choice = first_choice
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:
Your goal is to show that changing door leads to 0.66 probability of getting the car.
Upvotes: 0
Reputation: 36
Your code could work 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 switch 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
Upvotes: 1
Reputation: 39354
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.
Upvotes: 1