-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_simulator.py
393 lines (315 loc) · 13.4 KB
/
test_simulator.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# test_simulation.py
# pip install pytest
# pytest -q test_simulation.py
import pytest
import numpy as np
import pandas as pd
from SWRsimulation import SWRsimulationCE
# mixed return and weights
RETURN_URL = 'http://www.stern.nyu.edu/~adamodar/pc/datasets/histretSP.xls'
RETURN_FILE = 'histretSP'
def download_returns():
data_sheet = "Returns by year"
# these may change as rows as Damodaran website is updated
skiprows = range(17)
skipfooter = 10
download_df = pd.read_excel(RETURN_URL,
sheet_name=data_sheet,
skiprows=skiprows,
skipfooter=skipfooter)
download_df = download_df.set_index('Year')
download_df.to_pickle('%s.pickle' % RETURN_FILE)
return download_df
def load_returns():
return pd.read_pickle('%s.pickle' % RETURN_FILE)
def trial_generator(df, start_year, n_years):
"""given a dataframe of returns, starting year, number of years, generate schedule of returns"""
for t in df.loc[start_year:start_year+n_years-1].itertuples():
yield tuple(t)
def test_zero():
"""no returns, no spending, just check shape"""
download_df = load_returns()
return_df = download_df.iloc[:, [0, 2]].copy()
return_df.columns = ['stocks', 'tbonds']
return_df['stocks'] = 0
return_df['tbonds'] = 0
trials = [trial_generator(return_df, 1928, 30)].copy()
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'n_ret_years': 30,
'n_assets': 2,
'trials': trials},
'allocation': {},
'withdrawal': {'fixed_pct': 0.0,
'variable_pct': 0.0,
'floor_pct': 0.0,
},
'evaluation': {},
})
z = s.simulate_trial(trial_generator(return_df, 1928, 30))
assert len(z) == 30
assert (z.index[0]) == 1928, "start year == 1928"
assert (z.index[-1]) == 1957, "end year == 1957"
def test_fixed1():
"""zero returns, fixed spending, check starting, ending vals"""
# zero returns, spend 2% per year, check ending value declines to 0.4
RETURN = 0.0
FIXED = 2.0
VARIABLE = 0.0
FLOOR = 0.0
NYEARS = 30
download_df = load_returns()
return_df = download_df.iloc[:, [0, 2]].copy()
return_df.columns = ['stocks', 'tbonds']
return_df['stocks'] = RETURN
return_df['tbonds'] = RETURN
trials = [trial_generator(return_df, 1928, NYEARS)]
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'n_ret_years': NYEARS,
'n_assets': 2,
'trials': trials},
'allocation': {},
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR},
'evaluation': {},
})
z = s.simulate_trial(trial_generator(return_df, 1928, 30))
assert (z['start_port'].iloc[0]) == 100, "start port value == 100"
assert (z['end_port'].iloc[-1]) == 40, "ending port value == 40"
def test_variable1():
"""zero returns, variable spending, check starting, ending vals"""
RETURN = 0.0
FIXED = 0
VARIABLE = 2.0
FLOOR = 0.0
NYEARS = 30
download_df = load_returns()
return_df = download_df.iloc[:, [0, 2]].copy()
return_df.columns = ['stocks', 'tbonds']
return_df['stocks'] = RETURN
return_df['tbonds'] = RETURN
trials = [trial_generator(return_df, 1928, NYEARS)]
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'n_ret_years': NYEARS,
'n_assets': 2,
'trials': trials},
'allocation': {},
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR,
},
'evaluation': {},
})
print(s)
z = s.simulate_trial(trial_generator(return_df, 1928, 30))
assert (z['start_port'].iloc[0]) == 100, "start port value == 100"
assert z['end_port'].iloc[-1] == pytest.approx(100 * ((1 - VARIABLE / 100) ** NYEARS), 0.000001)
z
def test_fixed2():
"""fixed returns, fixed spending, check starting, ending vals"""
# 4% real return, spend fixed 4% of starting, assert ending value unchanged
RETURN = 0.04
FIXED = 4
VARIABLE = 0.0
FLOOR = 0.0
NYEARS = 30
download_df = load_returns()
return_df = download_df.iloc[:, [0, 2]].copy()
return_df.columns = ['stocks', 'tbonds']
return_df['stocks'] = RETURN
return_df['tbonds'] = RETURN
trials = [trial_generator(return_df, 1928, NYEARS)]
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'n_ret_years': 30,
'n_assets': 2,
'trials': trials},
'allocation': {},
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR,
},
'evaluation': {},
})
z = s.simulate_trial(trial_generator(return_df, 1928, 30))
assert (z['start_port'].iloc[0]) == 100, "start port value == 100"
assert (z['end_port'].iloc[-1]) == 100, "end port value correct"
def test_variable2():
"""fixed returns, fixed spending, check starting, ending vals"""
# return 0.02% variable spending 0.02/1.02, check final value unchanged
RETURN = 0.02
FIXED = 0.0
FLOOR = 0.0
VARIABLE = 0.02 / 1.02 * 100
NYEARS = 30
download_df = load_returns()
return_df = download_df.iloc[:, [0, 2]].copy()
return_df.columns = ['stocks', 'tbonds']
return_df['stocks'] = RETURN
return_df['tbonds'] = RETURN
trials = [trial_generator(return_df, 1928, NYEARS)]
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'n_ret_years': 30,
'n_assets': 2,
'trials': trials},
'allocation': {},
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR,
},
'evaluation': {},
})
z = s.simulate_trial(trial_generator(return_df, 1928, 30))
assert (z['start_port'].iloc[0]) == 100, "start port value == 100"
assert (z['end_port'].iloc[-1]) == 100, "end port value correct"
def test_bengen1():
"""matches Bengen values, modulo using real values throughout"""
# per appendix of Bengen paper https://www.retailinvestor.org/pdf/Bengen1.pdf
# nominal return 10% for stocks, 5% for bonds
# inflation 3%
# fixed spending of 4% of orig port
STOCK_RETURN = (1.1 / 1.03) - 1
BOND_RETURN = (1.05 / 1.03) - 1
VARIABLE = 0.0
FIXED = 4.0
FLOOR = 0.0
NYEARS = 30
download_df = load_returns()
return_df = download_df.iloc[:, [0, 2]].copy()
return_df.columns = ['stocks', 'tbonds']
return_df['stocks'] = STOCK_RETURN
return_df['tbonds'] = BOND_RETURN
trials = [trial_generator(return_df, 1928, NYEARS)]
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'n_ret_years': NYEARS,
'n_assets': 2,
'trials': trials},
'allocation': {},
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR,
},
'evaluation': {},
})
z = s.simulate_trial(trial_generator(return_df, 1928, 30))
# match figures in appendix
# example uses nominal vals with 3% inflation, we use real vals
assert z.iloc[0]['before_spend'] * 1.03 == pytest.approx(107.5, 0.000001)
assert z.iloc[0]['spend'] * 1.03 == 4.12, "spend does not match Bengen"
assert z.iloc[0]['end_port'] * 1.03 == pytest.approx(103.38, 0.000001), "ending port does not match Bengen"
def test_bengen2():
"""matches Bengen values, use historical return data"""
FIXED = 4.0
VARIABLE = 0.0
FLOOR = 4.0
NYEARS = 30
download_df = load_returns()
return_df = download_df.iloc[:, [0, 3, 12]]
return_df.columns=['stocks', 'bonds', 'cpi']
real_return_df = return_df.copy()
real_return_df['stocks'] = (1 + real_return_df['stocks']) / (1 + real_return_df['cpi']) - 1
real_return_df['bonds'] = (1 + real_return_df['bonds']) / (1 + real_return_df['cpi']) - 1
real_return_df.drop('cpi', axis=1, inplace=True)
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'returns_df': real_return_df,
'n_ret_years': NYEARS,
},
'allocation': {}, # no args, default equal weight
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR},
'evaluation': {'gamma': 0},
})
z = s.simulate()
assert z[0]['trial'].iloc[0]['spend'] == 4.0, "bad value: cohort 0 year 0 spend"
assert z[0]['trial'].iloc[0]['end_port'] == pytest.approx(120.955061, 0.000001), "bad value: cohort 0 year 0 end port"
assert z[0]['trial'].iloc[-1]['spend'] == 4.0, "bad value: cohort 0 final year spend"
assert z[0]['trial'].iloc[-1]['end_port'] == pytest.approx(189.255136, 0.000001), "bad value: cohort 0 final year end port"
def test_45():
"""4/5% using historical returns"""
FIXED = -1
VARIABLE = 5.0
FLOOR = 4.0
NYEARS = 30
download_df = load_returns()
return_df = download_df.iloc[:, [0, 3, 12]]
return_df.columns=['stocks', 'bonds', 'cpi']
real_return_df = return_df.copy()
real_return_df['stocks'] = (1 + real_return_df['stocks']) / (1 + real_return_df['cpi']) - 1
real_return_df['bonds'] = (1 + real_return_df['bonds']) / (1 + real_return_df['cpi']) - 1
real_return_df.drop('cpi', axis=1, inplace=True)
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'returns_df': real_return_df,
'n_ret_years': NYEARS,
},
'allocation': {}, # no args, default equal weight
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR},
'evaluation': {'gamma': 0},
})
z = s.simulate()
assert z[0]['trial'].iloc[0]['spend'] == pytest.approx(5.247753, 0.000001), "bad value: cohort 0 year 0 spend"
assert z[0]['trial'].iloc[0]['end_port'] == pytest.approx(119.707308, 0.000001), "bad value: cohort 0 year 0 end port"
assert z[0]['trial'].iloc[-1]['spend'] == pytest.approx(5.690874, 0.000001), "bad value: cohort 0 final year spend"
assert z[0]['trial'].iloc[-1]['end_port'] == pytest.approx(128.126609, 0.000001), "bad value: cohort 0 final year end port"
def test_high_gamma():
"""a gamma 16 rule using historical returns"""
FIXED = 3.5
VARIABLE = 1.1
FLOOR = 3.8
NYEARS = 30
STOCK_PCT = 0.73
BOND_PCT = 0.27
download_df = load_returns()
return_df = download_df.iloc[:, [0, 3, 12]]
return_df.columns=['stocks', 'bonds', 'cpi']
real_return_df = return_df.copy()
real_return_df['stocks'] = (1 + real_return_df['stocks']) / (1 + real_return_df['cpi']) - 1
real_return_df['bonds'] = (1 + real_return_df['bonds']) / (1 + real_return_df['cpi']) - 1
real_return_df.drop('cpi', axis=1, inplace=True)
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'returns_df': real_return_df,
'n_ret_years': NYEARS,
},
'allocation': {'asset_weights': np.array([STOCK_PCT, BOND_PCT])},
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR},
'evaluation': {'gamma': 0},
})
z = s.simulate()
assert z[0]['trial'].iloc[0]['spend'] == pytest.approx(4.978399, 0.000001), "bad value: cohort 0 year 0 spend"
assert z[0]['trial'].iloc[0]['end_port'] == pytest.approx(129.421552, 0.000001), "bad value: cohort 0 year 0 end port"
assert z[0]['trial'].iloc[-1]['spend'] == pytest.approx(5.068669, 0.000001), "bad value: cohort 0 final year spend"
assert z[0]['trial'].iloc[-1]['end_port'] == pytest.approx(137.537621, 0.000001), "bad value: cohort 0 final year end port"
def test_low_gamma():
"""a lower gamma rule using historical returns"""
FIXED = 0.7
VARIABLE = 5.8
FLOOR = 3.4
NYEARS = 30
STOCK_PCT = 0.89
BOND_PCT = 0.11
download_df = load_returns()
return_df = download_df.iloc[:, [0, 3, 12]]
return_df.columns=['stocks', 'bonds', 'cpi']
real_return_df = return_df.copy()
real_return_df['stocks'] = (1 + real_return_df['stocks']) / (1 + real_return_df['cpi']) - 1
real_return_df['bonds'] = (1 + real_return_df['bonds']) / (1 + real_return_df['cpi']) - 1
real_return_df.drop('cpi', axis=1, inplace=True)
s = SWRsimulationCE.SWRsimulationCE({
'simulation': {'returns_df': real_return_df,
'n_ret_years': NYEARS,
},
'allocation': {'asset_weights': np.array([STOCK_PCT, BOND_PCT])},
'withdrawal': {'fixed_pct': FIXED,
'variable_pct': VARIABLE,
'floor_pct': FLOOR},
'evaluation': {'gamma': 0},
})
z = s.simulate()
assert z[0]['trial'].iloc[0]['spend'] == pytest.approx(8.876278, 0.000001), "bad value: cohort 0 year 0 spend"
assert z[0]['trial'].iloc[0]['end_port'] == pytest.approx(132.094032, 0.000001), "bad value: cohort 0 year 0 end port"
assert z[0]['trial'].iloc[-1]['spend'] == pytest.approx(5.135414, 0.000001), "bad value: cohort 0 final year spend"
assert z[0]['trial'].iloc[-1]['end_port'] == pytest.approx(71.337240, 0.000001), "bad value: cohort 0 final year end port"
print("running standalone")