在学习受限玻尔兹曼模型的时候去看了条件随机场和概率图模型,那篇博客中有提到了一个三门问题,感觉这个结论非常反直觉,于是做了个小实验,过程很简单,但是代码写的很啰嗦,哈哈。
import numpy as np
a = [False, False, False] # 三个门
result0 = 0 #不换
result1 = 0
n = 5000
for i in range(n):
D = 0 # 假设车在第一个门后边
F = np.random.randint(0, 3) # F为第一次选择的门
a[F] = True # 把门打开
if D==F: #第一次选中
H = np.random.randint(1,3) # 主持人在2,3之间随机选一个门打开
else:#第一次没选中
H = 2 if F == 1 else 1 # 主持人打开剩下的门
print("第",i,"次实验",a,"主持人打开第",H,"扇门")
I = np.random.randint(0,3) #随机选择换或不换
while I == H:
I = np.random.randint(0,3)
a[H] = False
a[I] = True
if I == 0 and I == F:
print("不换得车")
result0 += 1
if I == 0 and not(I == F):
print("换门得车")
result1 += 1
if not(I == 0) and I == F:
print("不换不得车")
if not(I == 0) and not(I == F):
print("换门不得车")
a[I] = False
#print("换后:",a ,"\n")
print("不换:",result0/n, "换:", result1/n)
结果如下:
确实是换的话得奖概率更大一些哈哈。