通用和门蒙提霍尔问题模拟

General n-door Monty Hall Problem simulator

我正在尝试用 n 门模拟 Monty Hall 问题。我的理解是,对于n门,如果参赛者选择不切换,那么获胜的概率预计为1 / n,而如果策略是切换,每次Monty打开一扇门,参赛者切换选择,最终获胜的预期概率变为 1-1/n。我正在尝试在 Python 中使用此模拟重现相同内容。这适用于 3 门,但不会产生更多门的预期结果。我想了解是否存在错误或我遗漏了什么。

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

存在缩进错误。

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