-
-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
light touch Market class refactoring #765
Changes from 10 commits
98fd4a4
fddb98d
8feb583
fabec0c
f8343cf
9ee55c2
15682cd
7444dc5
e6f1022
0ef77f7
b7b91ab
7b78cd7
2220fa5
206ca76
3b678d1
7694a56
7e45b5b
a6e2de9
3237b56
9cc7428
85f204c
6c31ea8
f812249
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -616,7 +616,7 @@ def getEconomyData(self, Economy): | |
''' | ||
self.T_sim = Economy.act_T # Need to be able to track as many periods as economy runs | ||
self.kInit = Economy.KSS # Initialize simulation assets to steady state | ||
self.MrkvInit = Economy.MrkvNow_init # Starting Markov state for the macroeconomy | ||
self.MrkvInit = Economy.sow_init['MrkvNow'] # Starting Markov state for the macroeconomy | ||
self.Mgrid = Economy.MSS*self.MgridBase # Aggregate market resources grid adjusted around SS capital ratio | ||
self.AFunc = Economy.AFunc # Next period's aggregate savings function | ||
self.DeprFac = Economy.DeprFac # Rate of capital depreciation | ||
|
@@ -1377,11 +1377,12 @@ def __init__(self, | |
''' | ||
agents = agents if agents is not None else list() | ||
params = init_cobb_douglas.copy() | ||
params['sow_vars'] = ['MaggNow', 'AaggNow', 'RfreeNow', | ||
'wRteNow', 'PermShkAggNow', | ||
'TranShkAggNow', 'KtoLnow'] | ||
params.update(kwds) | ||
|
||
Market.__init__(self, agents=agents, | ||
sow_vars=['MaggNow', 'AaggNow', 'RfreeNow', | ||
'wRteNow', 'PermShkAggNow', 'TranShkAggNow', 'KtoLnow'], | ||
reap_vars=['aLvlNow', 'pLvlNow'], | ||
track_vars=['MaggNow', 'AaggNow'], | ||
dyn_vars=['AFunc'], | ||
|
@@ -1442,13 +1443,14 @@ def update(self): | |
self.convertKtoY = lambda KtoY: KtoY**(1.0/(1.0 - self.CapShare)) # converts K/Y to K/L | ||
self.Rfunc = lambda k: (1.0 + self.CapShare*k**(self.CapShare-1.0) - self.DeprFac) | ||
self.wFunc = lambda k: ((1.0-self.CapShare)*k**(self.CapShare)) | ||
self.KtoLnow_init = self.kSS | ||
self.MaggNow_init = self.kSS | ||
self.AaggNow_init = self.kSS | ||
self.RfreeNow_init = self.Rfunc(self.kSS) | ||
self.wRteNow_init = self.wFunc(self.kSS) | ||
self.PermShkAggNow_init = 1.0 | ||
self.TranShkAggNow_init = 1.0 | ||
|
||
self.sow_init['KtoLnow'] = self.kSS | ||
self.sow_init['MaggNow'] = self.kSS | ||
self.sow_init['AaggNow'] = self.kSS | ||
self.sow_init['RfreeNow'] = self.Rfunc(self.kSS) | ||
self.sow_init['wRteNow'] = self.wFunc(self.kSS) | ||
self.sow_init['PermShkAggNow'] = 1.0 | ||
self.sow_init['TranShkAggNow'] = 1.0 | ||
self.makeAggShkDstn() | ||
self.AFunc = AggregateSavingRule(self.intercept_prev, self.slope_prev) | ||
|
||
|
@@ -1678,12 +1680,12 @@ def update(self): | |
self.KtoLnow_init = self.kSS | ||
self.Rfunc = ConstantFunction(self.Rfree) | ||
self.wFunc = ConstantFunction(self.wRte) | ||
self.RfreeNow_init = self.Rfunc(self.kSS) | ||
self.wRteNow_init = self.wFunc(self.kSS) | ||
self.MaggNow_init = self.kSS | ||
self.AaggNow_init = self.kSS | ||
self.PermShkAggNow_init = 1.0 | ||
self.TranShkAggNow_init = 1.0 | ||
self.sow_init['RfreeNow'] = self.Rfunc(self.kSS) | ||
self.sow_init['wRteNow'] = self.wFunc(self.kSS) | ||
self.sow_init['MaggNow'] = self.kSS | ||
self.sow_init['AaggNow'] = self.kSS | ||
self.sow_init['PermShkAggNow'] = 1.0 | ||
self.sow_init['TranShkAggNow'] = 1.0 | ||
self.makeAggShkDstn() | ||
self.AFunc = ConstantFunction(1.0) | ||
|
||
|
@@ -1825,6 +1827,15 @@ def __init__(self, | |
agents=None, | ||
tolerance=0.0001, | ||
act_T=1200, | ||
sow_vars=['MaggNow', | ||
'AaggNow', | ||
'RfreeNow', | ||
'wRteNow', | ||
'PermShkAggNow', | ||
'TranShkAggNow', | ||
'KtoLnow', | ||
'MrkvNow' # This one is new | ||
], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When all the sow_vars are "Now", "Now" can be left out of the name... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I don't dare. |
||
**kwds): | ||
''' | ||
Make a new instance of CobbDouglasMarkovEconomy by filling in attributes | ||
|
@@ -1849,8 +1860,14 @@ def __init__(self, | |
params = init_mrkv_cobb_douglas.copy() | ||
params.update(kwds) | ||
|
||
CobbDouglasEconomy.__init__(self, agents=agents, tolerance=tolerance, act_T=act_T, **params) | ||
self.sow_vars.append('MrkvNow') | ||
CobbDouglasEconomy.__init__(self, | ||
agents=agents, | ||
tolerance=tolerance, | ||
act_T=act_T, | ||
sow_vars = sow_vars, | ||
**params) | ||
|
||
self.sow_init['MrkvNow'] = params['MrkvNow_init'] | ||
|
||
def update(self): | ||
''' | ||
|
@@ -2011,7 +2028,7 @@ def makeMrkvHist(self): | |
cutoffs = np.cumsum(self.MrkvArray, axis=1) | ||
loops = 0 | ||
go = True | ||
MrkvNow = self.MrkvNow_init | ||
MrkvNow = self.sow_init['MrkvNow'] | ||
t = 0 | ||
StateCount = self.MrkvArray.shape[0] | ||
|
||
|
@@ -2247,14 +2264,14 @@ def update(self): | |
self.convertKtoY = lambda KtoY: KtoY**(1.0/(1.0 - self.CapShare)) # converts K/Y to K/L | ||
self.rFunc = lambda k: self.CapShare*k**(self.CapShare-1.0) | ||
self.Wfunc = lambda k: ((1.0-self.CapShare)*k**(self.CapShare)) | ||
self.KtoLnow_init = self.KtoLSS | ||
self.Mnow_init = self.MSS | ||
self.Aprev_init = self.KSS | ||
self.Rnow_init = self.RSS | ||
self.Wnow_init = self.WSS | ||
self.sow_init['KtoLnow'] = self.KtoLSS | ||
self.sow_init['Mnow'] = self.MSS | ||
self.sow_init['Aprev'] = self.KSS | ||
self.sow_init['Rnow'] = self.RSS | ||
self.sow_init['Wnow'] = self.WSS | ||
self.PermShkAggNow_init = 1.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, not missing because these are not sow_vars in KS |
||
self.TranShkAggNow_init = 1.0 | ||
self.Mrkv_init = 0 | ||
self.sow_init['MrkvNow'] = 0 | ||
self.makeMrkvArray() | ||
|
||
def reset(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
from builtins import str | ||
from builtins import range | ||
from builtins import object | ||
from collections import OrderedDict | ||
import sys | ||
import os | ||
from distutils.dir_util import copy_tree | ||
|
@@ -926,11 +927,23 @@ def __init__(self, agents=None, sow_vars=None, reap_vars=None, const_vars=None, | |
None | ||
''' | ||
self.agents = agents if agents is not None else list() # NOQA | ||
self.reap_vars = reap_vars if reap_vars is not None else list() # NOQA | ||
|
||
reap_vars = reap_vars if reap_vars is not None else list() # NOQA | ||
self.reap_vars = OrderedDict([(var, []) for var in reap_vars]) | ||
|
||
self.sow_vars = sow_vars if sow_vars is not None else list() # NOQA | ||
self.const_vars = const_vars if const_vars is not None else list() # NOQA | ||
# dictionaries for tracking initial and current values | ||
# of the sow variables. | ||
self.sow_init = OrderedDict([(var, None) for var in self.sow_vars]) | ||
self.sow_state = OrderedDict([(var, None) for var in self.sow_vars]) | ||
|
||
const_vars = const_vars if const_vars is not None else list() # NOQA | ||
self.const_vars = OrderedDict([(var, None) for var in const_vars]) | ||
|
||
## TODO: track_vars handling is not right | ||
self.track_vars = track_vars if track_vars is not None else list() # NOQA | ||
self.dyn_vars = dyn_vars if dyn_vars is not None else list() # NOQA | ||
|
||
if millRule is not None: # To prevent overwriting of method-based millRules | ||
self.millRule = millRule | ||
if calcDynamics is not None: # Ditto for calcDynamics | ||
|
@@ -946,6 +959,7 @@ def __init__(self, agents=None, sow_vars=None, reap_vars=None, const_vars=None, | |
# "solveAgents" one time. If set to false, the error will never | ||
# print. See "solveAgents" for why this prints once or never. | ||
|
||
|
||
def solveAgents(self): | ||
''' | ||
Solves the microeconomic problem for all AgentTypes in this market. | ||
|
@@ -1024,7 +1038,7 @@ def reap(self): | |
harvest = [] | ||
for this_type in self.agents: | ||
harvest.append(getattr(this_type, var_name)) | ||
setattr(self, var_name, harvest) | ||
self.reap_vars[var_name] = harvest | ||
|
||
def sow(self): | ||
''' | ||
|
@@ -1039,10 +1053,11 @@ def sow(self): | |
------- | ||
none | ||
''' | ||
for var_name in self.sow_vars: | ||
this_seed = getattr(self, var_name) | ||
for sow_var in self.sow_state: | ||
for this_type in self.agents: | ||
setattr(this_type, var_name, this_seed) | ||
setattr(this_type, | ||
sow_var, | ||
self.sow_state[sow_var]) | ||
|
||
def mill(self): | ||
''' | ||
|
@@ -1058,20 +1073,15 @@ def mill(self): | |
none | ||
''' | ||
# Make a dictionary of inputs for the millRule | ||
reap_vars_string = '' | ||
for name in self.reap_vars: | ||
reap_vars_string += ' \'' + name + '\' : self.' + name + ',' | ||
const_vars_string = '' | ||
for name in self.const_vars: | ||
const_vars_string += ' \'' + name + '\' : self.' + name + ',' | ||
mill_dict = eval('{' + reap_vars_string + const_vars_string + '}') | ||
mill_dict = copy(self.reap_vars) | ||
mill_dict.update(self.const_vars) | ||
|
||
# Run the millRule and store its output in self | ||
product = self.millRule(**mill_dict) | ||
for j in range(len(self.sow_vars)): | ||
this_var = self.sow_vars[j] | ||
this_product = getattr(product, this_var) | ||
setattr(self, this_var, this_product) | ||
|
||
for sow_var in self.sow_state: | ||
this_product = getattr(product, sow_var) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This simplifies if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chose for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (closer to Dolo, supposed to be good for numba) |
||
self.sow_state[sow_var] = this_product | ||
|
||
def cultivate(self): | ||
''' | ||
|
@@ -1107,8 +1117,7 @@ def reset(self): | |
for var_name in self.track_vars: # Reset the history of tracked variables | ||
self.history[var_name] = [] | ||
for var_name in self.sow_vars: # Set the sow variables to their initial levels | ||
initial_val = getattr(self, var_name + '_init') | ||
setattr(self, var_name, initial_val) | ||
self.sow_state[var_name] = self.sow_init[var_name] | ||
for this_type in self.agents: # Reset each AgentType in the market | ||
this_type.reset() | ||
|
||
|
@@ -1126,7 +1135,10 @@ def store(self): | |
none | ||
''' | ||
for var_name in self.track_vars: | ||
sbenthall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value_now = getattr(self, var_name) | ||
try: | ||
value_now = getattr(self, var_name) | ||
except: | ||
value_now = self.sow_state[var_name] | ||
self.history[var_name].append(value_now) | ||
|
||
def makeHistory(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did I miss one here? i think so