forked from druce/safewithdrawal_tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_safewithdrawal_02.py
executable file
·115 lines (85 loc) · 8.27 KB
/
run_safewithdrawal_02.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
#!/home/ubuntu/anaconda2/bin/python
# MIT License
# Copyright (c) 2016 Druce Vertes [email protected]
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import print_function
import argparse
import pickle
from time import strftime
import sys
import os
import numpy as np
import pandas as pd
gamma = 2.0
fileprefix = "best%02.0f" % gamma
bestfile = "%s.pickle" % (fileprefix)
max_unimproved_steps = 1000
# startval = 100
# years_retired = 30
# const_spend_pct = .02
# const_spend = startval * const_spend_pct
# var_spend_pcts = pd.Series(np.ones(years_retired) * 0.02)
# var_spend_pcts[-1]=1.0
# stock_allocations = pd.Series(np.ones(years_retired) * 0.65)
startval = 100
years_retired = 30
const_spend_pct = .0225
# var spending a function of years left
var_spend_pcts = pd.Series([ 0.5/(30-ix) for ix in range(30)])
var_spend_pcts[29] = 1.0
# stocks starting at 82%, decreasing 0.5% per year
stock_allocations = pd.Series([0.82 - 0.005* ix for ix in range(30)])
#Objective: 7.225471
const_spend_pct = 0.006323027839
var_spend_pcts = pd.Series([0.041955996437244091, 0.04330334517797383, 0.044636384124929727, 0.045979281351362096, 0.047276513506711228, 0.048608679185249012, 0.050238851777836178, 0.052054054501765791, 0.054126246577344307, 0.056510375743627911, 0.058984995472597057, 0.061794466477926767, 0.064834367236227569, 0.068079377746198086, 0.071466400325203447, 0.07529495935465641, 0.080070069323110726, 0.08568926569872802, 0.092339110179697509, 0.09977282056678359, 0.10847027994271509, 0.11902334116825858, 0.1325774751092168, 0.14951428294648331, 0.17160594991481343, 0.20230812657887748, 0.24797672544596061, 0.3255755716983077, 0.48380769234788362, 1.0])
stock_allocations = pd.Series([0.99159144675378219, 0.98893401585624829, 0.98785147667162365, 0.98699498308354117, 0.9869801567778711, 0.98455311274433566, 0.97611766325577298, 0.97088866198228574, 0.96127223864070932, 0.96001058197868938, 0.95908638294452175, 0.9558672183378959, 0.95536557143474909, 0.95403140734494851, 0.94577623884269824, 0.9417830723560785, 0.93336441130384518, 0.92895848371087542, 0.92786582414437779, 0.92108982617642354, 0.91847934982408042, 0.91679896277447881, 0.91167565250471583, 0.91122062573672691, 0.90454820360770094, 0.89623747306826418, 0.88823358312565648, 0.87888043125357096, 0.86550562110733698, 0.84589008353957529])
#Objective: 7.229701
const_spend_pct = 0.006534786564
var_spend_pcts = pd.Series([0.04174705097516157, 0.043083534077090695, 0.04441023334668151, 0.045749153557012291, 0.047041933520236404, 0.048366589228790377, 0.049989570191720713, 0.051793827399547923, 0.05385080105874418, 0.056217879445626062, 0.058672782673295967, 0.061457519080897791, 0.064467583015610494, 0.067686586956812303, 0.071049490707252361, 0.074843876527600203, 0.0795731571597936, 0.085124071123558465, 0.09169390434987143, 0.099050623616367461, 0.10769089923147132, 0.11819658801607207, 0.13173123723070346, 0.1487196630406151, 0.17090216696641344, 0.20173247950300049, 0.24751239964531688, 0.32516433490791191, 0.48338203509127564, 1.0])
stock_allocations = pd.Series([0.99139250740346296, 0.98894734266247208, 0.98849970347551708, 0.98839878932455705, 0.98822784546993403, 0.98736551449223808, 0.97870885525901274, 0.97309693413974563, 0.96235686610308335, 0.96117270108273989, 0.96067685532739122, 0.95744403399463607, 0.95697057491053183, 0.95623422267804448, 0.94805826252062841, 0.9437788755550599, 0.9346952574504177, 0.92994429420084734, 0.92886689565139524, 0.92232531804520468, 0.91962588766388409, 0.91785678562806516, 0.91238368695340466, 0.91191802059776195, 0.90507699621521753, 0.89663572663249669, 0.88858497789918511, 0.8790963088668946, 0.86560076935488295, 0.84594711772542264])
#Objective: 7.230124
const_spend_pct = 0.006552370364
var_spend_pcts = pd.Series([0.041730014207925568, 0.043065651075911322, 0.044391848948679202, 0.045730440177750603, 0.047022816965112998, 0.048346854518581055, 0.049969252055051136, 0.051772622323800851, 0.053828385020876654, 0.056194109823193096, 0.058647469272752537, 0.061430273332266326, 0.064438039403519207, 0.067655066151180415, 0.071016143107789587, 0.074807893911769438, 0.079533558511105107, 0.085078881506057774, 0.091641883431848273, 0.09899157045610095, 0.10762592554110764, 0.11812604039793548, 0.13165713119186895, 0.14864813209593752, 0.17083703874322942, 0.20167773095583116, 0.24746721196031321, 0.32512381064907941, 0.48333998072296896, 1.0])
stock_allocations = pd.Series([0.99137369522670638, 0.98885341565377172, 0.98853266160074194, 0.98853265562562076, 0.98847113777216078, 0.98763386395728725, 0.97895620169145092, 0.97330777065660135, 0.96246008385366832, 0.96125482183682598, 0.96085761420863125, 0.95759469314358425, 0.95712388008397342, 0.95644478062223526, 0.94827644839850334, 0.94396968736714648, 0.93482252170417734, 0.9300385381586419, 0.92896263937769419, 0.92244346173934189, 0.91973551340762705, 0.91795796843178379, 0.91245139825526989, 0.91198474415684894, 0.90512754469761536, 0.89667383350312313, 0.88861861158190592, 0.87911697564751901, 0.8656098900505439, 0.84595260388317473])
#Objective: 7.232013
const_spend_pct = 0.006633196051
var_spend_pcts = pd.Series([0.041651782115649724, 0.04298351383672569, 0.044307405688720158, 0.045644563126512307, 0.046935371105069679, 0.048256594099694364, 0.049876320240762828, 0.051675640492043345, 0.053725939553432397, 0.056085607010629972, 0.058531950342040277, 0.061306089393194735, 0.064303573280149864, 0.067511781568926968, 0.070864795122752594, 0.074644853152054047, 0.079354357195997025, 0.084874368957208082, 0.091405892559806134, 0.098722229027103148, 0.10732707056538419, 0.11779806370242611, 0.13130833549322588, 0.1483069190268195, 0.17052212615693424, 0.20140944216392345, 0.24724327540518604, 0.32492166343973394, 0.48312975250382012, 1.0])
stock_allocations = pd.Series([0.99128140981577162, 0.9890186164406034, 0.9889931746406847, 0.98899317444042156, 0.98897294365892874, 0.98892702392459531, 0.98017623383520169, 0.97434793556657606, 0.96296843154230494, 0.96175969914252735, 0.96164944382766815, 0.95833816100296942, 0.95788049761186367, 0.95748409443075122, 0.94935349502063981, 0.94491171918437289, 0.93545099514061691, 0.9305036848920506, 0.92943528021152122, 0.92302678056280996, 0.92027686390605101, 0.91845762356824345, 0.91278569545635735, 0.91231424523945759, 0.90537710239072444, 0.89686206541538249, 0.88878472270546383, 0.87921904341544299, 0.86565496875257597, 0.84597976461898239])
bond_allocations = 1 - stock_allocations
# save starting scenario
pickle_list = [const_spend_pct, var_spend_pcts, stock_allocations, bond_allocations]
pickle.dump( pickle_list, open( bestfile, "wb" ) )
# start with a learning rate that learns quickly, gradually reduce it
# run once with 50 or 100 steps to see which learning rates are effective
# then plug in that solution and run each til no improvement for a large number of steps
for learning_rate in [
#0.00001, # too coarse, may be NaN
0.00003, # too coarse, may be NaN
0.000001, # coarse
0.000003, # coarse
0.0000001, # workhorse
0.00000003,
0.00000001, # diminishing returns
0.000000003,
#0.000000001, #superfine
#0.0000000003,
#0.0000000001,
#0.00000000001,
]:
cmdstr = './safewithdrawal.py %.12f %d %f %s' % (learning_rate, max_unimproved_steps, gamma, fileprefix)
print(cmdstr)
os.system(cmdstr)