https://neal.fun/absurd-trolley-problems/

In [1]:
# Life Expectancy Modeling in Python

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Parameters for life expectancy
mean_life_expectancy = 80  # Mean life expectancy in years
std_dev_life_expectancy = 8  # Standard deviation for life expectancy in years

# Define age x
x = 70  # Example: current age

# Simulate life expectancy distribution for a general population
def simulate_life_expectancy(mean, std_dev, size=10000):
    """
    Simulates a life expectancy distribution.
    """
    return np.random.normal(mean, std_dev, size=size)

# Filter life expectancies for individuals of age x
def remaining_life_expectancy_distribution(age, life_expectancies):
    """
    Adjust life expectancy distribution for someone of current age.
    """
    return life_expectancies[life_expectancies > age] - age

# Simulate general life expectancy
general_life_expectancies = simulate_life_expectancy(mean_life_expectancy, std_dev_life_expectancy)

# Compute remaining life expectancy distribution for a person of age x
remaining_life_expectancies = remaining_life_expectancy_distribution(x, general_life_expectancies)

# Plot distributions
sns.set(style="whitegrid")
plt.figure(figsize=(12, 6))

# General life expectancy distribution
sns.histplot(general_life_expectancies, bins=50, kde=True, label="General Life Expectancy", color="blue", stat="density")

# Remaining life expectancy distribution
sns.histplot(remaining_life_expectancies + x, bins=50, kde=True, label=f"Total Life Expectancy for Age {x}", color="green", stat="density")

# Add mean lines
plt.axvline(mean_life_expectancy, color='blue', linestyle='dashed', linewidth=2, label="Mean General Life Expectancy")
plt.axvline(np.mean(remaining_life_expectancies) + x, color='green', linestyle='dashed', linewidth=2, label=f"Mean Total Life Expectancy for Age {x}")

plt.title(f"Life Expectancy Distributions (Age {x})")
plt.xlabel("Years")
plt.ylabel("Density")
plt.legend()
plt.show()

# Display some statistics
print(f"General Life Expectancy Mean: {np.mean(general_life_expectancies):.2f} years")
print(f"Total Life Expectancy Mean for Age {x}: {np.mean(remaining_life_expectancies) + x:.2f} years")
No description has been provided for this image
General Life Expectancy Mean: 80.04 years
Total Life Expectancy Mean for Age 70: 81.72 years
In [2]:
number_of_peeps = 100

# Simulate population with current age and expected life expectancy
def simulate_population_with_current_age_and_le(size, mean, std_dev):
    """
    Simulate a population with random current ages and corresponding life expectancies.
    """
    life_expectancies = simulate_life_expectancy(mean, std_dev, size=size)
    current_ages = np.random.uniform(0, life_expectancies, size=size)  # Random age within life expectancy
    remaining_life = life_expectancies - current_ages
    return list(zip(current_ages, remaining_life))

# Simulate 100 individuals
population_data = simulate_population_with_current_age_and_le(number_of_peeps, mean_life_expectancy, std_dev_life_expectancy)

# Display simulated data
print("\nSimulated Population (Current Age and Remaining Life Expectancy):")
for i, (age, remaining) in enumerate(population_data):
    print(f"Individual {i + 1}: Current Age = {age:.2f} years, Remaining Life Expectancy = {remaining:.2f} years")

# Plot current age and remaining life expectancy
ages, remaining_life = zip(*population_data)

plt.figure(figsize=(12, 6))
bars = plt.bar(range(len(ages)), remaining_life, color=["green" if r > 50 else "orange" if r > 10 else "red" for r in remaining_life])

# Add lines at 10 and 50 years
plt.axhline(10, color='red', linestyle='dashed', linewidth=1, label="10 Years Remaining")
plt.axhline(50, color='orange', linestyle='dashed', linewidth=1, label="50 Years Remaining")

