Core¶
Highlevel functions and classes for solving a wide variety of economic models. The “core” of HARK is a framework for “microeconomic” and “macroeconomic” models. A micro model concerns the dynamic optimization problem for some type of agents, where agents take the inputs to their problem as exogenous. A macro model adds an additional layer, endogenizing some of the inputs to the micro problem by finding a general equilibrium dynamic rule.

class
HARK.core.
AgentType
(solution_terminal=None, pseudo_terminal=True, tolerance=1e06, seed=0, **kwds)¶ Bases:
HARK.core.Model
A superclass for economic agents in the HARK framework. Each model should specify its own subclass of AgentType, inheriting its methods and overwriting as necessary. Critically, every subclass of AgentType should define class specific static values of the attributes time_vary and time_inv as lists of strings. Each element of time_vary is the name of a field in AgentSubType that varies over time in the model. Each element of time_inv is the name of a field in AgentSubType that is constant over time in the model.
Parameters:  solution_terminal (Solution) – A representation of the solution to the terminal period problem of this AgentType instance, or an initial guess of the solution if this is an infinite horizon problem.
 cycles (int) – The number of times the sequence of periods is experienced by this AgentType in their “lifetime”. cycles=1 corresponds to a lifecycle model, with a certain sequence of one period problems experienced once before terminating. cycles=0 corresponds to an infinite horizon model, with a sequence of one period problems repeating indefinitely.
 pseudo_terminal (boolean) – Indicates whether solution_terminal isn’t actually part of the solution to the problem (as a known solution to the terminal period problem), but instead represents a “scrap value”style termination. When True, solution_terminal is not included in the solution; when false, solution_terminal is the last element of the solution.
 tolerance (float) – Maximum acceptable “distance” between successive solutions to the one period problem in an infinite horizon (cycles=0) model in order for the solution to be considered as having “converged”. Inoperative when cycles>0.
 seed (int) – A seed for this instance’s random number generator.

state_vars
¶ The string labels for this AgentType’s model state variables.
Type: list of string

add_to_time_inv
(*params)¶ Adds any number of parameters to time_inv for this instance.
Parameters: params (string) – Any number of strings naming attributes to be added to time_inv Returns: Return type: None

add_to_time_vary
(*params)¶ Adds any number of parameters to time_vary for this instance.
Parameters: params (string) – Any number of strings naming attributes to be added to time_vary Returns: Return type: None

check_elements_of_time_vary_are_lists
()¶ A method to check that elements of time_vary are lists.

check_restrictions
()¶ A method to check that various restrictions are met for the model class.

clear_history
()¶ Clears the histories of the attributes named in self.track_vars.
Parameters: None – Returns: Return type: None

del_from_time_inv
(*params)¶ Removes any number of parameters from time_inv for this instance.
Parameters: params (string) – Any number of strings naming attributes to be removed from time_inv Returns: Return type: None

del_from_time_vary
(*params)¶ Removes any number of parameters from time_vary for this instance.
Parameters: params (string) – Any number of strings naming attributes to be removed from time_vary Returns: Return type: None

get_controls
()¶ Gets values of control variables for the current period, probably by using current states. Does nothing by default, but can be overwritten by subclasses of AgentType.
Parameters: None – Returns: Return type: None

get_mortality
()¶ Simulates mortality or agent turnover according to some modelspecific rules named sim_death and sim_birth (methods of an AgentType subclass). sim_death takes no arguments and returns a Boolean array of size AgentCount, indicating which agents of this type have “died” and must be replaced. sim_birth takes such a Boolean array as an argument and generates initial postdecision states for those agent indices.
Parameters: None – Returns: Return type: None

get_poststates
()¶ Gets values of postdecision state variables for the current period, probably by current states and controls and maybe marketlevel events or shock variables. Does nothing by default, but can be overwritten by subclasses of AgentType.
DEPRECATED: New models should use the state now/previous rollover functionality instead of poststates.
Parameters: None – Returns: Return type: None

get_shocks
()¶ Gets values of shock variables for the current period. Does nothing by default, but can be overwritten by subclasses of AgentType.
Parameters: None – Returns: Return type: None

get_states
()¶ Gets values of state variables for the current period. By default, calls transition function and assigns values to the state_now dictionary.
Parameters: None – Returns: Return type: None

initialize_sim
()¶ Prepares this AgentType for a new simulation. Resets the internal random number generator, makes initial states for all agents (using sim_birth), clears histories of tracked variables.
Parameters: None – Returns: Return type: None

