linearsolve.model

class linearsolve.model(equations=None, n_states=None, n_exo_states=None, var_names=None, shock_names=None, parameters=None, parameter_names=None)

Creates an instance of linearsolve.model that stores equilibrium conditions for a DSGE model.

Parameters:
  • equations (function) –

    A function that represents the equilibirum conditions for a DSGE model. The function should return an n-dimensional array with each element of the returned array being equaling an equilibrium condition of the model solved for zero. The function should accept three arguments:

    • variables_forward: Endogenous variables dated t+1.

    • variables_current: Endogenous variables dated t.

    • parameters: The parameters of the model.

  • n_states (int) – The number of state variables in the model.

  • n_exo_states (int) – The number of state variables with exogenous shocks.

  • var_names (list) – A list of strings with the names of the endogenous variables. The state variables with exogenous shocks must be ordered first, followed by state variables without exogenous shocks, followed by control variables. E.g., for a 3-variables RBC model, var_names = [‘a’,’k’,’c’].

  • shock_names (list) – A list of strings with the names of the exogenous shocks to each state variable. The order of names must agree with the relevant elements of var_names.

  • parameters (Pandas.Series) – Pandas Series object with parameter name strings as the index OR a list or an array of parameter values.

Attributes:

equilibrium_fun:

(function) – Function input with the equations parameter.

n_vars:

(int) – Number of endogenous variables.

n_states:

(int) – Number of state variables.

n_exo_states:

(int) – The number of exogenous state variables.

n_endo_states:

(int) – The number of endogenous state variables.

n_costates:

(int) – Number of control or costate variables.

names:

(dict) – A dictionary with keys ‘variables’, ‘shocks’, and ‘param’ that stores the names of the model’s variables, shocks, and parameters.

parameters:

(Pandas.Series) – A Pandas Series with parameter name strings as the index.

Methods:

approximate_and_solve(log_linear=True, eigenvalue_warnings=True)

Method approximates and solves a dynamic stochastic general equilibrium (DSGE) model by constructing the log-linear approximation (if the model isn’t log-linear) and solving the model using Klein’s (2000) method.

Parameters:
  • log_linear (bool) – Whether to compute log-linear or linear approximation. Default: True

  • eigenvalue_warnings – Whether to print warnings that there are too many or few eigenvalues. Default: True

Variables:
  • a (Numpy.ndarray) – Coefficient matrix on forward-dated variables.

  • b (Numpy.ndarray) – Coefficient matrix on current-dated variables.

  • f (Numpy.ndarray) – Solution matrix coeffients on s(t) in control equation.

  • p (Numpy.ndarray) – Solution matrix coeffients on u(t) in state equation.

  • stab (int) – Indicates solution stability and uniqueness. stab =1: too many stable eigenvalues, stab = -1: too few stable eigenvalues, stab = 0: just enough stable eigenvalues

  • eig (Numpy.ndarray) – Generalized eigenvalues from the Schur decomposition

approximated(round=True, precision=4)

Returns a string containing the log-linear approximation to the equilibrium conditions.

Parameters:
  • round (bool) – Whether to round the coefficents in the linear equations. Default: True

  • precision (int) – Number of decimals to round the coefficients. Default: 4

Returns:

string containing the log-linear approximation to the equilibrium conditions

Return type:

string

check_ss()

Uses Numpy.isclose() to print whether each steady state equilibrium condition evaluates to something close to zero.

Returns:

Numpy.ndarry of booleans indicating whether the stored staeady state satisfies each equilibrium condition

Return type:

Numpy.ndarray

compute_ss(guess=None, method='fsolve', options={})

Attempts to solve for the steady state of the model. Stores results as ss attribute.

Parameters:
  • guess (Pandas.Series or Numpy.ndarray or list) – An initial guess for the steady state solution. The result is highly sensisitve to the intial guess chosen, so be careful. If the guess is a Numpy ndarray or a list then the elements must be ordered to conform with self.names[‘variables’].

  • method (string) – The function from the Scipy library to use. Your choices are: ‘root’, ‘fsolve’ (default), broyden1, broyden2.

  • options (dictionary) – A dictionary of optional arguments to pass to the numerical solver. Check out the Scipy documentation to see the options available for each routine: http://docs.scipy.org/doc/scipy/reference/optimize.html

Variables:

ss (Pandas.Series) – The steady state of the model.

impulse(T=51, t0=1, shocks=None, percent=False, diff=True)

Computes impulse responses for shocks to each state variable.

