Skip to content

Commit

Permalink
Add tests for ConsRiskyAssetModel and fix simulation
Browse files Browse the repository at this point in the history
Simulation didn't work for ConsRiskyAssetConsumerType because Share was never being set. Tests were missing so it was never caught before.
  • Loading branch information
mnwhite committed Mar 22, 2024
1 parent c5ff3e3 commit bbc0f0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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
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()

0 comments on commit bbc0f0d

Please sign in to comment.