Skip to content
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

Merged
merged 23 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
98fd4a4
storing sow_var states and initial values in namespace
sbenthall Jul 21, 2020
fddb98d
Merge branch 'master' into sb-market
sbenthall Jul 21, 2020
8feb583
Merge branch 'master' into sb-market
sbenthall Jul 22, 2020
fabec0c
updates to KS model to fit market refactoring
sbenthall Jul 22, 2020
f8343cf
Merge branch 'sb-market' of github.com:sbenthall/HARK into sb-market
sbenthall Jul 22, 2020
9ee55c2
Merge branch 'master' into sb-market
sbenthall Jul 22, 2020
15682cd
update SmallOpenEconomy
sbenthall Jul 22, 2020
7444dc5
const_vars now in dictionary
sbenthall Jul 22, 2020
e6f1022
reap variables to dictionary
sbenthall Jul 23, 2020
0ef77f7
collect sow var dicts in with sow vars
sbenthall Jul 23, 2020
b7b91ab
adding ConsAggShockModel tests for Market history tracking
sbenthall Jul 23, 2020
7b78cd7
fixing trak_vars
sbenthall Jul 23, 2020
2220fa5
millRule returning a tuple of values same size/order as sow_vars
sbenthall Jul 23, 2020
206ca76
Merge branch 'master' into sb-market
sbenthall Jul 23, 2020
3b678d1
missed a sow_var in Small Open
sbenthall Jul 23, 2020
7694a56
Merge branch 'sb-market' of github.com:sbenthall/HARK into sb-market
sbenthall Jul 23, 2020
7e45b5b
Merge branch 'master' into sb-market
llorracc Jul 30, 2020
a6e2de9
replace OrderedDict with dict -- 3.7 dicts ordered by default
sbenthall Jul 30, 2020
3237b56
Merge branch 'sb-market' of github.com:sbenthall/HARK into sb-market
sbenthall Jul 30, 2020
9cc7428
Merge branch 'master' into sb-market
sbenthall Aug 4, 2020
85f204c
Merge branch 'master' into sb-market
sbenthall Aug 5, 2020
6c31ea8
Merge branch 'master' into sb-market
sbenthall Aug 13, 2020
f812249
Merge branch 'master' into sb-market
llorracc Aug 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions HARK/ConsumptionSaving/ConsAggShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -1678,12 +1680,12 @@ def update(self):
self.KtoLnow_init = self.kSS
Copy link
Contributor Author

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

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)

Expand Down Expand Up @@ -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
],
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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...

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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):
'''
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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):
Expand Down
52 changes: 32 additions & 20 deletions HARK/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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):
'''
Expand All @@ -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):
'''
Expand All @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This simplifies if millRule returns a dict-like: self.sow_state.update(product)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chose for millRule to return a tuple

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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):
'''
Expand Down Expand Up @@ -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()

Expand All @@ -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):
Expand Down