Parameters:
  • T (int) – Number of periods to simulate. Default: 1

  • t0 (int) – Period in which the shocks are to be realized. Must be greater than or equal to 0. default: 1

  • shocks (list or Numpy.ndarray) – An array of shock values with length equal to the number of shocks. If shocks=None and log_linear=True, shocks is set to a vector of 0.01s. If shocks=None and log_linear=False, shocks is set to a vector of 1s. Default: None

  • percent (bool) – Whether to multiply simulated values by 100. Only works for log-linear approximations. Default: False

  • diff (bool) – Subtract steady state for linear approximations (or log steady state for log-linear approximations). Default: True

Variables:

irs (dict) – A dictionary containing Pandas DataFrames. Has the form: self.irs[‘shock name’][‘endog var name’]

linear_approximation(steady_state=None)

Given a nonlinear rational expectations model in the form:

(1)\[\psi_1[x_{t+1},x_t] = \psi_2[x_{t+1},x_t]\]

this method returns the linear approximation of the model with matrices \(A\) and \(B\) such that:

(2)\[A y_{t+1} = B y_t\]

where \(y_t = x_t - x\) is the log deviation of the vector \(x\) from its steady state value.

Parameters:

steady_state (Pandas.Series) – Coefficient matrix on forward-dated variables.

Variables:
  • a (Numpy.ndarray) – Coefficient matrix on forward-dated variables.

  • b (Numpy.ndarray) – Coefficient matrix on current-dated variables.

  • log_linear – Whether the model is log-linear. Sets to False.

log_linear_approximation(steady_state=None)

Given a nonlinear rational expectations model in the form:

(3)\[\psi_1[x_{t+1},x_t] = \psi_2[x_{t+1},x_t]\]

this method returns the log-linear approximation of the model with matrices \(A\) and \(B\) such that:

(4)\[A y_{t+1} = B y_t\]

where \(y_t = \log x_t - \log x\) is the log-deviation of the vector \(x\) from its steady state value.

Parameters:

steady_state (Pandas.Series) – Coefficient matrix on forward-dated variables.

Variables:
  • a (Numpy.ndarray) – Coefficient matrix on forward-dated variables.

  • b (Numpy.ndarray) – Coefficient matrix on current-dated variables.

  • log_linear – Whether the model is log-linear. Sets to True.

set_ss(steady_state)

Directly set the steady state of the model. Stores results as ss attribute.

Parameters:

steady_state (Pandas.Series or Numpy.ndarray or list) – The steady state of the model.

Variables:

ss (Pandas.Series) – The steady state of the model.

solve_klein(a=None, b=None, eigenvalue_warnings=True)

Solves a linear rational expectations model of the form:

(5)\[A x_{t+1} = B x_t\]

this method computes the log-linear approximation of the model with matrices \(A\) and \(B\) such that:

(6)\[\begin{split}u_t &= fs_t + \epsilon_t\\\end{split}\]

and:

(7)\[\begin{split}s_{t+1} &= ps_t\\\end{split}\]

where \(s_t\) denotes the vector of state variables and \(f_t\) denotes the vector of forward-looking variables.

Parameters:
  • a (Numpy.ndarray) – Coefficient matrix on forward-dated variables.

  • b (Numpy.ndarray) – Coefficient matrix on current-dated variables.

  • eigenvalue_warnings – Whether to print warnings that there are too many or few eigenvalues. Default: True

Variables:
  • f (Numpy.ndarray) – coeficient matrix.

  • p (Numpy.ndarray) – coeficient matrix.

  • stab (int) – Indicates solution stability and uniqueness. stab =1: too many stable eigenvalues, stab = -1: too few stable eigenvalues, stab = 0: just enough stable eigenvalues

  • eig (Numpy.ndarray) – Generalized eigenvalues from the Schur decomposition

solved(round=True, precision=4)

Returns a string containing the solution to the linear system

Parameters:
  • round (bool) – Whether to round the coefficents in the linear equations. Default: True

  • precision (int) – Number of decimals to round the coefficients. Default: 4

Returns:

string containing the solution to linear system

Return type:

string

stoch_sim(T=51, drop_first=300, cov_mat=None, seed=None, percent=False, diff=True)

Computes a stohcastic simulation of the model.

Parameters:
  • T (int) – Number of periods to simulate. Default: 1

  • drop_first (int) – Number of periods to simulate before generating the simulated periods. Default: 300

  • cov_mat (list or Numpy.ndarray) – Covariance matrix shocks. If cov_mat is None, it’s set to Numpy.eye(n_states). Default: None. If None, then exogenous shock standard deviations are set to 0.01.

  • seed (int) – Sets the seed for the Numpy random number generator. Default: None

  • percent (bool) – Whether to multiply simulated values by 100. Only works for log-linear approximations. Default: False

  • diff (bool) – Subtract steady state for linear approximations (or log steady state for log-linear approximations). Default: True

Variables:

simulated (Pandas.DataFrame) – A DataFrame with a column for each variable.