Simulation#

Functions to support Monte Carlo simulation of models.

class HARK.simulation.monte_carlo.AgentTypeMonteCarloSimulator(calibration, block: DBlock, dr, initial, seed=0, agent_count=1, T_sim=10)#

Bases: Simulator

A Monte Carlo simulation engine based on the HARK.core.AgentType framework.

Unlike HARK.core.AgentType, this class does not do any model solving, and depends on dynamic equations, shocks, and decision rules paased into it.

The purpose of this class is to provide a way to simulate models without relying on inheritance from the AgentType class.

This simulator makes assumptions about population birth and mortality which are not generic. All agents are replaced with newborns when they expire.

Parameters:
  • calibration (Mapping[str, Any])

  • block (DBlock) – Has shocks, dynamics, and rewards

  • dr (Mapping[str, Callable])

  • initial (dict)

  • seed (int) – A seed for this instance’s random number generator.

Variables:
  • agent_count (int) – The number of agents of this type to use in simulation.

  • T_sim (int) – The number of periods to simulate.

clear_history()#

Clears the histories.

get_mortality()#

Simulates mortality or agent turnover. Agents die when their states live is less than or equal to zero.

initialize_sim()#

Prepares for a new simulation. Resets the internal random number generator, makes initial states for all agents (using sim_birth), clears histories of tracked variables.

make_shock_history()#

Makes a pre-specified history of shocks for the simulation. Shock variables should be named in self.shock, a mapping from shock names to distributions. This method runs a subset of the standard simulation loop by simulating only mortality and shocks; each variable named in shocks is stored in a T_sim x agent_count array in history dictionary self.history[X]. Automatically sets self.read_shocks to True so that these pre-specified shocks are used for all subsequent calls to simulate().

Returns:

shock_history – The subset of simulation history that are the shocks for each agent and time.

Return type:

dict

reset_rng()#

Reset the random number generator for this type.

sim_birth(which_agents)#

Makes new agents for the simulation. Takes a boolean array as an input, indicating which agent indices are to be “born”. Does nothing by default, must be overwritten by a subclass.

Parameters:

which_agents (np.array(Bool)) – Boolean array of size self.agent_count indicating which agents should be “born”.

Return type:

None

sim_one_period()#

Simulates one period for this type. Calls the methods get_mortality(), get_shocks() or read_shocks, get_states(), get_controls(), and get_poststates(). These should be defined for AgentType subclasses, except get_mortality (define its components sim_death and sim_birth instead) and read_shocks.

simulate(sim_periods=None)#

Simulates this agent type for a given number of periods. Defaults to self.T_sim if no input. Records histories of attributes named in self.track_vars in self.history[varname].

Parameters:

None

Returns:

history – The history tracked during the simulation.

Return type:

dict

state_vars = []#
class HARK.simulation.monte_carlo.Simulator#

Bases: object

HARK.simulation.monte_carlo.calibration_by_age(ages, calibration)#

Returns calibration for this model, but with vectorized values which map age-varying values to agent ages.

Parameters:
  • ages (np.array) – An array of agent ages.

  • calibration (dict) – A calibration dictionary

Returns:

aged_calibration – A dictionary of parameter values. If a parameter is age-varying, the value is a vector corresponding to the values for each input age.

Return type:

dict

HARK.simulation.monte_carlo.draw_shocks(shocks: Mapping[str, Distribution], conditions: Sequence[int])#

Draw from each shock distribution values, subject to given conditions.

Parameters:
  • Mapping[str (shocks) – A dictionary-like mapping from shock names to distributions from which to draw

  • Distribution] – A dictionary-like mapping from shock names to distributions from which to draw

  • conditions (Sequence[int]) – An array of conditions, one for each agent. Typically these will be agent ages.

  • draws (Mapping[str, Sequence]) – A mapping from shock names to drawn shock values.

HARK.simulation.monte_carlo.simulate_dynamics(dynamics: Mapping[str, Callable | Control], pre: Mapping[str, Any], dr: Mapping[str, Callable])#

From the beginning-of-period state (pre), follow the dynamics, including any decision rules, to compute the end-of-period state.

Parameters:

dynamics (Mapping[str, Callable]) – Maps variable names to functions from variables to values. Can include Controls ## TODO: Make collection of equations into a named type

preMapping[str, Any]

Bound values for all variables that must be known before beginning the period’s dynamics.

drMapping[str, Callable]

Decision rules for all the Control variables in the dynamics.