Skip to content

Commit

Permalink
Fixed imports in model files (#224)
Browse files Browse the repository at this point in the history
* Fixed imports in model files

All of the consumption-saving model files used a style of import that didn't work when the file was run directly (rather than called as a module), even though they have a __main__ block (and main() function).  This has now been fixed.

Also mostly removed extraneous file RepAgentModel.py, which seems to be an old name of ConsRepAgentModel.py.  This file now simply imports all of ConsRepAgentModel and warns the user to use that instead.

* import ConsumerParameters -> import HARK.ConsumptionSaving.ConsumerParameters
  • Loading branch information
mnwhite authored and llorracc committed Feb 15, 2019
1 parent 73791b0 commit 3c3b064
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 402 deletions.
8 changes: 6 additions & 2 deletions HARK/ConsumptionSaving/ConsAggShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
CRRAutility_invP, CRRAutility_inv, combineIndepDstns,\
approxMeanOneLognormal
from HARK.simulation import drawDiscrete, drawUniform
from .ConsIndShockModel import ConsumerSolution, IndShockConsumerType
from HARK.ConsumptionSaving.ConsIndShockModel import ConsumerSolution, IndShockConsumerType
from HARK import HARKobject, Market, AgentType
from copy import deepcopy
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -1753,7 +1753,7 @@ def __init__(self,AFunc):
###############################################################################

def main():
from . import ConsumerParameters as Params
import HARK.ConsumptionSaving.ConsumerParameters as Params
from time import clock
from HARK.utilities import plotFuncs
mystr = lambda number : "{:.4f}".format(number)
Expand Down Expand Up @@ -1793,6 +1793,7 @@ def main():
mMin = AggShockExample.solution[0].mNrmMin(M)
c_at_this_M = AggShockExample.cFunc[0](m_grid+mMin,M*np.ones_like(m_grid))
plt.plot(m_grid+mMin,c_at_this_M)
plt.ylim(0.,None)
plt.show()

if solve_agg_shocks_market:
Expand All @@ -1813,6 +1814,7 @@ def main():
mMin = AggShockExample.solution[0].mNrmMin(M)
c_at_this_M = AggShockExample.cFunc[0](m_grid+mMin,M*np.ones_like(m_grid))
plt.plot(m_grid+mMin,c_at_this_M)
plt.ylim(0.,None)
plt.show()

######### EXAMPLE IMPLEMENTATIONS OF AggShockMarkovConsumerType ###########
Expand Down Expand Up @@ -1843,6 +1845,7 @@ def main():
mMin = AggShockMrkvExample.solution[0].mNrmMin[i](M)
c_at_this_M = AggShockMrkvExample.cFunc[0][i](m_grid+mMin,M*np.ones_like(m_grid))
plt.plot(m_grid+mMin,c_at_this_M)
plt.ylim(0.,None)
plt.show()

if solve_markov_market:
Expand All @@ -1861,6 +1864,7 @@ def main():
mMin = AggShockMrkvExample.solution[0].mNrmMin[i](M)
c_at_this_M = AggShockMrkvExample.cFunc[0][i](m_grid+mMin,M*np.ones_like(m_grid))
plt.plot(m_grid+mMin,c_at_this_M)
plt.ylim(0.,None)
plt.show()

if solve_krusell_smith:
Expand Down
7 changes: 5 additions & 2 deletions HARK/ConsumptionSaving/ConsGenIncProcessModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
CRRAutility_invP, CRRAutility_inv, CRRAutilityP_invP,\
getPercentiles
from HARK.simulation import drawLognormal, drawDiscrete, drawUniform
from .ConsIndShockModel import ConsIndShockSetup, ConsumerSolution, IndShockConsumerType
from HARK.ConsumptionSaving.ConsIndShockModel import ConsIndShockSetup, ConsumerSolution, IndShockConsumerType

utility = CRRAutility
utilityP = CRRAutilityP
Expand Down Expand Up @@ -1276,7 +1276,7 @@ def updatepLvlNextFunc(self):
###############################################################################

def main():
from . import ConsumerParameters as Params
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import plotFuncs
from time import clock
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -1306,6 +1306,7 @@ def main():
C = ExplicitExample.solution[0].cFunc(M_temp,p*np.ones_like(M_temp))
plt.plot(M_temp,C)
plt.xlim(0.,20.)
plt.ylim(0.,None)
plt.xlabel('Market resource level mLvl')
plt.ylabel('Consumption level cLvl')
plt.show()
Expand All @@ -1328,6 +1329,7 @@ def main():
C = ExplicitExample.solution[0].cFunc(M_temp,p*np.ones_like(M_temp))
plt.plot(M_temp/p,C/p)
plt.xlim(0.,20.)
plt.ylim(0.,None)
plt.xlabel('Normalized market resources mNrm')
plt.ylabel('Normalized consumption cNrm')
plt.show()
Expand Down Expand Up @@ -1382,6 +1384,7 @@ def main():
C = PersistentExample.solution[0].cFunc(M_temp,p*np.ones_like(M_temp))
plt.plot(M_temp,C)
plt.xlim(0.,20.)
plt.ylim(0.,None)
plt.xlabel('Market resource level mLvl')
plt.ylabel('Consumption level cLvl')
plt.show()
Expand Down
2 changes: 1 addition & 1 deletion HARK/ConsumptionSaving/ConsIndShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2426,7 +2426,7 @@ def constructAssetsGrid(parameters):
####################################################################################################

def main():
from . import ConsumerParameters as Params
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import plotFuncsDer, plotFuncs
from time import clock
mystr = lambda number : "{:.4f}".format(number)
Expand Down
10 changes: 4 additions & 6 deletions HARK/ConsumptionSaving/ConsMarkovModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
from builtins import range
from copy import deepcopy
import numpy as np
from .ConsIndShockModel import ConsIndShockSolver, ValueFunc, MargValueFunc, ConsumerSolution, IndShockConsumerType
from .ConsAggShockModel import AggShockConsumerType
from HARK.utilities import combineIndepDstns, warnings # Because of "patch" to warnings modules
from HARK import Market, HARKobject
from HARK.ConsumptionSaving.ConsIndShockModel import ConsIndShockSolver, ValueFunc, \
MargValueFunc, ConsumerSolution, IndShockConsumerType
from HARK.simulation import drawDiscrete, drawUniform
from HARK.interpolation import CubicInterp, LowerEnvelope, LinearInterp
from HARK.utilities import CRRAutility, CRRAutilityP, CRRAutilityPP, CRRAutilityP_inv, \
Expand Down Expand Up @@ -974,7 +972,7 @@ def makeEulerErrorFunc(self,mMax=100,approx_inc_dstn=True):
###############################################################################

def main():
from . import ConsumerParameters as Params
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import plotFuncs
from time import clock
from copy import copy
Expand Down Expand Up @@ -1026,7 +1024,7 @@ def main():
start_time = clock()
SerialUnemploymentExample.solve()
end_time = clock()
print('Solving a Markov consumer took ' + mystr(end_time-start_time) + ' seconds.')
print('Solving a Markov consumer with serially correlated unemployment took ' + mystr(end_time-start_time) + ' seconds.')
print('Consumption functions for each discrete state:')
plotFuncs(SerialUnemploymentExample.solution[0].cFunc,0,50)
if SerialUnemploymentExample.vFuncBool:
Expand Down
11 changes: 5 additions & 6 deletions HARK/ConsumptionSaving/ConsMedModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
from HARK.utilities import approxLognormal, addDiscreteOutcomeConstantMean, CRRAutilityP_inv,\
CRRAutility, CRRAutility_inv, CRRAutility_invP, CRRAutilityPP,\
makeGridExpMult, NullFunc
from HARK.simulation import drawLognormal
from .ConsIndShockModel import ConsumerSolution
from HARK.ConsumptionSaving.ConsIndShockModel import ConsumerSolution
from HARK.interpolation import BilinearInterpOnInterp1D, TrilinearInterp, BilinearInterp, CubicInterp,\
LinearInterp, LowerEnvelope3D, UpperEnvelope, LinearInterpOnInterp1D,\
VariableLowerBoundFunc3D
from .ConsGenIncProcessModel import ConsGenIncProcessSolver, PersistentShockConsumerType,\
ValueFunc2D, MargValueFunc2D, MargMargValueFunc2D, \
VariableLowerBoundFunc2D
from HARK.ConsumptionSaving.ConsGenIncProcessModel import ConsGenIncProcessSolver,\
PersistentShockConsumerType, ValueFunc2D, MargValueFunc2D,\
MargMargValueFunc2D, VariableLowerBoundFunc2D
from copy import deepcopy

utility_inv = CRRAutility_inv
Expand Down Expand Up @@ -1359,7 +1358,7 @@ def solveConsMedShock(solution_next,IncomeDstn,MedShkDstn,LivPrb,DiscFac,CRRA,CR
###############################################################################

def main():
from . import ConsumerParameters as Params
import HARK.ConsumptionSaving.ConsumerParameters as Params
from HARK.utilities import CRRAutility_inv
from time import clock
import matplotlib.pyplot as plt
Expand Down
10 changes: 8 additions & 2 deletions HARK/ConsumptionSaving/ConsPrefShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from builtins import range
import numpy as np
from HARK.utilities import approxMeanOneLognormal
from .ConsIndShockModel import IndShockConsumerType, ConsumerSolution, ConsIndShockSolver, \
from HARK.ConsumptionSaving.ConsIndShockModel import IndShockConsumerType, ConsumerSolution, ConsIndShockSolver, \
ValueFunc, MargValueFunc, KinkedRconsumerType, ConsKinkedRsolver
from HARK.interpolation import LinearInterpOnInterp1D, LinearInterp, CubicInterp, LowerEnvelope

Expand Down Expand Up @@ -594,7 +594,7 @@ def solveConsKinkyPref(solution_next,IncomeDstn,PrefShkDstn,
###############################################################################

def main():
from . import ConsumerParameters as Params
import HARK.ConsumptionSaving.ConsumerParameters as Params
import matplotlib.pyplot as plt
from HARK.utilities import plotFuncs
from time import clock
Expand All @@ -618,13 +618,17 @@ def main():
PrefShk = PrefShockExample.PrefShkDstn[0][1][j]
c = PrefShockExample.solution[0].cFunc(m,PrefShk*np.ones_like(m))
plt.plot(m,c)
plt.xlim([0.,None])
plt.ylim([0.,None])
plt.show()

print('Consumption function (and MPC) when shock=1:')
c = PrefShockExample.solution[0].cFunc(m,np.ones_like(m))
k = PrefShockExample.solution[0].cFunc.derivativeX(m,np.ones_like(m))
plt.plot(m,c)
plt.plot(m,k)
plt.xlim([0.,None])
plt.ylim([0.,None])
plt.show()

if PrefShockExample.vFuncBool:
Expand Down Expand Up @@ -657,13 +661,15 @@ def main():
PrefShk = KinkyPrefExample.PrefShkDstn[0][1][j]
c = KinkyPrefExample.solution[0].cFunc(m,PrefShk*np.ones_like(m))
plt.plot(m,c)
plt.ylim([0.,None])
plt.show()

print('Consumption function (and MPC) when shock=1:')
c = KinkyPrefExample.solution[0].cFunc(m,np.ones_like(m))
k = KinkyPrefExample.solution[0].cFunc.derivativeX(m,np.ones_like(m))
plt.plot(m,c)
plt.plot(m,k)
plt.ylim([0.,None])
plt.show()

if KinkyPrefExample.vFuncBool:
Expand Down
4 changes: 2 additions & 2 deletions HARK/ConsumptionSaving/ConsRepAgentModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
from HARK.interpolation import LinearInterp
from HARK.simulation import drawUniform, drawDiscrete
from .ConsIndShockModel import IndShockConsumerType, ConsumerSolution, MargValueFunc
from HARK.ConsumptionSaving.ConsIndShockModel import IndShockConsumerType, ConsumerSolution, MargValueFunc

def solveConsRepAgent(solution_next,DiscFac,CRRA,IncomeDstn,CapShare,DeprFac,PermGroFac,aXtraGrid):
'''
Expand Down Expand Up @@ -331,7 +331,7 @@ def main():
from copy import deepcopy
from time import clock
from HARK.utilities import plotFuncs
from . import ConsumerParameters as Params
import HARK.ConsumptionSaving.ConsumerParameters as Params

# Make a quick example dictionary
RA_params = deepcopy(Params.init_idiosyncratic_shocks)
Expand Down
Loading

0 comments on commit 3c3b064

Please sign in to comment.