Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic monte carlo simulation #1296

Merged
merged 30 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c9e06ba
basic implementation of generic sim_one_period. see #1295
sbenthall Jun 30, 2023
74382c3
draw_shocks and docs
sbenthall Jul 20, 2023
f6a672d
automated test for draw_shock
sbenthall Jul 20, 2023
0041d2c
AgentTypeMonteCarloSimulator class draft
sbenthall Jul 21, 2023
3004643
generic draw_shocks distinguishes between time-varying, non-time vary…
sbenthall Jul 24, 2023
53a7f65
Working test for AgentTypeMonteCarloSimulator.
sbenthall Jul 24, 2023
e8799da
var value updates working for Python 3.8 now
sbenthall Jul 24, 2023
98ca16f
tighter make_shock_history, with test
sbenthall Jul 26, 2023
ddaf185
fixing handling of saved newborn inits
sbenthall Jul 26, 2023
dde1e88
removing warning for very unlikely scenario (T_sim set by default)
sbenthall Jul 26, 2023
4ea021a
remove sim_death from MonteCarloSimulator, just use 'live' state
sbenthall Jul 27, 2023
411703a
use age-varying parameters in MonteCarloSimulator
sbenthall Aug 10, 2023
ea3e7b1
age-varying decision rules in generic monte carlo
sbenthall Aug 28, 2023
e858d2d
Merge branch 'master' into i1295
sbenthall Oct 9, 2023
ce1fe31
updating CHANGELOG
sbenthall Oct 9, 2023
4c0850c
use HARK.model classes in HARK.simulate functions
sbenthall Oct 9, 2023
5ef3a19
adjust PF python model so you only need to initialize p, not y
sbenthall Oct 9, 2023
2cc60bf
when creating new data arrays for variables NOW in generic monte carl…
sbenthall Oct 9, 2023
b95b0f4
adding example notebook for comparing HARK PF and Generic Monte Carlo
sbenthall Oct 9, 2023
0c52963
adding python model configuration for normalized perfect foresight; g…
sbenthall Oct 11, 2023
799de9a
put loading initial values to newborns with read_shocks into sim_birth
sbenthall Oct 17, 2023
2d212ae
track 'who_dies' to t-1 spot in history for HARK 0.13 core; generic m…
sbenthall Nov 13, 2023
e7a6981
Merge branch 'master' into i1295
sbenthall Nov 13, 2023
1bd53cf
black
sbenthall Nov 13, 2023
c4feeb3
Update core.py -- fixing bad merge
sbenthall Nov 13, 2023
1a33be6
pre-commit fixes
sbenthall Nov 13, 2023
62e8d5f
ruff format the example notebook
sbenthall Nov 13, 2023
4f4b9ba
improving documentation for Generic Monte Carlo
sbenthall Nov 27, 2023
cd4c05a
Merge branch 'master' into i1295
sbenthall Nov 27, 2023
a329473
adding model.rst file to docs
sbenthall Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

'''
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops ignore the stuff below this line. I was going to develop more robust tests from this material later.

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)
'''