MacroModelling.add_calibration_equation!Method
add_calibration_equation!(
    𝓂,
    new_equation;
    parameters,
    verbose,
    silent
)

Add a new calibration equation to the model.

The equation must use the lhs = rhs | param syntax, and param must already be part of the model and not yet calibrated. A batched form is supported by passing a Vector/Tuple of equations.

Arguments

  • 𝓂: object created by @model and @parameters.
  • new_equation [Type: Union{Expr, String}]: the calibration equation to add. Must contain the | param syntax.

Alternatively:

  • new_equations [Type: Union{Vector, Tuple}]: a collection of calibration equations to add in one rebuild.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).
  • silent [Default: true, Type: Bool]: suppress informational warnings from the rebuild pipeline.

Returns

  • nothing. The model 𝓂 is updated in place.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

# pin down β by targeting a value for c at the steady state
add_calibration_equation!(RBC, :(c[ss] = 1.0 | β))
source
MacroModelling.add_equation!Method
add_equation!(𝓂, new_equation; parameters, verbose, silent)

Append one or more equations to the model.

Pass either a single equation (Expr or String) or a Vector/Tuple of equations to add several at once. Each addition is recorded in the revision history (see get_revision_history).

Arguments

  • 𝓂: object created by @model and @parameters.
  • new_equation [Type: Union{Expr, String}]: the equation to add.

Alternatively:

  • new_equations [Type: Union{Vector, Tuple}]: a collection of equations to add in one rebuild.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).
  • silent [Default: true, Type: Bool]: suppress informational warnings from the rebuild pipeline.

Returns

  • nothing. The model 𝓂 is updated in place.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

add_equation!(RBC, :(y[0] = c[0] + k[0] - (1 - δ) * k[-1]))

add_equation!(RBC, [
    :(inv[0] = k[0] - (1 - δ) * k[-1]),
    :(log_c[0] = log(c[0])),
])
source
MacroModelling.get_autocorrelationMethod
get_autocorrelation(
    𝓂;
    autocorrelation_periods,
    parameters,
    steady_state_function,
    algorithm,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    verbose,
    tol,
    caching,
    use_workspaces
)

Return the autocorrelations of endogenous variables using the first, pruned second, or pruned third order perturbation solution.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

Keyword Arguments

  • autocorrelation_periods [Default: 1:5, Type: UnitRange{Int}]: periods for which to return the autocorrelation
  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows and autocorrelation periods in columns.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

get_autocorrelation(RBC)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   Autocorrelation_periods ∈ 5-element UnitRange{Int64}
And data, 4×5 Matrix{Float64}:
        (1)         (2)         (3)         (4)         (5)
  (:c)    0.966974    0.927263    0.887643    0.849409    0.812761
  (:k)    0.971015    0.931937    0.892277    0.853876    0.817041
  (:q)    0.32237     0.181562    0.148347    0.136867    0.129944
  (:z)    0.2         0.04        0.008       0.0016      0.00032
source
MacroModelling.get_calibrated_parametersMethod
get_calibrated_parameters(𝓂; values)

Returns the parameters (and optionally the values) which are determined by a calibration equation.

Arguments

Keyword Arguments

  • values [Default: false, Type: Bool]: return the values together with the parameter names.

Returns

  • Vector{String} of the calibrated parameters or Vector{Pair{String, Float64}} of the calibrated parameters and values if values is set to true.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_calibrated_parameters(RBC)
# output
1-element Vector{String}:
 "δ"
source
MacroModelling.get_calibration_equation_parametersMethod
get_calibration_equation_parameters(𝓂)

Returns the parameters used in calibration equations which are not used in the equations of the model (see capital_to_output in Examples).

Arguments

Returns

  • Vector{String} of the parameters.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_calibration_equation_parameters(RBC)
# output
1-element Vector{String}:
 "capital_to_output"
source
MacroModelling.get_calibration_equationsMethod
get_calibration_equations(𝓂; filter)

Return the calibration equations declared in the @parameters block. Calibration equations are additional equations which are part of the non-stochastic steady state problem. The additional equation is matched with a calibated parameter which is part of the equations declared in the @model block and can be retrieved with: get_calibrated_parameters

In case programmatic model writing was used this function returns the parsed equations (see loop over shocks in example).

Note that the output assumes the equations are equal to 0. As in, k / (q * 4) - capital_to_output implies k / (q * 4) - capital_to_output = 0 and therefore: k / (q * 4) = capital_to_output.

Arguments

Keyword Arguments

  • filter [Default: nothing, Type: Union{Symbol, String, Nothing}]: filter equations by variable name. Specify a variable name (e.g., :k or "k") to return only equations containing that variable. Time subscripts (except [ss]) are ignored for calibration equations.

Returns

  • Vector{Expr} of the calibration equations as expressions.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_calibration_equations(RBC)
# output
1-element Vector{Expr}:
 :(k / (q * 4) - capital_to_output)
source
MacroModelling.get_conditional_forecastMethod
get_conditional_forecast(
    𝓂,
    conditions;
    shocks,
    initial_state,
    periods,
    parameters,
    steady_state_function,
    variables,
    conditions_in_levels,
    algorithm,
    levels,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    conditional_forecast_solver,
    caching,
    use_workspaces
)

Return the conditional forecast given restrictions on endogenous variables and shocks (optional). By default, the values represent absolute deviations from the relevant steady state (see levels for details). The non-stochastic steady state (NSSS) is relevant for first order solutions and the stochastic steady state for higher order solutions. A constrained minimisation problem is solved to find the combination of shocks with the smallest squared magnitude fulfilling the conditions.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

  • 𝓂: object created by @model and @parameters.
  • conditions [Type: Union{Matrix{Union{Nothing,Float64}}, SparseMatrixCSC{Float64}, KeyedArray{Union{Nothing,Float64}}, KeyedArray{Float64}}]: conditions for which to find the corresponding shocks. The input can have multiple formats, but for all types of entries, the first dimension corresponds to variables and the second dimension to the number of periods. The conditions can be specified using a matrix of type Matrix{Union{Nothing,Float64}}. In this case the conditions are matrix elements of type Float64 and all remaining (free) entries are nothing. A SparseMatrixCSC{Float64} can also be used as input. In this case only non-zero elements are taken as conditions. Note that conditioning variables to be zero using a SparseMatrixCSC{Float64} as input is not possible (use other input formats to do so). Another possibility to input conditions is by using a KeyedArray. The KeyedArray type is provided by the AxisKeys package. A KeyedArray{Union{Nothing,Float64}} can be used where, similar to Matrix{Union{Nothing,Float64}}, all entries of type Float64 are recognised as conditions and all other entries have to be nothing. Furthermore, in the primary axis a subset of variables (of type Symbol or String) for which conditions are specified can be included and all other variables are considered free. The same goes for the case when using KeyedArray{Float64}} as input, whereas in this case the conditions for the specified variables bind for all periods specified in the KeyedArray, because there are no nothing entries permitted with this type.

Keyword Arguments

  • shocks [Default: nothing, Type: Union{Matrix{Union{Nothing,Float64}}, SparseMatrixCSC{Float64}, KeyedArray{Union{Nothing,Float64}}, KeyedArray{Float64}, Nothing}]: known values of shocks. This argument allows including certain shock values. By entering restrictions on the shocks in this way the problem to match the conditions on endogenous variables is restricted to the remaining free shocks in the respective period. The input can have multiple formats, but for all types of entries, the first dimension corresponds to shocks and the second dimension to the number of periods. shocks can be specified using a matrix of type Matrix{Union{Nothing,Float64}}. In this case the shocks are matrix elements of type Float64 and all remaining (free) entries are nothing. A SparseMatrixCSC{Float64} can also be used as input. In this case only non-zero elements are taken as certain shock values. Note that conditioning shocks to be zero using a SparseMatrixCSC{Float64} as input is not possible (use other input formats to do so). Another possibility to input known shocks is by using a KeyedArray. The KeyedArray type is provided by the AxisKeys package. A KeyedArray{Union{Nothing,Float64}} can be used where, similar to Matrix{Union{Nothing,Float64}}, all entries of type Float64 are recognised as known shocks and all other entries have to be nothing. Furthermore, in the primary axis a subset of shocks (of type Symbol or String) for which values are specified can be included and all other shocks are considered free. The same goes for the case when using KeyedArray{Float64}} as input, whereas in this case the values for the specified shocks bind for all periods specified in the KeyedArray, because there are no nothing entries permitted with this type.
  • initial_state [Default: [0.0], Type: Union{Vector{Vector{Float64}},Vector{Float64}}]: The initial state defines the starting point for the model. In the case of pruned solution algorithms the initial state can be given as multiple state vectors (Vector{Vector{Float64}}). For multiple state vectors the initial state vectors must be given in deviations from the non-stochastic steady state. In all other cases (incl. for pruned solutions) the initial state must be given in levels. If a pruned solution algorithm is selected and initial_state is a Vector{Float64} then it impacts the first order initial state vector only. The state includes all variables as well as exogenous variables in leads or lags if present. get_irf(𝓂, shocks = :none, variables = :all, periods = 1, levels = true) returns a KeyedArray with all variables in levels. The KeyedArray type is provided by the AxisKeys package.
  • periods [Default: 40, Type: Int]: the total number of periods is the sum of the argument provided here and the maximum of periods of the shocks or conditions argument.
  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • variables [Default: all_excluding_obc]: variables for which to show the results. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). all_excluding_obc contains all shocks less those related to auxiliary variables. all will contain all variables.
  • conditions_in_levels [Default: true, Type: Bool]: indicator whether the conditions are provided in levels. If true the input to the conditions argument will have the relevant steady state subtracted (non-stochastic or stochastic steady state depending on the solution algorithm).
  • levels [Default: false, Type: Bool]: return levels or absolute deviations from the relevant steady state corresponding to the solution algorithm (e.g. stochastic steady state for higher order solution algorithms).
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables and shocks in rows, and periods in columns.

Examples

using MacroModelling
using SparseArrays, AxisKeys

@model RBC_CME begin
    y[0]=A[0]*k[-1]^alpha
    1/c[0]=beta*1/c[1]*(alpha*A[1]*k[0]^(alpha-1)+(1-delta))
    1/c[0]=beta*1/c[1]*(R[0]/Pi[+1])
    R[0] * beta =(Pi[0]/Pibar)^phi_pi
    A[0]*k[-1]^alpha=c[0]+k[0]-(1-delta*z_delta[0])*k[-1]
    z_delta[0] = 1 - rho_z_delta + rho_z_delta * z_delta[-1] + std_z_delta * delta_eps[x]
    A[0] = 1 - rhoz + rhoz * A[-1]  + std_eps * eps_z[x]
end

@parameters RBC_CME  begin
    alpha = .157
    beta = .999
    delta = .0226
    Pibar = 1.0008
    phi_pi = 1.5
    rhoz = .9
    std_eps = .0068
    rho_z_delta = .9
    std_z_delta = .005
end

# c is conditioned to deviate by 0.01 in period 1 and y is conditioned to deviate by 0.02 in period 2
conditions = KeyedArray(Matrix{Union{Nothing,Float64}}(undef,2,2),Variables = [:c,:y], Periods = 1:2)
conditions[1,1] = .01
conditions[2,2] = .02

# in period 2 second shock (eps_z) is conditioned to take a value of 0.05
shocks = Matrix{Union{Nothing,Float64}}(undef,2,1)
shocks[1,1] = .05

get_conditional_forecast(RBC_CME, conditions, shocks = shocks, conditions_in_levels = false)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables_and_shocks ∈ 9-element Vector{Symbol}
→   Periods ∈ 42-element UnitRange{Int64}
And data, 9×42 Matrix{Float64}:
                   (1)            …  (41)            (42)
  (:A)               0.0313639          0.000221372     0.000199235
  (:Pi)              0.000780257       -0.000146071    -0.000140137
  (:R)               0.00117156        -0.000219325    -0.000210417
  (:c)               0.01               0.00213278      0.00203751
  (:k)               0.034584     …     0.0397631       0.0380482
  (:y)               0.0446375          0.00129544      0.001222
  (:z_delta)         0.00025            3.69522e-6      3.3257e-6
  (:delta_eps₍ₓ₎)    0.05               0.0             0.0
  (:eps_z₍ₓ₎)        4.61234            0.0             0.0

The same can be achieved with the other input formats:

# conditions = Matrix{Union{Nothing,Float64}}(undef,7,2)
# conditions[4,1] = .01
# conditions[6,2] = .02

