-
Notifications
You must be signed in to change notification settings - Fork 0
/
compoundInterest.py
233 lines (201 loc) · 9.13 KB
/
compoundInterest.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from webapp import application
import dash
import dash_bootstrap_components as dbc
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import numpy as np
import pandas as pd
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], server=application, routes_pathname_prefix='/dash/compoundCalc/')
app.layout = html.Div(style={
'fontFamily': 'Arial, sans-serif',
'margin': '50px',
'padding': '10px',
'border': '1px solid #ddd',
'borderRadius': '5px',
'width': '75%',
'marginLeft': '12.5%',
'marginRight': '12.5%'
}, children=[
html.H1("Compound Interest Investment Calculator", style={
'textAlign': 'center',
'marginBottom': '30px'
}),
dbc.Row([
# Column 1
dbc.Col([
html.Div([
html.Label("Investment Time (in years)"),
dcc.Slider(
id='investmentTime-slider',
min=1,
max=50,
value=20,
marks={i: '{}'.format(i) for i in range(0, 51, 5)},
step=1
)
], style={'marginBottom': '20px'}),
html.Div([
html.Label("Yield Rate (in annual %) "),
dcc.Input(id='yieldRate-input', value=15, type='number')
], style={'marginBottom': '20px'}),
html.Div([
html.Label("Initial Contribution "),
dcc.Input(id='initialContribution-input', value=0, type='number')
], style={'marginBottom': '20px'}),
], width=6, align="center"),
# Column 2
dbc.Col([
html.Div([
html.Label("Monthly Contributions "),
dcc.Input(id='monthlyContributions-input', value=500, type='number')
], style={'marginBottom': '20px'}),
html.Div([
html.Label("Yearly Productivity Gain (in %) "),
dcc.Input(id='yearlyGainOnContributions-input', value=3, type='number')
], style={'marginBottom': '20px'}),
html.Div([
html.Label("Expected Inflation (in %) "),
dcc.Input(id='expectedInflation-input', value=6, type='number')
], style={'marginBottom': '20px'}),
], width=6, align="center")
], style={
'marginBottom': '30px',
'border': '1px solid #ccc',
'padding': '20px',
'borderRadius': '5px'
}),
# Plot
html.Div(style={
'marginBottom': '30px',
'padding': '20px'
}, children=[
dcc.Graph(id='compound-plot')
]),
dbc.Row([
dbc.Col(html.H3(id='final-balance-display', children='', style={'textAlign': 'center'}), width=6),
dbc.Col(html.H3(id='comparison-display', children='', style={'textAlign': 'center'}), width=6)
], style={'textAlign': 'center'})
])
def compound_interest_over_time(initialContribution,
monthlyContributions,
yieldRate,
investmentTime,
yearlyGainOnContributions,
expectedInflation):
# Convert number from Human Readable to decimal
yieldRate /= 100
yearlyGainOnContributions /= 100
expectedInflation /= 100
# Convert annual rate to monthly
monthly_rate = (1 + yieldRate) ** (1/12) - 1
monthly_contrib_growth = (1 + yearlyGainOnContributions) ** (1/12) - 1
monthly_inflation = (1 + expectedInflation) ** (1/12) - 1
# ------ Initializing columns
current_balance = [initialContribution]
current_interest = [initialContribution * monthly_rate]
current_inflation = [initialContribution * monthly_inflation]
current_contribution = [monthlyContributions]
final_balance = [current_balance[0] + current_interest[0] - current_inflation[0] + current_contribution[0]]
investmentTime *= 12 # converting years to months
for _ in range(1, investmentTime):
# ------ Row by row calculation
local_balance = final_balance[-1]
local_interest = local_balance * monthly_rate
local_inflation = local_balance * monthly_inflation
local_contribution = monthlyContributions
local_result = local_balance + local_interest - local_inflation + local_contribution
# ------ Appending values to columns
current_balance.append(local_balance)
current_interest.append(local_interest)
current_inflation.append(local_inflation)
current_contribution.append(local_contribution)
final_balance.append(local_result)
# Alter the monthly contribution for the next month
# Increase by productivity gain
# Decrease by inflation
monthlyContributions *= (1 + monthly_contrib_growth - monthly_inflation)
df = pd.DataFrame({
'Initial Balance': current_balance,
'Interest': current_interest,
'Inflation': current_inflation,
'Monthly Investment': current_contribution,
'Final Balance': final_balance
})
df = df.round(2)
df['Months'] = np.arange(1, investmentTime + 1)
df['Current Year'] = df['Months'] // 12
return df
def generate_scatter_trace(df, name, hover_name):
return go.Scatter(
x=df['Months'],
y=df['Final Balance'],
mode='lines',
name=name,
text=df['Current Year'],
hovertemplate=f'Month: %{{x}}<br>{hover_name}: $%{{y:,.2f}}<br>Current Year: %{{text}}'
)
@app.callback(
[Output('compound-plot', 'figure'),
Output('final-balance-display', 'children'),
Output('final-balance-display', 'style'),
Output('comparison-display', 'children'),
Output('comparison-display', 'style')],
[
Input('investmentTime-slider', 'value'),
Input('yieldRate-input', 'value'),
Input('initialContribution-input', 'value'),
Input('monthlyContributions-input', 'value'),
Input('yearlyGainOnContributions-input', 'value'),
Input('expectedInflation-input', 'value')
]
)
def update_values(investmentTime, yieldRate, initialContribution, monthlyContributions, yearlyGainOnContributions, expectedInflation):
# Calculate dataframes
df = compound_interest_over_time(initialContribution,
monthlyContributions,
yieldRate,
investmentTime,
yearlyGainOnContributions,
expectedInflation)
df_Treasury = compound_interest_over_time(initialContribution,
monthlyContributions,
2,
investmentTime,
yearlyGainOnContributions,
expectedInflation)
df_stocks10 = compound_interest_over_time(initialContribution,
monthlyContributions,
12.39,
investmentTime,
yearlyGainOnContributions,
expectedInflation)
df_stocks20 = compound_interest_over_time(initialContribution,
monthlyContributions,
9.75,
investmentTime,
yearlyGainOnContributions,
expectedInflation)
# Create traces
traces = [
generate_scatter_trace(df, 'Your Investment', 'Final Balance'),
generate_scatter_trace(df_Treasury, 'Average Treasury 3-month yield (2%)', 'Final Balance'),
generate_scatter_trace(df_stocks10, 'Average Stock market yield, last 10 years (12.39%)', 'Final Balance'),
generate_scatter_trace(df_stocks20, 'Average Stock market yield, last 20 years (9.75%)', 'Final Balance')
]
layout = go.Layout(title="Compound Interest Over Time", xaxis=dict(title="Time in Months"), yaxis=dict(title="Amount"))
# Display values
final_balance = df['Final Balance'].iloc[-1]
stock_balance = df_stocks10['Final Balance'].iloc[-1]
difference = final_balance - stock_balance
final_balance_content = f"Your Final Balance: ${final_balance:,.2f}"
final_balance_style = {'fontSize': '24px', 'fontWeight': 'bold'}
comparison_content = f"Difference compared to average stock market (last 10 years): ${difference:,.2f}"
comparison_style = {
'fontSize': '24px',
'fontWeight': 'bold',
'color': 'green' if difference >= 0 else 'red'
}
return {'data': traces, 'layout': layout}, final_balance_content, final_balance_style, comparison_content, comparison_style
if __name__ == '__main__':
application.run_server(debug=False)