蒙蒂霍尔问题_模拟蒙蒂·霍尔问题

蒙蒂霍尔问题

I’ve been hearing about the Monty Hall problem for years and its never quite made sense to me, so I decided to program up a quick simulation.

多年来,我一直在听说有关Monty Hall的问题 ,但对我而言从来没有这么有意义,所以我决定编写一个快速仿真程序。

In the Monty Hall problem, there is a car behind one of three doors. There are goats behind the other two doors. The contestant picks one of the three doors. Monty Hall (the game show host) then reveals that one of the two unchosen doors has a goat behind it. The question is whether the constestant should change the door they picked or keep their choice.

在蒙蒂·霍尔(Monty Hall)问题中,三个门之一后方有一辆汽车。 另外两扇门后面有山羊。 参赛者选择三个门之一。 然后,蒙蒂·霍尔(游戏节目主持人)揭示了两个未选择的门中的一扇后面有一只山羊。 问题是,野兽应该改变他们选择的门还是保持选择。

My first intuition was that it doesn’t matter whether the contestant changes their choice because its equally probable that the car is behind either of the two unopened doors, but I’ve been told this is incorrect! Instead, the contestant is more likely to win the car if they change their choice.

我的第一个直觉是,参赛者是否更改选择并不重要,因为赛车同样有可能在两个未打开的门中的任何一个后面,但是我被告知这是错误的! 相反,如果他们改变选择,则参赛者更有可能赢得赛车。

How can this be? Well, I decided to create a simple simulation of the Monty Hall problem in order to prove to myself that there really is an advantage to changing the chosen door and (hopefully) gain an intuition into how this works.

怎么会这样? 好吧,我决定为Monty Hall问题创建一个简单的模拟,以便向自己证明改变所选门的确有好处,并且(希望)对它的工作原理有所了解。

Below I’ve written my little simulation. A jupyter notebook with this code is available on my github.

下面,我写了我的小模拟。 我的github上有一个带有此代码的jupyter笔记本。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26

Here I plot the results

在这里我绘制结果

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
Probability of choosing correctly if change choice: 0.67
Probability of choosing correctly if do not change choice: 0.33
Probability of difference arising from chance: 0.00000
Probability of choosing correctly if change choice: 0.67
Probability of choosing correctly if do not change choice: 0.33
Probability of difference arising from chance: 0.00000
 

Clearly, the contestant should change their choice!

显然,参赛者应该改变选择!

So now, just to make sure I am not crazy, I decided to simulate the Monty Hall problem with the contestant choosing what door to open after Monty Hall opens a door with a goat.

所以现在,为了确保我不发疯,我决定模拟模拟Monty Hall的问题,让参赛者选择在Monty Hall用山羊打开一扇门之后要打开的门。

change, no_change = [],[]
for i in range(samples):
    #shuffle data
    vect = copy.copy(start_vect)
    random.shuffle(vect)

    #show bad door
    bad = vect.pop(int(np.where(np.array(vect)==0)[0][0]))

    #make choice
    choice = vect.pop(random.randint(0,1))
    no_change.append(choice)

    change.append(vect)
change , no_change = [],[]
 for i in range ( samples ):
     #shuffle data
     vect = copy . copy ( start_vect )
     random . shuffle ( vect )
 
     #show bad door
     bad = vect . pop ( int ( np . where ( np . array ( vect ) == 0 )[ 0 ][ 0 ]))
 
     #make choice
     choice = vect . pop ( random . randint ( 0 , 1 ))
     no_change . append ( choice )
 
     change . append ( vect )
 
plt.bar([0.5,1.5],[np.mean(change),np.mean(no_change)],width=1.0)
plt.xlim((0,3))
plt.ylim((0,1))
plt.ylabel('Proportion Correct Choice')
plt.xticks((1.0,2.0),['Change Choice', 'Do not chance choice'])

obs = np.array([[np.sum(change), np.sum(no_change)], [samples, samples]])
print('Probability of choosing correctly if change choice: %0.2f' % np.mean(change))
print('Probability of choosing correctly if do not change choice: %0.2f' % np.mean(no_change))
print('Probability of difference arising from chance: %0.5f' % stats.chi2_contingency(obs)[1])
plt . bar ([ 0.5 , 1.5 ],[ np . mean ( change ), np . mean ( no_change )], width = 1.0 )
 plt . xlim (( 0 , 3 ))
 plt . ylim (( 0 , 1 ))
 plt . ylabel ( 'Proportion Correct Choice' )
 plt . xticks (( 1.0 , 2.0 ),[ 'Change Choice' , 'Do not chance choice' ])
 
 obs = np . array ([[ np . sum ( change ), np . sum ( no_change )], [ samples , samples ]])
 print ( 'Probability of choosing correctly if change choice:  %0.2f ' % np . mean ( change ))
 print ( 'Probability of choosing correctly if do not change choice:  %0.2f ' % np . mean ( no_change ))
 print ( 'Probability of difference arising from chance:  %0.5f ' % stats . chi2_contingency ( obs )[ 1 ])
 

Now, there is clearly no difference between whether the contestant changes their choice or not.

现在,参赛者是否更改选择之间显然没有区别。

翻译自: https://www.pybloggers.com/2016/12/simulating-the-monty-hall-problem/

蒙蒂霍尔问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值