# using SparseArrays
# conditions = spzeros(7,2)
# conditions[4,1] = .01
# conditions[6,2] = .02

# shocks = KeyedArray(Matrix{Union{Nothing,Float64}}(undef,1,1),Variables = [:delta_eps], Periods = [1])
# shocks[1,1] = .05

# using SparseArrays
# shocks = spzeros(2,1)
# shocks[1,1] = .05
source
MacroModelling.get_conditional_variance_decompositionMethod
get_conditional_variance_decomposition(
    𝓂;
    periods,
    parameters,
    steady_state_function,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return the conditional variance decomposition of endogenous variables with regards to the shocks using the linearised solution.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

Keyword Arguments

  • periods [Default: [1:20...,Inf], Type: Union{Vector{Int},Vector{Float64},UnitRange{Int64}}]: vector of periods for which to calculate the conditional variance decomposition. If the vector contains Inf, also the unconditional variance decomposition is calculated (same output as get_variance_decomposition).
  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows, shocks in columns, and periods as the third dimension.

Examples

using MacroModelling

@model RBC_CME begin
    y[0]=A[0]*k[-1]^alpha
    1/c[0]=beta*1/c[1]*(alpha*A[1]*k[0]^(alpha-1)+(1-delta))
    1/c[0]=beta*1/c[1]*(R[0]/Pi[+1])
    R[0] * beta =(Pi[0]/Pibar)^phi_pi
    A[0]*k[-1]^alpha=c[0]+k[0]-(1-delta*z_delta[0])*k[-1]
    z_delta[0] = 1 - rho_z_delta + rho_z_delta * z_delta[-1] + std_z_delta * delta_eps[x]
    A[0] = 1 - rhoz + rhoz * A[-1]  + std_eps * eps_z[x]
end

@parameters RBC_CME  begin
    alpha = .157
    beta = .999
    delta = .0226
    Pibar = 1.0008
    phi_pi = 1.5
    rhoz = .9
    std_eps = .0068
    rho_z_delta = .9
    std_z_delta = .005
end

get_conditional_variance_decomposition(RBC_CME)
# output
3-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 7-element Vector{Symbol}
→   Shocks ∈ 2-element Vector{Symbol}
◪   Periods ∈ 21-element Vector{Float64}
And data, 7×2×21 Array{Float64, 3}:
[showing 3 of 21 slices]
[:, :, 1] ~ (:, :, 1.0):
              (:delta_eps)  (:eps_z)
  (:A)         0.0           1.0
  (:Pi)        0.00158668    0.998413
  (:R)         0.00158668    0.998413
  (:c)         0.0277348     0.972265
  (:k)         0.00869568    0.991304
  (:y)         0.0           1.0
  (:z_delta)   1.0           0.0

[:, :, 11] ~ (:, :, 11.0):
              (:delta_eps)  (:eps_z)
  (:A)         0.0           1.0
  (:Pi)        0.0245641     0.975436
  (:R)         0.0245641     0.975436
  (:c)         0.0175249     0.982475
  (:k)         0.00869568    0.991304
  (:y)         7.63511e-5    0.999924
  (:z_delta)   1.0           0.0

[:, :, 21] ~ (:, :, Inf):
              (:delta_eps)  (:eps_z)
  (:A)         0.0           1.0
  (:Pi)        0.0156771     0.984323
  (:R)         0.0156771     0.984323
  (:c)         0.0134672     0.986533
  (:k)         0.00869568    0.991304
  (:y)         0.000313462   0.999687
  (:z_delta)   1.0           0.0
source
MacroModelling.get_correlationMethod
get_correlation(args; kwargs...)

Return the correlations of endogenous variables using the first, pruned second, or pruned third order perturbation solution.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows and columns.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

get_correlation(RBC)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑠 ∈ 4-element Vector{Symbol}
And data, 4×4 Matrix{Float64}:
        (:c)       (:k)       (:q)       (:z)
  (:c)   1.0        0.999812   0.550168   0.314562
  (:k)   0.999812   1.0        0.533879   0.296104
  (:q)   0.550168   0.533879   1.0        0.965726
  (:z)   0.314562   0.296104   0.965726   1.0
source
MacroModelling.get_dynamic_auxiliary_variablesMethod
get_dynamic_auxiliary_variables(𝓂)

Returns the auxiliary variables, without timing subscripts, part of the augmented system of equations describing the model dynamics. Augmented means that, in case of variables with leads or lags larger than 1, or exogenous shocks with leads or lags, the system is augemented by auxiliary variables containing variables or shocks in lead or lag. Because the original equations included variables with leads or lags certain expression cannot be negative (e.g. given log(c/q) an auxiliary variable is created for c/q).

See get_dynamic_equations for more details on the auxiliary variables and equations.

Arguments

Returns

  • Vector{String} of the auxiliary parameters.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_dynamic_auxiliary_variables(RBC)
# output
3-element Vector{String}:
 "kᴸ⁽⁻²⁾"
 "kᴸ⁽⁻³⁾"
 "kᴸ⁽⁻¹⁾"
source
MacroModelling.get_dynamic_equationsMethod
get_dynamic_equations(𝓂; filter)

Return the augmented system of equations describing the model dynamics. Augmented means that, when variables have leads or lags with absolute value larger than 1, or exogenous shocks have leads or lags, auxiliary equations containing lead/lag variables are added. The augmented system contains only variables in the present [0], future [1], or past [-1]. For example, Δk_4q[0] = log(k[0]) - log(k[-3]) contains k[-3]. Introducing two auxiliary variables (kᴸ⁽⁻¹⁾ and kᴸ⁽⁻²⁾, where denotes the lead/lag operator) and augmenting the system with kᴸ⁽⁻²⁾[0] = kᴸ⁽⁻¹⁾[-1] and kᴸ⁽⁻¹⁾[0] = k[-1] ensures that all timing indices have absolute value at most 1: Δk_4q[0] - (log(k[0]) - log(kᴸ⁽⁻²⁾[-1])).

In case programmatic model writing was used this function returns the parsed equations (see loop over shocks in example).

Note that the output assumes the equations are equal to 0. As in, kᴸ⁽⁻¹⁾[0] - k[-1] implies kᴸ⁽⁻¹⁾[0] - k[-1] = 0 and therefore: kᴸ⁽⁻¹⁾[0] = k[-1].

Arguments

Keyword Arguments

  • filter [Default: nothing, Type: Union{Symbol, String, Nothing}]: filter equations by variable name. Specify a variable name (e.g., :k or "k") to return only equations containing that variable. Optionally include timing (e.g., "k[-1]" or "eps[x]") to match exact timing.

Returns

  • Vector{Expr} of the dynamic model equations as expressions.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_dynamic_equations(RBC)
# output
12-element Vector{Expr}:
 :(1 / c[0] - (β / c[1]) * (α * exp(z{TFP}[1]) * k[0] ^ (α - 1) + (1 - exp(z{δ}[1]) * δ)))
 :((c[0] + k[0]) - ((1 - exp(z{δ}[0]) * δ) * k[-1] + q[0]))
 :(q[0] - exp(z{TFP}[0]) * k[-1] ^ α)
 :(eps_news{TFP}[0] - eps_news{TFP}[x])
 :(z{TFP}[0] - (ρ{TFP} * z{TFP}[-1] + σ{TFP} * (eps{TFP}[x] + eps_news{TFP}[-1])))
 :(eps_news{δ}[0] - eps_news{δ}[x])
 :(z{δ}[0] - (ρ{δ} * z{δ}[-1] + σ{δ} * (eps{δ}[x] + eps_news{δ}[-1])))
 :(Δc_share[0] - (log(c[0] / q[0]) - log(c[-1] / q[-1])))
 :(kᴸ⁽⁻³⁾[0] - kᴸ⁽⁻²⁾[-1])
 :(kᴸ⁽⁻²⁾[0] - kᴸ⁽⁻¹⁾[-1])
 :(kᴸ⁽⁻¹⁾[0] - k[-1])
 :(Δk_4q[0] - (log(k[0]) - log(kᴸ⁽⁻³⁾[-1])))
source
MacroModelling.get_equationsMethod
get_equations(𝓂; filter)

Return the equations of the model. In case programmatic model writing was used this function returns the parsed equations (see loop over shocks in Examples).

Arguments

Keyword Arguments

  • filter [Default: nothing, Type: Union{Symbol, String, Nothing}]: filter equations by variable name. Specify a variable name (e.g., :k or "k") to return only equations containing that variable. Optionally include timing (e.g., "k[-1]" or "eps[x]") to match exact timing.

Returns

  • Vector{Expr} of the parsed equations as expressions.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_equations(RBC)
# output
7-element Vector{Expr}:
 :(1 / c[0] = (β / c[1]) * (α * exp(z{TFP}[1]) * k[0] ^ (α - 1) + (1 - exp(z{δ}[1]) * δ)))
 :(c[0] + k[0] = (1 - exp(z{δ}[0]) * δ) * k[-1] + q[0])
 :(q[0] = exp(z{TFP}[0]) * k[-1] ^ α)
 :(z{TFP}[0] = ρ{TFP} * z{TFP}[-1] + σ{TFP} * (eps{TFP}[x] + eps_news{TFP}[x - 1]))
 :(z{δ}[0] = ρ{δ} * z{δ}[-1] + σ{δ} * (eps{δ}[x] + eps_news{δ}[x - 1]))
 :(Δc_share[0] = log(c[0] / q[0]) - log(c[-1] / q[-1]))
 :(Δk_4q[0] = log(k[0]) - log(k[-4]))
source
MacroModelling.get_estimated_shocksMethod
get_estimated_shocks(
    𝓂,
    data;
    parameters,
    steady_state_function,
    algorithm,
    filter,
    warmup_iterations,
    data_in_levels,
    smooth,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return the estimated shocks based on the inversion filter (depending on the filter keyword argument), or Kalman filter or smoother (depending on the smooth keyword argument) using the provided data and (non-)linear solution of the model. By default MacroModelling chooses the Kalman filter for first order solutions and the inversion filter for higher order ones, and only enables smoothing when the Kalman filter is used. Data is by default assumed to be in levels unless data_in_levels is set to false.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

  • 𝓂: object created by @model and @parameters.
  • data [Type: KeyedArray]: data matrix with variables (String or Symbol) in rows and periods in columns. Periods can have any format and will be used for the output. missing and nothing entries are treated as unobserved observations, and non-finite numeric entries are handled the same way. Interior unobserved entries remain in the sample and are handled period by period using only the available observations, while fully unobserved leading and trailing periods are removed automatically with an informational message. KeyedArray is provided by the AxisKeys package.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • filter [Default: selector that chooses kalman in case algorithm = first_order and :inversion otherwise, Type: Symbol]: filter used to compute the variables and shocks given the data, model, and parameters. The Kalman filter only works for linear problems, whereas the inversion filter (:inversion) works for linear and nonlinear models. If a nonlinear solution algorithm is selected and the default is used, the inversion filter is applied automatically.
  • data_in_levels [Default: true, Type: Bool]: indicator whether the data is provided in levels. If true the input to the data argument will have the non-stochastic steady state subtracted.
  • smooth [Default: selector that enables smoothing when filter = kalman and disables it otherwise, Type: Bool]: whether to return smoothed (true) or filtered (false) shocks/variables. Smoothing is only available for the Kalman filter. The inversion filter only returns filtered shocks/variables, so the default turns smoothing off in that case.
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with shocks in rows, and periods in columns.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

import Random; Random.seed!(3)

simulation = simulate(RBC)

get_estimated_shocks(RBC,simulation([:c],:,:simulate))
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Shocks ∈ 1-element Vector{Symbol}
→   Periods ∈ 40-element UnitRange{Int64}
And data, 1×40 Matrix{Float64}:
               (1)         (2)        …  (39)         (40)
  (:eps_z₍ₓ₎)    0.190898    1.24786       -0.676457    -0.00870749
source
MacroModelling.get_estimated_variable_standard_deviationsMethod
get_estimated_variable_standard_deviations(
    𝓂,
    data;
    parameters,
    steady_state_function,
    data_in_levels,
    smooth,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return the standard deviations of the Kalman smoother or filter (depending on the smooth keyword argument) estimates of the model variables based on the provided data and first order solution of the model. For the default settings this function relies on the Kalman filter and therefore keeps smoothing enabled. Data is by default assumed to be in levels unless data_in_levels is set to false.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

  • 𝓂: object created by @model and @parameters.
  • data [Type: KeyedArray]: data matrix with variables (String or Symbol) in rows and periods in columns. Periods can have any format and will be used for the output. missing and nothing entries are treated as unobserved observations, and non-finite numeric entries are handled the same way. Interior unobserved entries remain in the sample and are handled period by period using only the available observations, while fully unobserved leading and trailing periods are removed automatically with an informational message. KeyedArray is provided by the AxisKeys package.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • data_in_levels [Default: true, Type: Bool]: indicator whether the data is provided in levels. If true the input to the data argument will have the non-stochastic steady state subtracted.
  • smooth [Default: selector that enables smoothing when filter = kalman and disables it otherwise, Type: Bool]: whether to return smoothed (true) or filtered (false) shocks/variables. Smoothing is only available for the Kalman filter. The inversion filter only returns filtered shocks/variables, so the default turns smoothing off in that case.
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with standard deviations in rows, and periods in columns.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

import Random; Random.seed!(3)

simulation = simulate(RBC)

get_estimated_variable_standard_deviations(RBC,simulation([:c],:,:simulate))
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Standard_deviations ∈ 4-element Vector{Symbol}
→   Periods ∈ 40-element UnitRange{Int64}
And data, 4×40 Matrix{Float64}:
        (1)           (2)            …  (39)            (40)
  (:c)    1.31709e-9    1.16415e-10        8.23181e-11     0.0
  (:k)    0.00509299    0.000382934        9.31323e-10     1.6131e-9
  (:q)    0.0612887     0.0046082          9.31323e-10     9.31323e-10
  (:z)    0.00961766    0.000723136        0.0             1.64636e-10
source
MacroModelling.get_estimated_variablesMethod
get_estimated_variables(
    𝓂,
    data;
    parameters,
    steady_state_function,
    algorithm,
    filter,
    warmup_iterations,
    data_in_levels,
    levels,
    smooth,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return the estimated variables (in levels by default, see levels keyword argument) based on the inversion filter (depending on the filter keyword argument), or Kalman filter or smoother (depending on the smooth keyword argument) using the provided data and (non-)linear solution of the model. With the default options the Kalman filter is applied to first order solutions, while the inversion filter is used for higher order methods; smoothing is activated automatically only when the Kalman filter is available. Data is by default assumed to be in levels unless data_in_levels is set to false.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

  • 𝓂: object created by @model and @parameters.
  • data [Type: KeyedArray]: data matrix with variables (String or Symbol) in rows and periods in columns. Periods can have any format and will be used for the output. missing and nothing entries are treated as unobserved observations, and non-finite numeric entries are handled the same way. Interior unobserved entries remain in the sample and are handled period by period using only the available observations, while fully unobserved leading and trailing periods are removed automatically with an informational message. KeyedArray is provided by the AxisKeys package.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • filter [Default: selector that chooses kalman in case algorithm = first_order and :inversion otherwise, Type: Symbol]: filter used to compute the variables and shocks given the data, model, and parameters. The Kalman filter only works for linear problems, whereas the inversion filter (:inversion) works for linear and nonlinear models. If a nonlinear solution algorithm is selected and the default is used, the inversion filter is applied automatically.
  • data_in_levels [Default: true, Type: Bool]: indicator whether the data is provided in levels. If true the input to the data argument will have the non-stochastic steady state subtracted.
  • levels [Default: true, Type: Bool]: return levels or absolute deviations from the relevant steady state corresponding to the solution algorithm (e.g. stochastic steady state for higher order solution algorithms).
  • smooth [Default: selector that enables smoothing when filter = kalman and disables it otherwise, Type: Bool]: whether to return smoothed (true) or filtered (false) shocks/variables. Smoothing is only available for the Kalman filter. The inversion filter only returns filtered shocks/variables, so the default turns smoothing off in that case.
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows, and periods in columns.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

import Random; Random.seed!(3)

simulation = simulate(RBC)

get_estimated_variables(RBC,simulation([:c],:,:simulate))
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   Periods ∈ 40-element UnitRange{Int64}
And data, 4×40 Matrix{Float64}:
        (1)           (2)          …  (39)           (40)
  (:c)    5.94073       5.94913          5.9249         5.92515
  (:k)   47.4339       47.5121          47.2781        47.2796
  (:q)    6.90055       6.97596          6.86123        6.87224
  (:z)    0.00205528    0.0128896       -0.00223228    -0.000533531
source
MacroModelling.get_irfMethod
get_irf(
    𝓂;
    periods,
    algorithm,
    parameters,
    steady_state_function,
    variables,
    shocks,
    negative_shock,
    generalised_irf,
    generalised_irf_warmup_iterations,
    generalised_irf_draws,
    initial_state,
    levels,
    shock_size,
    ignore_obc,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return impulse response functions (IRFs) of the model. By default, the values represent absolute deviations from the relevant steady state (see levels for details). The non-stochastic steady state (NSSS) is relevant for first order solutions and the stochastic steady state for higher order solutions.

If the model contains occasionally binding constraints and ignore_obc = false they are enforced using shocks.

Arguments

Keyword Arguments

  • periods [Default: 40, Type: Int]: number of periods for which to calculate the output. In case a matrix of shocks was provided, periods defines how many periods after the series of shocks the output continues.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • variables [Default: all_excluding_obc]: variables for which to show the results. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). all_excluding_obc contains all shocks less those related to auxiliary variables. all will contain all variables.
  • shocks [Default: all_excluding_obc]: shocks for which to calculate the IRFs. Inputs can be a shock name passed on as either a Symbol or String (e.g. :y, or "y"), or Tuple, Matrix or Vector of String or Symbol. :simulate triggers random draws of all shocks (excluding occasionally binding constraints (obc) related shocks). all_excluding_obc will contain all shocks but not the obc related ones. all will contain also the obc related shocks. A series of shocks can be passed on using either a Matrix{Float64}, or a KeyedArray{Float64} as input with shocks (Symbol or String) in rows and periods in columns. The KeyedArray type is provided by the AxisKeys package. The period of the simulation will correspond to the length of the input in the period dimension + the number of periods defined in periods. If the series of shocks is input as a KeyedArray{Float64} make sure to name the rows with valid shock names of type Symbol. Any shocks not part of the model will trigger a warning. :none in combination with an initial_state can be used for deterministic simulations.
  • negative_shock [Default: false, Type: Bool]: if true, calculates IRFs for a negative shock. Only affects shocks that are not passed on as a Matrix or KeyedArray or set to :none.
  • generalised_irf [Default: false, Type: Bool]: calculate generalised IRFs. Relevant for nonlinear (higher order perturbation) solutions only. Reference steady state for deviations is the stochastic steady state. initial_state has no effect on generalised IRFs. Occasionally binding constraint are not respected for generalised IRF.
  • generalised_irf_warmup_iterations [Default: 100, Type: Int]: number of warm-up iterations used to draw the baseline paths in the generalised IRF simulation. Only applied when generalised_irf = true.
  • generalised_irf_draws [Default: 50, Type: Int]: number of Monte Carlo draws used to compute the generalised IRF. Only applied when generalised_irf = true.
  • initial_state [Default: [0.0], Type: Union{Vector{Vector{Float64}},Vector{Float64}}]: The initial state defines the starting point for the model. In the case of pruned solution algorithms the initial state can be given as multiple state vectors (Vector{Vector{Float64}}). For multiple state vectors the initial state vectors must be given in deviations from the non-stochastic steady state. In all other cases (incl. for pruned solutions) the initial state must be given in levels. If a pruned solution algorithm is selected and initial_state is a Vector{Float64} then it impacts the first order initial state vector only. The state includes all variables as well as exogenous variables in leads or lags if present. get_irf(𝓂, shocks = :none, variables = :all, periods = 1, levels = true) returns a KeyedArray with all variables in levels. The KeyedArray type is provided by the AxisKeys package.
  • levels [Default: false, Type: Bool]: return levels or absolute deviations from the relevant steady state corresponding to the solution algorithm (e.g. stochastic steady state for higher order solution algorithms).
  • shock_size [Default: 1, Type: Real]: size of the shocks in standard deviations. Only affects shocks that are not passed on as a Matrix or KeyedArray or set to :none. A negative value will flip the sign of the shock.
  • ignore_obc [Default: false, Type: Bool]: solve the model ignoring the occasionally binding constraints.
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows, periods in columns, and shocks as the third dimension.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

get_irf(RBC)
# output
3-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   Periods ∈ 40-element UnitRange{Int64}
◪   Shocks ∈ 1-element Vector{Symbol}
And data, 4×40×1 Array{Float64, 3}:
[:, :, 1] ~ (:, :, :eps_z):
        (1)           (2)           …  (39)            (40)
  (:c)    0.00674687    0.00729773        0.00146962      0.00140619
  (:k)    0.0620937     0.0718322         0.0146789       0.0140453
  (:q)    0.0688406     0.0182781         0.00111425      0.00106615
  (:z)    0.01          0.002             2.74878e-29     5.49756e-30
source
MacroModelling.get_jump_variablesMethod
get_jump_variables(𝓂)

Returns the jump variables of the model. Jump variables occur in the future and not in the past or occur in all three: past, present, and future.

In case programmatic model writing was used this function returns the parsed variables (see z in example).

Arguments

Returns

  • Vector{String} of the jump variables.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_jump_variables(RBC)
# output
3-element Vector{String}:
 "c"
 "z{TFP}"
 "z{δ}"
source
MacroModelling.get_loglikelihoodMethod
get_loglikelihood(
    𝓂,
    data,
    parameter_values;
    steady_state_function,
    algorithm,
    filter,
    on_failure_loglikelihood,
    warmup_iterations,
    presample_periods,
    initial_covariance,
    filter_algorithm,
    tol,
    quadratic_matrix_equation_algorithm,
    lyapunov_algorithm,
    sylvester_algorithm,
    verbose,
    caching,
    use_workspaces
)

Return the loglikelihood of the model given the data and parameters provided. The loglikelihood is either calculated based on the inversion or the Kalman filter (depending on the filter keyword argument). By default the package selects the Kalman filter for first order solutions and the inversion filter for nonlinear (higher order) solution algorithms. The data must be provided as a KeyedArray{Float64} with the names of the variables to be matched in rows and the periods in columns. The KeyedArray type is provided by the AxisKeys package.

This function is differentiable and supports both the Kalman and inversion likelihoods.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

  • 𝓂: object created by @model and @parameters.
  • data [Type: KeyedArray]: data matrix with variables (String or Symbol) in rows and periods in columns. Periods can have any format and will be used for the output. missing and nothing entries are treated as unobserved observations, and non-finite numeric entries are handled the same way. Interior unobserved entries remain in the sample and are handled period by period using only the available observations, while fully unobserved leading and trailing periods are removed automatically with an informational message. KeyedArray is provided by the AxisKeys package.
  • parameter_values [Type: Vector]: Parameter values.

Keyword Arguments

  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • filter [Default: selector that chooses kalman in case algorithm = first_order and :inversion otherwise, Type: Symbol]: filter used to compute the variables and shocks given the data, model, and parameters. The Kalman filter only works for linear problems, whereas the inversion filter (:inversion) works for linear and nonlinear models. If a nonlinear solution algorithm is selected and the default is used, the inversion filter is applied automatically.
  • warmup_iterations [Default: 0, Type: Int]: number of warmup periods added before the first observation, used to reduce sensitivity to the initial state. For the inversion filter, values greater than 1 add hidden warmup shocks before the first observation; warmup_iterations = 1 leaves the result unchanged (same as 0). Ignored for the Kalman filter (forced to 0).
  • presample_periods [Default: 0, Type: Int]: periods at the beginning of the retained data sample for which the loglikelihood is discarded. Values above the retained sample length are clamped down automatically with an informational message.
  • initial_covariance [Default: :theoretical, Type: Union{Symbol,AbstractMatrix{<:Real}}]: defines the method to initialise the Kalman filters covariance matrix. It can be initialised with the theoretical long run values (option :theoretical), large values (10.0) along the diagonal (option :diagonal), or a user-supplied matrix of appropriate size (number of observables and states).
  • initial_state [Default: [0.0], Type: Union{Vector{Vector{Float64}},Vector{Float64}}]: The initial state defines the starting point for the model. In the case of pruned solution algorithms the initial state can be given as multiple state vectors (Vector{Vector{Float64}}). For multiple state vectors the initial state vectors must be given in deviations from the non-stochastic steady state. In all other cases (incl. for pruned solutions) the initial state must be given in levels. If a pruned solution algorithm is selected and initial_state is a Vector{Float64} then it impacts the first order initial state vector only. The state includes all variables as well as exogenous variables in leads or lags if present. get_irf(𝓂, shocks = :none, variables = :all, periods = 1, levels = true) returns a KeyedArray with all variables in levels. The KeyedArray type is provided by the AxisKeys package.
  • on_failure_loglikelihood [Default: -Inf, Type: AbstractFloat]: value to return if the loglikelihood calculation fails. Setting this to a finite value can avoid errors in codes that rely on finite loglikelihood values, such as e.g. slice samplers (in Pigeons.jl).
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • <:AbstractFloat loglikelihood

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

import Random; Random.seed!(3)

simulated_data = simulate(RBC)

get_loglikelihood(RBC, simulated_data([:k], :, :simulate), RBC.parameter_values)
# output
53.76735680353869
source
MacroModelling.get_loglikelihoodMethod
get_loglikelihood(
    𝓂,
    data,
    parameter_values,
    shocks,
    measurement_error_std;
    steady_state_function,
    algorithm,
    warmup_iterations,
    on_failure_loglikelihood,
    tol,
    quadratic_matrix_equation_algorithm,
    lyapunov_algorithm,
    sylvester_algorithm,
    verbose,
    caching,
    use_workspaces
)

Return the filter-free loglikelihood of the model given the data, parameters, and a path of latent structural shocks. Unlike get_loglikelihood — which integrates out the latent shocks via a Kalman or inversion filter — this function evaluates the joint likelihood of data and shocks by forward-simulating the model with the supplied shocks and comparing the implied observable path to the data under a Gaussian measurement-error model. This is the building block needed to estimate (potentially nonlinear, non-Gaussian) DSGE models with HMC samplers by treating the shocks as additional latent parameters (Childers, Fernández-Villaverde, Perla, Rackauckas & Wu, 2025).

Only the measurement part of the joint loglikelihood is returned. The prior on the shocks (typically standard Normal) and the prior on measurement_error_std are expected to be declared by the user in their probabilistic-programming model.

The filter-free primal path and its analytical reverse-mode rrule are implemented for :first_order, :second_order, :pruned_second_order, :third_order, and :pruned_third_order.

Arguments

  • 𝓂: object created by @model and @parameters.
  • data [Type: KeyedArray]: data matrix with variables (String or Symbol) in rows and periods in columns. Periods can have any format and will be used for the output. missing and nothing entries are treated as unobserved observations, and non-finite numeric entries are handled the same way. Interior unobserved entries remain in the sample and are handled period by period using only the available observations, while fully unobserved leading and trailing periods are removed automatically with an informational message. KeyedArray is provided by the AxisKeys package.
  • If fully unobserved leading or trailing periods are discarded, any separately supplied shock path and any matrix-valued measurement_error_std input are aligned to the retained sample automatically.
  • parameter_values [Type: Vector]: Parameter values.
  • shocks [Type: AbstractMatrix]: Matrix of latent structural shocks with shape nExo × (T + max(warmup_iterations - 1, 0)), where T matches the number of observations in data. When warmup_iterations > 1, the leading warmup_iterations - 1 shock columns are used only to warm the latent state before the first scored observation.
  • measurement_error_std [Type: Real, AbstractVector, or AbstractMatrix]: Standard deviation(s) of the Gaussian measurement error added to each observable. Pass a scalar to use the same measurement-error std-dev on every observable, a vector of length equal to the number of observables for observable-specific std-devs, or a matrix of shape (n_observables, n_periods) for period-specific measurement error. If fully unobserved leading or trailing periods are discarded, matrix-valued inputs are trimmed to the same retained sample automatically.

Keyword Arguments

  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: :second_order, Type: Symbol]: solution algorithm. Supported perturbation algorithms are :first_order, :second_order, :pruned_second_order, :third_order, and :pruned_third_order.
  • warmup_iterations [Default: DEFAULT_WARMUP_ITERATIONS, Type: Int]: Number of filter-style warmup iterations. In the filter-free case this prepends max(warmup_iterations - 1, 0) latent-shock periods before the first scored observation.
  • initial_state [Default: [0.0], Type: Union{Vector{Vector{Float64}},Vector{Float64}}]: The initial state defines the starting point for the model. In the case of pruned solution algorithms the initial state can be given as multiple state vectors (Vector{Vector{Float64}}). For multiple state vectors the initial state vectors must be given in deviations from the non-stochastic steady state. In all other cases (incl. for pruned solutions) the initial state must be given in levels. If a pruned solution algorithm is selected and initial_state is a Vector{Float64} then it impacts the first order initial state vector only. The state includes all variables as well as exogenous variables in leads or lags if present. get_irf(𝓂, shocks = :none, variables = :all, periods = 1, levels = true) returns a KeyedArray with all variables in levels. The KeyedArray type is provided by the AxisKeys package.
  • on_failure_loglikelihood [Default: -Inf, Type: AbstractFloat]: value to return if the loglikelihood calculation fails (e.g. solution did not converge or measurement-error std-dev is non-positive).
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • <:AbstractFloat loglikelihood
source
MacroModelling.get_missing_parametersMethod
get_missing_parameters(𝓂)

Returns the parameters which are required by the model but have not been assigned values in the @parameters block. These parameters must be provided via the parameters keyword argument in functions like get_irf, get_SS, simulate, etc. before the model can be solved.

Arguments

Returns

  • Vector{String} of the missing parameters.

Examples

using MacroModelling

@model RBC_incomplete begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC_incomplete  report_missing_parameters = false begin
    std_z = 0.01
    ρ = 0.2
    # Note: α, β, δ are not defined
end

get_missing_parameters(RBC_incomplete)
# output
3-element Vector{String}:
 "α"
 "β"
 "δ"
source
MacroModelling.get_model_estimatesMethod
get_model_estimates(
    𝓂,
    data;
    parameters,
    steady_state_function,
    algorithm,
    filter,
    warmup_iterations,
    data_in_levels,
    levels,
    smooth,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return the vertical concatenation of get_estimated_variables and get_estimated_shocks as a single KeyedArray with a common first axis named Estimates and the second axis Periods. Variables appear first, followed by shocks.

All keyword arguments are forwarded to the respective functions. See the docstrings of get_estimated_variables and get_estimated_shocks for details.

Arguments

  • 𝓂: object created by @model and @parameters.
  • data [Type: KeyedArray]: data matrix with variables (String or Symbol) in rows and periods in columns. Periods can have any format and will be used for the output. missing and nothing entries are treated as unobserved observations, and non-finite numeric entries are handled the same way. Interior unobserved entries remain in the sample and are handled period by period using only the available observations, while fully unobserved leading and trailing periods are removed automatically with an informational message. KeyedArray is provided by the AxisKeys package.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • filter [Default: selector that chooses kalman in case algorithm = first_order and :inversion otherwise, Type: Symbol]: filter used to compute the variables and shocks given the data, model, and parameters. The Kalman filter only works for linear problems, whereas the inversion filter (:inversion) works for linear and nonlinear models. If a nonlinear solution algorithm is selected and the default is used, the inversion filter is applied automatically.
  • data_in_levels [Default: true, Type: Bool]: indicator whether the data is provided in levels. If true the input to the data argument will have the non-stochastic steady state subtracted.
  • levels [Default: true, Type: Bool]: return levels or absolute deviations from the relevant steady state corresponding to the solution algorithm (e.g. stochastic steady state for higher order solution algorithms).
  • smooth [Default: selector that enables smoothing when filter = kalman and disables it otherwise, Type: Bool]: whether to return smoothed (true) or filtered (false) shocks/variables. Smoothing is only available for the Kalman filter. The inversion filter only returns filtered shocks/variables, so the default turns smoothing off in that case.
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables followed by shocks in rows, and periods in columns.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

import Random; Random.seed!(3)

simulation = simulate(RBC)

get_model_estimates(RBC,simulation([:c],:,:simulate))
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables_and_shocks ∈ 5-element Vector{Symbol}
→   Periods ∈ 40-element UnitRange{Int64}
And data, 5×40 Matrix{Float64}:
               (1)           (2)          …  (39)           (40)
  (:c)           5.94073       5.94913          5.9249         5.92515
  (:k)          47.4339       47.5121          47.2781        47.2796
  (:q)           6.90055       6.97596          6.86123        6.87224
  (:z)           0.00205528    0.0128896       -0.00223228    -0.000533531
  (:eps_z₍ₓ₎)    0.190898      1.24786    …    -0.676457      -0.00870749
source
MacroModelling.get_momentsMethod
get_moments(
    𝓂;
    parameters,
    steady_state_function,
    non_stochastic_steady_state,
    mean,
    standard_deviation,
    variance,
    covariance,
    correlation,
    variables,
    derivatives,
    parameter_derivatives,
    algorithm,
    silent,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    verbose,
    tol,
    caching,
    use_workspaces
)

Return the first and second moments of endogenous variables using the first, pruned second, or pruned third order perturbation solution. By default returns: non-stochastic steady state (NSSS), and standard deviations, but can optionally return variances, covariance matrix, and correlation matrix. Derivatives of the moments can also be provided by setting derivatives to true.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • non_stochastic_steady_state [Default: true, Type: Bool]: switch to return SS of endogenous variables
  • mean [Default: false, Type: Bool]: switch to return mean of endogenous variables (the mean for the linearised solutoin is the NSSS)
  • standard_deviation [Default: true, Type: Bool]: switch to return standard deviation of endogenous variables
  • variance [Default: false, Type: Bool]: switch to return variance of endogenous variables
  • covariance [Default: false, Type: Bool]: switch to return covariance matrix of endogenous variables
  • correlation [Default: false, Type: Bool]: switch to return correlation matrix of endogenous variables
  • variables [Default: all_excluding_obc]: variables for which to show the results. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). all_excluding_obc contains all shocks less those related to auxiliary variables. all will contain all variables.
  • derivatives [Default: true, Type: Bool]: calculate derivatives with respect to the parameters.
  • parameter_derivatives [Default: :all]: parameters for which to calculate partial derivatives. Inputs can be a parameter name passed on as either a Symbol or String (e.g. :alpha, or "alpha"), or Tuple, Matrix or Vector of String or Symbol. :all will include all parameters.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • Dict{Symbol,KeyedArray} containing the selected moments. All moments have variables as rows and the moment as the first column followed by partial derivatives wrt parameters. Covariance and correlation matrices are returned as 2D KeyedArrays (or 3D when derivatives = true). The KeyedArray type is provided by the AxisKeys package.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

moments = get_moments(RBC);

moments[:non_stochastic_steady_state]
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   Steady_state_and_∂steady_state∂parameter ∈ 6-element Vector{Symbol}
And data, 4×6 Matrix{Float64}:
        (:Steady_state)  (:std_z)  (:ρ)     (:δ)      (:α)       (:β)
  (:c)   5.93625          0.0       0.0   -116.072    55.786     76.1014
  (:k)  47.3903           0.0       0.0  -1304.95    555.264   1445.93
  (:q)   6.88406          0.0       0.0    -94.7805   66.8912   105.02
  (:z)   0.0              0.0       0.0      0.0       0.0        0.0
moments[:standard_deviation]
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   Standard_deviation_and_∂standard_deviation∂parameter ∈ 6-element Vector{Symbol}
And data, 4×6 Matrix{Float64}:
        (:Standard_deviation)  (:std_z)  …  (:δ)       (:α)       (:β)
  (:c)   0.0266642              2.66642     -0.384359   0.2626     0.144789
  (:k)   0.264677              26.4677      -5.74194    2.99332    6.30323
  (:q)   0.0739325              7.39325     -0.974722   0.726551   1.08
  (:z)   0.0102062              1.02062      0.0        0.0        0.0

Correlation matrix (returned when correlation = true):

get_moments(RBC, non_stochastic_steady_state = false, standard_deviation = false, correlation = true, derivatives = false)[:correlation]
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   𝑉𝑎𝑟𝑖𝑎𝑏𝑙𝑒𝑠 ∈ 4-element Vector{Symbol}
And data, 4×4 Matrix{Float64}:
        (:c)       (:k)       (:q)       (:z)
  (:c)   1.0        0.999812   0.550168   0.314562
  (:k)   0.999812   1.0        0.533879   0.296104
  (:q)   0.550168   0.533879   1.0        0.965726
  (:z)   0.314562   0.296104   0.965726   1.0
source
MacroModelling.get_non_stochastic_steady_state_residualsMethod
get_non_stochastic_steady_state_residuals(
    𝓂,
    values;
    parameters,
    steady_state_function,
    tol,
    verbose,
    caching,
    use_workspaces
)

Calculate the residuals of the non-stochastic steady state equations of the model for a given set of values. Values not provided, will be filled with the non-stochastic steady state values corresponding to the current parameters.

Arguments

  • 𝓂: object created by @model and @parameters.
  • values [Type: Union{Vector{Float64}, Dict{Symbol, Float64}, Dict{String, Float64}, KeyedArray{Float64, 1}}]: A Vector, Dict, or KeyedArray containing the values of the variables and calibrated parameters in the non-stochastic steady state equations (including calibration equations). The KeyedArray type is provided by the AxisKeys package.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) containing the absolute values of the residuals of the non-stochastic steady state equations.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    k[ss] / q[ss] = 2.5 | α
    β = 0.95
end

steady_state = SS(RBC, derivatives = false)

get_non_stochastic_steady_state_residuals(RBC, steady_state)
# output
1-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Equation ∈ 5-element Vector{Symbol}
And data, 5-element Vector{Float64}:
 (:Equation₁)             0.0
 (:Equation₂)             0.0
 (:Equation₃)             0.0
 (:Equation₄)             0.0
 (:CalibrationEquation₁)  0.0

Passing approximate values returns the residuals at those values:

get_non_stochastic_steady_state_residuals(RBC, [1.1641597, 3.0635781, 1.2254312, 0.0, 0.18157895])
# 1-dimensional KeyedArray(NamedDimsArray(...)) with keys:
# ↓   Equation ∈ 5-element Vector{Symbol}
# And data, 5-element Vector{Float64}:
#  (:Equation₁)             2.7360991250446887e-10
#  (:Equation₂)             6.199999980083248e-8
#  (:Equation₃)             2.7897102183871425e-8
#  (:Equation₄)             0.0
#  (:CalibrationEquation₁)  8.160392850342646e-8
source
MacroModelling.get_nonnegativity_auxiliary_variablesMethod
get_nonnegativity_auxiliary_variables(𝓂)

Returns the auxiliary variables, without timing subscripts, added to the non-stochastic steady state problem because certain expression cannot be negative (e.g. given log(c/q) an auxiliary variable is created for c/q).

See get_steady_state_equations for more details on the auxiliary variables and equations.

Arguments

Returns

  • Vector{String} of the auxiliary parameters.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_nonnegativity_auxiliary_variables(RBC)
# output
3-element Vector{String}:
 "➕₁"
 "➕₂"
 "➕₃"
source
MacroModelling.get_parametersMethod
get_parameters(𝓂; values)

Returns the parameters (and optionally the values) which have an impact on the model dynamics but do not depend on other parameters and are not determined by calibration equations.

In case programmatic model writing was used this function returns the parsed parameters (see σ in Examples).

Arguments

Keyword Arguments

  • values [Default: false, Type: Bool]: return the values together with the parameter names.

Returns

  • Vector{String} of the parameters or Vector{Pair{String, Float64}} of parameters and values if values is set to true.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_parameters(RBC)
# output
7-element Vector{String}:
 "σ{TFP}"
 "σ{δ}"
 "ρ{TFP}"
 "ρ{δ}"
 "capital_to_output"
 "alpha"
 "β"
source
MacroModelling.get_parameters_defined_by_parametersMethod
get_parameters_defined_by_parameters(𝓂)

Returns the parameters which are defined by other parameters which are not necessarily used in the equations of the model (see α in Examples).

Arguments

Returns

  • Vector{String} of the parameters.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_parameters_defined_by_parameters(RBC)
# output
1-element Vector{String}:
 "α"
source
MacroModelling.get_parameters_defining_parametersMethod
get_parameters_defining_parameters(𝓂)

Returns the parameters which define other parameters in the @parameters block which are not necessarily used in the equations of the model (see alpha in Examples).

Arguments

Returns

  • Vector{String} of the parameters.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_parameters_defining_parameters(RBC)
# output
1-element Vector{String}:
 "alpha"
source
MacroModelling.get_parameters_in_equationsMethod
get_parameters_in_equations(𝓂)

Returns the parameters contained in the model equations. Note that these parameters might be determined by other parameters or calibration equations defined in the @parameters block.

In case programmatic model writing was used this function returns the parsed parameters (see σ in Examples).

Arguments

Returns

  • Vector{String} of the parameters.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_parameters_in_equations(RBC)
# output
7-element Vector{String}:
 "α"
 "β"
 "δ"
 "ρ{TFP}"
 "ρ{δ}"
 "σ{TFP}"
 "σ{δ}"
source
MacroModelling.get_revision_historyMethod
get_revision_history(𝓂)

Return the recorded history of equation modifications for the model.

Each entry is a NamedTuple with the fields:

  • timestamp: the DateTime when the modification was applied.
  • action: one of :update_equation, :add_equation, :remove_equation, :update_calibration_equation, :add_calibration_equation, :remove_calibration_equation.
  • equation_index: the 1-based index that was affected.
  • old_equation: the previous equation as an Expr, or nothing for additions.
  • new_equation: the new equation as an Expr, or nothing for removals.

The list is append-only and ordered chronologically.

Arguments

Returns

  • Vector{RevisionEntry} — a copy of the model's revision history.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

add_equation!(RBC, :(y[0] = c[0] + k[0] - (1 - δ) * k[-1]))
get_revision_history(RBC)
# output
1-element Vector{@NamedTuple{timestamp::Dates.DateTime, action::Symbol, equation_index::Union{Nothing, Int64}, old_equation::Union{Nothing, Expr}, new_equation::Union{Nothing, Expr}}}:
 (timestamp = ..., action = :add_equation, equation_index = 5, old_equation = nothing, new_equation = :(y[0] = (c[0] + k[0]) - (1 - δ) * k[-1]))
source
MacroModelling.get_shock_decompositionMethod
get_shock_decomposition(
    𝓂,
    data;
    parameters,
    steady_state_function,
    algorithm,
    filter,
    data_in_levels,
    warmup_iterations,
    smooth,
    marginal_contribution,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return the shock decomposition in absolute deviations from the relevant steady state. The non-stochastic steady state (NSSS) is relevant for first order solutions and the stochastic steady state for higher order solutions. The deviations are based on the Kalman smoother or filter (depending on the smooth keyword argument) or inversion filter using the provided data and solution of the model. When the defaults are used, the filter is selected automatically—Kalman for first order solutions and inversion otherwise—and smoothing is only enabled when the Kalman filter is active. Data is by default assumed to be in levels unless data_in_levels is set to false.

In case of pruned second and pruned third order perturbation algorithms the decomposition additionally contains a term Nonlinearities. This term represents the nonlinear interaction between the states in the periods after the shocks arrived and in the case of pruned third order, the interaction between (pruned second order) states and contemporaneous shocks.

Setting marginal_contribution = true (only meaningful for :pruned_second_order and :pruned_third_order) instead replaces the separate Nonlinearities column by an Aumann–Shapley allocation of the incremental response above the zero-shock path, using the path-integral identity with Gauss–Legendre quadrature. Equivalently, each shock's column carries its standalone effect plus its marginal-contribution share of the cross-shock interaction, while Initial_values remains separate. The implementation starts from the low-order Gauss–Legendre rule (2 * nᵉ propagations at second order, 3 * nᵉ at third order) and incrementally reruns with up to 7 nodes when the relative Shapley-efficiency closure error exceeds 1e-3. For first-order solutions the option has no effect (silent fallback).

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

  • 𝓂: object created by @model and @parameters.
  • data [Type: KeyedArray]: data matrix with variables (String or Symbol) in rows and periods in columns. Periods can have any format and will be used for the output. missing and nothing entries are treated as unobserved observations, and non-finite numeric entries are handled the same way. Interior unobserved entries remain in the sample and are handled period by period using only the available observations, while fully unobserved leading and trailing periods are removed automatically with an informational message. KeyedArray is provided by the AxisKeys package.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • filter [Default: selector that chooses kalman in case algorithm = first_order and :inversion otherwise, Type: Symbol]: filter used to compute the variables and shocks given the data, model, and parameters. The Kalman filter only works for linear problems, whereas the inversion filter (:inversion) works for linear and nonlinear models. If a nonlinear solution algorithm is selected and the default is used, the inversion filter is applied automatically.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • data_in_levels [Default: true, Type: Bool]: indicator whether the data is provided in levels. If true the input to the data argument will have the non-stochastic steady state subtracted.
  • smooth [Default: selector that enables smoothing when filter = kalman and disables it otherwise, Type: Bool]: whether to return smoothed (true) or filtered (false) shocks/variables. Smoothing is only available for the Kalman filter. The inversion filter only returns filtered shocks/variables, so the default turns smoothing off in that case.
  • marginal_contribution [Default: false, Type: Bool]: if true and the algorithm is :pruned_second_order or :pruned_third_order, replace the separate Nonlinearities column by an Aumann–Shapley allocation across shock columns while keeping Initial_values separate.
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows, shocks in columns, and periods as the third dimension.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

import Random; Random.seed!(3)

simulation = simulate(RBC)

get_shock_decomposition(RBC,simulation([:c],:,:simulate))
# output
3-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 4-element Vector{Symbol}
→   Shocks ∈ 2-element Vector{Symbol}
◪   Periods ∈ 40-element UnitRange{Int64}
And data, 4×2×40 Array{Float64, 3}:
[showing 3 of 40 slices]
[:, :, 1] ~ (:, :, 1):
        (:eps_z₍ₓ₎)  (:Initial_values)
  (:c)   0.00128797   0.00319151
  (:k)   0.0118536    0.0318
  (:q)   0.0131415    0.00335202
  (:z)   0.00190898   0.000146294

[:, :, 21] ~ (:, :, 21):
        (:eps_z₍ₓ₎)  (:Initial_values)
  (:c)  -0.0428897    0.00132724
  (:k)  -0.425096     0.0132567
  (:q)  -0.0721742    0.00100629
  (:z)  -0.00622294   1.73472e-18

[:, :, 40] ~ (:, :, 40):
        (:eps_z₍ₓ₎)   (:Initial_values)
  (:c)  -0.0116806     0.000573923
  (:k)  -0.116386      0.00573246
  (:q)  -0.012256      0.00043514
  (:z)  -0.000533531   1.0842e-19
source
MacroModelling.get_shocksMethod
get_shocks(𝓂)

Returns the exogenous shocks.

In case programmatic model writing was used this function returns the parsed variables (see eps in example).

Arguments

Returns

  • Vector{String} of the exogenous shocks.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_shocks(RBC)
# output
4-element Vector{String}:
 "eps_news{TFP}"
 "eps_news{δ}"
 "eps{TFP}"
 "eps{δ}"
source
MacroModelling.get_solutionMethod
get_solution(
    𝓂;
    parameters,
    steady_state_function,
    algorithm,
    silent,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    caching,
    use_workspaces
)

Return the solution of the model. In the linear case it returns the non-stochastic steady state (NSSS) followed by the linearised solution of the model. In the nonlinear case (higher order perturbation) the function returns a multidimensional array with the endogenous variables as the second dimension and the state variables, shocks, and perturbation parameter (:Volatility) as the other dimensions.

The values of the output represent the NSSS in the case of a linear solution and below it the effect that deviations from the NSSS of the respective past states, shocks, and perturbation parameter have (perturbation parameter = 1) on the present value (NSSS deviation) of the model variables.

Arguments

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with the endogenous variables including the auxiliary endogenous and exogenous variables (due to leads and lags > 1) as columns. The rows and other dimensions (depending on the chosen perturbation order) include the NSSS for the linear case only, followed by the states, and exogenous shocks. Subscripts following variable names indicate the timing (e.g. variable₍₋₁₎ indicates the variable being in the past). Superscripts indicate leads or lags (e.g. variableᴸ⁽²⁾ indicates the variable being in lead by two periods). If no super- or subscripts follow the variable name, the variable is in the present.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

get_solution(RBC)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Steady_state__States__Shocks ∈ 4-element Vector{Symbol}
→   Variables ∈ 4-element Vector{Symbol}
And data, 4×4 adjoint(::Matrix{Float64}) with eltype Float64:
                   (:c)         (:k)        (:q)        (:z)
  (:Steady_state)   5.93625     47.3903      6.88406     0.0
  (:k₍₋₁₎)          0.0957964    0.956835    0.0726316   0.0
  (:z₍₋₁₎)          0.134937     1.24187     1.37681     0.2
  (:eps_z₍ₓ₎)       0.00674687   0.0620937   0.0688406   0.01
source
MacroModelling.get_state_variablesMethod
get_state_variables(𝓂)

Returns the state variables of the model. State variables occur in the past and not in the future or occur in all three: past, present, and future.

In case programmatic model writing was used this function returns the parsed variables (see z in example).

Arguments

Returns

  • Vector{String} of the state variables.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_state_variables(RBC)
# output
10-element Vector{String}:
 "c"
 "eps_news{TFP}"
 "eps_news{δ}"
 "k"
 "kᴸ⁽⁻²⁾"
 "kᴸ⁽⁻³⁾"
 "kᴸ⁽⁻¹⁾"
 "q"
 "z{TFP}"
 "z{δ}"
source
MacroModelling.get_statisticsMethod
get_statistics(
    𝓂,
    parameter_values;
    parameters,
    steady_state_function,
    non_stochastic_steady_state,
    mean,
    standard_deviation,
    variance,
    covariance,
    correlation,
    autocorrelation,
    autocorrelation_periods,
    algorithm,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    lyapunov_algorithm,
    verbose,
    tol,
    caching,
    use_workspaces
)

Return the first and second moments of endogenous variables using either the linearised solution or the pruned second or pruned third order perturbation solution. By default returns a Dict with: non-stochastic steady state (NSSS), and standard deviations, but can also return variances, covariance matrix, and correlation matrix. Values are returned in the order given for the specific moment. Function to use when differentiating model moments with respect to parameters.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

  • 𝓂: object created by @model and @parameters.
  • parameter_values [Type: Vector]: Parameter values. If parameter_names is not explicitly defined, parameter_values are assumed to correspond to the parameters and the order of the parameters declared in the @parameters block.

Keyword Arguments

  • parameters [Type: Vector{Symbol}]: Corresponding names in the same order as parameter_values.
  • non_stochastic_steady_state [Default: Symbol[], Type: Union{Symbol_input,String_input}]: variables for which to show the NSSS of selected variables. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. :all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). :all_excluding_obc contains all shocks less those related to auxiliary variables. :all will contain all variables.
  • mean [Default: Symbol[], Type: Union{Symbol_input,String_input}]: variables for which to show the mean of selected variables (the mean for the linearised solution is the NSSS). Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. :all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). :all_excluding_obc contains all shocks less those related to auxiliary variables. :all will contain all variables.
  • standard_deviation [Default: Symbol[], Type: Union{Symbol_input,String_input}]: variables for which to show the standard deviation of selected variables. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. :all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). :all_excluding_obc contains all shocks less those related to auxiliary variables. :all will contain all variables.
  • variance [Default: Symbol[], Type: Union{Symbol_input,String_input}]: variables for which to show the variance of selected variables. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. :all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). :all_excluding_obc contains all shocks less those related to auxiliary variables. :all will contain all variables.
  • covariance [Default: Symbol[], Type: Union{Symbol_input,String_input}]: variables for which to show the covariance of selected variables. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. For grouped covariance computation, pass a Vector of Vectors (e.g. [[:y, :c], [:k, :i]]) to compute covariances only within each group, returning a single covariance matrix where cross-group covariances are set to zero. This allows more granular control over which covariances to compute. Any variables not part of the model will trigger a warning. :all_excluding_auxiliary_and_obc contains all variables less those related to auxiliary variables and related to occasionally binding constraints (obc). :all_excluding_obc contains all variables less those related to occasionally binding constraints. :all will contain all variables.
  • correlation [Default: Symbol[], Type: Union{Symbol_input,String_input}]: variables for which to show the correlation matrix of selected variables. Inputs follow the same format as covariance, including grouped input (e.g. [[:y, :c], [:k, :i]]) which restricts the returned matrix to within-group correlations and sets cross-group entries to zero. Variables with non-positive variance produce NaN entries (left unchanged). :all_excluding_auxiliary_and_obc contains all variables less those related to auxiliary variables and related to occasionally binding constraints (obc). :all_excluding_obc contains all variables less those related to occasionally binding constraints. :all will contain all variables.
  • autocorrelation [Default: Symbol[], Type: Union{Symbol_input,String_input}]: variables for which to show the autocorrelation of selected variables. Inputs can be a variable name passed on as either a Symbol or String (e.g. :y or "y"), or Tuple, Matrix or Vector of String or Symbol. Any variables not part of the model will trigger a warning. :all_excluding_auxiliary_and_obc contains all shocks less those related to auxiliary variables and related to occasionally binding constraints (obc). :all_excluding_obc contains all shocks less those related to auxiliary variables. :all will contain all variables.
  • autocorrelation_periods [Default: 1:5, Type = UnitRange{Int}]: periods for which to return the autocorrelation of selected variables
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • Dict with the name of the statistics and the corresponding vectors (NSSS, mean, standard deviation, variance) or matrices (covariance, correlation, autocorrelation).

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

get_statistics(RBC, RBC.parameter_values, standard_deviation = get_variables(RBC))
# output
Dict{Symbol, AbstractArray{Float64}} with 1 entry:
  :standard_deviation => [0.0266642, 0.264677, 0.0739325, 0.0102062]

For grouped covariance (computing covariances only within specified groups; cross-group entries are set to zero):

get_statistics(RBC, RBC.parameter_values, covariance = [[:c, :k], [:q, :z]])
# Dict{Symbol, AbstractArray{Float64}} with 1 entry:
#   :covariance => [0.00071098 0.00705609 0.0 0.0; 0.0 0.0700541 0.0 0.0; 0.0 0.0…

For correlation (returns the correlation matrix among the selected variables; diagonal is 1; supports the same grouped input as covariance, with cross-group entries set to zero):

get_statistics(RBC, RBC.parameter_values, correlation = [:c, :k])
# Dict{Symbol, AbstractArray{Float64}} with 1 entry:
#   :correlation => [1.0 0.999812; 0.999812 1.0]
source
MacroModelling.get_steady_stateMethod
get_steady_state(
    𝓂;
    parameters,
    steady_state_function,
    derivatives,
    stochastic,
    algorithm,
    parameter_derivatives,
    return_variables_only,
    verbose,
    silent,
    tol,
    quadratic_matrix_equation_algorithm,
    sylvester_algorithm,
    caching,
    use_workspaces
)