make_shock_history
()¶ Makes a prespecified history of shocks for the simulation. Shock variables should be named in self.shock_vars, a list of strings that is subclassspecific. This method runs a subset of the standard simulation loop by simulating only mortality and shocks; each variable named in shock_vars is stored in a T_sim x AgentCount array in history dictionary self.history[X]. Automatically sets self.read_shocks to True so that these prespecified shocks are used for all subsequent calls to simulate().
Parameters: None – Returns: Return type: None

post_solve
()¶ A method that is run immediately after the model is solved, to finalize the solution in some way. Does nothing here.
Parameters: none – Returns: Return type: none

pre_solve
()¶ A method that is run immediately before the model is solved, to check inputs or to prepare the terminal solution, perhaps.
Parameters: none – Returns: Return type: none

read_shocks_from_history
()¶ Reads values of shock variables for the current period from history arrays. For each variable X named in self.shock_vars, this attribute of self is set to self.history[X][self.t_sim,:].
This method is only ever called if self.read_shocks is True. This can be achieved by using the method make_shock_history() (or manually after storing a “handcrafted” shock history).
Parameters: None – Returns: Return type: None

reset_rng
()¶ Reset the random number generator for this type.
Parameters: none – Returns: Return type: none

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.AgentCount indicating which agents should be “born”. Returns: Return type: None

sim_death
()¶ Determines which agents in the current population “die” or should be replaced. Takes no inputs, returns a Boolean array of size self.AgentCount, which has True for agents who die and False for those that survive. Returns all False by default, must be overwritten by a subclass to have replacement events.
Parameters: None – Returns: who_dies – Boolean array of size self.AgentCount indicating which agents die and are replaced. Return type: np.array

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.
Parameters: None – Returns: Return type: None

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

solve
(verbose=False)¶ Solve the model for this instance of an agent type by backward induction. Loops through the sequence of one period problems, passing the solution from period t+1 to the problem for period t.
Parameters: verbose (boolean) – If True, solution progress is printed to screen. Returns: Return type: none

state_vars
= []

transition
()¶ Parameters:  None –
 to match dolo spec ([Eventually,) –
 endogenous_prev, controls, exogenous, parameters] (exogenous_prev,) –
Returns: endogenous_state – Tuple with new values of the endogenous states
Return type: ()

unpack
(parameter)¶ Unpacks a parameter from a solution object for easier access. After the model has been solved, the parameters (like consumption function) reside in the attributes of each element of ConsumerType.solution (e.g. cFunc). This method creates a (time varying) attribute of the given parameter name that contains a list of functions accessible by ConsumerType.parameter.
Parameters: parameter (str) – Name of the function to unpack from the solution Returns: Return type: none

class
HARK.core.
Market
(agents=None, sow_vars=None, reap_vars=None, const_vars=None, track_vars=None, dyn_vars=None, mill_rule=None, calc_dynamics=None, act_T=1000, tolerance=1e06, **kwds)¶ Bases:
HARK.core.Model
A superclass to represent a central clearinghouse of information. Used for dynamic general equilibrium models to solve the “macroeconomic” model as a layer on top of the “microeconomic” models of one or more AgentTypes.
Parameters:  agents ([AgentType]) – A list of all the AgentTypes in this market.
 sow_vars ([string]) – Names of variables generated by the “aggregate market process” that should be “sown” to the agents in the market. Aggregate state, etc.
 reap_vars ([string]) – Names of variables to be collected (“reaped”) from agents in the market to be used in the “aggregate market process”.
 const_vars ([string]) – Names of attributes of the Market instance that are used in the “aggregate market process” but do not come from agents– they are constant or simply parameters inherent to the process.
 track_vars ([string]) – Names of variables generated by the “aggregate market process” that should be tracked as a “history” so that a new dynamic rule can be calculated. This is often a subset of sow_vars.
 dyn_vars ([string]) – Names of variables that constitute a “dynamic rule”.
 mill_rule (function) – A function that takes inputs named in reap_vars and returns a tuple the same size and order as sow_vars. The “aggregate market process” that transforms individual agent actions/states/data into aggregate data to be sent back to agents.
 calc_dynamics (function) – A function that takes inputs named in track_vars and returns an object with attributes named in dyn_vars. Looks at histories of aggregate variables and generates a new “dynamic rule” for agents to believe and act on.
 act_T (int) – The number of times that the “aggregate market process” should be run in order to generate a history of aggregate variables.
 tolerance (float) – Minimum acceptable distance between “dynamic rules” to consider the Market solution process converged. Distance is a userdefined metric.

cultivate
()¶ Has each AgentType in agents perform their market_action method, using variables sown from the market (and maybe also “private” variables). The market_action method should store new results in attributes named in reap_vars to be reaped later.
Parameters: none – Returns: Return type: none

make_history
()¶ Runs a loop of sow–>cultivate–>reap–>mill act_T times, tracking the evolution of variables X named in track_vars in dictionary fields history[X].
Parameters: none – Returns: Return type: none

