This repository implements machine learning techniques to solve Dynamic Stochastic General Equilibrium (DSGE) models, with a focus on neural network-based policy function approximation.
Traditional methods for solving DSGE models (e.g., perturbation, projection, value function iteration) can be computationally expensive and may struggle with high-dimensional state spaces or non-linearities. This project explores neural networks as universal function approximators to learn policy functions that satisfy the model's equilibrium conditions (e.g., Euler equations) across a wide range of parameter values.
-
Policy Function Approximation: Instead of solving for the exact policy function analytically, we train a neural network to approximate it by minimizing Euler equation residuals.
-
Wide Parameter Space Learning: Rather than solving for a single calibration, the neural network learns the policy function over a range of structural parameters (e.g., discount factor, risk aversion, capital share). This allows the model to generalize across different calibrations without retraining.
-
Residual-Based Training: The network is trained to satisfy the model's equilibrium conditions (typically Euler equations) by minimizing the squared residuals:
Loss = E[(Euler_residual)²]where the expectation is taken over random samples of state variables and parameters.
-
Comparison with Traditional Methods: Results are validated against traditional solution methods (e.g., Time Iteration with cubic splines) to ensure accuracy.
- Scalability: Neural networks can handle high-dimensional state spaces more efficiently than grid-based methods.
- Generalization: A single trained network can approximate policies across a wide parameter space.
- Flexibility: Easy to extend to more complex models (e.g., multiple agents, additional shocks, non-linearities).
- Differentiability: Automatic differentiation enables efficient gradient-based optimization.
ML-DSGE/
├── full-rbc/ # Full RBC model implementation
│ ├── learn_rbc.py # Neural network solver for RBC
│ ├── rbc_TimeIter.py # Traditional Time Iteration solver
│ └── compare_rbc.py # Comparison script (NN vs TI)
├── poc/ # Proof-of-concept implementations
│ └── neural_net.py # Early neural network experiments
├── lstm/ # LSTM-based approaches (exploratory)
└── README.md # This file
The RBC model features:
- State variables: Capital stock (k) and productivity (A)
- Control variable: Consumption (c)
- Structural parameters: Capital share (α), discount factor (β), depreciation (δ), risk aversion (γ), persistence (ρ), shock volatility (σ_ε)
- Inputs: Normalized (k, A, α, β, δ, ρ, γ) — 7 dimensions
- Output: Consumption fraction (sigmoid → [0,1])
- Architecture: Feedforward network with ELU activations
- Training: Adam optimizer minimizing Euler equation residuals
-
Train the neural network (or load from checkpoint):
python full-rbc/learn_rbc.py
This trains over a wide parameter range and saves the model to
rbc_nn.pt. -
Compare with Time Iteration:
python full-rbc/compare_rbc.py
This loads the trained NN (or trains if missing), solves via TI, runs simulations with the same seed, and generates comparison plots.
-
Specify calibration: Edit
get_calibration_params()incompare_rbc.pyto change the parameter set used for comparison.
- Sampling: Random batches of (k, A, α, β, δ, ρ, γ) are sampled uniformly within specified bounds.
- Residual Computation: For each sample, the Euler equation residual is computed using Hermite-Gauss quadrature for expectations.
- Steady State: Per-sample steady-state capital is computed to maintain consistent state-space scaling across parameters.
- Euler Residuals: Training loss measures how well the network satisfies equilibrium conditions.
- Simulation Comparison: Side-by-side comparison with Time Iteration at the same calibration and shock sequence.
- Parameter Generalization: Test the network at parameter values not seen during training.
- Extend to more complex DSGE models (e.g., New Keynesian, heterogeneous agents)
- Explore alternative architectures (e.g., attention mechanisms, graph neural networks)
- Investigate uncertainty quantification and robustness
- Compare with other ML approaches (e.g., reinforcement learning, physics-informed neural networks)
- PyTorch
- NumPy
- Matplotlib
- SciPy (for Time Iteration comparison)
This work is inspired by recent research on using neural networks for solving economic models, including:
- Deep learning for solving high-dimensional PDEs (which share structure with HJB equations)
- Universal function approximation for policy functions
- Residual-based training for economic equilibrium conditions
[Specify your license here]