-
Notifications
You must be signed in to change notification settings - Fork 7
/
SWRsimulation.py
136 lines (109 loc) · 4.05 KB
/
SWRsimulation.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
from dataclasses import dataclass, field
import pprint
from typing import List
import numpy as np
import pandas as pd
# from plotly import graph_objects as go
# from plotly.subplots import make_subplots
# import plotly.express as px
# TODO: by default don't keep all trials, only if specified explicitly
START_PORTVAL = 100.0
@dataclass
class Trialdata:
"""
Class for keeping track of a latest_trial.
"""
year: int = 0
spend: float = 0
iteration: int = 0
portval: float = START_PORTVAL
years: List[int] = field(default_factory=list)
start_ports: List[float] = field(default_factory=list)
asset_allocations: List[np.array] = field(default_factory=list)
port_returns: List[float] = field(default_factory=list)
before_spends: List[float] = field(default_factory=list)
end_ports: List[float] = field(default_factory=list)
spends: List[float] = field(default_factory=list)
trial_df: pd.DataFrame = None
class SWRsimulation:
"""abstract base class template for safe withdrawal simulations
"""
def __init__(self, config):
"""initialize class from a config dict
Args:
config (dict): dict containing at least required keys:
{'simulation': {}
'allocation': {}
'withdrawal': {}
'visualization': {}
}
"""
# promote everything in config to instance variables for more readable code
self.simulation = config.get('simulation')
self.init_simulation()
self.allocation = config.get('allocation')
self.init_allocation()
self.withdrawal = config.get('withdrawal')
self.init_withdrawal()
self.evaluation = config.get('evaluation')
self.visualization = config.get('visualization')
self.latest_trial = Trialdata()
self.latest_simulation = [] # list of all trial data in latest simulation
def init_simulation(self):
"""initialize trial generator eg. historical, monte carlo. prep for next()
"""
pass
def simulate_trial(self, trial_rows):
"""simulate a single historical cohort or montecarlo generated cohort
Args:
trial_rows (list or other iterator): asset returns for this cohort trial
"""
pass
def simulate(self, do_eval=True, return_both=True):
"""simulate all available cohorts
Args:
do_eval (bool, optional): whether to run eval on each cohort. (Default True)
return_both (bool, optional): whether to save both eval and full cohort
dataframe in self.latest_simulation. (Default True)
"""
pass
def init_allocation(self):
"""set up equal weight, allocation parameters etc. based on simulation config
"""
pass
def get_allocations(self):
"""return array of allocations for current simulation iteration based on config, current state
"""
pass
def init_withdrawal(self):
"""initialize withdrawal parameters based on simulation config
"""
pass
def get_withdrawal(self):
"""return withdrawal amount for current simulation iteration based on config, current state
"""
pass
def eval_trial(self):
"""return dict of metrics for a current trial
single historical cohort or montecarlo generated cohort
eg years to exhaustion, CE adjusted spending
"""
pass
def visualize(self):
"""run the analytics and data viz for a completed simulation
"""
pass
def __repr__(self):
"""Generate string representation of simulation
Returns:
[string]: string representation
"""
retstr = "Simulation:\n"
retstr += pprint.pformat(self.simulation)
retstr += "\n\nAllocation:\n"
retstr += pprint.pformat(self.allocation)
retstr += "\n\nWithdrawal:\n"
retstr += pprint.pformat(self.withdrawal)
return retstr
if __name__ == '__main__':
print('Executing as standalone script')