plt.title("Remaining Life Expectancy")
plt.xlabel("Individual")
plt.ylabel("Remaining Life Expectancy (years)")
plt.legend()
plt.show()
Simulated Population (Current Age and Remaining Life Expectancy):
Individual 1: Current Age = 5.78 years, Remaining Life Expectancy = 74.88 years
Individual 2: Current Age = 8.37 years, Remaining Life Expectancy = 73.20 years
Individual 3: Current Age = 15.24 years, Remaining Life Expectancy = 61.56 years
Individual 4: Current Age = 66.02 years, Remaining Life Expectancy = 22.07 years
Individual 5: Current Age = 29.35 years, Remaining Life Expectancy = 69.70 years
Individual 6: Current Age = 17.38 years, Remaining Life Expectancy = 66.27 years
Individual 7: Current Age = 1.92 years, Remaining Life Expectancy = 72.07 years
Individual 8: Current Age = 54.13 years, Remaining Life Expectancy = 22.64 years
Individual 9: Current Age = 50.62 years, Remaining Life Expectancy = 30.55 years
Individual 10: Current Age = 26.79 years, Remaining Life Expectancy = 58.26 years
Individual 11: Current Age = 56.97 years, Remaining Life Expectancy = 14.82 years
Individual 12: Current Age = 80.61 years, Remaining Life Expectancy = 7.07 years
Individual 13: Current Age = 87.61 years, Remaining Life Expectancy = 0.40 years
Individual 14: Current Age = 69.45 years, Remaining Life Expectancy = 10.47 years
Individual 15: Current Age = 0.16 years, Remaining Life Expectancy = 73.37 years
Individual 16: Current Age = 41.13 years, Remaining Life Expectancy = 47.37 years
Individual 17: Current Age = 73.88 years, Remaining Life Expectancy = 14.40 years
Individual 18: Current Age = 80.87 years, Remaining Life Expectancy = 3.09 years
Individual 19: Current Age = 6.97 years, Remaining Life Expectancy = 80.53 years
Individual 20: Current Age = 21.92 years, Remaining Life Expectancy = 72.44 years
Individual 21: Current Age = 40.99 years, Remaining Life Expectancy = 27.62 years
Individual 22: Current Age = 42.73 years, Remaining Life Expectancy = 37.72 years
Individual 23: Current Age = 24.61 years, Remaining Life Expectancy = 43.93 years
Individual 24: Current Age = 30.41 years, Remaining Life Expectancy = 57.89 years
Individual 25: Current Age = 85.52 years, Remaining Life Expectancy = 4.94 years
Individual 26: Current Age = 68.92 years, Remaining Life Expectancy = 9.75 years
Individual 27: Current Age = 47.24 years, Remaining Life Expectancy = 36.36 years
Individual 28: Current Age = 19.51 years, Remaining Life Expectancy = 59.73 years
Individual 29: Current Age = 11.37 years, Remaining Life Expectancy = 88.02 years
Individual 30: Current Age = 47.06 years, Remaining Life Expectancy = 32.08 years
Individual 31: Current Age = 54.77 years, Remaining Life Expectancy = 30.44 years
Individual 32: Current Age = 59.25 years, Remaining Life Expectancy = 10.42 years
Individual 33: Current Age = 66.87 years, Remaining Life Expectancy = 5.11 years
Individual 34: Current Age = 65.62 years, Remaining Life Expectancy = 21.78 years
Individual 35: Current Age = 58.03 years, Remaining Life Expectancy = 33.03 years
Individual 36: Current Age = 10.28 years, Remaining Life Expectancy = 64.40 years
Individual 37: Current Age = 51.81 years, Remaining Life Expectancy = 22.58 years
Individual 38: Current Age = 37.17 years, Remaining Life Expectancy = 52.47 years
Individual 39: Current Age = 37.86 years, Remaining Life Expectancy = 56.57 years
Individual 40: Current Age = 56.50 years, Remaining Life Expectancy = 22.68 years
Individual 41: Current Age = 34.50 years, Remaining Life Expectancy = 43.86 years
Individual 42: Current Age = 1.00 years, Remaining Life Expectancy = 79.22 years
Individual 43: Current Age = 1.63 years, Remaining Life Expectancy = 83.44 years
Individual 44: Current Age = 36.81 years, Remaining Life Expectancy = 57.68 years
Individual 45: Current Age = 47.09 years, Remaining Life Expectancy = 35.37 years
Individual 46: Current Age = 22.56 years, Remaining Life Expectancy = 40.71 years
Individual 47: Current Age = 1.45 years, Remaining Life Expectancy = 62.13 years
Individual 48: Current Age = 59.18 years, Remaining Life Expectancy = 26.38 years
Individual 49: Current Age = 14.84 years, Remaining Life Expectancy = 65.78 years
Individual 50: Current Age = 84.37 years, Remaining Life Expectancy = 3.11 years
Individual 51: Current Age = 40.51 years, Remaining Life Expectancy = 28.08 years
Individual 52: Current Age = 8.25 years, Remaining Life Expectancy = 64.65 years
Individual 53: Current Age = 63.60 years, Remaining Life Expectancy = 16.93 years
Individual 54: Current Age = 65.38 years, Remaining Life Expectancy = 12.39 years
Individual 55: Current Age = 19.27 years, Remaining Life Expectancy = 68.68 years
Individual 56: Current Age = 41.41 years, Remaining Life Expectancy = 53.60 years
Individual 57: Current Age = 28.84 years, Remaining Life Expectancy = 46.00 years
Individual 58: Current Age = 12.23 years, Remaining Life Expectancy = 52.55 years
Individual 59: Current Age = 16.44 years, Remaining Life Expectancy = 71.74 years
Individual 60: Current Age = 34.24 years, Remaining Life Expectancy = 42.85 years
Individual 61: Current Age = 57.94 years, Remaining Life Expectancy = 31.48 years
Individual 62: Current Age = 43.67 years, Remaining Life Expectancy = 27.89 years
Individual 63: Current Age = 5.51 years, Remaining Life Expectancy = 65.29 years
Individual 64: Current Age = 17.41 years, Remaining Life Expectancy = 57.80 years
Individual 65: Current Age = 46.85 years, Remaining Life Expectancy = 9.60 years
Individual 66: Current Age = 20.11 years, Remaining Life Expectancy = 54.77 years
Individual 67: Current Age = 50.21 years, Remaining Life Expectancy = 22.81 years
Individual 68: Current Age = 74.92 years, Remaining Life Expectancy = 5.72 years
Individual 69: Current Age = 31.35 years, Remaining Life Expectancy = 45.30 years
Individual 70: Current Age = 17.58 years, Remaining Life Expectancy = 53.28 years
Individual 71: Current Age = 27.37 years, Remaining Life Expectancy = 44.33 years
Individual 72: Current Age = 61.31 years, Remaining Life Expectancy = 29.93 years
Individual 73: Current Age = 76.17 years, Remaining Life Expectancy = 1.85 years
Individual 74: Current Age = 90.87 years, Remaining Life Expectancy = 3.85 years
Individual 75: Current Age = 11.45 years, Remaining Life Expectancy = 73.68 years
Individual 76: Current Age = 86.45 years, Remaining Life Expectancy = 2.18 years
Individual 77: Current Age = 43.75 years, Remaining Life Expectancy = 40.33 years
Individual 78: Current Age = 31.27 years, Remaining Life Expectancy = 67.55 years
Individual 79: Current Age = 75.75 years, Remaining Life Expectancy = 2.06 years
Individual 80: Current Age = 53.70 years, Remaining Life Expectancy = 42.04 years
Individual 81: Current Age = 38.30 years, Remaining Life Expectancy = 34.51 years
Individual 82: Current Age = 4.58 years, Remaining Life Expectancy = 79.08 years
Individual 83: Current Age = 70.03 years, Remaining Life Expectancy = 11.47 years
Individual 84: Current Age = 35.70 years, Remaining Life Expectancy = 43.44 years
Individual 85: Current Age = 41.63 years, Remaining Life Expectancy = 37.25 years
Individual 86: Current Age = 7.12 years, Remaining Life Expectancy = 57.38 years
Individual 87: Current Age = 2.97 years, Remaining Life Expectancy = 62.16 years
Individual 88: Current Age = 49.23 years, Remaining Life Expectancy = 22.44 years
Individual 89: Current Age = 20.19 years, Remaining Life Expectancy = 66.78 years
Individual 90: Current Age = 51.86 years, Remaining Life Expectancy = 51.39 years
Individual 91: Current Age = 47.09 years, Remaining Life Expectancy = 30.06 years
Individual 92: Current Age = 32.54 years, Remaining Life Expectancy = 51.04 years
Individual 93: Current Age = 23.85 years, Remaining Life Expectancy = 71.64 years
Individual 94: Current Age = 41.83 years, Remaining Life Expectancy = 35.14 years
Individual 95: Current Age = 52.38 years, Remaining Life Expectancy = 16.08 years
Individual 96: Current Age = 50.13 years, Remaining Life Expectancy = 27.50 years
Individual 97: Current Age = 53.76 years, Remaining Life Expectancy = 27.19 years
Individual 98: Current Age = 9.32 years, Remaining Life Expectancy = 70.35 years
Individual 99: Current Age = 70.33 years, Remaining Life Expectancy = 13.06 years
Individual 100: Current Age = 58.29 years, Remaining Life Expectancy = 27.46 years
No description has been provided for this image
In [3]:
def costs(x=10, factor=5, output=True, pop=population_data):
    max_x_counter = [min(remaining, x) for _, remaining in pop]
    max_y_counter = [min(remaining, x*factor) for _, remaining in pop]
    
    average_max_x = np.mean(max_x_counter)
    average_max_y = np.mean(max_y_counter)
    
    total_cost_x = factor*average_max_x
    total_cost_y = average_max_y

    if output:
        print("#############")
        print(f"avg cost of 1x {factor*x}: {total_cost_y:.2f} years")
        print(f"avg cost of {factor}x {x}: {total_cost_x:.2f} years")
        print(f"cost of split = {(100*total_cost_x/total_cost_y)-100:.2f}%")
        
    
    return total_cost_x, total_cost_y
