Skip to content

Commit

Permalink
Loading default parameters into class during initialization (#466)
Browse files Browse the repository at this point in the history
* loading init_perfect_foresight parameters by default. See #446

* loading init_idiosyncratic_shock into IndShochConsumerType on initialization #446

* using default ConsIndShock parameters when configuring RAexample

* loading kinked_R parameters into class by default

* Using default params in initializer for classes in ConsPrefShockModel

* removing act_T from default Cobb-Douglas parameters because it's already in __init__

* loading default params into initialization, last cases. see #446

* Fixing issue with multiple cycles arguments in IndShockExplicitPermIncConsumerType() and MedShockConsumerType()

Co-authored-by: Christopher Llorracc Carroll <1320319+llorracc@users.noreply.github.com>
  • Loading branch information
2 people authored and Mridul Seth committed Jan 9, 2020
1 parent 2bee725 commit ad83315
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 34 deletions.
39 changes: 30 additions & 9 deletions HARK/ConsumptionSaving/ConsAggShockModel.py
Expand Up @@ -20,6 +20,8 @@
from HARK import HARKobject, Market, AgentType
from copy import deepcopy
import matplotlib.pyplot as plt
import HARK.ConsumptionSaving.ConsumerParameters as Params


utility = CRRAutility
utilityP = CRRAutilityP
Expand Down Expand Up @@ -79,6 +81,9 @@ def __init__(self, time_flow=True, **kwds):
Make a new instance of AggShockConsumerType, an extension of
IndShockConsumerType. Sets appropriate solver and input lists.
'''
params = Params.init_agg_shocks.copy()
params.update(kwds)
kwds = params
AgentType.__init__(self, solution_terminal=deepcopy(IndShockConsumerType.solution_terminal_),
time_flow=time_flow, pseudo_terminal=False, **kwds)

Expand Down Expand Up @@ -375,6 +380,9 @@ class AggShockMarkovConsumerType(AggShockConsumerType):
state is subject to Markov-style discrete state evolution.
'''
def __init__(self, **kwds):
params = Params.init_agg_mrkv_shocks.copy()
params.update(kwds)
kwds = params
AggShockConsumerType.__init__(self, **kwds)
self.addToTimeInv('MrkvArray')
self.solveOnePeriod = solveConsAggMarkov
Expand Down Expand Up @@ -886,7 +894,11 @@ class CobbDouglasEconomy(Market):
Note: The current implementation assumes a constant labor supply, but
this will be generalized in the future.
'''
def __init__(self, agents=[], tolerance=0.0001, act_T=1000, **kwds):
def __init__(self,
agents=[],
tolerance=0.0001,
act_T=1200,
**kwds):
'''
Make a new instance of CobbDouglasEconomy by filling in attributes
specific to this kind of market.
Expand All @@ -906,6 +918,9 @@ def __init__(self, agents=[], tolerance=0.0001, act_T=1000, **kwds):
-------
None
'''
params = Params.init_cobb_douglas.copy()
params.update(kwds)
kwds = params
Market.__init__(self, agents=agents,
sow_vars=['MaggNow', 'AaggNow', 'RfreeNow',
'wRteNow', 'PermShkAggNow', 'TranShkAggNow', 'KtoLnow'],
Expand Down Expand Up @@ -1330,7 +1345,11 @@ class CobbDouglasMarkovEconomy(CobbDouglasEconomy):
productivity growth factor can vary over time.
'''
def __init__(self, agents=[], tolerance=0.0001, act_T=1000, **kwds):
def __init__(self,
agents=[],
tolerance=0.0001,
act_T=1200,
**kwds):
'''
Make a new instance of CobbDouglasMarkovEconomy by filling in attributes
specific to this kind of market.
Expand All @@ -1350,6 +1369,9 @@ def __init__(self, agents=[], tolerance=0.0001, act_T=1000, **kwds):
-------
None
'''
params = Params.init_mrkv_cobb_douglas.copy()
params.update(kwds)
kwds = params
CobbDouglasEconomy.__init__(self, agents=agents, tolerance=tolerance, act_T=act_T, **kwds)
self.sow_vars.append('MrkvNow')

Expand Down Expand Up @@ -1773,7 +1795,6 @@ def __init__(self, AFunc):
###############################################################################

def main():
import HARK.ConsumptionSaving.ConsumerParameters as Params
from time import clock
from HARK.utilities import plotFuncs

Expand All @@ -1791,11 +1812,11 @@ def mystr(number): return "{:.4f}".format(number)

if solve_agg_shocks_micro or solve_agg_shocks_market:
# Make an aggregate shocks consumer type
AggShockExample = AggShockConsumerType(**Params.init_agg_shocks)
AggShockExample = AggShockConsumerType()
AggShockExample.cycles = 0

# Make a Cobb-Douglas economy for the agents
EconomyExample = CobbDouglasEconomy(agents=[AggShockExample], **Params.init_cobb_douglas)
EconomyExample = CobbDouglasEconomy(agents=[AggShockExample])
EconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks

# Have the consumers inherit relevant objects from the economy
Expand Down Expand Up @@ -1842,12 +1863,12 @@ def mystr(number): return "{:.4f}".format(number)

if solve_markov_micro or solve_markov_market or solve_krusell_smith:
# Make a Markov aggregate shocks consumer type
AggShockMrkvExample = AggShockMarkovConsumerType(**Params.init_agg_mrkv_shocks)
AggShockMrkvExample = AggShockMarkovConsumerType()
AggShockMrkvExample.IncomeDstn[0] = 2*[AggShockMrkvExample.IncomeDstn[0]]
AggShockMrkvExample.cycles = 0

# Make a Cobb-Douglas economy for the agents
MrkvEconomyExample = CobbDouglasMarkovEconomy(agents=[AggShockMrkvExample], **Params.init_mrkv_cobb_douglas)
MrkvEconomyExample = CobbDouglasMarkovEconomy(agents=[AggShockMrkvExample])
MrkvEconomyExample.DampingFac = 0.2 # Turn down damping
MrkvEconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks
AggShockMrkvExample.getEconomyData(
Expand Down Expand Up @@ -1934,14 +1955,14 @@ def mystr(number): return "{:.4f}".format(number)
PolyMrkvArray[StateCount-1, StateCount-1] += 0.5*(1.0 - Persistence)

# Make a consumer type to inhabit the economy
PolyStateExample = AggShockMarkovConsumerType(**Params.init_agg_mrkv_shocks)
PolyStateExample = AggShockMarkovConsumerType()
PolyStateExample.MrkvArray = PolyMrkvArray
PolyStateExample.PermGroFacAgg = PermGroFacAgg
PolyStateExample.IncomeDstn[0] = StateCount*[PolyStateExample.IncomeDstn[0]]
PolyStateExample.cycles = 0

# Make a Cobb-Douglas economy for the agents
PolyStateEconomy = CobbDouglasMarkovEconomy(agents=[PolyStateExample], **Params.init_mrkv_cobb_douglas)
PolyStateEconomy = CobbDouglasMarkovEconomy(agents=[PolyStateExample])
PolyStateEconomy.MrkvArray = PolyMrkvArray
PolyStateEconomy.PermGroFacAgg = PermGroFacAgg
PolyStateEconomy.PermShkAggStd = StateCount*[0.006]
Expand Down
10 changes: 7 additions & 3 deletions HARK/ConsumptionSaving/ConsGenIncProcessModel.py
Expand Up @@ -18,6 +18,7 @@
getPercentiles
from HARK.simulation import drawLognormal, drawDiscrete, drawUniform
from HARK.ConsumptionSaving.ConsIndShockModel import ConsIndShockSetup, ConsumerSolution, IndShockConsumerType
import HARK.ConsumptionSaving.ConsumerParameters as Params

utility = CRRAutility
utilityP = CRRAutilityP
Expand Down Expand Up @@ -965,7 +966,7 @@ class GenIncProcessConsumerType(IndShockConsumerType):
solution_terminal_ = ConsumerSolution(cFunc=cFunc_terminal_, mNrmMin=0.0, hNrm=0.0, MPCmin=1.0, MPCmax=1.0)
poststate_vars_ = ['aLvlNow', 'pLvlNow']

def __init__(self, cycles=1, time_flow=True, **kwds):
def __init__(self, cycles=0, time_flow=True, **kwds):
'''
Instantiate a new ConsumerType with given data.
See ConsumerParameters.init_explicit_perm_inc for a dictionary of the
Expand All @@ -982,6 +983,10 @@ def __init__(self, cycles=1, time_flow=True, **kwds):
-------
None
'''
params = Params.init_explicit_perm_inc.copy()
params.update(kwds)
kwds = params

# Initialize a basic ConsumerType
IndShockConsumerType.__init__(self, cycles=cycles, time_flow=time_flow, **kwds)
self.solveOnePeriod = solveConsGenIncProcess # idiosyncratic shocks solver with explicit persistent income
Expand Down Expand Up @@ -1310,7 +1315,6 @@ def updatepLvlNextFunc(self):
###############################################################################

def main():
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import plotFuncs
from time import clock
import matplotlib.pyplot as plt
Expand All @@ -1326,7 +1330,7 @@ def mystr(number): return "{:.4f}".format(number)
print('percentile is ' + str(Params.init_explicit_perm_inc['pLvlPctiles'][-1]*100) + '.\n')

# Make and solve an example "explicit permanent income" consumer with idiosyncratic shocks
ExplicitExample = IndShockExplicitPermIncConsumerType(**Params.init_explicit_perm_inc)
ExplicitExample = IndShockExplicitPermIncConsumerType()
t_start = clock()
ExplicitExample.solve()
t_end = clock()
Expand Down
49 changes: 40 additions & 9 deletions HARK/ConsumptionSaving/ConsIndShockModel.py
Expand Up @@ -22,6 +22,7 @@
import numpy as np
from scipy.optimize import newton
from HARK import AgentType, Solution, NullFunc, HARKobject
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import warnings # Because of "patch" to warnings modules
from HARK.interpolation import CubicInterp, LowerEnvelope, LinearInterp
from HARK.simulation import drawDiscrete, drawLognormal, drawUniform
Expand Down Expand Up @@ -1596,7 +1597,12 @@ class PerfForesightConsumerType(AgentType):
poststate_vars_ = ['aNrmNow','pLvlNow']
shock_vars_ = []

def __init__(self,cycles=1, time_flow=True,verbose=False,quiet=False, **kwds):
def __init__(self,
cycles=1,
time_flow=True,
verbose=False,
quiet=False,
**kwds):
'''
Instantiate a new consumer type with given data.
See ConsumerParameters.init_perfect_foresight for a dictionary of
Expand All @@ -1613,6 +1619,11 @@ def __init__(self,cycles=1, time_flow=True,verbose=False,quiet=False, **kwds):
-------
None
'''

params = Params.init_perfect_foresight.copy()
params.update(kwds)
kwds = params

# Initialize a basic AgentType
AgentType.__init__(self,solution_terminal=deepcopy(self.solution_terminal_),
cycles=cycles,time_flow=time_flow,pseudo_terminal=False,**kwds)
Expand Down Expand Up @@ -1969,7 +1980,12 @@ class IndShockConsumerType(PerfForesightConsumerType):
time_inv_.remove('MaxKinks') # This is in the PerfForesight model but not ConsIndShock
shock_vars_ = ['PermShkNow','TranShkNow']

def __init__(self,cycles=1,time_flow=True,verbose=False,quiet=False,**kwds):
def __init__(self,
cycles=1,
time_flow=True,
verbose=False,
quiet=False,
**kwds):
'''
Instantiate a new ConsumerType with given data.
See ConsumerParameters.init_idiosyncratic_shocks for a dictionary of
Expand All @@ -1986,9 +2002,18 @@ def __init__(self,cycles=1,time_flow=True,verbose=False,quiet=False,**kwds):
-------
None
'''

params = Params.init_idiosyncratic_shocks.copy()
params.update(kwds)
kwds = params

# Initialize a basic AgentType
PerfForesightConsumerType.__init__(self,cycles=cycles,time_flow=time_flow,
verbose=verbose,quiet=quiet, **kwds)
PerfForesightConsumerType.__init__(self,
cycles=cycles,
time_flow=time_flow,
verbose=verbose,
quiet=quiet,
**kwds)

# Add consumer-type specific objects, copying to create independent versions
self.solveOnePeriod = solveConsIndShock # idiosyncratic shocks solver
Expand Down Expand Up @@ -2324,7 +2349,10 @@ class KinkedRconsumerType(IndShockConsumerType):
time_inv_.remove('Rfree')
time_inv_ += ['Rboro', 'Rsave']

def __init__(self,cycles=1,time_flow=True,**kwds):
def __init__(self,
cycles=1,
time_flow=True,
**kwds):
'''
Instantiate a new ConsumerType with given data.
See ConsumerParameters.init_kinked_R for a dictionary of
Expand All @@ -2341,6 +2369,10 @@ def __init__(self,cycles=1,time_flow=True,**kwds):
-------
None
'''
params = Params.init_kinked_R.copy()
params.update(kwds)
kwds = params

# Initialize a basic AgentType
PerfForesightConsumerType.__init__(self,cycles=cycles,time_flow=time_flow,**kwds)

Expand Down Expand Up @@ -2669,15 +2701,14 @@ def constructAssetsGrid(parameters):
####################################################################################################

def main():
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import plotFuncsDer, plotFuncs
from time import time
mystr = lambda number : "{:.4f}".format(number)

do_simulation = True

# Make and solve an example perfect foresight consumer
PFexample = PerfForesightConsumerType(**Params.init_perfect_foresight)
PFexample = PerfForesightConsumerType()
PFexample.cycles = 0 # Make this type have an infinite horizon

start_time = time()
Expand All @@ -2701,7 +2732,7 @@ def main():
###############################################################################

# Make and solve an example consumer with idiosyncratic income shocks
IndShockExample = IndShockConsumerType(**Params.init_idiosyncratic_shocks)
IndShockExample = IndShockConsumerType()
IndShockExample.cycles = 0 # Make this type have an infinite horizon

start_time = time()
Expand Down Expand Up @@ -2795,7 +2826,7 @@ def main():
###############################################################################

# Make and solve an agent with a kinky interest rate
KinkyExample = KinkedRconsumerType(**Params.init_kinked_R)
KinkyExample = KinkedRconsumerType()
KinkyExample.cycles = 0 # Make the Example infinite horizon

start_time = time()
Expand Down
10 changes: 7 additions & 3 deletions HARK/ConsumptionSaving/ConsMedModel.py
Expand Up @@ -18,6 +18,7 @@
from HARK.ConsumptionSaving.ConsGenIncProcessModel import ConsGenIncProcessSolver,\
PersistentShockConsumerType, ValueFunc2D, MargValueFunc2D,\
MargMargValueFunc2D, VariableLowerBoundFunc2D
import HARK.ConsumptionSaving.ConsumerParameters as Params
from copy import deepcopy

utility_inv = CRRAutility_inv
Expand Down Expand Up @@ -509,7 +510,7 @@ class MedShockConsumerType(PersistentShockConsumerType):
'''
shock_vars_ = PersistentShockConsumerType.shock_vars_ + ['MedShkNow']

def __init__(self,cycles=1,time_flow=True,**kwds):
def __init__(self,cycles=0,time_flow=True,**kwds):
'''
Instantiate a new ConsumerType with given data, and construct objects
to be used during solution (income distribution, assets grid, etc).
Expand All @@ -527,6 +528,10 @@ def __init__(self,cycles=1,time_flow=True,**kwds):
-------
None
'''
params = Params.init_medical_shocks.copy()
params.update(kwds)
kwds = params

PersistentShockConsumerType.__init__(self,cycles=cycles,**kwds)
self.solveOnePeriod = solveConsMedShock # Choose correct solver
self.addToTimeInv('CRRAmed')
Expand Down Expand Up @@ -1361,7 +1366,6 @@ def solveConsMedShock(solution_next,IncomeDstn,MedShkDstn,LivPrb,DiscFac,CRRA,CR
###############################################################################

def main():
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import CRRAutility_inv
from time import clock
import matplotlib.pyplot as plt
Expand All @@ -1370,7 +1374,7 @@ def main():
do_simulation = True

# Make and solve an example medical shocks consumer type
MedicalExample = MedShockConsumerType(**Params.init_medical_shocks)
MedicalExample = MedShockConsumerType()
t_start = clock()
MedicalExample.solve()
t_end = clock()
Expand Down

0 comments on commit ad83315

Please sign in to comment.