mill
()¶ Processes the variables collected from agents using the function mill_rule, storing the results in attributes named in aggr_sow.
Parameters: none – Returns: Return type: none

reap
()¶ Collects attributes named in reap_vars from each AgentType in the market, storing them in respectively named attributes of self.
Parameters: none – Returns: Return type: none

reset
()¶ Reset the state of the market (attributes in sow_vars, etc) to some userdefined initial state, and erase the histories of tracked variables.
Parameters: none – Returns: Return type: none

solve
()¶ “Solves” the market by finding a “dynamic rule” that governs the aggregate market state such that when agents believe in these dynamics, their actions collectively generate the same dynamic rule.
Parameters: None – Returns: Return type: None

solve_agents
()¶ Solves the microeconomic problem for all AgentTypes in this market.
Parameters: None – Returns: Return type: None

sow
()¶ Distributes attrributes named in sow_vars from self to each AgentType in the market, storing them in respectively named attributes.
Parameters: none – Returns: Return type: none

store
()¶ Record the current value of each variable X named in track_vars in an dictionary field named history[X].
Parameters: none – Returns: Return type: none

update_dynamics
()¶ Calculates a new “aggregate dynamic rule” using the history of variables named in track_vars, and distributes this rule to AgentTypes in agents.
Parameters: none – Returns: dynamics – The new “aggregate dynamic rule” that agents believe in and act on. Should have attributes named in dyn_vars. Return type: instance

class
HARK.core.
MetricObject
¶ Bases:
object
A superclass for object classes in HARK. Comes with two useful methods: a generic/universal distance method and an attribute assignment method.

distance
(other)¶ A generic distance method, which requires the existence of an attribute called distance_criteria, giving a list of strings naming the attributes to be considered by the distance metric.
Parameters: other (object) – Another object to compare this instance to. Returns: (unnamed) – The distance between this object and another, using the “universal distance” metric. Return type: float

distance_criteria
= []¶


class
HARK.core.
Model
¶ Bases:
object
A class with special handling of parameters assignment.

assign_parameters
(**kwds)¶ Assign an arbitrary number of attributes to this agent.
Parameters: **kwds (keyword arguments) – Any number of keyword arguments of the form key=value. Each value will be assigned to the attribute named in self. Returns: Return type: none

get_parameter
(name)¶ Returns a parameter of this model
Parameters: name (string) – The name of the parameter to get Returns: The value of the parameter Return type: value


HARK.core.
distance_metric
(thing_a, thing_b)¶ A “universal distance” metric that can be used as a default in many settings.
Parameters:

HARK.core.
distribute_params
(agent, param_name, param_count, distribution)¶ Distributes heterogeneous values of one parameter to the AgentTypes in self.agents. :param agent: An agent to clone. :type agent: AgentType :param param_name: Name of the parameter to be assigned. :type param_name: string :param param_count: Number of different values the parameter will take on. :type param_count: int :param distribution: A distribution. :type distribution: Distribution
Returns: agent_set – A list of param_count agents, ex ante heterogeneous with respect to param_name. The AgentCount of the original will be split between the agents of the returned list in proportion to the given distribution. Return type: [AgentType]

HARK.core.
make_one_period_oo_solver
(solver_class)¶ Returns a function that solves a single period consumptionsaving problem. :param solver_class: A class of Solver to be used. :type solver_class: Solver :param ——: :param solver_function: A function for solving one period of a problem. :type solver_function: function

HARK.core.
solve_agent
(agent, verbose)¶ Solve the dynamic model for one agent type using backwards induction. This function iterates on “cycles” of an agent’s model either a given number of times or until solution convergence if an infinite horizon model is used (with agent.cycles = 0).
Parameters:  agent (AgentType) – The microeconomic AgentType whose dynamic problem is to be solved.
 verbose (boolean) – If True, solution progress is printed to screen (when cycles != 1).
Returns: solution – A list of solutions to the one period problems that the agent will encounter in his “lifetime”.
Return type: [Solution]

HARK.core.
solve_one_cycle
(agent, solution_last)¶ Solve one “cycle” of the dynamic model for one agent type. This function iterates over the periods within an agent’s cycle, updating the timevarying parameters and passing them to the single period solver(s).
Parameters:  agent (AgentType) – The microeconomic AgentType whose dynamic problem is to be solved.
 solution_last (Solution) – A representation of the solution of the period that comes after the end of the sequence of one period problems. This might be the term inal period solution, a “pseudo terminal” solution, or simply the solution to the earliest period from the succeeding cycle.
Returns: solution_cycle – A list of one period solutions for one “cycle” of the AgentType’s microeconomic model.
Return type: [Solution]