-
Notifications
You must be signed in to change notification settings - Fork 0
/
DualIncomeForecast.nb.py
299 lines (238 loc) · 11.4 KB
/
DualIncomeForecast.nb.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# %%
## Imports
import math
from IPython.core.display import display
import sim
import solve
import couple_rulesets
import present
import matplotlib.pyplot as plt
import plot_utils
import display_utils
from display_utils import set
from display_utils import with_colour
from display_utils import br
import IPython.display
##############################################################################
simulation = sim.Dual_Income_Simulation()
partner1 = simulation.partner1_parameters
partner2 = simulation.partner2_parameters
# Configuration
## Fill in details for the first person in the couple:
## Given name?
partner1.name = "Partner 1"
### When were you born?
partner1.year_of_birth = set.this.value
### When will simulated-you retire?
partner1.age_at_retirement = set.this.value
### Good news: the sooner you die, the less money you need to save!
partner1.age_at_death = set.this.value
### What's your current salary?
partner1.initial_salary = set.this.value
### What savings do you currently have?
partner1.initial_savings_rrsp = set.this.value
partner1.initial_savings_tfsa = set.this.value
### Where do you predict your salary will eventually peak at? (In the simulation, it will plateau at this value.)
partner1.salary_plateau = set.this.value
### By what rate do you predict your salary to increase per year, on average, up to its peak? (Eg, if you expect it to increase by 5% each year, put 'salary_compound_rate=0.05')
partner1.salary_compound_rate = set.this.value
## Fill in details for the second person in the couple:
## Given name?
partner2.name = "Partner 2"
### When were you born?
partner2.year_of_birth = set.this.value
### When will simulated-you retire?
partner2.age_at_retirement = set.this.value
### Good news: the sooner you die, the less money you need to save!
partner2.age_at_death = set.this.value
### What's your current salary?
partner2.initial_salary = set.this.value
### What savings do you currently have?
partner2.initial_savings_rrsp = set.this.value
partner2.initial_savings_tfsa = set.this.value
### Where do you predict your salary will eventually peak at? (In the simulation, it will plateau at this value.)
partner2.salary_plateau = set.this.value
### By what rate do you predict your salary to increase per year, on average, up to its peak? (Eg, if you expect it to increase by 5% each year, put 'salary_compound_rate=0.05')
partner2.salary_compound_rate = set.this.value
## Fill in additional details:
### In what year is the simulation starting? (Typically the current year)
simulation.initial_year = set.this.value
### How much money should be left over at the end? (What do you want your beneficiaries to inherit?)
simulation.final_savings = 0
### How much interest do your invested savings earn? This depends on your investments, which in turn depends on your risk tolerance, time horizon, etc. (If your interests have an annual return of, eg, 6%, put 'interest_rate = 0.06')
interest_rate = 0.08
### How should the simulation try to distribute your contributions to retirement saving? The higher the value of increase_savings_weight, the more of any increase in salary will be put towards increased saving; the lower the value, the more of any increase in salary will be put towards immediate increased spending instead. Either way, the simulation will find an overall spending vs. savings balance that will allow you to maintain a steady income in retirement; the effect of this parameter is to determine when the bulk of savings occur. A high increase_savings_weight will 'back-load' savings, giving a higher spending early, but with less increase in spending over time; whereas a low increase_savings_weight will 'front-load' savings, giving a lower spending earlier on, but a more significant increase over time.
increase_savings_weight = 0.5
### Should the simulation try to optimize the way your savings are made? If should_optimize is set to True, the simulation will try to find the balance of investment contributions over time, across different investment types and for both individuals, which will maximize lifetime spending. (Cha-ching!) This is good! But it also takes noticeably longer to run. (And sometimes it doesn't work.) We recommend you to set this to True when you've finalized the other parameters.
should_optimize = False
## Some final parameters to tweak, for the adventurous. If should_optimize = True, the simulation will search for optimum values for these parameters, using the provided values as initial guesses.
### The proportion of savings to be put in the TFSA. initial_tfsa is used in the first year, final_tfsa is used in the last year of working before retirement, and intermediate values scale linearly between the two.
initial_tfsa = 0.5
final_tfsa = 0.5
### The equalize_income_weighting parameter is used to determine how to distribute RRSP contributions between partners with unequal salaries. A high parameter value maximises the contribution of the higher-earning couple, to minimize marginal tax right now; a low parameter value spreads the contribution evenly, to minimize marginal tax later when withdrawing from the RRSP. Again, it scales linearly during the simulation from the initial value in the first year to the final value in the last year.
initial_equalize_income_weighting = 0.5
final_equalize_income_weighting = 0.5
### A correction factor used when determining how to take from RRSP vs TFSA during retirement.
rrsp_adjustment = 0.05
##############################################################################
# Simulation code, you shouldn't normally need to modify this.
optimize = solve.Optimizing_Solver(solve.binary_solver, should_invert=True)
optimize.is_optimization_disabled = not should_optimize
simulation.set_solver(optimize.solve)
ruleset = couple_rulesets.bad_seed(
partner1.salary_compound_rate,
partner1.salary_plateau,
partner2.salary_compound_rate,
partner2.salary_plateau,
simulation.initial_year,
increase_savings_weight,
initial_tfsa,
final_tfsa,
initial_equalize_income_weighting,
final_equalize_income_weighting,
partner1.year_of_retirement,
partner2.year_of_retirement,
simulation.final_year,
rrsp_adjustment,
interest_rate,
interest_rate,
optimize,
)
simulation.set_ruleset(ruleset)
simulation.run()
##############################################################################
# Text output
if not simulation.was_solution_found:
display(
with_colour(
"**No solution was found for given inputs! Showing closest outcome.**",
"red",
)
)
display(with_colour(f"Message: {simulation.run_message}", "red"))
else:
display(with_colour("Solution found.", "green"))
display(br())
presenter = present.Dual_Income_Simulation_Presenter(simulation)
def dround(raw: float):
zeroes = int(math.log10(raw)) - 1
zeroes = min(3, zeroes) # Don't round to more than 1000
return f"${int(round(raw, -zeroes)):,.0f}"
def perc(raw: float):
return f"{raw*100:.1f}%"
## Info table
info_table = display_utils.table("Simulation details", "")
info_table.set_alignments("--:", ":--")
info_table.append_row(f"{partner1.name} retires in:", partner1.year_of_retirement)
info_table.append_row(f"{partner2.name} retires in:", partner2.year_of_retirement)
info_table.append_row(
f"{partner1.name}'s salary:",
f"\${partner1.initial_salary:,.0f} to \${partner1.salary_plateau:,.0f}, increasing by {perc(partner1.salary_compound_rate)} each year",
)
info_table.append_row(
f"{partner2.name}'s salary:",
f"\${partner2.initial_salary:,.0f} to \${partner2.salary_plateau:,.0f}, increasing by {perc(partner2.salary_compound_rate)} each year",
)
info_table.append_row("Interest rate:", perc(interest_rate))
info_table.append_row("Increase savings weight", increase_savings_weight)
display(info_table.close())
display(br())
## Summary text
display(
with_colour(
f"{partner1.name} worked until {partner1.year_of_retirement}. {partner2.name} worked until {partner2.year_of_retirement}. In the first year ({presenter.years_series[1]}) they spent {dround(presenter.first_year_spending)}. In retirement they had an income of {dround(presenter.retirement_spending)} a year. Their lifetime average yearly spending was {dround(presenter.average_yearly_spending)} a year.",
"black",
)
)
display(br())
## Savings table
savings_table = display_utils.table(
"Year",
f"{partner1.name}'s salary",
f"{partner2.name}'s salary",
"Monthly spending",
"Monthly saving",
)
YEARS_TO_SHOW_SAVINGS_FOR = 5
for i in range(1, YEARS_TO_SHOW_SAVINGS_FOR + 1):
savings_table.append_row(
f"**{presenter.years_series[i]}**",
dround(presenter.partner1.salary_series[i]),
dround(presenter.partner2.salary_series[i]),
dround(presenter.spending_monthly_series[i]),
dround(presenter.combined_savings_monthly_series[i]),
)
display(savings_table.close())
display(br())
## Detailed savings table
savings_breakdown_table = display_utils.table(
"Year",
f"{partner1.name} TFSA",
f"{partner1.name} RRSP",
f"{partner2.name} TFSA",
f"{partner2.name} RRSP",
)
for i in range(1, YEARS_TO_SHOW_SAVINGS_FOR + 1):
savings_breakdown_table.append_row(
f"**{presenter.years_series[i]}**",
dround(presenter.partner1.tfsa_monthly_series[i]),
dround(presenter.partner1.rrsp_monthly_series[i]),
dround(presenter.partner2.tfsa_monthly_series[i]),
dround(presenter.partner2.rrsp_monthly_series[i]),
)
display(with_colour("Detailed savings breakdown, monthly contributions:", "black"))
display(savings_breakdown_table.close())
# %%
# General-interest graphs
base_fig_size = (10, 4)
def fig_size(stretch_y: float = 1):
return (base_fig_size[0], base_fig_size[1] * stretch_y)
fig1, ax1 = plt.subplots(figsize=fig_size())
ax1.plot(
presenter.partner1.career_year_series,
presenter.partner1.career_salary_series,
"-",
label=f"{partner1.name}",
)
ax1.plot(
presenter.partner2.career_year_series,
presenter.partner2.career_salary_series,
"-",
label=f"{partner2.name}",
)
ax1.set_title("Salary")
ax1.legend()
fig2, ax2 = plt.subplots(figsize=fig_size())
ax2.plot(presenter.years_series, presenter.spending_monthly_series, "-")
ax2.set_title("Monthly spending")
fig3, ax3 = plt.subplots(figsize=fig_size())
ax3.plot(presenter.career_years_series, presenter.career_combined_savings_monthly_series)
ax3.set_title("Monthly total saving")
# fig4, ax4 = plt.subplots(figsize=fig_size())
# ax4.plot(presenter.partner1.career_year_series,presenter.partner1.career_tfsa_monthly_series, '-', label=f"{partner1.name} TFSA")
# ax4.plot(presenter.partner1.career_year_series,presenter.partner1.career_rrsp_monthly_series, '-', label=f"{partner1.name} RRSP")
# ax4.plot(presenter.partner2.career_year_series,presenter.partner2.career_tfsa_monthly_series, '-', label=f"{partner2.name} TFSA")
# ax4.plot(presenter.partner2.career_year_series,presenter.partner2.career_rrsp_monthly_series, '-', label=f"{partner2.name} RRSP")
# ax4.set_title("Monthly saving breakdown")
# ax4.legend()
plt.show()
# %%
# Re-import modified dependencies
import importlib
import present
import sim
import couple_rulesets
import couple_spending_rules
import couple_savings_rules
import solve
import display_utils
importlib.reload(present)
importlib.reload(sim)
importlib.reload(couple_rulesets)
importlib.reload(couple_spending_rules)
importlib.reload(solve)
importlib.reload(display_utils)
importlib.reload(couple_savings_rules)
display("Reimported modules")
# %%
# %%