gEconpy.model.perturbation.linearize_model#

gEconpy.model.perturbation.linearize_model(variables, equations, shocks, cache=None, loglin_variables=None, order=1, eq_order=None, var_order=None)#

Compute the log-linearized Jacobian matrices of a DSGE model using pytensor autodiff.

Builds four Jacobian matrices A, B, C, D representing the first-order approximation of the model around its steady state:

\[A \hat{y}_{t-1} + B \hat{y}_t + C \hat{y}_{t+1} + D \varepsilon_t = 0\]

Log-linearization is implemented as a change-of-variables: for each variable to be log-linearized, the substitution \(y \to \exp(\tilde{y})\) is applied before differentiation, then \(\tilde{y} \to \log(y)\) is substituted back. The chain rule produces the classical T-matrix multiplication \(\partial F / \partial (\log y) = (\partial F / \partial y) \cdot y_{ss}\). Variables not in the loglin set use identity substitutions, yielding bare derivatives.

A pt.switch guard on the steady-state value prevents log(negative) for variables whose steady states are not know to be non-positive. This is a numerical safety net: rewrite_pregrad does not simplify exp(log(x)) when x < 0. This is avoided if a variable is known to be positive or negative via the Assumptions block of the GCN file.

Parameters:
variableslist of TimeAwareSymbol

Model variables, expressed at time t.

equationslist of sp.Expr

Model equations as sympy expressions.

shockslist of TimeAwareSymbol

Exogenous shocks.

cachedict, optional

Sympytensor cache mapping (name, assumptions) tuples to pytensor nodes. If provided, sympy-to-pytensor conversion reuses existing nodes. If None, a new cache is created.

loglin_variableslist of TimeAwareSymbol, optional

Variables to log-linearize. If None, all variables are log-linearized.

orderint, default 1

Order of approximation. Only order=1 is currently supported.

eq_orderndarray of int, optional

Permutation of equation indices placing equations in [static | lag-only | lead-only | both] order so A’s and C’s structural-zero row blocks become contiguous. Computed from the equations if not supplied.

var_orderndarray of int, optional

Permutation of variable indices placing variables in [static | predetermined-only | mixed | forward-only] order so A’s and C’s structural-zero column blocks become contiguous. Computed from the equations if not supplied.

Returns:
jacobianslist of TensorVariable

Four pytensor matrix graph nodes [A, B, C, D]. Rows are in eq_order and the variable axis (cols of A/B/C) is in var_order; D’s columns are shocks (no permutation).

ss_input_nodeslist of TensorVariable

Steady-state variable input nodes needed to evaluate the Jacobians. Parameter nodes are also embedded in the graph but must be discovered by the caller via explicit_graph_inputs.

eq_order_outndarray of int

The equation permutation actually applied (same as eq_order if supplied).

var_order_outndarray of int

The variable permutation actually applied (same as var_order if supplied).