蒙蒂霍尔 贝叶斯_蒙蒂·霍尔遇见蒙特·卡洛遇见蒙蒂·Python

蒙蒂霍尔 贝叶斯

Combining statistics and programming… The Full Monty you might say.

将统计数据和编程结合起来……您可能会说“蒙蒂”。

One of my favourite statistical conundrums is the Monty Hall problem. The Monty Hall problem is a brain teaser loosely based on the American television game show “Let’s Make a Deal”. It is named after its original host, Monty Hall.

我最喜欢的统计难题之一是Monty Hall问题。 Monty Hall的问题是根据美国电视游戏节目“ Let's Make a Deal ”松散地挑逗。 它以其原始主人Monty Hall的名字命名。

The basis of the problem is simple. Let’s suppose you’re on a game show, and you’re given the choice of three doors. Behind one door is a car and behind the other two doors are goats (I am not really sure why all examples given of this problem use goats… perhaps they were freely available in 1975 to make guest appearances on game shows). So you pick a door, let’s say you decide on door number one. The game show host can see you are now profusely sweating from the brow as you contemplate the consequences of owning a goat and offers you a welcome reprieve. Being such a kind game show host, he helps you out, and opens one of the doors you didn’t choose to reveal a goat. Let us say that door was door number three. He turns to you and says, “Do you want to change your pick to door number two or stick with your original choice of door number one?” Your world has been thrown into turmoil… Why is he offering you this, surely it doesn’t matter at all? What are you going to do?

问题的基础很简单。 假设您正在观看一场游戏节目,并且可以选择三个门。 一扇门的后面是一辆汽车,而另一扇门的后面是山羊(我不太确定为什么给出这个问题的所有示例都使用山羊……也许在1975年可以免费使用它们在游戏节目中露面)。 因此,您选择一扇门,假设您决定第一扇门。 游戏节目主持人可以看到您现在正想着拥有一只山羊的后果,并且额头上满是汗水,这为您提供了令人欣慰的缓刑。 作为这样一个善良的游戏节目主持人,他可以帮助您,并打开您没有选择露出山羊的一扇门。 让我们说门是三号门。 他转过身对你说:“您想将选择改为第二号门还是坚持最初选择的第一号门?” 您的世界陷入了动荡……他为什么要向您提供这个,肯定没有关系吗? 你会怎样做?

答案很简单,您切换! 您每次都切换! (The answer is simple, YOU SWITCH! You switch every single time!)

Before we find out why, let me first introduce two more topics. I have already shared my favourite statistical problem, now let me introduce one of my favourite statistical methods, “The Monte Carlo Simulation”, aptly named after the popular gambling destination in Monaco. This method entails using randomly generated variables and repeated sampling to predict odds. The random variables can follow different distributions and you can repeat the experiment as many times as you like.

在我们找出原因之前,让我先介绍两个主题。 我已经分享了我最喜欢的统计问题,现在让我介绍一下我最喜欢的统计方法之一,即“ 蒙特卡洛模拟 ”,以摩纳哥流行的赌博胜地恰当地命名。 此方法需要使用随机生成的变量和重复采样来预测赔率。 随机变量可以遵循不同的分布,并且您可以根据需要重复多次实验。

To test whether you should switch or not, we are going to perform a Monte Carlo Simulation on the Monty Hall problem. We will randomly select a door for the car each time and also randomly select the door we will pick (these will follow a uniform distribution, meaning there is an equal chance of each outcome). We will then repeat this experiment, both with switching and without switching. Once we repeat this experiment several times, we can get an understanding of why you should switch.

为了测试您是否应该切换,我们将对Monty Hall问题执行Monte Carlo模拟。 我们将每次为汽车随机选择一个门,也将随机选择我们将选择的门(这些门将遵循均匀分布,这意味着每个结果的机会均等)。 然后,我们将在切换和不切换的情况下重复此实验。 一旦我们重复此实验几次,我们就可以了解为什么要切换了。

I will then demonstrate the random experiments as simplistically as I can in Python code and we will run each experiment 10 000 times (we could do more but I think 10 000 is enough to get my point across). I decided to use Python as the programming language to demonstrate this because Guido van Rossum, the creator of Python, decided to name it after “Monty Python’s Flying Circus”, a BBC comedy series from the 1970s. To quote the official Python documentation, “Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python”. I like it mostly because it was officially the third Monty I needed to create a catchy name for this article.

然后,我将在Python代码中尽可能简单地演示随机实验,并且每个实验将运行10,000次(我们可以做更多,但我认为10,000足以使我的观点通过)。 我决定使用Python作为编程语言来说明这一点,因为Python的创建者Guido van Rossum决定将其命名为“ Monty Python's Flying Circus ”,这是1970年代的BBC喜剧系列。 引用Python的官方文档,“ Van Rossum认为他需要一个简短,独特且略带神秘的名称,因此他决定将其命名为Python语言 ”。 我之所以喜欢它,主要是因为它是我为本文起个醒目的名字所需要的第三个Monty。

We’ll begin by playing the game as if the talk show host did not cunningly give the contestant the option to change their mind. Let’s first run a quick control to consider how many times the contestant would end up going home with a shiny new car and not that terrible forsaken goat (I personally have nothing against goats). I define a simple function that randomly generates a number 1, 2 or 3 which represents the three doors on the game show. The chance of picking either door is equal, approximately 33% or one third. I use the function to randomly decide what door the car is behind, and I use it again to randomly select which door a contestant might choose. If the doors match, it is a successful experiment and the happy contestant is driving home with a lovely car. If the doors do not match, it is a failed experiment and they get to walk a goat home (makes me wonder if there are any goat owners in the world and if they take their goats for walks). I run this experiment 10 000 times and as expected, we successfully pick the car door 33% of the time. Feel free to run the code yourself and add a few more trials if you would like.

我们从玩游戏开始,好像脱口秀主持人没有巧妙地让参赛者改变主意。 让我们首先进行快速控制,以考虑参赛者最终会带着一辆闪亮的新车回家,而不是那辆可怕的被遗弃的山羊回家的次数(我个人没有对抗山羊的经验)。 我定义了一个简单的函数,该函数随机生成一个数字1、2或3,分别代表游戏节目中的三个门。 捡到任一扇门的机会相等,大约为33%或三分之一。 我使用该函数随机决定汽车后面的门,然后再次使用它随机选择参赛者可能选择的门。 如果门匹配,则表明实验是成功的,并且高兴的参赛者将驾驶一辆可爱的汽车回家。 如果门不匹配,则说明实验失败,他们只能将山羊带回家(这使我想知道世界上是否有山羊主人,以及是否带山羊去散步)。 我进行了1万次实验,正如预期的那样,我们成功地在33%的时间内选好了车门。 随意自己运行代码,并根据需要添加更多试验。

import random


doors = [1, 2, 3]
trials = 10000
successful_guesses = 0


for i in range(trials):
    car_door = random.choice(doors) 
    selected_door = random.choice(doors)
    if car_door ==  selected_door:
        successful_guesses += 1


score = successful_guesses / trials
print(score)

Now let’s run the same experiment again, this time we grant the contestant an option to switch but they decide they are happy with their choice and decide not to change. Shockingly, if you do not switch, the experiment remains exactly the same! The fact that the host has revealed a goat has no effect on the probabilities. When the contestant made their original decision, the chance of them revealing the car was one third and now they stick with their original choice, the chance is still only one third. I illustrated this new choice in the code below but as you can see, the new choice essentially just remains the same as the contestant’s first choice and the probabilities hence do not change at all.

现在让我们再次运行相同的实验,这次我们为参赛者提供了一个切换选项,但他们认为自己对选择感到满意,并且决定不进行更改。 令人震惊的是,如果您不进行切换,实验将保持完全相同! 宿主发现山羊的事实对概率没有影响。 当参赛者做出他们最初的决定时,他们揭露赛车的机会只有三分之一,而现在他们坚持原来的选择,机会仍然只有三分之一。 我在下面的代码中说明了这一新选择,但是正如您所看到的,新选择实际上与参赛者的第一选择相同,因此概率完全不变。

import random


doors = [1, 2, 3]
trials = 10000
successful_guesses = 0


for i in range(trials):
    car_door = random.choice(doors) 
    selected_door = random.choice(doors)
    new_choice = selected_door
    if car_door ==  new_choice:
        successful_guesses += 1


score = successful_guesses / trials
print(score)

Now this is where it gets interesting. Let’s see what happens when you switch. The logic is a bit trickier here but stay with me. Let me define what happens now.

现在,这变得有趣了。 让我们看看切换时会发生什么。 这里的逻辑有点棘手,但请和我在一起。 让我定义一下现在会发生什么。

The door the game show host opens has to be a goat door, this means there is now one goat door remaining and the car door. If the contestant had originally selected the car door, the game show host now has an option to open one of two doors, but if they had selected a goat door, the game show host now only has an option to open one door. If we declare the available doors as the ones the game show host can open, it can either be an option of two doors or just one door depending on how good the contestant’s initial guess was. If it is two doors, we model that he can randomly open either one of them. The contestant only has one available option to switch to, which is the one they did not select first and the door that the game show host did not open.

游戏节目主持人打开的门必须是山羊门,这意味着现在只剩下一个山羊门和车门。 如果参赛者最初选择了车门,那么游戏节目主持人现在可以选择打开两个门之一,但是如果他们选择了山羊皮门,那么游戏节目主持人现在只能选择打开一个门。 如果我们将可用的门声明为游戏节目主持人可以打开的门,则可以选择两个门,也可以选择一个门,具体取决于参赛者的最初猜测。 如果是两扇门,我们模拟他可以随机打开其中一扇。 参赛者只有一个可用选项可以切换到该选项,这是他们没有首先选择的选项,并且游戏节目主持人没有打开门。

import random


doors = [1, 2, 3]
trials = 10000
successful_guesses = 0


for i in range(trials):
    car_door = random.choice(doors)
    selected_door = random.choice(doors)
    available_doors = [x for x in doors if x!= selected_door and x!= car_door]
    opened_door = random.choice(available_doors)
    new_choice = [x for x in doors if x!= selected_door and x!= opened_door][0]
    if car_door ==  new_choice:
        successful_guesses += 1


score = successful_guesses / trials
print(score)

I repeat the experiment above and lo and behold, approximately 67% or two thirds of the time the contestant gets the car door if they switch! AMAZING right? There is a lot of statistical reasons for this but basically it comes down to the fact that by opening the one door, the game show host has essentially squished a two thirds probability behind the door the contestant has an option to switch to.

我重复上面的实验,瞧瞧,如果他们切换了,大约67%或三分之二的时间,当参赛者获得车门! 太好了吧? 有很多统计上的原因,但基本上可以归结为一个事实,即打开一扇门,游戏节目主持人实质上已经将参赛者可以选择切换到门后的概率降低了三分之二。

Are you still not convinced? Let us recreate the experiment now using one hundred doors, this time the only catch being that the game show host opens 98 doors. Would you switch now? We see without switching, the contestant has only a 1% chance that they selected the correct door.

您仍然不相信吗? 现在让我们使用一百扇门重新创建实验,这次唯一的收获就是游戏节目主持人打开了98扇门。 您现在要切换吗? 我们看到,无需切换,参赛者选择正确门的机会只有1%。

import random


doors = list(range(100))
trials = 10000
successful_guesses = 0


for i in range(trials):
    car_door = random.choice(doors) 
    selected_door = random.choice(doors)
    new_choice = selected_door
    if car_door ==  new_choice:
        successful_guesses += 1


score = successful_guesses / trials
print(score)

This is where the magic comes, when the contestant switches in this 100 door game, they stand a 99% chance of winning! This is because the game show host, by opening 98 doors, has essentially given them the option to switch to a 99% probability instead. What do you back more, the possibility that you correctly guessed a 1 in 100 chance at the door or the possibility that you didn’t guess it correctly initially?

这就是神奇的地方,当参赛者切换到100门游戏时,他们就有99%的获胜机会! 这是因为游戏节目主持人通过打开98扇门,实际上已为他们提供了切换为99%概率的选项。 您还支持什么呢?是您在门口正确地猜中了100分之一的可能性,还是在最初时没有正确地猜中了?

import random


doors = list(range(100))
trials = 10000
successful_guesses = 0


for i in range(trials):
    car_door = random.choice(doors)
    selected_door = random.choice(doors)
    available_doors = [x for x in doors if x!= selected_door and x!= car_door]
    opened_doors = random.sample(available_doors, len(doors)-2)
    new_choice = [x for x in doors if x not in opened_doors if x != selected_door][0]
    if car_door ==  new_choice:
        successful_guesses += 1


score = successful_guesses / trials
print(score)

The code for all the experiments is available over at my GitHub.

所有实验的代码都可以在我的GitHub上找到。

I hope you enjoyed this article as much as I enjoyed writing it. Let’s make statistics great again😉

希望您喜欢我喜欢写这篇文章。 让统计再一次精彩😉

翻译自: https://levelup.gitconnected.com/monty-hall-meets-monte-carlo-meets-monty-python-a9b3c8883506

蒙蒂霍尔 贝叶斯

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值