-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
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 74382c3
draw_shocks and docs
sbenthall f6a672d
automated test for draw_shock
sbenthall 0041d2c
AgentTypeMonteCarloSimulator class draft
sbenthall 3004643
generic draw_shocks distinguishes between time-varying, non-time vary…
sbenthall 53a7f65
Working test for AgentTypeMonteCarloSimulator.
sbenthall e8799da
var value updates working for Python 3.8 now
sbenthall 98ca16f
tighter make_shock_history, with test
sbenthall ddaf185
fixing handling of saved newborn inits
sbenthall dde1e88
removing warning for very unlikely scenario (T_sim set by default)
sbenthall 4ea021a
remove sim_death from MonteCarloSimulator, just use 'live' state
sbenthall 411703a
use age-varying parameters in MonteCarloSimulator
sbenthall ea3e7b1
age-varying decision rules in generic monte carlo
sbenthall e858d2d
Merge branch 'master' into i1295
sbenthall ce1fe31
updating CHANGELOG
sbenthall 4c0850c
use HARK.model classes in HARK.simulate functions
sbenthall 5ef3a19
adjust PF python model so you only need to initialize p, not y
sbenthall 2cc60bf
when creating new data arrays for variables NOW in generic monte carl…
sbenthall b95b0f4
adding example notebook for comparing HARK PF and Generic Monte Carlo
sbenthall 0c52963
adding python model configuration for normalized perfect foresight; g…
sbenthall 799de9a
put loading initial values to newborns with read_shocks into sim_birth
sbenthall 2d212ae
track 'who_dies' to t-1 spot in history for HARK 0.13 core; generic m…
sbenthall e7a6981
Merge branch 'master' into i1295
sbenthall 1bd53cf
black
sbenthall c4feeb3
Update core.py -- fixing bad merge
sbenthall 1a33be6
pre-commit fixes
sbenthall 62e8d5f
ruff format the example notebook
sbenthall 4f4b9ba
improving documentation for Generic Monte Carlo
sbenthall cd4c05a
Merge branch 'master' into i1295
sbenthall a329473
adding model.rst file to docs
sbenthall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
''' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.