-
Notifications
You must be signed in to change notification settings - Fork 0
/
finance.py
152 lines (117 loc) · 3.5 KB
/
finance.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.15.2
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %%
import math
from common import configvar
import pandas as pd
import io
import functools
import util
# %%
@configvar(type=float)
def annual_income():
"""
Total annual income for the group of people in the analysis.
"""
# %%
# TODO this is really naive; can get this from 30-year bond yields instead
DISCOUNT_RATE_PCT = 4.0
# %%
def money_to_utility(m):
return math.log(m)
def utility_to_money(u):
return math.exp(u)
# TODO should this hardcoded 25 be 100 / DISCOUNT_RATE_PCT ?
def annual_dollars_to_capital(d):
return d * 25
def capital_to_annual_dollars(c):
return c / 25
# %%
def _gdp_growth_df_raw():
return pd.read_csv(
io.StringIO(
util.web_get('https://a.usafacts.org/api/v4/Metrics/csv/116290').text
)
)
@functools.cache
def _gdp_growth_df():
df = _gdp_growth_df_raw()[:1].set_index('Years').T
df.index = df.index.astype('int')
df['Annual percent change in real GDP (%)'] = df[
'Annual percent change in real GDP (%)'
].astype('float')
return df
@functools.cache
def annual_us_real_gdp_growth():
"""
Compute the average annual US real GDP growth, where e.g. 0.03 means 3% annual growth.
"""
return (
_gdp_growth_df()['Annual percent change in real GDP (%)'] + 1
).product() ** (1 / len(_gdp_growth_df())) - 1
# %%
def _inflation_df_raw():
return pd.read_csv(
io.StringIO(
util.web_get('https://a.usafacts.org/api/v4/Metrics/csv/27246').text
)
)
@functools.cache
def _inflation_df():
df = _inflation_df_raw()[:1].set_index('Years').T
df.index = df.index.astype('int')
df['Average inflation rate (%)'] = df['Average inflation rate (%)'].astype('float')
return df
@functools.cache
def annual_us_inflation_rate():
"""
Compute the average annual US inflation rate, where e.g. 0.02 means 2% inflation rate.
"""
return (_inflation_df()['Average inflation rate (%)'] + 1).product() ** (
1 / len(_gdp_growth_df())
) - 1
# %%
def _population_df_raw():
return pd.read_csv(
io.StringIO(
util.web_get('https://a.usafacts.org/api/v4/Metrics/csv/12818').text
)
)
@functools.cache
def _population_df():
df = _population_df_raw()[:1].set_index('Years').T
df.index = df.index.astype('int')
df['Resident Population (People)'] = df['Resident Population (People)'].astype(
'int'
)
return df
@functools.cache
def annual_us_pop_growth_rate():
"""
Compute the average annual US population growth rate, where e.g. 0.01 means 1% growth rate.
"""
start_year = _gdp_growth_df().iloc[0].name
end_year = _gdp_growth_df().iloc[-1].name
return (
_population_df().loc[end_year, 'Resident Population (People)']
/ _population_df().loc[start_year, 'Resident Population (People)']
) ** (1 / (end_year - start_year)) - 1
@functools.cache
def annual_us_real_gdp_per_capita_growth():
return (1 + annual_us_real_gdp_growth()) / (1 + annual_us_pop_growth_rate()) - 1
# %% tags=["active-ipynb"]
# print(f'{annual_us_real_gdp_growth()=:.4f}')
# print(f'{annual_us_inflation_rate()=:.4f}')
# print(f'{annual_us_pop_growth_rate()=:.4f}')
# print(f'{annual_us_real_gdp_per_capita_growth()=:.4f}')