Return the (non-stochastic) steady state, calibrated parameters, and derivatives with respect to model parameters.

Arguments

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • derivatives [Default: true, Type: Bool]: calculate derivatives with respect to the parameters.
  • parameter_derivatives [Default: :all]: parameters for which to calculate partial derivatives. Inputs can be a parameter name passed on as either a Symbol or String (e.g. :alpha, or "alpha"), or Tuple, Matrix or Vector of String or Symbol. :all will include all parameters.
  • stochastic [Default: false, Type: Bool]: return stochastic steady state using second order perturbation if no other higher order perturbation algorithm is provided in algorithm.
  • return_variables_only [Default: false, Type: Bool]: return only variables and not calibrated parameters.
  • algorithm [Default: first_order, Type: Symbol]: algorithm to solve for the dynamics of the model. Available algorithms: :first_order, :second_order, :pruned_second_order, :third_order, :pruned_third_order
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • sylvester_algorithm [Default: selector that uses doubling for smaller problems and switches to bicgstab for larger problems, Type: Union{Symbol,Vector{Symbol},Tuple{Symbol,Vararg{Symbol}}}]: algorithm to solve the Sylvester equation (A * X * B + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :dqgmres, :gmres. Input argument can contain up to two elements in a Vector or Tuple. The first (second) element corresponds to the second (third) order perturbation solutions' Sylvester equation. If only one element is provided it corresponds to the second order perturbation solutions' Sylvester equation.
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows. The columns show the (non-stochastic) steady state and parameters for which derivatives are taken.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

get_steady_state(RBC)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables_and_calibrated_parameters ∈ 4-element Vector{Symbol}
→   Steady_state_and_∂steady_state∂parameter ∈ 6-element Vector{Symbol}
And data, 4×6 Matrix{Float64}:
        (:Steady_state)  (:std_z)  (:ρ)     (:δ)      (:α)       (:β)
  (:c)   5.93625          0.0       0.0   -116.072    55.786     76.1014
  (:k)  47.3903           0.0       0.0  -1304.95    555.264   1445.93
  (:q)   6.88406          0.0       0.0    -94.7805   66.8912   105.02
  (:z)   0.0              0.0       0.0      0.0       0.0        0.0
source
MacroModelling.get_steady_state_equationsMethod
get_steady_state_equations(𝓂; filter)

Return the non-stochastic steady state (NSSS) equations of the model. The difference to the equations as they were written in the @model block is that exogenous shocks are set to 0, time subscripts are eliminated (e.g. c[-1] becomes c), trivial simplifications are carried out (e.g. log(k) - log(k) = 0), and auxiliary variables are added for expressions that cannot become negative.

Auxiliary variables facilitate the solution of the NSSS problem. The package substitutes expressions which cannot become negative with auxiliary variables and adds another equation to the system of equations determining the NSSS. For example, log(c/q) cannot be negative and c/q is substituted by an auxiliary variable ➕₁ and an additional equation is added: ➕₁ = c / q.

Note that the output assumes the equations are equal to 0. As in, -z{δ} * ρ{δ} + z{δ} implies -z{δ} * ρ{δ} + z{δ} = 0 and therefore: z{δ} * ρ{δ} = z{δ}.

Arguments

Keyword Arguments

  • filter [Default: nothing, Type: Union{Symbol, String, Nothing}]: filter equations by variable name. Specify a variable name (e.g., :k or "k") to return only equations containing that variable. Time subscripts are ignored for steady state equations.

Returns

  • Vector{Expr} of the NSSS equations as expressions.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_steady_state_equations(RBC)
# output
9-element Vector{Expr}:
 :((-β * ((k ^ (α - 1) * α * exp(z{TFP}) - δ * exp(z{δ})) + 1)) / c + 1 / c)
 :(((c - k * (-δ * exp(z{δ}) + 1)) + k) - q)
 :(-(k ^ α) * exp(z{TFP}) + q)
 :(-(z{TFP}) * ρ{TFP} + z{TFP})
 :(-(z{δ}) * ρ{δ} + z{δ})
 :(➕₁ - c / q)
 :(➕₂ - c / q)
 :((Δc_share - log(➕₁)) + log(➕₂))
 :(Δk_4q - 0)
source
MacroModelling.get_variablesMethod
get_variables(𝓂)

Returns the variables of the model without timing subscripts and not including auxiliary variables.

In case programmatic model writing was used this function returns the parsed variables (see z in Examples).

Arguments

Returns

  • Vector{String} of the variables.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z{TFP}[1]) * k[0]^(α - 1) + (1 - exp(z{δ}[1]) * δ))
    c[0] + k[0] = (1 - exp(z{δ}[0])δ) * k[-1] + q[0]
    q[0] = exp(z{TFP}[0]) * k[-1]^α
    for shock in [TFP, δ]
        z{shock}[0] = ρ{shock} * z{shock}[-1] + σ{shock} * (eps{shock}[x] + eps_news{shock}[x-1])
    end
    Δc_share[0] = log(c[0]/q[0]) - log(c[-1]/q[-1])
    Δk_4q[0] = log(k[0]) - log(k[-4])
