Life Probability Simulation¶
In [1]:
import numpy as np
In [22]:
def get_mortality(age, gender):
if gender in ("Male", "male", "m", "M"):
age_thresholds = [(4, 25.5), (14, 16.1), (24, 121.9), (34, 221.1), (44, 325.5),
(54, 601.5), (64, 1323.3), (74, 2581), (84, 5937), (float('inf'), 16354)]
elif gender in ("Female", "female", "f", "F"):
age_thresholds = [(4, 20), (14, 11), (24, 45), (34, 96), (44, 171),
(54, 349), (64, 773), (74, 1627), (84, 4261), (float('inf'), 14560)]
for threshold, rate in age_thresholds:
if age <= threshold:
return rate / 100000
In [45]:
def simulate_lifespan(start_age, gender):
age = start_age
while True:
if np.random.rand() < get_mortality(age, gender):
break
age += 1
return age
def prob_die_before(start_age, end_age, gender='m', runs=10000):
count = 0
for _ in range(runs):
death_age = simulate_lifespan(start_age, gender)
count += death_age <= end_age
return count / runs
def prob_kid_die_first(kid_start_age, adult_start_age, kid_gender="Male", adult_gender="Male", kid_max_age=999, runs=10000):
count = 0
for _ in range(runs):
kid = simulate_lifespan(kid_start_age, kid_gender)
adult = simulate_lifespan(adult_start_age, adult_gender)
count += (kid - kid_start_age < adult - adult_start_age) and (kid < kid_max_age)
return count / runs
def prob_parent_die_first(kid_start_age, adult_start_age, kid_gender="Male", adult_gender="Male", kid_max_age=999, runs=10000):
count = 0
for _ in range(runs):
kid = simulate_lifespan(kid_start_age, kid_gender)
adult = simulate_lifespan(adult_start_age, adult_gender)
count += (kid - kid_start_age > adult - adult_start_age) and (adult < kid_max_age-kid_start_age+adult_start_age)
return count / runs
def prob_lose_partner(my_start_age, partner_start_age, my_gender="Male", partner_gender="Female", my_age_cap=999, runs=10000):
count = 0
for _ in range(runs):
my_age = simulate_lifespan(my_start_age, my_gender)
partner_age = simulate_lifespan(partner_start_age, partner_gender)
count += (my_age - my_start_age > partner_age - partner_start_age) and (partner_age - partner_start_age + my_start_age <= my_age_cap)
return count / runs
1.失独:娃小于20岁没了¶
In [28]:
print("male parent of son:", prob_kid_die_first(3, 30, kid_max_age=20))
print("female parent of daughter:", prob_kid_die_first(3, 30, kid_gender='f', adult_gender='f', kid_max_age=20))
male parent of son: 0.0086 female parent of daughter: 0.0033
2.白发人送黑发人:大人去世之前娃没了¶
In [30]:
print("male parent of son:", prob_kid_die_first(3, 30))
print("female parent of son:", prob_kid_die_first(3, 30, adult_gender='f'))
print("male parent of daughter:", prob_kid_die_first(3, 30, kid_gender='f', adult_gender='m'))
print("female parent of daughter:", prob_kid_die_first(3, 30, kid_gender='f', adult_gender='f'))
male parent of son: 0.1142 female parent of son: 0.1558 male parent of daughter: 0.0658 female parent of daughter: 0.0877
3.子欲养而亲不待:老人在中年人四十岁之前没了¶
In [37]:
print("male parent of son:", prob_parent_die_first(30, 60, kid_max_age=40))
print("female parent of son:", prob_parent_die_first(30, 60, adult_gender='f', kid_max_age=40))
print("male parent of daughter:", prob_parent_die_first(30, 60, kid_gender='f', adult_gender='m', kid_max_age=40))
print("female parent of daughter:", prob_parent_die_first(30, 60, kid_gender='f', adult_gender='f', kid_max_age=40))
male parent of son: 0.1764 female parent of son: 0.1073 male parent of daughter: 0.1787 female parent of daughter: 0.1129
4.中年丧偶:五十岁之前伴侣没了¶
In [41]:
print("husband loses wife", prob_lose_partner(30, 30, my_age_cap=50))
print("wife loses husband", prob_lose_partner(30, 30, 'f', 'm', my_age_cap=50))
husband loses wife 0.0389 wife loses husband 0.0796
5.自己活过40,50,60,70的概率¶
In [46]:
for age in (40,50,60,70):
print(f"male die before {age}:", prob_die_before(30, age))
for age in (40,50,60,70):
print(f"female die before {age}:", prob_die_before(30, age, 'f'))
male die before 40: 0.0273 male die before 50: 0.0791 male die before 60: 0.1663 male die before 70: 0.3243 female die before 40: 0.0153 female die before 50: 0.0435 female die before 60: 0.0973 female die before 70: 0.2107
In [ ]:
In [ ]: