【问题标题】:Python Monty Hall Simulation didn't give expected answerPython Monty Hall Simulation 没有给出预期的答案
【发布时间】:2021-09-25 14:58:32
【问题描述】:

我在 Monty Hall 模拟中得到 0.5 的答案。

来自教科书:我们假设汽车是通过滚动一个三面骰子放在门后的,这使得所有三个选择的可能性相同。蒙蒂知道汽车在哪里,而且总是打开一扇门,后面有一只山羊。最后,我们假设如果 Monty 可以选择门(即参赛者选择了后面有汽车的门),他以 1/2 的概率选择每个门。玛丽莲显然希望她的读者会认为游戏是以这种方式进行的。

玛丽莲的答案是 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)

【问题讨论】:

  • 是什么让你认为答案应该是0.66
  • 代码很难理解 - 例如。你在循环内外定义monty_choose
  • 我还建议将 cmets 放在自己的行上,而不是在最后添加。
  • 来自《概率导论》一书中的蒙蒂霍尔问题。我们假设汽车是通过滚动一个三面骰子放在门后的,这使得所有三个选择的可能性相同。蒙蒂知道汽车在哪里,而且总是打开一扇门,后面有一只山羊。此外,我们假设如果 Monty 可以选择门(即,参赛者选择了后面有车的门),他选择每个门的概率为 1/2。这就是玛丽莲的想法,她的答案是 0.66(2/3)

标签: python logic simulation


【解决方案1】:

您的代码可以找到,直到您到达最后一点。你在随便挑门:


 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

【讨论】:

    【解决方案2】:

    您的代码计算0.5 的概率仅仅是因为s = random.choice(doors) 平等地从汽车或山羊中进行选择。

    您的代码没有反映蒙蒂霍尔问题的工作原理。

    如果参赛者做出选择并坚持选择,那么概率显然是0.33。你永远不允许ichoose 坚持他们的选择。

    不太明显的部分是参赛者可以改变他们的选择,然后概率是0.66。你永远不允许ichoose 改变他们的选择。

    【讨论】:

      【解决方案3】:
      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)
      

      所以基本上,你是在混淆选择的对象和时间:

      1. A 在 [1, 2, 3] 中选择一扇门
      2. B 知道汽车在哪里,并显示一扇空门(A 没有选择)
      3. 现在可以选择保留门或更换门

      您的目标是表明换门导致获得汽车的概率为 0.66。

      【讨论】:

      • 嗨!感谢您的回答!但是,我很奇怪为什么Monty可以先选择,因为根据教科书,参赛者(我)应该先选择,然后Monty选择剩下的门,后面没有车。
      • 是的,A(或代码中的 i)是参赛者,A 做出选择,然后 Monty (B) 显示一扇空门并询问参赛者是否愿意改变他们的选择。如果参赛者始终保持第一选择,您将获得 ~0.33,如果参赛者始终改变,您将获得 ~0.66
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多