Hi everyone,
I’m thinking on developing a Python library to create and manage Dynare .mod files. The project has two main goals:
-
Make .mod file construction more reliable by catching common errors early through static type checking and validation (e.g., undeclared variables, inconsistent equations)
-
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