In [1]:
import linearsolve as ls
In [2]:
help(ls)
Help on package linearsolve:
NAME
linearsolve
PACKAGE CONTENTS
CLASSES
builtins.object
model
class model(builtins.object)
| model(equations=None, nstates=None, varNames=None, shockNames=None, parameters=None, parameterNames=None)
|
| This program defines a class -- linearsolve.model -- with associated methods for solving and
| simulating dynamic stochastic general equilibrium (DSGE) models.
|
| Methods defined here:
|
| __init__(self, equations=None, nstates=None, varNames=None, shockNames=None, parameters=None, parameterNames=None)
| Initializing an instance linearsolve.model requires values for the following variables:
|
| Args:
| equations: (fun) A function that represents the equilibirum conditions for a DSGE model.
| The function should accept three arguments:
| * vars_fwd: endogenous variables dated t+1
| * vars_cur: endogenous variables dated t
| * parameters: the parameters of the model
| The function should return an n-dimensional array. Each element of the returned
| array is a list with the first element equaling the left-hand side of a single
| equilibrium condition and the second elemtn equals the right-hand side.
| nstates: (int) The number of state variables in the model.
| varNames: (list) A list of strings with the names of the endogenous variables. The state
| variables must be ordered first.
| shockNames: (list) A list of strings with the names of the exogenous shocks to each state
| variable. The order of names must agree with varNames.
| parameters: (list or Pandas Series) Either a list of parameter values OR a Pandas Series object
| with parameter name strings as the index.
| parameterNames: (list) Optional. If parameters is given as a list,then this list of strings will
| be used to save the parameters with names as a Pandas Series object.
|
| Returns:
| None
|
| Attributes:
| nvars: (int) The number of variables in the model.
| nstates: (int) The number of state variables in the model.
| ncostates: (int) The number of costate or control variables in the model.
| parameters: (Pandas Seris) A Pandas Series with parameter name strings as the
| index. If parameterNames wasn't supplied,then parameters are labeled
| 'parameter 1',parameter2',etc.
| names: (dict) A dictionary with keys 'variables','shocks',and 'param' that
| stores the names of the model's variables and parameters.
| equilibrium_fun: (fun) Function that returns the equilibrium comditions of the model
| ss: (None)
|
| approximate_and_solve(self, loglinear=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. Arguments:
|
| Args:
| loglinear: (bool) whether to compute log-linear or linear approximation. Default: True
|
| Returns:
| None
|
| Attributes:
| a: (Numpy ndarray)
| b: (Numpy ndarray)
| f: (Numpy ndarray) Solution matrix coeffients on s(t)
| p: (Numpy ndarray) Solution matrix coeffients on s(t)
| stab: (int) Indicates solution stability and uniqueness
| stab == 1: too many stable eigenvalues
| stab == -1: too few stable eigenvalues
| stab == 0: just enoughstable eigenvalues
| eig: The generalized eigenvalues from the Schur decomposition
| loglinear: (bool) Whether the model is loglinear. Sets to loglinear.
|
| approximated(self, round=True, precision=4)
| Returns a string containing the log-linear approximation to the equilibrium conditions
|
| Args:
| round: (bool) Whether to round the coefficents in the linear equations. Default: True
| precisions: (int) Number of decimals to round the coefficients. Default: 4
|
| Returns:
| String with the log-linear approximation to the equilibrium conditions.
|
| Attributes:
| None
|
| check_ss(self)
| This method allows the user to directly set the steady state of the model.
|
| Args:
| None
|
| Returns:
| None
|
| Attributes:
| None
|
| compute_ss(self, guess=None, method='fsolve', options={})
| This method attempts to solve for the steady state of the model.
|
| Args:
| guess: (Pandas Series,Numpy array,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: (str) The function from the Scipy library to use. Your choices are:
| a. root
| b. fsolve (default)
| c. broyden1
| d. broyden2
| options: (dict) 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
|
| Returns:
| None
|
| Attributes:
| ss: (Pandas Series) Steady state values of endogenous variables
|
| impulse(self, T=51, t0=1, shocks=None, percent=False, diff=True)
| Method for computing impulse responses for shocks to each state variable.
|
| Arguments:
| T: (int) Number of periods to simulate. Default: 51
| t0: (int) Period in which the shocks are realized. May be equal to 0. Default: 1
| shocks: (list or Numpy array) An (ns x 1) list of shock values. If shocks==None,shocks is set to a vector of 0.01s. 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
|
| Returns
| None
|
| Attributes:
| irs: (dict) A dictionary containing Pandas DataFrames. Has the form: self.irs['shock name']['endog var name']
|
| linear_approximation(self, steady_state=None)
| Given a nonlinear rational expectations model in the form:
|
| 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:
|
| 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.
|
| Args:
| steady_state: (Pandas Series or numpy array)
|
| Returns:
| None
|
| Attributes:
| loglinear: (bool) Whether the model is loglinear. Sets to False.
| a: (Numpy ndarray)
| b: (Numpy ndarray)
|
| log_linear_approximation(self, steady_state=None)
| Given a nonlinear rational expectations model in the form:
|
| 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:
|
| 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.
|
| Args:
| steady_state: (Pandas Series or numpy array)
|
| Returns:
| None
|
| Attributes:
| loglinear: (bool) Whether the model is loglinear. Sets to True.
| a: (Numpy ndarray)
| b: (Numpy ndarray)
|
| set_ss(self, steady_state)
| This method allows the user to directly set the steady state of the model Uses Numpy.isclose()
| to print whether each steady state equilibrium condition evaluates to something close to zero.
|
| Args:
| steady_state: (Pandas Series,Numpy array,or list)
|
| Returns:
| None
|
| Attributes:
| ss: (Pandas Series) Steady state values of endogenous variables
|
| solve_klein(self, a=None, b=None)
| Method solves a linear rational expectations model of the form:
|
| a * x(t+1) = b * x(t) + e(t)
|
| where z(t) is a VAR(1) exogenous forcing process with autocorrelation matrix rho.
|
| The method returns the solution to the law of motion:
|
| u(t) = f*s(t) + e(t)
| s(t+1) = p*s(t)
|
| Args:
| a: (Numpy ndarray) coefficient matrix
| b: (Numpy ndarray) coefficient matrix
|
| Returns:
| None
|
| Attributes:
| f: (Numpy ndarray) Solution matrix coeffients on s(t)
| p: (Numpy ndarray) Solution matrix coeffients on s(t)
| stab: (int) Indicates solution stability and uniqueness
| stab == 1: too many stable eigenvalues
| stab == -1: too few stable eigenvalues
| stab == 0: just enoughstable eigenvalues
| eig: The generalized eigenvalues from the Schur decomposition
|
| solved(self, round=True, precision=4)
| Returns a string containing the solution to the log-linear system
|
| Args:
| round: (bool) Whether to round the coefficents in the solution equations. Default: True
| precisions: (int) Number of decimals to round the coefficients. Default: 4
|
| Returns:
| String with the log-linear approximation to the equilibrium conditions.
|
| Attributes:
| None
|
| stoch_sim(self, T=51, dropFirst=300, covMat=None, seed=None, percent=False, diff=True)
| Method for computing impulse responses for shocks to each state variable.
|
| Arguments:
| T: (int) Number of periods to simulate. Default: 51
| dropFirst: (int) Number of periods to simulate before saving output. Default: 300
| covMat: (list or Numpy.ndarray) Covariance matrix shocks. If covMat is None,it's set to eye(nstates). Default: None
| 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
|
| Returns
| None
|
| Attributes:
| simulated: (Pandas DataFrame)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
FUNCTIONS
ir(f, p, eps, s0=None)
Function for simulating a model in the following form:
u(t) = f*s(t) + e(t)
s(t+1) = p*s(t)
where s(t) is an (nstates x 1) vector of state variables,u(t) is an (ncostate x 1) vector of costate
variables,and e(t) is an (nstates x 1) vector of structural shocks.
Args:
f: (Numpy ndarray) matrix of appropriate size
p: (Numpy ndarray) matrix of appropriate size
eps: (Numpy ndarray) T x nstates array of exogenous strucural shocks.
s0: (Numpy ndarray) 1 x nstates array of zeros of initial state value. Optional; Default: 0.
Returns
s: (Numpy ndarray) states simulated from t = 0,1,...,T-1
u: (Numpy ndarray) costates simulated from t = 0,1,...,T-1
klein(a=None, b=None, c=None, rho=None, nstates=None)
This function solves linear dynamic models with the form of:
a*Et[x(t+1)] = b*x(t) + c*z(t)
with x(t) = [s(t); u(t)] where s(t) is a vector of predetermined (state) variables and u(t) is
a vector of nonpredetermined costate variables. z(t) is a vector of exogenous driving variables with
autocorrelation matrix rho. The solution to the model is a set of matrices f,n,p,l such that:
u(t) = f*s(t) + n*z(t)
s(t+1) = p*s(t) + l*z(t).
The solution algorithm is based on Klein (2000) and is based on his solab.m Matlab program.
Args:
a: (Numpy ndarray) coefficient matrix
b: (Numpy ndarray) coefficient matrix
c: (Numpy ndarray) coefficient matrix
rho: (Numpy ndarray) autocorrelation of exogenous forcing variables
nstates: (int) number of state variables
Returns:
f: (Numpy ndarray) Solution matrix coeffients on s(t)
p: (Numpy ndarray) Solution matrix coeffients on s(t)
n: (Numpy ndarray) Solution matrix coeffients on z(t)
l: (Numpy ndarray) Solution matrix coeffients on z(t)
stab: (int) Indicates solution stability and uniqueness
stab == 1: too many stable eigenvalues
stab == -1: too few stable eigenvalues
stab == 0: just enoughstable eigenvalues
eig: The generalized eigenvalues from the Schur decomposition
DATA
division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192...
print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...
FILE
/Users/bcjenkin/anaconda3/lib/python3.7/site-packages/linearsolve/__init__.py
In [7]:
A = ['a','name','n_state','ac','abc']
A.sort()
A
Out[7]:
['a', 'abc', 'ac', 'n_state', 'name']
In [ ]:
Content source: letsgoexploring/linearsolve-package
Similar notebooks: