forked from econ-ark/HARK
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic implementation of generic sim_one_period. see econ-ark#1295
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from inspect import signature | ||
from typing import Callable, Mapping | ||
|
||
|
||
class Control: | ||
""" | ||
Should go in HARK.model | ||
""" | ||
|
||
def __init__(self, args): | ||
pass | ||
|
||
def sim_one_period( | ||
dynamics : Mapping[str, Callable], | ||
pre : Mapping, | ||
dr : Mapping[str, Callable] | ||
): | ||
vals = pre.copy() | ||
|
||
for varn in dynamics: | ||
# Using the fact that Python dictionaries are ordered | ||
|
||
feq = dynamics[varn] | ||
|
||
if isinstance(feq, Control): | ||
vals[varn] = dr[varn](*[ | ||
vals[var] | ||
for var | ||
in signature(dr[varn]).parameters]) # TODO: test for signature match with Control | ||
else: | ||
vals[varn] = feq(*[vals[var] for var in signature(feq).parameters]) | ||
|
||
return vals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
""" | ||
This file implements unit tests for the Monte Carlo simulation module | ||
""" | ||
import unittest | ||
|
||
from HARK.simulation.monte_carlo import * | ||
|
||
|
||
pre = { | ||
'R' : 1.05, | ||
'aNrm' : 1, | ||
'gamma' : 1.1, | ||
'psi' : 1.1, # TODO: draw this from a shock, | ||
'theta' : 1.1 # TODO: draw this from a shock | ||
} | ||
|
||
dynamics = { | ||
'G' : lambda gamma, psi : gamma * psi, | ||
'Rnrm' : lambda R, G : R / G, | ||
'bNrm' : lambda Rnrm, aNrm : Rnrm * aNrm, | ||
'mNrm' : lambda bNrm, theta : bNrm + theta, | ||
'cNrm' : Control(['mNrm']), | ||
'aNrm' : lambda mNrm, cNrm : mNrm - cNrm | ||
} | ||
|
||
dr = { | ||
'cNrm' : lambda mNrm : mNrm / 2 | ||
} | ||
|
||
|
||
class test_sim_one_period(unittest.TestCase): | ||
def test_sim_one_period(self): | ||
|
||
post = sim_one_period(dynamics, pre, dr) | ||
|
||
self.assertAlmostEqual(post['cNrm'], 0.98388429) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
###############################################################3 | ||
|
||
''' | ||
init_parameters = {} | ||
init_parameters["PermGroFac"] = 1.05 | ||
init_parameters["PermShkStd"] = 1.5 | ||
init_parameters["PermShkCount"] = 5 | ||
init_parameters["TranShkStd"] = 3.0 | ||
init_parameters["TranShkCount"] = 5 | ||
init_parameters["RiskyAvg"] = 1.05 | ||
init_parameters["RiskyStd"] = 1.5 | ||
init_parameters["RiskyCount"] = 5 | ||
init_parameters["Rfree"] = 1.03 | ||
frames_A = [ | ||
Frame(("bNrm",), ("aNrm",), transition=lambda Rfree, aNrm: Rfree * aNrm), | ||
Frame(("mNrm",), ("bNrm", "TranShk"), transition=lambda bNrm: mNrm), | ||
Frame(("cNrm"), ("mNrm",), control=True), | ||
Frame( | ||
("U"), | ||
("cNrm", "CRRA"), # Note CRRA here is a parameter not a state var | ||
transition=lambda cNrm, CRRA: (CRRAutility(cNrm, CRRA),), | ||
reward=True, | ||
context={"CRRA": 2.0}, | ||
), | ||
Frame(("aNrm"), ("mNrm", "cNrm"), transition=lambda mNrm, cNrm: (mNrm - cNrm,)), | ||
] | ||
class test_FrameModel(unittest.TestCase): | ||
def setUp(self): | ||
self.model = FrameModel(frames_A, init_parameters) | ||
def test_init(self): | ||
self.model.frames.var("aNrm") | ||
self.assertTrue( | ||
isinstance( | ||
list(self.model.frames.var("bNrm").parents.values())[0], | ||
BackwardFrameReference, | ||
) | ||
) | ||
self.assertTrue( | ||
isinstance( | ||
list(self.model.frames.var("aNrm").children.values())[0], | ||
ForwardFrameReference, | ||
) | ||
) | ||
def test_make_terminal(self): | ||
terminal_model = self.model.make_terminal() | ||
self.assertEqual(len(self.model.make_terminal().frames.var("aNrm").children), 0) | ||
def test_prepend(self): | ||
double_model = self.model.prepend(self.model) | ||
self.assertEqual(len(double_model.frames), 10) | ||
def test_repeat(self): | ||
repeat_model = self.model.repeat({"bNrm": {"Rfree": [1.01, 1.03, 1.02]}}) | ||
self.assertEqual(len(repeat_model.frames), 15) | ||
self.assertEqual(repeat_model.frames.var("bNrm_1").context["Rfree"], 1.03) | ||
''' |