通用n门Monty Hall问题模拟器

时间:2019-11-23 08:07:00

标签: python statistics simulation

我正在尝试用n门模拟Monty Hall问题。我的理解是,对于n门,如果参赛者选择不切换,则获胜的概率预计为1 / n,而如果要切换策略,则每当Monty打开一扇门时,参赛者就会切换选择,最后获胜的预期概率变为1-1 / n。我试图在Python中使用此模拟重现相同的内容。这对于三扇门效果很好,但对于更多数量的门无法产生预期的结果。我想了解是否有错误或缺少某些东西。

class MontyHall(object):
    def __init__(self, doors, num_trials=10000):
        self.doors = doors
        self.trials = num_trials

    def get_door_to_open(self, opened_doors):
        all_doors = set(list(range(1, self.doors+1)))
        valid_doors = all_doors - opened_doors - set([self.car, self.contestant])
        return np.random.choice(list(valid_doors))

    def switch_the_door(self, opened_doors):
        all_doors = set(list(range(1, self.doors+1)))
        valid_doors = all_doors - opened_doors - set([self.contestant])
        return np.random.choice(list(valid_doors))

    def results_with_switch_strategy(self):
        count_win = 0
        for t in range(self.trials):
            opened = set()
            self.contestant = random.randint(1, self.doors)
            self.car = random.randint(1, self.doors)
            while len(opened) < self.doors-2:
                # Monty opens a goat door.
                opened.add(self.get_door_to_open(opened))
                # Contestant switches.
                self.contestant = self.switch_the_door(opened)
            if self.contestant == self.car:
                count_win = count_win + 1
        return count_win/self.trials

    def results_with_no_switch_strategy(self):
        count_win = 0
        for _ in range(self.trials):
            self.contestant = random.randint(1, self.doors)
            self.car = random.randint(1, self.doors)
            if self.contestant == self.car:
                count_win = count_win + 1
        return count_win/self.trials

    def compare_strategies(self):
        print("Monty Hall %d doors: Switch strategy and win probability is: %f" % (self.doors, self.results_with_switch_strategy()))
        print("Monty Hall %d doors: No switch strategy and win probability is: %f \n" % (self.doors, self.results_with_no_switch_strategy()))

MH_doors_3 = MontyHall(3)
MH_doors_3.compare_strategies()
MH_doors_4 = MontyHall(4)
MH_doors_4.compare_strategies()
MH_doors_100 = MontyHall(100, num_trials=10000)
MH_doors_100.compare_strategies()

结果如下:

Monty Hall 3 doors: Switch strategy and win probability is: 0.664400
Monty Hall 3 doors: No switch strategy and win probability is: 0.332700 

Monty Hall 4 doors: Switch strategy and win probability is: 0.630900
Monty Hall 4 doors: No switch strategy and win probability is: 0.250300 

Monty Hall 100 doors: Switch strategy and win probability is: 0.623800
Monty Hall 100 doors: No switch strategy and win probability is: 0.011800

1 个答案:

答案 0 :(得分:2)

存在缩进错误。

def results_with_switch_strategy(self):
    count_win = 0
    for t in range(self.trials):
        opened = set()
        self.contestant = random.randint(1, self.doors)
        self.car = random.randint(1, self.doors)
        while len(opened) < self.doors-2:
            # Monty opens a goat door.
            opened.add(self.get_door_to_open(opened))
        #Contestant can only switch once per trial.
        self.contestant = self.switch_the_door(opened) #Correction
        if self.contestant == self.car:
            count_win = count_win + 1
    return count_win/self.trials