A comprehensive Python implementation of the Smets and Wouters (2007) DSGE model with modern extensions, featuring modular design, multiple solution methods, and advanced estimation capabilities.
The project follows a modular design pattern with clear separation of concerns and extensive configuration options:
graph TD
A[Configuration Module] --> B[Model Core]
A --> C[Data Fetching & Processing]
A --> D[Solution Methods]
B --> B1[Base SW Model]
B --> B2[Extension: Financial Frictions]
B --> B3[Extension: Open Economy]
B --> B4[Extension: Fiscal Policy]
D --> D1[Perturbation Methods]
D --> D2[Projection Methods]
C --> E[Estimation Module]
D --> E
B --> E
E --> F[Analysis & Diagnostics]
E --> G[Forecasting]
F --> H[Visualization]
G --> H
H --> I[Results & Reports]
- Python: 3.8 or higher
- Operating System: Windows, macOS, or Linux
- RAM: Minimum 4GB (8GB+ recommended for larger models)
- Disk Space: At least 500MB for installation and example data
# Create and activate a virtual environment (recommended)
python -m venv dsge-env
# On Windows
dsge-env\Scripts\activate
# On macOS/Linux
source dsge-env/bin/activate
# Install the package
pip install git+https://github.com/username/macroeconomic-sim.git# Clone the repository
git clone https://github.com/username/macroeconomic-sim.git
cd macroeconomic-sim
# Create and activate a virtual environment (recommended)
python -m venv venv
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate
# Install dependencies
pip install -e .
# Install development dependencies (optional)
pip install -e ".[dev]"
# Install JAX for GPU acceleration (optional)
pip install -e ".[jax]"The DSGE model implementation uses two configuration files:
- Estimation Configuration:
config/estimation_config.json - Forecast Configuration:
config/forecast_config.json
You can customize these files or create your own configuration files to modify model parameters, enable/disable extensions, and configure solution methods.
The package dependencies are managed through setup.py. Core dependencies include:
numpyandscipyfor numerical computingpandasandpandas_datareaderfor data managementmatplotlibandseabornfor visualizationpymcfor Bayesian estimationsympyfor symbolic manipulation
Optional dependencies:
numbafor JIT compilation of performance-critical routinesjaxandjaxlibfor automatic differentiation and GPU acceleration
To verify that the installation is working correctly:
# Run a simple example
python examples/simple_example.py
# Verify the output
ls results/example/
# Should show impulse_responses.pngYou should see a new directory results/example/ with impulse response visualizations.
macroeconomic-sim/
├── config/ # Configuration files
├── data/ # Data storage
│ ├── raw/ # Raw data from FRED
│ └── processed/ # Processed data
├── docs/ # Documentation
├── dsge/ # Main package
│ ├── core/ # Core model components
│ │ ├── base_model.py # Base Smets-Wouters model
│ │ └── steady_state.py # Steady state solver
│ ├── extensions/ # Optional model extensions
│ │ ├── financial.py # Financial frictions module
│ │ ├── open_economy.py # Open economy features
│ │ └── fiscal.py # Fiscal policy extensions
│ ├── data/ # Data handling
│ │ ├── fetcher.py # FRED data acquisition
│ │ └── processor.py # Data transformation
│ ├── solution/ # Solution methods
│ │ ├── perturbation.py # 1st, 2nd, 3rd order perturbation
│ │ └── projection.py # Projection methods
│ ├── estimation/ # Estimation methods
│ │ ├── bayesian.py # Bayesian MCMC methods
│ │ ├── priors.py # Prior distributions
│ │ └── posteriors.py # Posterior analysis
│ ├── analysis/ # Analysis tools
│ │ ├── impulse_response.py # IRF computation
│ │ ├── decomposition.py # Shock decomposition
│ │ └── diagnostics.py # Model diagnostics
│ ├── forecasting/ # Forecasting tools
│ │ ├── baseline.py # Baseline forecasts
│ │ ├── scenarios.py # Alternative scenarios
│ │ └── uncertainty.py # Uncertainty quantification
│ └── visualization/ # Visualization tools
│ ├── plots.py # Standard plots
│ ├── publication.py # Publication-quality figures
│ └── interactive.py # Interactive visualizations
├── tests/ # Testing framework
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ ├── functional/ # Functional tests
│ ├── performance/ # Performance tests
│ ├── debug/ # Debugging scripts
│ ├── utils/ # Test utilities
│ ├── conftest.py # Test fixtures
│ ├── run_tests.py # Master test script
│ └── README.md # Testing documentation
├── examples/ # Example scripts
├── setup.py # Package installation
├── run_estimation.py # Main estimation script
├── run_forecast.py # Main forecasting script
└── README.md # Project documentation
from config.config_manager import ConfigManager
from dsge.core import SmetsWoutersModel
from dsge.solution import PerturbationSolver
# Create configuration
config = ConfigManager()
# Create model
model = SmetsWoutersModel(config)
# Solve model using first-order perturbation
solver = PerturbationSolver(model, order=1)
solution = solver.solve()
# Simulate model for 40 periods
states, controls = solver.simulate(periods=40)
print("Simulation complete!")from dsge.analysis import ImpulseResponseFunctions
import matplotlib.pyplot as plt
# Create IRF analyzer
irf = ImpulseResponseFunctions(model, config)
# Compute IRFs for technology shock
irfs = irf.compute_irfs(
shock_names=["technology"],
periods=40,
shock_size=1.0
)
# Plot output response to technology shock
plt.figure(figsize=(10, 6))
plt.plot(irfs["technology"]["output"])
plt.title("Output Response to Technology Shock")
plt.xlabel("Periods")
plt.ylabel("Deviation from Steady State")
plt.grid(True)
plt.show()# Run estimation with default configuration
python run_estimation.py
# Run estimation with custom configuration
python run_estimation.py --config path/to/custom_config.json# Run forecasting with default configuration
python run_forecast.py
# Run forecasting with custom configuration
python run_forecast.py --config path/to/custom_config.json# Run all tests
python tests/run_tests.py --all
# Run specific test types
python tests/run_tests.py --unit
python tests/run_tests.py --integration
python tests/run_tests.py --functional
python tests/run_tests.py --performance
# Run tests with coverage
python tests/run_tests.py --all --coverageFor more detailed examples, please see the examples/ directory and the comprehensive usage guide in the docs/ directory.
The central configuration module will allow users to:
- Toggle model extensions (financial, open economy, fiscal)
- Select solution method (perturbation or projection)
- Choose perturbation order (1st, 2nd, or 3rd)
- Configure model parameters (monetary policy, inflation targets, etc.)
- Set estimation options (priors, MCMC settings)
- Define data sources and preprocessing steps
- Complete implementation of all equations from SW (2007)
- Household utility maximization
- Firm production and pricing
- Wage setting
- Monetary policy rule
- Shock processes
- Market clearing conditions
Each extension will be implemented as a modular component that can be toggled on/off:
Financial Frictions
- Based on BGG (1999) and Gertler-Karadi (2011) frameworks
- Financial intermediaries with balance sheet constraints
- Credit spreads and risk premiums
- Financial accelerator mechanisms
Open Economy Features
- Based on Adolfson et al. (2007) and Justiniano-Preston (2010)
- Exchange rate dynamics
- International trade
- Uncovered interest parity condition
- Global shock transmission
Fiscal Policy Extensions
- Based on recent central bank models (e.g., Leeper, Traum, Walker)
- Detailed government sector
- Various fiscal rules
- Tax distortions
- Government spending multipliers
Perturbation Methods
- First-order (linear) approximation
- Second-order approximation (capturing some non-linearities)
- Third-order approximation (better accuracy with larger shocks)
- Policy and transition functions
Projection Methods
- Collocation with Chebyshev polynomials
- Finite elements method option
- Handling of occasionally binding constraints
Data Acquisition
- Automatic fetching from FRED using pandas_datareader
- Support for key macroeconomic series:
- GDP
- Inflation
- Interest rates
- Consumption
- Investment
- Wages
- Employment/hours worked
Data Processing
- Transformation to stationary series
- Detrending options (HP filter, one-sided filters, etc.)
- Seasonal adjustment
- Outlier detection and handling
- Missing value imputation
Bayesian Estimation
- MCMC implementation (Metropolis-Hastings algorithm)
- Prior distribution configuration
- Proposal density adaptation
- Convergence diagnostics
- Posterior analysis
Model Evaluation
- Marginal likelihood calculation
- Model comparison metrics
- In-sample fit statistics
- Out-of-sample forecast evaluation
Impulse Response Functions
- Linear and non-linear IRFs
- Conditional IRFs
- Confidence bands
Decompositions
- Historical shock decomposition
- Variance decomposition
- Forecast error variance decomposition
Diagnostic Tools
- Parameter identification analysis
- Sensitivity analysis
- Robustness checks
Baseline Forecasting
- Point forecasts
- Density forecasts
- Conditioning on observables
Scenario Analysis
- Alternative policy scenarios
- Stress testing
- Counterfactual analysis
Uncertainty Visualization
- Fan charts
- Probability bands
- Density plots
Publication-Quality Figures
- LaTeX-compatible outputs
- High-resolution images
- Customizable styles
- Automated figure generation
The project includes a comprehensive testing framework to ensure code quality and correctness:
- Unit Tests: Test individual components in isolation
- Integration Tests: Test interactions between components
- Functional Tests: Test end-to-end workflows
- Performance Tests: Measure execution time and resource usage
- Automated Test Discovery: Tests are automatically discovered and executed
- Code Coverage: Measure code coverage to identify untested code
- Test Reports: Generate detailed reports of test results
- Mock Data Generation: Generate synthetic data for testing
- Fixtures: Common test fixtures for reuse across tests
- Parameterized Tests: Test multiple inputs with a single test function
The master test script (tests/run_tests.py) provides a unified interface for running tests:
# Run all tests
python tests/run_tests.py --all
# Run specific test types
python tests/run_tests.py --unit
python tests/run_tests.py --integration
python tests/run_tests.py --functional
python tests/run_tests.py --performance
# Run tests with coverage
python tests/run_tests.py --all --coverage
# Generate a test report
python tests/run_tests.py --all --report test_report.jsonFor more information about the testing framework, see the tests/README.md file.
- Set up project structure and basic package organization
- Implement configuration system
- Create data fetching and processing modules
- Develop base model equations and steady state solver
- Implement perturbation methods (1st, 2nd, 3rd order)
- Develop projection methods
- Create testing framework for solution accuracy
- Implement financial frictions module
- Develop open economy extensions
- Create fiscal policy enhancements
- Ensure proper integration with base model
- Implement Bayesian estimation framework
- Develop analysis tools (IRFs, decompositions)
- Create model diagnostics
- Build forecasting capabilities
- Implement visualization library
- Create comprehensive documentation
- Develop example scripts
- Write unit tests
- numpy, scipy: Numerical computing
- pandas, pandas_datareader: Data management and FRED access
- matplotlib, seaborn: Visualization
- PyMC: Bayesian estimation (minimal external dependencies)
- sympy: Symbolic manipulation for model equations
- numba: JIT compilation for performance-critical routines
- jax: Automatic differentiation and GPU acceleration (advanced users)
- pytest: Test framework
- coverage: Code coverage measurement
- pytest-benchmark: Performance testing
sequenceDiagram
participant User
participant Config as Configuration
participant Data as Data Module
participant Model as Model Core
participant Solution as Solution Method
participant Estimation as Estimation Engine
participant Analysis as Analysis Tools
participant Viz as Visualization
User->>Config: Set parameters & options
Config->>Data: Configure data sources
Data->>Data: Fetch from FRED
Data->>Data: Preprocess data
Config->>Model: Configure model structure
Model->>Model: Initialize with extensions
Config->>Solution: Select method & order
Solution->>Model: Solve model
Model->>Estimation: Provide structure
Data->>Estimation: Provide processed data
Estimation->>Estimation: Run MCMC
Estimation->>Analysis: Estimated parameters
Analysis->>Analysis: Compute IRFs, decompositions
Analysis->>Viz: Results for visualization
Viz->>User: Diagnostic plots & statistics
sequenceDiagram
participant User
participant Config as Configuration
participant Model as Estimated Model
participant Forecast as Forecasting Engine
participant Scenario as Scenario Generator
participant Viz as Visualization
User->>Config: Set forecast parameters
Config->>Model: Load estimated model
Model->>Forecast: Provide structure & parameters
Forecast->>Forecast: Generate baseline forecast
User->>Scenario: Define alternative scenarios
Scenario->>Forecast: Modify assumptions
Forecast->>Forecast: Generate scenario forecasts
Forecast->>Viz: Forecast results
Viz->>Viz: Create fan charts & distributions
Viz->>User: Forecast visualizations
sequenceDiagram
participant User
participant Runner as Test Runner
participant Unit as Unit Tests
participant Integration as Integration Tests
participant Functional as Functional Tests
participant Performance as Performance Tests
participant Coverage as Coverage Tool
participant Report as Test Report
User->>Runner: Run tests
Runner->>Unit: Execute unit tests
Unit->>Runner: Unit test results
Runner->>Integration: Execute integration tests
Integration->>Runner: Integration test results
Runner->>Functional: Execute functional tests
Functional->>Runner: Functional test results
Runner->>Performance: Execute performance tests
Performance->>Runner: Performance test results
Runner->>Coverage: Measure code coverage
Coverage->>Runner: Coverage results
Runner->>Report: Generate test report
Report->>User: Test summary and details
The implementation follows Python best practices:
- Type hints throughout the codebase
- Comprehensive docstrings following NumPy/Google style
- Exception handling with custom exceptions
- Unit tests for all components
- Integration tests for workflows
- Documentation generation from docstrings
For bug reports, feature requests, and contributions, please open an issue or pull request on the GitHub repository.
This project is licensed under the MIT License - see the LICENSE file for details.
If you use this implementation in your research, please cite:
@software{smets_wouters_dsge,
author = {Research Team},
title = {Smets and Wouters DSGE Model Implementation},
year = {2025},
url = {https://github.com/username/macroeconomic-sim}
}
And the original Smets and Wouters (2007) paper:
@article{smets2007shocks,
title={Shocks and frictions in US business cycles: A Bayesian DSGE approach},
author={Smets, Frank and Wouters, Rafael},
journal={American economic review},
volume={97},
number={3},
pages={586--606},
year={2007}
}