Today I found an interesting revision of Avengers: Endgame. It says:
初代复仇者六人组,每人带一宝石手套,去找灭霸决战,到了草屋,七人围圈而坐,六对一,互相打响指意图消灭对方。灭霸人少,给予先打响指的权利,灭霸打后,超级英雄全化灰就灭霸胜,没全化灰就由一个活着的超级英雄打一个响指,看二分之一消失的概率能不能把灭霸打成灰。没成灰就继续第二回合,灭霸继续优先打响指。问灭霸死的概率是多少?
The original six avengers, each carrying a stone and fight with Thanos. Thanos snaps his fingers first and each avenger has a 50% probability to die. If all of the avengers die, thanos wins. If not, one of the alive avengers snap fingers and thanos has 50% percent to die. The fights will continue until one side wins. the question is: what is the probability of thanos death?
I still didn’t have a mathematical depict of the probability. But I simulate the data to find out the probability of Thanos’ death is about 80%.
Like Dr. Strange, I also find the result from 14000605 times test.
Refer to this repo
import numpy as np
%pylab inline
from tqdm import tqdm_notebook as tqdm
import seaborn as sns
import pandas as pd
np.random.seed(1111)
#setup figure template
figure_template_path = 'bin'
if figure_template_path not in sys.path:
sys.path.append(figure_template_path)
from importlib import reload
import figure_template
#force reload of the module
reload(figure_template)
from figure_template import display_dataframe, embed_pdf_figure, embed_pdf_pages,std_plot
Populating the interactive namespace from numpy and matplotlib
np.random.randint(0,2,size=6)
array([0, 1, 1, 1, 0, 0])
def check_survive(input_num):
'''
input_num: how many people to be decided to survive by stones
status: 0 means dead, 1 means alive
'''
random_output = np.random.randint(0,2,size=input_num)
alive = random_output.sum()
dead = random_output.shape[0] - alive
return alive, dead
avengers_alive_num = 6
round_num = 0
while True:
round_num += 1
avengers_alive_num,avengers_dead_num = check_survive(avengers_alive_num)
print ('Round {}, avengers alive: {}, dead: {}'.format(round_num,avengers_alive_num,avengers_dead_num) )
if avengers_alive_num ==0:
print ('Avengers die first')
break
elif np.random.randint(2) ==0:
print ('Thanos die first')
break
Round 1, avengers alive: 1, dead: 5
Thanos die first
def summarize(avengers_alive_num = 6):
round_num = 0
while True:
round_num += 1
avengers_alive_num,avengers_dead_num = check_survive(avengers_alive_num)
#print ('Round {}, avengers alive: {}, dead: {}'.format(round_num,avengers_alive_num,avengers_dead_num) )
if avengers_alive_num ==0:
#print ('Avengers die first')
return round_num, 1
elif np.random.randint(2) ==0:
#print ('Thanos die first')
return round_num, 0
test_time = 14000605
summarize_data = np.ndarray([test_time,2])
for i in tqdm(range(test_time)):
summarize_data[i] = summarize()
HBox(children=(IntProgress(value=0, max=14000605), HTML(value='')))
np.savetxt('summarize_data_num_6.txt',summarize_data.astype('uint8'),fmt='%1d')
fig,ax=plt.subplots(figsize=(8,4))
sns.distplot(summarize_data[:,1],kde=0,ax=ax)
embed_pdf_figure()
fig,ax=plt.subplots(figsize=(8,4))
sns.distplot(summarize_data[np.where(summarize_data[:,1]==0)][:,0],kde=0,ax=ax,color='r')
sns.distplot(summarize_data[np.where(summarize_data[:,1]==1)][:,0],kde=0,ax=ax,color='m')
embed_pdf_figure()
print ('Avengers win {}, Thanos win {}'.format(1 - summarize_data[:,1].sum()/test_time,summarize_data[:,1].sum()/test_time) )
Avengers win 0.7940833985388489, Thanos win 0.20591660146115115
unique_sum = np.unique(summarize_data[np.where(summarize_data[:,1]==0)][:,0],return_counts=1)
df = pd.DataFrame(np.concatenate((unique_sum[1].reshape(-1,1),unique_sum[0].astype('int').reshape(-1,1),unique_sum[1].reshape(-1,1)/test_time),axis=1))
df.iloc[:,1] = np.array(df.iloc[:,1]).astype('int')
df = df.set_index(1)
df.columns = ['win time','win probability']
display_dataframe(df,filename='Avengers win times in {} times'.format(test_time))
win time | win probability | |
---|---|---|
1 | ||
1 | 6.89032e+06 | 0.492144 |
2 | 2.8789e+06 | 0.205627 |
3 | 964446 | 0.068886 |
4 | 281804 | 0.020128 |
5 | 75835 | 0.00541655 |
6 | 19639 | 0.00140273 |
7 | 4988 | 0.00035627 |
8 | 1274 | 9.09961e-05 |
9 | 331 | 2.36418e-05 |
10 | 91 | 6.49972e-06 |
11 | 17 | 1.21423e-06 |
12 | 5 | 3.57127e-07 |
13 | 1 | 7.14255e-08 |
unique_sum = np.unique(summarize_data[np.where(summarize_data[:,1]==1)][:,0],return_counts=1)
df = pd.DataFrame(np.concatenate((unique_sum[1].reshape(-1,1),unique_sum[0].astype('int').reshape(-1,1),unique_sum[1].reshape(-1,1)/test_time),axis=1))
df.iloc[:,1] = np.array(df.iloc[:,1]).astype('int')
df = df.set_index(1)
df.columns = ['win time','win probability']
display_dataframe(df,filename='Thanos win times in {} times'.format(test_time))
win time | win probability | |
---|---|---|
1 | ||
1 | 218176 | 0.0155833 |
2 | 1.13563e+06 | 0.0811129 |
3 | 948005 | 0.0677117 |
4 | 402860 | 0.0287745 |
5 | 128810 | 0.00920032 |
6 | 36483 | 0.00260582 |
7 | 9634 | 0.000688113 |
8 | 2508 | 0.000179135 |
9 | 660 | 4.71408e-05 |
10 | 147 | 1.04995e-05 |
11 | 27 | 1.92849e-06 |
12 | 11 | 7.8568e-07 |
13 | 5 | 3.57127e-07 |
14 | 1 | 7.14255e-08 |
It’s not fair! In fact, if there are only two avengers, the game will be more fair.
灭霸死的有点惨,其实复仇者初始只有两个人的时候概率稍微对等
def summarize(avengers_alive_num = 6):
round_num = 0
while True:
round_num += 1
avengers_alive_num,avengers_dead_num = check_survive(avengers_alive_num)
#print ('Round {}, avengers alive: {}, dead: {}'.format(round_num,avengers_alive_num,avengers_dead_num) )
if avengers_alive_num ==0:
#print ('Avengers die first')
return round_num, 1
elif np.random.randint(2) ==0:
#print ('Thanos die first')
return round_num, 0
test_time = 14000605
summarize_data = np.ndarray([test_time,2])
for i in tqdm(range(test_time)):
summarize_data[i] = summarize(avengers_alive_num =2)
HBox(children=(IntProgress(value=0, max=14000605), HTML(value='')))
fig,ax=plt.subplots(2,1,figsize=(8,8))
sns.distplot(summarize_data[:,1],kde=0,ax=ax[0])
sns.distplot(summarize_data[np.where(summarize_data[:,1]==0)][:,0],kde=0,ax=ax[1],color='r')
sns.distplot(summarize_data[np.where(summarize_data[:,1]==1)][:,0],kde=0,ax=ax[1],color='m')
embed_pdf_figure()
print ('Avengers win {}, Thanos win {}'.format(1 - summarize_data[:,1].sum()/test_time,summarize_data[:,1].sum()/test_time) )
Avengers win 0.523922359069483, Thanos win 0.4760776409305169
unique_sum = np.unique(summarize_data[np.where(summarize_data[:,1]==0)][:,0],return_counts=1)
df = pd.DataFrame(np.concatenate((unique_sum[1].reshape(-1,1),unique_sum[0].astype('int').reshape(-1,1),unique_sum[1].reshape(-1,1)/test_time),axis=1))
df.iloc[:,1] = np.array(df.iloc[:,1]).astype('int')
df = df.set_index(1)
df.columns = ['win time','win probability']
display_dataframe(df,filename='Avengers win times in {} times'.format(test_time))
unique_sum = np.unique(summarize_data[np.where(summarize_data[:,1]==1)][:,0],return_counts=1)
df = pd.DataFrame(np.concatenate((unique_sum[1].reshape(-1,1),unique_sum[0].astype('int').reshape(-1,1),unique_sum[1].reshape(-1,1)/test_time),axis=1))
df.iloc[:,1] = np.array(df.iloc[:,1]).astype('int')
df = df.set_index(1)
df.columns = ['win time','win probability']
display_dataframe(df,filename='Thanos win times in {} times'.format(test_time))
win time | win probability | |
---|---|---|
1 | ||
1 | 5.25233e+06 | 0.37515 |
2 | 1.53058e+06 | 0.109322 |
3 | 409989 | 0.0292837 |
4 | 106393 | 0.00759917 |
5 | 26925 | 0.00192313 |
6 | 6764 | 0.000483122 |
7 | 1723 | 0.000123066 |
8 | 377 | 2.69274e-05 |
9 | 102 | 7.2854e-06 |
10 | 35 | 2.49989e-06 |
11 | 11 | 7.8568e-07 |
13 | 1 | 7.14255e-08 |
win time | win probability | |
---|---|---|
1 | ||
1 | 3.49891e+06 | 0.249911 |
2 | 2.18546e+06 | 0.156097 |
3 | 712026 | 0.0508568 |
4 | 199091 | 0.0142202 |
5 | 52039 | 0.00371691 |
6 | 13297 | 0.000949745 |
7 | 3438 | 0.000245561 |
8 | 816 | 5.82832e-05 |
9 | 215 | 1.53565e-05 |
10 | 65 | 4.64266e-06 |
11 | 17 | 1.21423e-06 |
12 | 5 | 3.57127e-07 |
13 | 1 | 7.14255e-08 |
print ('Avengers win {}, Thanos win {}'.format(1 - summarize_data[:,1].sum()/test_time,summarize_data[:,1].sum()/test_time) )
Avengers win 0.523922359069483, Thanos win 0.4760776409305169
np.savetxt('summarize_data_num_2.txt',summarize_data.astype('uint8'),fmt='%1d')