In [4]:
x= 10
factor = 5
xcost, ycost = costs(x, factor)
#############
avg cost of 1x 50: 34.42 years
avg cost of 5x 10: 46.44 years
cost of split = 34.91%
In [8]:
bigpop = simulate_population_with_current_age_and_le(100000, mean_life_expectancy, std_dev_life_expectancy)

xvals = [] 
yvals = []
for i in range(1, 11):
    xcost, ycost = costs(x=i, factor=5, output=True, pop=bigpop)
    xvals.append(5*i)
    yvals.append((100*xcost/ycost)-100)

plt.plot(xvals, yvals)

# Adding labels
plt.xlabel('Total Lifetime')
plt.ylabel('Cost of Splitting in %')
plt.title('Splitting of Lifetime penalty for 5 people')

# Display the plot
plt.show()
#############
avg cost of 1x 5: 4.84 years
avg cost of 5x 1: 4.97 years
cost of split = 2.62%
#############
avg cost of 1x 10: 9.37 years
avg cost of 5x 2: 9.87 years
cost of split = 5.40%
#############
avg cost of 1x 15: 13.58 years
avg cost of 5x 3: 14.71 years
cost of split = 8.37%
#############
avg cost of 1x 20: 17.47 years
avg cost of 5x 4: 19.49 years
cost of split = 11.55%
#############
avg cost of 1x 25: 21.06 years
avg cost of 5x 5: 24.21 years
cost of split = 14.94%
#############
avg cost of 1x 30: 24.33 years
avg cost of 5x 6: 28.86 years
cost of split = 18.60%
#############
avg cost of 1x 35: 27.29 years
avg cost of 5x 7: 33.45 years
cost of split = 22.58%
#############
avg cost of 1x 40: 29.92 years
avg cost of 5x 8: 37.97 years
cost of split = 26.90%
#############
avg cost of 1x 45: 32.24 years
avg cost of 5x 9: 42.43 years
cost of split = 31.61%
#############
avg cost of 1x 50: 34.24 years
avg cost of 5x 10: 46.83 years
cost of split = 36.77%
No description has been provided for this image