Skip to content

Commit

Permalink
Merge pull request #1400 from econ-ark/AddMoreCStests
Browse files Browse the repository at this point in the history
Add more consumption-saving tests
  • Loading branch information
alanlujan91 authored Mar 25, 2024
2 parents bbb07a5 + bbc0f0d commit 0ea895f
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 16 deletions.
6 changes: 3 additions & 3 deletions HARK/ConsumptionSaving/ConsBequestModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,13 @@ def __init__(

def def_utility_funcs(self):
super().def_utility_funcs()

self.warm_glow = UtilityFuncStoneGeary(self.BeqCRRA, self.BeqFac, self.BeqShift)
BeqFacEff = (1.0 - self.LivPrb) * self.BeqFac
self.warm_glow = UtilityFuncStoneGeary(self.BeqCRRA, BeqFacEff, self.BeqShift)

def calc_EndOfPrdvP(self):
super().calc_EndOfPrdvP()

self.EndofPrddvda = self.EndOfPrddvda + self.warm_glow.der(self.aNrm_tiled)
self.EndOfPrddvda = self.EndOfPrddvda + self.warm_glow.der(self.aNrm_tiled)
self.EndOfPrddvdaNvrs = self.uPinv(self.EndOfPrddvda)


Expand Down
1 change: 1 addition & 0 deletions HARK/ConsumptionSaving/ConsGenIncProcessModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ def solve(self):
# Make a dictionary for the "explicit permanent income" idiosyncratic shocks model
init_explicit_perm_inc = init_idiosyncratic_shocks.copy()
init_explicit_perm_inc["pLvlPctiles"] = pLvlPctiles
init_explicit_perm_inc["pLvlInitStd"] = 0.4 # This *must* be nonzero
# long run permanent income growth doesn't work yet
init_explicit_perm_inc["PermGroFac"] = [1.0]
init_explicit_perm_inc["aXtraMax"] = 30
Expand Down
8 changes: 4 additions & 4 deletions HARK/ConsumptionSaving/ConsRiskyAssetModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ def get_Rfree(self):
"""

RfreeNow = super().get_Rfree()
RiskyNow = self.shocks["Risky"]
# ShareNow = self.controls["Share"]
ShareNow = np.ones_like(RiskyNow) # Only asset is risky asset

Rport = (
self.controls["Share"] * self.shocks["Risky"]
+ (1.0 - self.controls["Share"]) * RfreeNow
)
Rport = ShareNow * RiskyNow + (1.0 - ShareNow) * RfreeNow
self.Rport = Rport
return Rport

Expand Down
61 changes: 61 additions & 0 deletions HARK/ConsumptionSaving/tests/test_ConsBequestModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import unittest
from HARK.tests import HARK_PRECISION
from HARK.ConsumptionSaving.ConsBequestModel import (
BequestWarmGlowConsumerType,
BequestWarmGlowPortfolioType,
)


class testWarmGlowConsumerType(unittest.TestCase):
def setUp(self):
self.agent = BequestWarmGlowConsumerType()
self.agent.vFuncBool = True
self.agent.solve()

def test_solution(self):
cFunc = self.agent.solution[0].cFunc
mNrm = 10.0
self.assertAlmostEqual(cFunc(mNrm).tolist(), 5.56409, places=HARK_PRECISION)

# TODO: Turn this on when solver overhaul branch is merged (needs correct value)
# def test_value(self):
# vFunc = self.agent.solution[0].vFunc
# mNrm = 10.0
# self.assertAlmostEqual(vFunc(mNrm), -0.0000, places=HARK_PRECISION)

def test_simulation(self):
self.agent.T_sim = 10
self.agent.track_vars = ["mNrm", "cNrm", "aNrm"]
self.agent.make_shock_history()
self.agent.initialize_sim()
self.agent.simulate()


class testBequestWarmGlowPortfolioType(unittest.TestCase):
def setUp(self):
self.agent = BequestWarmGlowPortfolioType()
self.agent.vFuncBool = True
self.agent.solve()

def test_consumption(self):
cFunc = self.agent.solution[0].cFuncAdj
mNrm = 10.0
self.assertAlmostEqual(cFunc(mNrm).tolist(), 2.19432, places=HARK_PRECISION)

def test_share(self):
ShareFunc = self.agent.solution[0].ShareFuncAdj
mNrm = 10.0
self.assertAlmostEqual(ShareFunc(mNrm).tolist(), 0.75504, places=HARK_PRECISION)

# TODO: Turn this on when solver overhaul branch is merged (needs correct value)
# def test_value(self):
# vFunc = self.agent.solution[0].vFuncAdj
# mNrm = 10.0
# self.assertAlmostEqual(vFunc(mNrm), -0.0000, places=HARK_PRECISION)

def test_simulation(self):
self.agent.T_sim = 10
self.agent.track_vars = ["mNrm", "cNrm", "aNrm", "Share"]
self.agent.make_shock_history()
self.agent.initialize_sim()
self.agent.simulate()
23 changes: 17 additions & 6 deletions HARK/ConsumptionSaving/tests/test_ConsGenIncProcessModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"aNrmInitMean": 0.0, # Mean of log initial assets (only matters for simulation)
"aNrmInitStd": 1.0, # Standard deviation of log initial assets (only for simulation)
"pLvlInitMean": 0.0, # Mean of log initial permanent income (only matters for simulation)
"pLvlInitStd": 0.0, # Standard deviation of log initial permanent income (only matters for simulation)
"pLvlInitStd": 0.4, # Standard deviation of log initial permanent income (only matters for simulation)
"PermGroFacAgg": 1.0, # Aggregate permanent income growth factor (only matters for simulation)
"T_age": None, # Age after which simulated agents are automatically killed
"T_cycle": 1, # Number of periods in the cycle for this agent type
Expand Down Expand Up @@ -64,13 +64,15 @@ def setUp(self):

def test_solution(self):
pLvlGrid = self.agent.pLvlGrid[0]
self.assertAlmostEqual(self.agent.pLvlGrid[0][0], 1.0)
self.assertAlmostEqual(
self.agent.pLvlGrid[0][0], 0.27916, places=HARK_PRECISION
)

self.assertAlmostEqual(self.agent.solution[0].mLvlMin(pLvlGrid[0]), 0.0)

self.assertAlmostEqual(
self.agent.solution[0].cFunc(10, pLvlGrid[5]).tolist(),
5.60301,
5.40844,
places=HARK_PRECISION,
)

Expand All @@ -91,17 +93,26 @@ def test_solution(self):

self.assertAlmostEqual(
self.agent.solution[0].cFunc(10, pLvlGrid[1]).tolist(),
5.60301,
5.28844,
places=HARK_PRECISION,
)

def test_value(self):
pLvlGrid = self.agent.pLvlGrid[0]

self.assertTrue(self.agent.vFuncBool)
self.assertAlmostEqual(
self.agent.solution[0].vFunc(10, pLvlGrid[3]),
-0.3655,
places=HARK_PRECISION,
)

def test_simulation(self):
self.agent.T_sim = 25

# why does ,"bLvlNow" not work here?
self.agent.track_vars = ["aLvl", "mLvl", "cLvl", "pLvl"]
self.agent.initialize_sim()
self.agent.simulate()

# simulation test -- seed/generator specific
# self.assertAlmostEqual(np.mean(self.agent.history["mLvl"]), 1.20439, place = HARK_PRECISION)
# self.assertAlmostEqual(np.mean(self.agent.history["mLvl"]), 1.20439, places=HARK_PRECISION)
23 changes: 21 additions & 2 deletions HARK/ConsumptionSaving/tests/test_ConsMedModel.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import unittest

from HARK.tests import HARK_PRECISION
from HARK.ConsumptionSaving.ConsMedModel import MedShockConsumerType


class testMedShockConsumerType(unittest.TestCase):
def setUp(self):
self.agent = MedShockConsumerType()
self.agent.vFuncBool = True
self.agent.solve()

def test_solution(self):
self.agent.solve()
cFunc = self.agent.solution[0].cFunc
MedFunc = self.agent.solution[0].MedFunc
mLvl = 10.0
pLvl = 2.0
Shk = 1.5
self.assertAlmostEqual(
cFunc(mLvl, pLvl, Shk).tolist(), 4.16259, places=HARK_PRECISION
)
self.assertAlmostEqual(
MedFunc(mLvl, pLvl, Shk).tolist(), 2.45412, places=HARK_PRECISION
)

def test_value(self):
vFunc = self.agent.solution[0].vFunc
mLvl = 10.0
pLvl = 2.0
self.assertAlmostEqual(vFunc(mLvl, pLvl), -0.32428, places=HARK_PRECISION)

def test_simulation(self):
self.agent.T_sim = 10
self.agent.track_vars = ["mLvl", "cLvl", "Med"]
self.agent.make_shock_history()
Expand Down
28 changes: 28 additions & 0 deletions HARK/ConsumptionSaving/tests/test_ConsRiskyAssetModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest
from HARK.tests import HARK_PRECISION
from HARK.ConsumptionSaving.ConsRiskyAssetModel import IndShockRiskyAssetConsumerType


class testRiskyAssetConsumerType(unittest.TestCase):
def setUp(self):
self.agent = IndShockRiskyAssetConsumerType()
self.agent.vFuncBool = False
self.agent.solve()

def test_solution(self):
cFunc = self.agent.solution[0].cFunc
mNrm = 10.0
self.assertAlmostEqual(cFunc(mNrm).tolist(), 5.637216, places=HARK_PRECISION)

# TODO: Turn this on after solver overhaul branch is merged
# def test_value(self):
# vFunc = self.agent.solution[0].vFunc
# mNrm = 10.0
# self.assertAlmostEqual(vFunc(mNrm), -0.0000, places=HARK_PRECISION)

def test_simulation(self):
self.agent.T_sim = 10
self.agent.track_vars = ["mNrm", "cNrm", "aNrm"]
self.agent.make_shock_history()
self.agent.initialize_sim()
self.agent.simulate()
3 changes: 2 additions & 1 deletion HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,8 @@ def test_calc_tran_matrix(self):
asset = example1.aPol_Grid # Normalized Asset Policy Grid

example1.calc_ergodic_dist()
vecDstn = example1.vec_erg_dstn # Distribution of market resources and permanent income as a vector (m*p)x1 vector where
vecDstn = example1.vec_erg_dstn
# Distribution of market resources and permanent income as a vector (m*p)x1 vector where

# Compute Aggregate Consumption and Aggregate Assets
gridc = np.zeros((len(c), len(p)))
Expand Down

0 comments on commit 0ea895f

Please sign in to comment.