Python Monty Hall Simulation 没有给出预期的答案
Python Monty Hall Simulation didn't give expected answer
我在 Monty Hall 模拟中得到答案 0.5。
来自教科书:我们假设通过滚动三面骰子将汽车放在门后,这使得所有三个选择的可能性相同。 Monty 知道汽车在哪里,并且总是打开一扇门,后面有一只山羊。最后,我们假设如果 Monty 可以选择门(即参赛者选择了后面有汽车的门),他选择每扇门的概率为 1/2。玛丽莲显然希望她的读者认为游戏是以这种方式进行的。
Marilyn 的答案是 0.66,我想模拟这个答案,但是我得到了 0.5,不知道我的代码有什么问题。
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)
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)
所以基本上,您混淆了选择的对象和时间:
- A 在 [1, 2, 3] 选了一扇门
- B 知道车在哪里,并露出一扇空门(A 没有选择)
- A 现在可以选择保留或更换门
你的目标是证明换门有 0.66 的概率得到汽车。
您的代码可以在您到达最后一点之前找到。您正在随机选择门:
s = random.choice(doors)
if s == car:
count = count + 1
当您想做的是换门时。您可以通过简单地删除您的第一个选择然后将列表索引为 0 来做到这一点。
doors.remove(ichoose)
if doors[0] == car:
count = count + 1
完整代码和结果
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
您的代码计算 0.5
的概率仅仅是因为 s = random.choice(doors)
从汽车或山羊中选择的概率相同。
您的代码没有反映 Monty Hall 问题的工作原理。
如果参赛者做出一个选择并坚持那个选择,那么概率显然是0.33
。你永远不允许 ichoose
坚持他们的选择。
不太明显的部分是参赛者可以改变他们的选择,那么概率是0.66
。你永远不允许 ichoose
改变他们的选择。
我在 Monty Hall 模拟中得到答案 0.5。
来自教科书:我们假设通过滚动三面骰子将汽车放在门后,这使得所有三个选择的可能性相同。 Monty 知道汽车在哪里,并且总是打开一扇门,后面有一只山羊。最后,我们假设如果 Monty 可以选择门(即参赛者选择了后面有汽车的门),他选择每扇门的概率为 1/2。玛丽莲显然希望她的读者认为游戏是以这种方式进行的。
Marilyn 的答案是 0.66,我想模拟这个答案,但是我得到了 0.5,不知道我的代码有什么问题。
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)
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)
所以基本上,您混淆了选择的对象和时间:
- A 在 [1, 2, 3] 选了一扇门
- B 知道车在哪里,并露出一扇空门(A 没有选择)
- A 现在可以选择保留或更换门
你的目标是证明换门有 0.66 的概率得到汽车。
您的代码可以在您到达最后一点之前找到。您正在随机选择门:
s = random.choice(doors)
if s == car:
count = count + 1
当您想做的是换门时。您可以通过简单地删除您的第一个选择然后将列表索引为 0 来做到这一点。
doors.remove(ichoose)
if doors[0] == car:
count = count + 1
完整代码和结果
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
您的代码计算 0.5
的概率仅仅是因为 s = random.choice(doors)
从汽车或山羊中选择的概率相同。
您的代码没有反映 Monty Hall 问题的工作原理。
如果参赛者做出一个选择并坚持那个选择,那么概率显然是0.33
。你永远不允许 ichoose
坚持他们的选择。
不太明显的部分是参赛者可以改变他们的选择,那么概率是0.66
。你永远不允许 ichoose
改变他们的选择。