You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python OOP实现45岁后递增死亡概率模型,寿命上限90岁

Python 面向对象实现年龄相关递增死亡概率模拟

核心逻辑

45岁后死亡概率逐年递增,90岁时死亡概率为100%。我们通过线性递增的方式计算每个年龄对应的死亡概率:从45岁的自定义初始概率均匀增长到90岁的100%,确保概率随年龄稳定上升而非随机波动。

代码实现

import random

class Person:
    def __init__(self, current_age, death_prob_at_45=0.01):
        self.age = current_age
        self.death_prob_base = death_prob_at_45  # 45岁初始死亡概率,默认1%
        self.max_age = 90

    def get_death_probability(self):
        if self.age < 45:
            return 0.0  # 45岁前死亡概率设为0,可按需调整
        elif self.age >= self.max_age:
            return 1.0  # 达到最高寿命,死亡概率100%
        
        total_increment_years = self.max_age - 45
        years_since_45 = self.age - 45
        # 线性递增公式:初始概率 + (100% - 初始概率) * 已过年份/总递增年份
        return self.death_prob_base + (1.0 - self.death_prob_base) * (years_since_45 / total_increment_years)

    def simulate_year(self):
        """模拟当年是否死亡,返回布尔值"""
        if self.age >= self.max_age:
            return True
        
        current_prob = self.get_death_probability()
        # 用随机数判断死亡,但概率随年龄递增
        return random.random() <= current_prob

使用示例

# 创建一个45岁的个体
individual = Person(current_age=45)

# 逐年模拟生存状态
while True:
    prob = individual.get_death_probability()
    print(f"年龄:{individual.age},当年死亡概率:{prob:.2%}")
    
    if individual.simulate_year():
        print(f"个体在{individual.age}岁死亡")
        break
    
    individual.age += 1
    if individual.age > individual.max_age:
        print(f"个体活到了最高寿命{individual.max_age}岁")
        break

扩展说明

  • 若需要非线性递增(比如后期死亡概率增速更快),只需修改get_death_probability中的计算逻辑,例如用指数函数替代线性公式:
    # 指数递增示例,概率增速随年龄增长加快
    return self.death_prob_base + (1.0 - self.death_prob_base) * ((years_since_45 / total_increment_years) ** 2)
    

内容的提问来源于stack exchange,提问作者Marcel Castro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 16:50:12