end

@parameters RBC  begin
    σ = 0.01
    ρ = 0.2
    capital_to_output = 1.5
    k[ss] / (4 * q[ss]) = capital_to_output | δ
    alpha = .5
    α = alpha
    β = 0.95
end

get_variables(RBC)
# output
7-element Vector{String}:
 "c"
 "k"
 "q"
 "z{TFP}"
 "z{δ}"
 "Δc_share"
 "Δk_4q"
source
MacroModelling.get_variance_decompositionMethod
get_variance_decomposition(
    𝓂;
    parameters,
    steady_state_function,
    algorithm,
    marginal_contribution,
    verbose,
    tol,
    quadratic_matrix_equation_algorithm,
    lyapunov_algorithm,
    caching,
    use_workspaces
)

Return the variance decomposition of endogenous variables with regards to the shocks. By default the linearised solution is used; with algorithm = :pruned_second_order or algorithm = :pruned_third_order the per-shock variance contributions are computed under the corresponding pruned higher-order solution and an extra column :Cross_shock_interaction is appended that captures the residual variance attributable to genuine cross-shock interaction terms in the centered higher moments (this column is zero whenever no products of distinct shocks appear in the model equations). Rows always sum to one.

Per-shock contributions are obtained by projecting the inner shock-cumulant block to a single shock i and re-solving the same pruned-state Lyapunov equation. At third order the projection is implemented as a binary mask on the augmented shock vector ê, retaining only those components whose exogenous-shock indices are all equal to i. Raw shares are returned without clipping; tiny negative entries can occur from numerical noise on near-zero contributions.

