Functions to support Monte Carlo simulation of models.

class HARK.simulation.monte_carlo.AgentTypeMonteCarloSimulator(parameters, shocks, dynamics, 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 (Mapping[str, Any]) –

  • shocks (Mapping[str, Distribution]) –

  • dynamics (Mapping[str, Union[Callable, Control]]) –

  • dr (Mapping[str, Callable]) –

  • initial (dict) –

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

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

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


Clears the histories.


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


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.


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().


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

Return type:



Reset the random number generator for this type.


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.


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

Return type:



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.


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].




history – The history tracked during the simulation.

Return type:


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

Bases: object

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

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

  • 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.parameters_by_age(ages, parameters)#

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

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

  • parameters (dict) – A parameters dictionary


aged_parameters – 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:


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.


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.