SkillAgentSearch skills...

MacroModelling.jl

Macros and functions to work with DSGE models.

Install / Use

/learn @thorek1/MacroModelling.jl

README

MacroModelling.jl

Documentation: Documentation

codecov CI DOI Package Downloads

<!-- [![Downloads](https://shields.io/endpoint?url=https://pkgs.genieframework.com/api/v1/badge/MacroModelling)](https://pkgs.genieframework.com?packages=MacroModelling) --> <!-- [![DOI](https://zenodo.org/badge/571475096.svg)](https://zenodo.org/badge/latestdoi/571475096) --> <!-- [![](https://img.shields.io/badge/docs-dev-blue.svg)](https://thorek1.github.io/MacroModelling.jl/dev) -->

Author: Thore Kockerols (@thorek1)

MacroModelling.jl is a Julia package for developing and solving dynamic stochastic general equilibrium (DSGE) models.

These kinds of models describe the behaviour of a macroeconomy and are particularly suited for counterfactual analysis (economic policy evaluation) and exploring / quantifying specific mechanisms (academic research). Due to the complexity of these models, efficient numerical tools are required, as analytical solutions are often unavailable. MacroModelling.jl serves as a tool for handling the complexities involved, such as forward-looking expectations, nonlinearity, and high dimensionality.

The goal of this package is to reduce coding time and speed up model development by providing functions for working with discrete-time DSGE models. The user-friendly syntax, automatic variable declaration, and effective steady state solver facilitate fast prototyping of models. Furthermore, the package allows the user to work with nonlinear model solutions (up to third order (pruned) perturbation) and estimate the model using gradient based samplers (e.g. NUTS, or HMC). Currently, DifferentiableStateSpaceModels.jl is the only other package providing functionality to estimate using gradient based samplers but they use the start-of-period timing convention instead of the end-of-period timing convention used in most other packages. The target audience for the package includes central bankers, regulators, graduate students, and others working in academia with an interest in DSGE modelling.

As of now the package can:

  • parse a model written with user friendly syntax (variables are followed by time indices ...[2], [1], [0], [-1], [-2]..., or [x] for shocks)
  • (tries to) solve the model only knowing the model equations and parameter values (no steady state file needed; possibility to define custom steady state function)
  • calculate first, second, and third order (pruned) perturbation solutions using symbolic derivatives
  • handle occasionally binding constraints for linear and nonlinear solutions
  • calculate (generalised) impulse response functions, simulate the model, or do conditional forecasts for linear and nonlinear solutions
  • calibrate parameters using (non-stochastic) steady state relationships
  • match model moments (also for pruned higher order solutions)
  • estimate the model on data (Kalman filter using first order perturbation) with gradient based samplers (e.g. NUTS, HMC) or estimate nonlinear models using the inversion filter
  • differentiate (forward AD) the model solution, Kalman filter loglikelihood (forward and reverse-mode AD), model moments, steady state, with respect to the parameters

The package is not:

  • guaranteed to find the non-stochastic steady state (solving systems of nonlinear equations is an active area of research)
  • the fastest package around if there is already a fast way to find the NSSS (time to first plot is long, time to second plot (with new parameters) is very short)

For more details have a look at the documentation.

Getting started

Installation

MacroModelling.jl requires julia version 1.10 or higher and an IDE is recommended (e.g. VS Code with the julia extension).

Once set up MacroModelling.jl can be installed (and StatsPlots in order to plot) by typing the following in the Julia REPL:

using Pkg; Pkg.add(["MacroModelling", "StatsPlots"])

Example

See below an implementation of a simple RBC model. You can find more detailed tutorials in the documentation.

using MacroModelling
import StatsPlots

@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;

plot_irf(RBC)

RBC IRF

Custom steady state function

By default, the package automatically solves for the non-stochastic steady state. For complex models, you can provide a custom steady state function to ensure efficient computation or in case the automatic solver fails:

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;

function rbc_steady_state!(ss, params)
    std_z, rho, delta, alpha, beta = params

    k_ss = ((1 / beta - 1 + delta) / alpha)^(1 / (alpha - 1))
    q_ss = k_ss^alpha
    c_ss = q_ss - delta * k_ss
    z_ss = 0.0

    ss[1] = c_ss
    ss[2] = k_ss
    ss[3] = q_ss
    ss[4] = z_ss
end

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

Delayed parameter definition

Parameters do not need to be defined upfront in the @parameters block. You can define the model first and provide parameter values later when calling functions. This is useful for loading parameter values from external sources (e.g., CSV files).

using MacroModelling
import StatsPlots

@model RBC_delayed 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_delayed begin
end;

plot_irf(RBC_delayed, parameters = (:std_z => 0.01, :ρ => 0.2, :δ => 0.02, :α => 0.5, :β => 0.95))

RBC IRF

Note: Calibration equations (using | syntax) and parameters defined as functions of other parameters must be declared in the @parameters block.

Steady state options:

  • Automatic solver (default) from the model equations.
  • Custom steady state function.

Parameter values can also be supplied later (delayed parameter definition) as illustrated above.

See the documentation for more details on the steady state.

Models

The package contains the following models in the models folder:

Comparison with other packages

||MacroModelling.jl|dynare|DSGE.jl|dolo.py|SolveDSGE.jl|[DifferentiableStateSpaceModels.jl](https://github.com

View on GitHub
GitHub Stars135
CategoryDevelopment
Updated5d ago
Forks18

Languages

Julia

Security Score

100/100

Audited on Apr 3, 2026

No findings