Setting marginal_contribution = true (only meaningful for the two pruned higher-order algorithms) instead allocates the cross-shock interaction across the individual shocks via marginal contributions (Shapley values). The result is a nVars × nExo table without a :Cross_shock_interaction column whose rows still sum to one (up to numerical noise; rows whose total variance is below eps() are reported as zero). The characteristic function V(S) is the same projected coalition variance used for the raw shares (state cumulants are kept at their full-shock values), so this is a marginal-contribution allocation of the projected higher-order variance, not a counterfactual recomputation of model variance under a sub-set of active shocks. Because V(S) need not be monotone, individual shares may be negative or exceed one. The allocation uses the Aumann–Shapley path-integral identity with Gauss–Legendre quadrature on the multilinear extension of V. The implementation starts from the low-order Gauss–Legendre rule (2 * n_e Lyapunov solves at second order, 3 * n_e at third) and incrementally reruns with up to 7 nodes when the relative Shapley-efficiency closure error exceeds 1e-3. At :first_order the option is silently ignored (with an @info notice) because the first-order decomposition is already additive across shocks.

If occasionally binding constraints are present in the model, they are not taken into account here.

Arguments

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • algorithm [Default: :first_order, Type: Symbol]: solution algorithm. Supports :first_order, :pruned_second_order, and :pruned_third_order.
  • marginal_contribution [Default: false, Type: Bool]: when true and algorithm is one of the pruned higher-order solutions, return marginal-contribution (Shapley-allocated) per-shock variance shares via the Aumann–Shapley path-integral driver. The implementation starts from the low-order Gauss–Legendre rule (2 * n_e Lyapunov solves at second order, 3 * n_e at third) and incrementally reruns with up to 7 nodes when the relative Shapley-efficiency closure error exceeds 1e-3. At :first_order the option is silently ignored (with an @info notice).
  • quadratic_matrix_equation_algorithm [Default: selector that uses schur for smaller problems and switches to doubling when the QME problem size exceeds 1000000, Type: Symbol]: algorithm to solve quadratic matrix equation (A * X ^ 2 + B * X + C = 0). Available algorithms: :schur, :doubling
  • lyapunov_algorithm [Default: doubling, Type: Symbol]: algorithm to solve Lyapunov equation (A * X * A' + C = X). Available algorithms: :doubling, :bartels_stewart, :bicgstab, :gmres, :dqgmres
  • tol [Default: Tolerances(), Type: Tolerances]: define various tolerances for the algorithm used to solve the model. See documentation of Tolerances for more details: ?Tolerances.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).

