-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubi.py
More file actions
79 lines (60 loc) · 2.86 KB
/
Copy pathubi.py
File metadata and controls
79 lines (60 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
import pandas as pd
import json
def future_value_annuity(pmt, rate, periods):
"""Calculate the future value of an annuity."""
r = rate / 12 # Monthly interest rate
n = periods * 12 # Total number of monthly payments
return pmt * ((1 + r)**n - 1) / r
def present_value_future_value(fv, rate, periods):
"""Calculate the present value needed to match a future value."""
r = rate / 12 # Monthly interest rate
n = periods * 12 # Total number of monthly payments
return fv / ((1 + r)**n)
def calculate_fv_and_pv_for_ages(start_age, end_age, initial_monthly_payment, annual_rate, inflation_rate):
"""Calculate the future value and present value for each age, adjusting for inflation."""
results = {}
for age in range(start_age, end_age):
years_invested = 80 - age
# Adjust monthly payment for inflation
inflated_monthly_payment = initial_monthly_payment * ((1 + inflation_rate) ** (years_invested))
fv = future_value_annuity(inflated_monthly_payment, annual_rate, years_invested)
pv = present_value_future_value(fv, annual_rate, years_invested)
results[age] = {'Future Value': fv, 'Lump Sum Needed': pv}
return results
def calculate_total_cost(age_distribution, lump_sums):
"""Calculate the total cost of providing lump sums to the population."""
total_cost = sum(age_distribution[age] * lump_sums.get(age, 0) for age in age_distribution)
return total_cost
# Parameters
start_age = 25
end_age = 100
initial_monthly_payment = 500
annual_rate = 0.03
inflation_rate = 0.02 # Example annual inflation rate
# Age distribution (number of U.S. citizens at each age)
age_distribution = {}
with open('demographic.json', 'r') as file:
json_string = file.read()
demographics = json.loads(json_string)
# Function to process age groups
def process_age_groups(age_groups):
for age in age_groups:
ageKey = int(age['age'])
if 18 <= ageKey <= 18: # Filter ages within the range 18 to 19
if ageKey not in age_distribution:
age_distribution[ageKey] = 0
age_distribution[ageKey] += age['actual_value']
# Process male demographics
process_age_groups(demographics['male']['values'][0]['age_groups'])
# Process female demographics
process_age_groups(demographics['female']['values'][0]['age_groups'])
# Calculate future values and lump sums
results = calculate_fv_and_pv_for_ages(start_age, end_age, initial_monthly_payment, annual_rate, inflation_rate)
# Extract lump sums from results
lump_sums = {age: results[age]['Lump Sum Needed'] for age in results}
# Calculate the total cost
total_cost = calculate_total_cost(age_distribution, lump_sums)
# Convert to dollars for readability
total_cost_formatted = '${:,.2f}'.format(total_cost)
print(f"Total Cost to Provide Age-Appropriate Lump Sums: {total_cost_formatted}")