A Python tool for analyzing, visualizing, and stress testing US Treasury yield curves using data from the Federal Reserve Economic Data (FRED).
- Fetch historical Treasury yield data across multiple maturities (1M to 30Y)
- Calculate and visualize yield curves at specific dates
- Generate historical yield visualizations
- Calculate forward rates (1y1y, 2y1y, 5y5y)
- Perform spread analysis (2s10s, 3m10y, 5s30s)
- Conduct stress testing based on historical moves
- Generate interactive plots using Plotly
- Python 3.7+
- pip package manager
pip install pandas pandas-datareader numpy plotly typing
from datetime import datetime from us_yield_curve_analyzer import USYieldCurveAnalyzer
analyzer = USYieldCurveAnalyzer()
yields_data = analyzer.fetch_yields(start_date=datetime(2015, 1, 1))
yield_curve = analyzer.plot_yield_curve() yield_curve.show()
historical = analyzer.plot_historical_yields(start_year=2015) historical.show()
forward_rates = analyzer.calculate_forward_rates() print(forward_rates.tail(1))
forward_plot = analyzer.plot_forward_rates() forward_plot.show()
stress_results = analyzer.stress_test_yields() print(stress_results)
stress_plot = analyzer.plot_stress_test_results(stress_results) stress_plot.show()
fetch_yields(start_date): Fetches Treasury yield data from FREDplot_yield_curve(date): Plots the yield curve for a specific dateplot_historical_yields(start_year): Visualizes historical yields across different maturities
calculate_forward_rate(start_tenor, end_tenor): Calculates forward rates between two tenorscalculate_forward_rates(): Calculates common forward rate combinations (1y1y, 2y1y, 5y5y)calculate_spreads(): Computes yield curve spreads (2s10s, 3m10y, 5s30s)
calculate_historical_moves(lookback_days): Analyzes historical yield movementsstress_test_yields(lookback_days): Performs stress testing based on historical scenariosplot_stress_test_results(stress_results): Visualizes stress test outcomes
The analyzer uses the following Treasury maturities:
- 1-month (1M)
- 3-month (3M)
- 6-month (6M)
- 1-year (1Y)
- 2-year (2Y)
- 3-year (3Y)
- 5-year (5Y)
- 7-year (7Y)
- 10-year (10Y)
- 20-year (20Y)
- 30-year (30Y)
The tool implements comprehensive error handling and logging:
- Failed data fetches are logged with detailed error messages
- Invalid tenor specifications raise ValueError
- Missing data scenarios are handled gracefully
All visualizations are created using Plotly, providing:
- Interactive plots
- Hover information
- Customizable layouts
- Export capabilities
The tool uses Python's built-in logging module with:
- INFO level for successful operations
- ERROR level for failed operations
- Detailed error messages for debugging
Feel free to submit issues and enhancement requests!