Returns

  • KeyedArray (from the AxisKeys package) with variables in rows and shocks in columns. Under :pruned_second_order and :pruned_third_order an additional :Cross_shock_interaction column is appended unless marginal_contribution = true, in which case only the per-shock columns are returned.

Examples

using MacroModelling

@model RBC_CME begin
    y[0]=A[0]*k[-1]^alpha
    1/c[0]=beta*1/c[1]*(alpha*A[1]*k[0]^(alpha-1)+(1-delta))
    1/c[0]=beta*1/c[1]*(R[0]/Pi[+1])
    R[0] * beta =(Pi[0]/Pibar)^phi_pi
    A[0]*k[-1]^alpha=c[0]+k[0]-(1-delta*z_delta[0])*k[-1]
    z_delta[0] = 1 - rho_z_delta + rho_z_delta * z_delta[-1] + std_z_delta * delta_eps[x]
    A[0] = 1 - rhoz + rhoz * A[-1]  + std_eps * eps_z[x]
end

@parameters RBC_CME  begin
    alpha = .157
    beta = .999
    delta = .0226
    Pibar = 1.0008
    phi_pi = 1.5
    rhoz = .9
    std_eps = .0068
    rho_z_delta = .9
    std_z_delta = .005
end

get_variance_decomposition(RBC_CME)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 7-element Vector{Symbol}
→   Shocks ∈ 2-element Vector{Symbol}
And data, 7×2 Matrix{Float64}:
              (:delta_eps)  (:eps_z)
  (:A)         0.0           1.0
  (:Pi)        0.0156771     0.984323
  (:R)         0.0156771     0.984323
  (:c)         0.0134672     0.986533
  (:k)         0.00869568    0.991304
  (:y)         0.000313462   0.999687
  (:z_delta)   1.0           0.0

The higher-order variants are illustrated on the Caldara, Fernández-Villaverde & Yao (2012) model, which has a level shock ϵᶻ and a stochastic-volatility shock ω:

using MacroModelling

@model Caldara_et_al_2012 begin
    V[0] = ((1 - β) * (c[0] ^ ν * (1 - l[0]) ^ (1 - ν)) ^ (1 - 1 / ψ) + β * V[1] ^ (1 - 1 / ψ)) ^ (1 / (1 - 1 / ψ))
    1 = (1 + ζ * exp(z[1]) * k[0] ^ (ζ - 1) * l[1] ^ (1 - ζ) - δ) * c[0] * β * (((1 - l[1]) / (1 - l[0])) ^ (1 - ν) * (c[1] / c[0]) ^ ν) ^ (1 - 1 / ψ) / c[1]
    Rᵏ[0] = ζ * exp(z[1]) * k[0] ^ (ζ - 1) * l[1] ^ (1 - ζ) - δ
    SDF⁺¹[0] = c[0] * β * (((1 - l[1]) / (1 - l[0])) ^ (1 - ν) * (c[1] / c[0]) ^ ν) ^ (1 - 1 / ψ) / c[1]
    1 + Rᶠ[0] = 1 / SDF⁺¹[0]
    (1 - ν) / ν * c[0] / (1 - l[0]) = (1 - ζ) * exp(z[0]) * k[-1] ^ ζ * l[0] ^ (-ζ)
    c[0] + i[0] = exp(z[0]) * k[-1] ^ ζ * l[0] ^ (1 - ζ)
    k[0] = i[0] + k[-1] * (1 - δ)
    z[0] = λ * z[-1] + σ[0] * ϵᶻ[x]
    y[0] = exp(z[0]) * k[-1] ^ ζ * l[0] ^ (1 - ζ)
    log(σ[0]) = (1 - ρ) * log(σ̄) + ρ * log(σ[-1]) + η * ω[x]
    dy[0] = 100 * (y[0] / y[-1] - 1) + dȳ
    dc[0] = 100 * (c[0] / c[-1] - 1) + dc̄
end

@parameters Caldara_et_al_2012 begin
    dȳ = 2.0
    dc̄ = 2.0
    β = 0.991
    l[ss] = 1/3 | ν
    ζ = 0.3
    δ = 0.0196
    λ = 0.95
    ψ = 0.5
    σ̄ = 0.021
    η = 0.1
    ρ = 0.9
end

get_variance_decomposition(Caldara_et_al_2012, algorithm = :pruned_second_order)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 13-element Vector{Symbol}
→   Shocks ∈ 3-element Vector{Symbol}
And data, 13×3 Matrix{Float64}:
            (:ω)  (:ϵᶻ)      (:Cross_shock_interaction)
  (:Rᵏ)      0.0   0.990522   0.0094784
  (:Rᶠ)      0.0   0.990522   0.0094784
  (:SDF⁺¹)   0.0   0.99052    0.00948043
  (:V)       0.0   0.990504   0.00949593
  (:c)       0.0   0.990526   0.00947405
  (:dc)      0.0   0.990504   0.00949552
  (:dy)      0.0   0.990505   0.00949515
  (:i)       0.0   0.990592   0.00940813
  (:k)       0.0   0.990564   0.00943632
  (:l)       0.0   0.990506   0.0094941
  (:y)       0.0   0.990554   0.0094464
  (:z)       0.0   0.9905     0.0095
  (:σ)       1.0   0.0        0.0

get_variance_decomposition(Caldara_et_al_2012, algorithm = :pruned_second_order, marginal_contribution = true)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 13-element Vector{Symbol}
→   Shocks ∈ 2-element Vector{Symbol}
And data, 13×2 Matrix{Float64}:
            (:ω)         (:ϵᶻ)
  (:Rᵏ)      0.0047392    0.995261
  (:Rᶠ)      0.0047392    0.995261
  (:SDF⁺¹)   0.00474022   0.99526
  (:V)       0.00474796   0.995252
  (:c)       0.00473702   0.995263
  (:dc)      0.00474776   0.995252
  (:dy)      0.00474757   0.995252
  (:i)       0.00470406   0.995296
  (:k)       0.00471816   0.995282
  (:l)       0.00474705   0.995253
  (:y)       0.0047232    0.995277
  (:z)       0.00475      0.99525
  (:σ)       1.0          0.0

get_variance_decomposition(Caldara_et_al_2012, algorithm = :pruned_third_order, marginal_contribution = true)
# output
2-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   Variables ∈ 13-element Vector{Symbol}
→   Shocks ∈ 2-element Vector{Symbol}
And data, 13×2 Matrix{Float64}:
            (:ω)         (:ϵᶻ)
  (:Rᵏ)      0.0093913    0.990609
  (:Rᶠ)      0.00939245   0.990608
  (:SDF⁺¹)   0.00939535   0.990605
  (:V)       0.00933759   0.990662
  (:c)       0.00928112   0.990719
  (:dc)      0.00936725   0.990633
  (:dy)      0.00934246   0.990658
  (:i)       0.00917571   0.990824
  (:k)       0.00916176   0.990838
  (:l)       0.00933518   0.990665
  (:y)       0.0092273    0.990773
  (:z)       0.00935325   0.990647
  (:σ)       1.0          0.0
source
MacroModelling.has_missing_parametersMethod
has_missing_parameters(𝓂)

Returns whether the model has missing parameters that need to be provided before solving.

Arguments

Returns

  • Bool indicating whether the model has missing parameters.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC  report_missing_parameters = false begin
    std_z = 0.01
    ρ = 0.2
end

has_missing_parameters(RBC)
# output
true
source
MacroModelling.remove_calibration_equation!Method
remove_calibration_equation!(
    𝓂,
    equation_or_index;
    parameters,
    verbose,
    silent
)

Remove a calibration equation.

When a calibration equation is removed, the parameter previously solved by it becomes a free parameter and needs a numeric value. By default that value is taken from the parameter's current non-stochastic steady state value. Use the parameters keyword to override this with explicit values.

A batched form is supported by passing a Vector/Tuple of selectors.

Arguments

  • 𝓂: object created by @model and @parameters.
  • equation_or_index [Type: Union{Int, Expr, String}]: the calibration equation to remove.

Alternatively:

  • removals [Type: Union{Vector, Tuple}]: a collection of selectors.

Keyword Arguments

  • parameters [Default: nothing]: optional value(s) used as the new fixed value for the parameter freed by the removal. Accepts the same forms as the parameters argument elsewhere — a Pair, a Dict, or a Vector/Tuple of Pairs — but only the entries matching freed parameters are used.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).
  • silent [Default: true, Type: Bool]: suppress informational warnings from the rebuild pipeline.

Returns

  • nothing. The model 𝓂 is updated in place.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    α = 0.5
    β = 0.95
    k[ss] / (4 * q[ss]) = 1.5 | δ
end

# remove the calibration equation, fixing δ to 0.02
remove_calibration_equation!(RBC, 1, parameters = :δ => 0.02)
source
MacroModelling.remove_equation!Method
remove_equation!(
    𝓂,
    equation_or_index;
    parameters,
    verbose,
    silent
)

Remove one or more equations from the model.

Equations can be selected by 1-based index, by their Expr, or by their String representation. Pass a Vector/Tuple of selectors to remove several equations in one rebuild. The model must always retain at least one equation.

Arguments

  • 𝓂: object created by @model and @parameters.
  • equation_or_index [Type: Union{Int, Expr, String}]: the equation to remove. When matching by Expr/String, comparison is done on a whitespace- and brace-insensitive canonical form.

Alternatively:

  • removals [Type: Union{Vector, Tuple}]: a collection of selectors.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).
  • silent [Default: true, Type: Bool]: suppress informational warnings from the rebuild pipeline.

Returns

  • nothing. The model 𝓂 is updated in place.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
    y[0] = c[0] + k[0] - (1 - δ) * k[-1]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

# remove by index
remove_equation!(RBC, 5)

