Would you like a Pythonic Compiler for Dynare .mod Files?

Hi everyone,

I’m thinking on developing a Python library to create and manage Dynare .mod files. The project has two main goals:

  1. Make .mod file construction more reliable by catching common errors early through static type checking and validation (e.g., undeclared variables, inconsistent equations)

  2. Provide a modern, Pythonic API for DSGE modeling (similar to statsmodels/scikit-learn), enabling easier scripting and code editor exposed methods documentation (like pandas)

The library also uses ruff to enforce consistent formatting and best practices in the generated .mod files, ensuring your models follow conventions like proper variable declaration order, consistent spacing, and clear section organization.

Example usage:

# Create model instance
model = Model()

# Define symbols and add model components as variable, parameter, equation, and shock definitions. 

y, c, pi, r= symbols("y c pi r")

# Add variables
model.add_variable(Var(
    symbol=C,
    latex_repr="C",
    description="Consumption",
    long_name="Consumption"
))
model.add_parameter(Parameter(
    symbol=alpha,
    latex_repr=r"\alpha",
    description="capital share",
    value=1/4,
    long_name="capital share"
))

# Add equations
model.add_equation(Equation(
    name="FOC Wages, eq. (7)",
    equation=Eq(W_real, C**sigma * N**varphi)
))

gali_model.add_shock(Shock(
    name="eps_a",
    variance=1,
    description="technology shock"
))

# Add Dynare commands in sequence
model.steady() \
    .check() \
    .get_latex_files() \
    .stoch_simul(
        variables=[y, c, pi, r],
        irf=20,
        order=1
    )
model.sensibility(graph_format='pdf')
model.identification(prior_mc=1000, advanced=1)
model.write_latek_steady_state_equation()
# Export the model with all commands
model.export("gali_2015.mod")

Would love to hear your thoughts on whether this would be useful for your workflow!

I’m currently using Pydantic for type validation and SymPy for symbolic mathematics. Another idea I have is to add a Ruff-like tool written in Rust to format .mod files according to standards established in a PEP style fashion. Would appreciate any thoughts on this tech stack or suggestions for alternatives!

Best,
Ian Contreras

1 Like

There are related efforts at Dynare / dynare-python · GitLab
and GitHub - nuvolos-cloud/dynare-model-language: Syntax highlighting in VSCode for Dynare model files
@danielsali and @normann may want to comment.

Hi Ian,

We have just released the first, evaluation version of dynare-python which is available on PyPI:
https://pypi.org/project/dynare-python/

This wraps the Dynare.jl library using JuliaCall to return a Python version of the Context structure.

The Pythonic expression of the model file is also an idea to consider, however, the dynare-python library still uses the current .mod files.

You can give the library a try by installing it from PyPI with:
pip install dynare-python

and using it like:

from dynare import dynare

context = dynare("<path_to_model_file.mod>")
print(context)

You’re mainly interested in the .mod files themselves it seems. But if you chose to develop a DSGE modeling framework in python, might I suggest an old project of mine as a jumping-off point: pynare.

It’s exclusively python (i.e. it does not simply wrap Dynare in either its Matlab or Octave implementations, nor does it wrap the Julia Dynare), and so it supports the object-oriented approach you have in your example usage. I intended it to be a learning experience, so it’s quite lacking in features, however.