Skip to content

Commit

Permalink
basic implementation of generic sim_one_period. see econ-ark#1295
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenthall committed Jun 30, 2023
1 parent 37134b9 commit c9e06ba
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
Empty file added HARK/simulation/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions HARK/simulation/monte_carlo.py
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
113 changes: 113 additions & 0 deletions HARK/simulation/test_monte_carlo.py
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)
'''

0 comments on commit c9e06ba

Please sign in to comment.