# remove by expression
remove_equation!(RBC, :(q[0] = exp(z[0]) * k[-1]^α))
source
MacroModelling.translate_mod_fileMethod
translate_mod_file(path_to_mod_file)

Reads in a dynare .mod-file, adapts the syntax, tries to capture parameter definitions, and writes a julia file in the same folder containing the model equations and parameters in MacroModelling.jl syntax. This function is not guaranteed to produce working code. It's purpose is to make it easier to port a model from dynare to MacroModelling.jl.

The recommended workflow is to use this function to translate a .mod-file, and then adapt the output so that it runs and corresponds to the input.

Note that this function copies the .mod-file to a temporary folder and executes it there. All references within that .mod-file are therefore not valid (because those filesare not copied) and must be made copied into the .mod-file.

Arguments

  • path_to_mod_file [Type: AbstractString]: path including filename of the .mod-file to be translated
source
MacroModelling.update_calibration_equations!Method
update_calibration_equations!(
    𝓂,
    old_equation_or_index,
    new_equation;
    parameters,
    verbose,
    silent
)

Replace an existing calibration equation.

Calibration equations use the lhs = rhs | param syntax and pin down a parameter so it is solved jointly with the non-stochastic steady state. The new equation must contain the | param marker and reference a parameter that is already part of the model.

A batched form is supported: pass a Vector/Tuple of (old_or_index => new_equation) Pairs or 2-tuples.

replace_calibration_equations! is an alias of this function.

Arguments

  • 𝓂: object created by @model and @parameters.
  • old_equation_or_index [Type: Union{Int, Expr, String}]: the calibration equation to replace, identified by its 1-based index in the calibration list or by its Expr/String representation.
  • new_equation [Type: Union{Expr, String}]: the replacement calibration equation. Must contain the | param syntax.

Alternatively:

  • updates [Type: Union{Vector, Tuple}]: a collection of (old_or_index => new_equation) Pairs or 2-tuples.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).
  • silent [Default: true, Type: Bool]: suppress informational warnings from the rebuild pipeline.

Returns

  • nothing. The model 𝓂 is updated in place.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    α = 0.5
    β = 0.95
    k[ss] / (4 * q[ss]) = 1.5 | δ
end

# retarget the calibration equation
update_calibration_equations!(RBC, 1, :(k[ss] / (4 * q[ss]) = 2.0 | δ))
source
MacroModelling.update_equations!Method
update_equations!(
    𝓂,
    old_equation_or_index,
    new_equation;
    parameters,
    verbose,
    silent
)

Replace an existing model equation with a new one.

The first equation argument selects which equation to update: pass either the 1-based index, the existing equation as an Expr, or as a String. The new equation can be passed as an Expr or a String.

A batched form is also supported: pass a Vector (or Tuple) of (old_or_index, new_equation) Pairs or 2-tuples to apply several updates in one rebuild.

After the update the revision history is appended (see get_revision_history), all caches are invalidated and — if all parameters are defined — the non-stochastic steady state is resolved and the first-order symbolic derivatives are rewritten.

replace_equations! is an alias of this function.

Arguments

  • 𝓂: object created by @model and @parameters.
  • old_equation_or_index [Type: Union{Int, Expr, String}]: the equation to replace, identified by its 1-based index, by the equation itself as an Expr, or by the equation as a String (parsed before matching). When matching by Expr/String, comparison is done on a whitespace- and brace-insensitive canonical form.
  • new_equation [Type: Union{Expr, String}]: the replacement equation.

Alternatively a single positional argument can be supplied:

  • updates [Type: Union{Vector, Tuple}]: a collection of (old_or_index => new_equation) Pairs or 2-tuples.

Keyword Arguments

  • parameters [Default: nothing]: If nothing is provided, the solution is calculated for the parameters defined previously. Acceptable inputs are a Vector of parameter values, a Vector or Tuple of Pairs of the parameter Symbol or String and value. If the new parameter values differ from the previously defined the solution will be recalculated.
  • verbose [Default: false, Type: Bool]: print information about results of the different solvers used to solve the model (non-stochastic steady state solver, Sylvester equations, Lyapunov equation, and quadratic matrix equation).
  • silent [Default: true, Type: Bool]: suppress informational warnings from the rebuild pipeline (e.g. about missing parameters).

Returns

  • nothing. The model 𝓂 is updated in place.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

# replace by index
update_equations!(RBC, 3, :(q[0] = exp(z[0]) * k[-1]^α + 0))

# replace by old equation expression
update_equations!(RBC,
    :(z[0] = ρ * z[-1] + std_z * eps_z[x]),
    :(z[0] = ρ * z[-1] + std_z * eps_z[x] + 0))

# batched update
update_equations!(RBC, [
    1 => :(1 / c[0] = (β / c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))),
    2 => :(c[0] + k[0] = (1 - δ) * k[-1] + q[0]),
])
source
MacroModelling.write_julia_model_fileMethod
write_julia_model_file(𝓂, filepath; overwrite)

Write the current model equations and parameter block to a Julia source file.

The generated file uses the @model and @parameters macros, so includeing it re-creates a model equivalent to the current state of 𝓂 (equations, calibration, parameter values and bounds).

Arguments

  • 𝓂: object created by @model and @parameters.
  • filepath [Type: String]: destination path for the generated .jl file.

Keyword Arguments

  • overwrite [Default: false, Type: Bool]: replace the file if it already exists. When false and the file exists, an error is raised.

Returns

  • String — the path written to (same as filepath).

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

write_julia_model_file(RBC, joinpath(tempdir(), "RBC.jl"), overwrite = true)
source
MacroModelling.write_mod_fileMethod
write_mod_file(𝓂; order, pruning, irf_periods)

Writes a dynare .mod-file in the current working directory. This function is not guaranteed to produce working code. It's purpose is to make it easier to port a model from MacroModelling.jl to dynare.

The recommended workflow is to use this function to write a .mod-file, and then adapt the output so that it runs and corresponds to the input.

Arguments

source
MacroModelling.@modelMacro

Parses the model equations and assigns them to an object.

Arguments

  • 𝓂: name of the object to be created containing the model information.
  • ex: equations

Optional arguments to be placed between 𝓂 and ex

  • max_obc_horizon [Default: 40, Type: Int]: maximum length of anticipated shocks and corresponding unconditional forecast horizon over which the occasionally binding constraint is to be enforced. Increase this number if no solution is found to enforce the constraint.

Variables must be defined with their time subscript in square brackets. Endogenous variables can have the following:

  • present: c[0]
  • non-stochastic steady state: c[ss] instead of ss any of the following is also a valid flag for the non-stochastic steady state: ss, stst, steady, steadystate, steady_state, and the parser is case-insensitive (SS or sTst will work as well).
  • past: c[-1] or any negative Integer: e.g. c[-12]
  • future: c[1] or any positive Integer: e.g. c[16] or c[+16]

Signed integers are recognised and parsed as such.

Exogenous variables (shocks) can have the following:

  • present: eps_z[x] instead of x any of the following is also a valid flag for exogenous variables: ex, exo, exogenous, and the parser is case-insensitive (Ex or exoGenous will work as well).
  • past: eps_z[x-1]
  • future: eps_z[x+1]

Parameters enter the equations without square brackets.

If an equation contains a max or min operator, the default dynamic (first order) solution of the model will enforce the occasionally binding constraint. This enforcement can be disabled by setting ignore_obc = true in the relevant function calls.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

Programmatic model writing

Parameters and variables can be indexed using curly braces: e.g. c{H}[0], eps_z{F}[x], or α{H}.

for loops can be used to write models programmatically. They can either be used to generate expressions where the time index or the index in curly braces is iterated over:

  • generate equation with different indices in curly braces: for co in [H,F] C{co}[0] + X{co}[0] + Z{co}[0] - Z{co}[-1] end = for co in [H,F] Y{co}[0] end
  • generate multiple equations with different indices in curly braces: for co in [H, F] K{co}[0] = (1-delta{co}) * K{co}[-1] + S{co}[0] end
  • generate equation with different time indices: Y_annual[0] = for lag in -3:0 Y[lag] end or R_annual[0] = for operator = :*, lag in -3:0 R[lag] end

Returns

  • Nothing. The macro creates the model 𝓂 in the calling scope.
source
MacroModelling.@parametersMacro

Adds parameter values and calibration equations to the previously defined model. Allows to provide an initial guess for the non-stochastic steady state (NSSS).

Arguments

  • 𝓂: name of the object previously created containing the model information.
  • ex: parameter, parameters values, and calibration equations

Parameters can be defined in either of the following ways:

  • plain number: δ = 0.02
  • expression containing numbers: δ = 1/50
  • expression containing other parameters: δ = 2 * std_z in this case it is irrelevant if std_z is defined before or after. The definitions including other parameters are treated as a system of equations and solved accordingly.
  • expressions containing a target parameter and an equations with endogenous variables in the non-stochastic steady state, and other parameters, or numbers: k[ss] / (4 * q[ss]) = 1.5 | δ or α | 4 * q[ss] = δ * k[ss] in this case the target parameter will be solved simultaneously with the non-stochastic steady state using the equation defined with it.

Optional arguments to be placed between 𝓂 and ex

  • guess [Type: Dict{Symbol, <:Real} or Dict{String, <:Real}]: Guess for the non-stochastic steady state. The keys must be variable (and calibrated parameter) names and the values the guesses. Missing values are filled with standard starting values.
  • steady_state_function [Default: missing, Type: Union{Function, Symbol, Nothing, Missing}]: Custom steady state routine applied instead of the internal solver when provided; accepts either f(parameters) returning [variables; calibrated parameters] or f!(out, parameters) filling an output vector in the same order. Use nothing to clear any previously set function and rely on the internal solver; missing keeps the current setting.
  • verbose [Default: false, Type: Bool]: print more information about how the non-stochastic steady state is solved
  • silent [Default: false, Type: Bool]: do not print any information
  • ss_symbolic_mode [Default: :single_equation, Type: Symbol]: controls symbolic steps in non-stochastic steady state (NSSS) setup. Use :none for numerical-only setup, :single_equation to allow symbolic solves only for single-equation blocks, or :full to allow symbolic solves for both single- and multi-equation blocks.
  • perturbation_order [Default: 1, Type: Int]: take derivatives only up to the specified order at this stage. When working with higher order perturbation later on, respective derivatives will be taken at that stage.
  • ss_solver_parameters_algorithm [Default: :ESCH, Type: Symbol]: global optimization routine used when searching for steady-state solver parameters after an initial failure; choose :ESCH (evolutionary) or :SAMIN (simulated annealing). :SAMIN is available only when Optim.jl is loaded.
  • ss_solver_parameters_maxtime [Default: 120.0, Type: Real]: time budget in seconds for the steady-state solver parameter search when ss_solver_parameters_algorithm is invoked

Delayed parameter definition

Not all parameters need to be defined in the @parameters macro. Calibration equations using the | syntax and parameters defined as functions of other parameters must be declared here, but simple parameter value assignments (e.g., α = 0.5) can be deferred and provided later by passing them to any function that accepts the parameters argument (e.g., get_irf, get_steady_state, simulate).

Parameter ordering: When some parameters are not defined in @parameters, the final parameter vector follows a specific order: first come the parameters defined in @parameters (in their declaration order), followed by any missing parameters (in alphabetical order). This ordering is important when passing parameter values by position rather than by name in subsequent function calls.

Examples

using MacroModelling

@model RBC begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC verbose = true begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    α = 0.5
    β = 0.95
end

@model RBC_calibrated begin
    1  /  c[0] = (β  /  c[1]) * (α * exp(z[1]) * k[0]^(α - 1) + (1 - δ))
    c[0] + k[0] = (1 - δ) * k[-1] + q[0]
    q[0] = exp(z[0]) * k[-1]^α
    z[0] = ρ * z[-1] + std_z * eps_z[x]
end

@parameters RBC_calibrated verbose = true guess = Dict(:k => 3) begin
    std_z = 0.01
    ρ = 0.2
    δ = 0.02
    k[ss] / q[ss] = 2.5 | α
    β = 0.95
end

Programmatic model writing

Variables and parameters indexed with curly braces can be either referenced specifically (e.g. c{H}[ss]) or generally (e.g. alpha). If they are referenced generally the parse assumes all instances (indices) are meant. For example, in a model where alpha has two indices H and F, the expression alpha = 0.3 is interpreted as two expressions: alpha{H} = 0.3 and alpha{F} = 0.3. The same goes for calibration equations.

Returns

  • Nothing. The macro assigns parameter values and calibration equations to 𝓂 in the calling scope.
source