Skip to content

Commit

Permalink
Merge pull request #1296 from sbenthall/i1295
Browse files Browse the repository at this point in the history
Generic monte carlo simulation
  • Loading branch information
sbenthall authored Nov 29, 2023
2 parents 960a7f7 + a329473 commit 1ad4731
Show file tree
Hide file tree
Showing 14 changed files with 2,615 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Documentation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Release Date: TBD

- Adds `HARK.core.AgentPopulation` class to represent a population of agents with ex-ante heterogeneous parametrizations as distributions. [#1237](https://github.com/econ-ark/HARK/pull/1237)
- Adds `HARK.core.Parameters` class to represent a collection of time varying and time invariant parameters in a model. [#1240](https://github.com/econ-ark/HARK/pull/1240)
- Adds `HARK.simulation.monte_carlo` module for generic Monte Carlo simulation functions using Python model configurations. [1296](https://github.com/econ-ark/HARK/pull/1296)

### Minor Changes

Expand All @@ -26,6 +27,8 @@ Release Date: TBD
- Fixes bug that prevented risky-asset consumer types from working with time-varying interest rates `Rfree`. [1343](https://github.com/econ-ark/HARK/pull/1343)
- Overhauls and expands condition checking for the ConsIndShock model [#1294](https://github.com/econ-ark/HARK/pull/1294). Condition values and a description of their interpretation is stored in the bilt dictionary of IndShockConsumerType.
- Creates a `models/` directory with Python model configurations for perfect foresight and Fisher 2-period models. [1347](https://github.com/econ-ark/HARK/pull/1347)
- Fixes bug in AgentType simulations where 'who_dies' for period t was being recorded in period t-1in the history Carlo simulation functions using Python model configurations.[1296](https://github.com/econ-ark/HARK/pull/1296)
- Removes unused `simulation.py` .[1296](https://github.com/econ-ark/HARK/pull/1296)

### 0.13.0

Expand Down
1 change: 1 addition & 0 deletions Documentation/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ API Reference
tools/frame
tools/helpers
tools/interpolation
tools/model
tools/numba_tools
tools/parallel
tools/rewards
Expand Down
7 changes: 7 additions & 0 deletions Documentation/reference/tools/model.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Model
-------------

.. automodule:: HARK.model
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion Documentation/reference/tools/simulation.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Simulation
------------

.. automodule:: HARK.simulation
.. automodule:: HARK.simulation.monte_carlo
:members:
:undoc-members:
:show-inheritance:
10 changes: 9 additions & 1 deletion HARK/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
problem by finding a general equilibrium dynamic rule.
"""
# Set logging and define basic functions
# Set logging and define basic functions
import logging
import sys
from collections import defaultdict, namedtuple
Expand Down Expand Up @@ -1061,7 +1062,14 @@ def simulate(self, sim_periods=None):
elif var_name in self.controls:
self.history[var_name][self.t_sim, :] = self.controls[var_name]
else:
self.history[var_name][self.t_sim, :] = getattr(self, var_name)
if var_name == "who_dies" and self.t_sim > 1:
self.history[var_name][self.t_sim - 1, :] = getattr(
self, var_name
)
else:
self.history[var_name][self.t_sim, :] = getattr(
self, var_name
)
self.t_sim += 1

return self.history
Expand Down
20 changes: 19 additions & 1 deletion HARK/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@
Tools for crafting models.
"""

from HARK.distribution import Distribution


class Aggregate:
"""
Used to designate a shock as an aggregate shock.
If so designated, draws from the shock will be scalar rather
than array valued.
"""

def __init__(self, dist: Distribution):
self.dist = dist


class Control:
"""
Should go in different model support module.
Used to designate a variabel that is a control variable.
Parameters
----------
args : list of str
The labels of the variables that are in the information set of this control.
"""

def __init__(self, args):
Expand Down
Empty file added HARK/models/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion HARK/models/perfect_foresight.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"BoroCnstArt": None,
},
"dynamics": {
"y": lambda p: p,
"m": lambda Rfree, a, y: Rfree * a + y,
"c": Control(["m"]),
"y": lambda p: p,
"p": lambda PermGroFac, p: PermGroFac * p,
"a": lambda m, c: m - c,
},
Expand Down
31 changes: 31 additions & 0 deletions HARK/models/perfect_foresight_normalized.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from HARK.distribution import Bernoulli
from HARK.model import Control

# This way of distributing parameters across the scope is clunky
# Can be handled better if parsed from a YAML file, probably
# But it would be better to have a more graceful Python version as well.
CRRA = (2.0,)
LivPrb = 0.98

model = {
"shocks": {
"live": Bernoulli(p=LivPrb),
},
"parameters": {
"DiscFac": 0.96,
"CRRA": CRRA,
"Rfree": 1.03,
"LivPrb": LivPrb,
"PermGroFac": 1.01,
"BoroCnstArt": None,
},
"dynamics": {
"p": lambda PermGroFac, p: PermGroFac * p,
"r_eff": lambda Rfree, PermGroFac: Rfree / PermGroFac,
"b_nrm": lambda r_eff, a_nrm: r_eff * a_nrm,
"m_nrm": lambda b_nrm: b_nrm + 1,
"c_nrm": Control(["m_nrm"]),
"a_nrm": lambda m_nrm, c_nrm: m_nrm - c_nrm,
},
"reward": {"u": lambda c: c ** (1 - CRRA) / (1 - CRRA)},
}
7 changes: 0 additions & 7 deletions HARK/simulation.py

This file was deleted.

Empty file added HARK/simulation/__init__.py
Empty file.
Loading

0 comments on commit 1ad4731

Please sign in to comment.