Skip to content

About

Bootstrap arbitrage-free yield curves from US Treasury CMT rates using piecewise linear or monotone cubic forward interpolation

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

20 Commits

Folders and files

Repository files navigation

Treasury CMT Yield Curve Bootstrap

Python 3.8+ License: MIT

Bootstrap discount factors and zero-coupon yield curves from US Treasury CMT rates.

Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Download Historical Data

Download the Treasury par yield curve data (1990-2023) from: https://home.treasury.gov/resource-center/data-chart-center/interest-rates/TextView?type=daily_treasury_yield_curve

Save as: data/par-yield-curve-rates-1990-2023.csv

3. Build Initial Dataset

# All data from 1990
python scripts/build_initial_treasury_file.py

# Or filter to recent years (recommended for faster processing)
python scripts/build_initial_treasury_file.py --start-date 2022-01-01

This creates Treasury_CMT_Data_Tool.xlsx with combined historical + current data.

4. Run Bootstrap

# Scheme 2 (piecewise linear forward - recommended)
python scripts/run_bootstrap.py --scheme 2

# Scheme 3 (monotone cubic forward - smoother) with Excel output
python scripts/run_bootstrap.py --scheme 3 --write-excel

Outputs:

  • Treasury_CMT_Data_Tool_curves_S2_YYYY-YYYY.npz (compressed data)
  • Treasury_CMT_Data_Tool_curves_S2_YYYY-YYYY.xlsx (human-readable)

5. Update Data (Ongoing)

python scripts/update_all_data.py --scheme 2

Refreshes Treasury CMT par rates and the SOFR/Fed-Funds short-rate history (data/short_rates/short_rate_combined.csv), then re-runs the bootstrap. The short-rate refresh matters for Schemes 2/3, which anchor to it β€” Scheme 1 doesn't use it. Running update_treasury_cmt.py alone updates the workbook but leaves the short-rate anchor stale for S2/S3; cmt_bootstrap.py will print a WARNING if that anchor falls more than 5 days behind the newest curve date. See Workflow 2 in the Bootstrap Guide for scheduling this as a daily cron/Task Scheduler job.

πŸ“Š Interactive Visualization

Explore bootstrapped yield curves with mathematically correct reconstruction:

# Run with sample data (2022-2026 included)
python -m streamlit run scripts/yield_curve_app.py

Why Curve Reconstruction Matters

Most yield curve tools use linear interpolation between tenor points - this is mathematically incorrect!

Our approach reconstructs continuous curves from bootstrap parameters:

  • S1: Piecewise constant forwards β†’ Exponential discount decay
  • S2: Piecewise linear forwards β†’ Smooth exponential decay
  • S3: Monotone cubic forwards β†’ Perfectly smooth curves

Features

Tab 1: Yield Curves

  • Par, spot, and forward rates (smooth reconstruction)
  • Discount factor curves (exponential decay)
  • Spot-par spread analysis
  • Data table with CSV export (expandable)

S3 Smooth Curves Scheme 3 shows perfectly smooth forward rates - true cubic interpolation, not linear approximation

Tab 2: Spread Analysis

  • Compare any two tenors (e.g., 10Y-2Y inversion)
  • Date range filtering
  • Distribution histograms with statistics

Tab 3: Forward Projections

  • Forward term structure: f(0, s, s+M)
  • Multiple tenors (1Mo to 30Yr)
  • Shows market rate expectations
  • Progressive smoothing demonstration

Forward Term Structure Progressive smoothing: 1Mo forward is jagged, 10Yr forward is smooth - mathematics visualized

See Visualization Guide for complete documentation.

Volatility Analysis App

Span-weighted PCA and tail-distribution fitting on S1 forward rate changes.

python -m streamlit run scripts/vol_analysis_app.py

Loading Data

Unlike the yield curve app, this app does not auto-detect sample files β€” point it at an NPZ panel yourself:

  • Click πŸ“‚ in the sidebar to open a native file browser, or
  • Paste a path directly into the "NPZ file" field and press Enter

⚠️ Requires an S1 (piecewise-constant forward) bootstrap file specifically β€” the PCA and tail-fit math is derived for S1's step-function forwards; S2/S3 files will load but produce incorrect results.

Ready-to-use sample: data/samples/Treasury_CMT_Data_Tool_curves_S1_1990-2026.npz (Jan 1990 – Jul 2026, ships with the repo) β€” the full history gives the PCA and tail fits far more observations than the 2022-2026 sample.

To generate your own S1 file:

python scripts/run_bootstrap.py --scheme 1 --write-excel

This produces Treasury_CMT_Data_Tool_curves_S1_<start>-<end>.npz (see Output Files).

Features

Tab 1 β€” PCA: Waterfall scree plot of variance explained, interactive PC loadings chart, and variance table. Variance threshold (90/95/99%) selector highlights the minimum PC count needed.

Tab 2 β€” Tail Distribution & ALCO Scenarios:

  • Corrected F(k, Ξ½) QQ plots for both k=5 (reduced-rank) and k=14 (full-rank) fits, with the C vs Ξ£ inflation factor c=(Ξ½-2)/Ξ½ applied throughout.
  • Seven preset ALCO stress scenarios (parallel, bear/bull steepen/flatten, bell, bowl) all calibrated to the same dΒ² as a parallel +100 bp/quarter shock. Five anchor sliders (3Mo/5Yr/10Yr/20Yr/30Yr) control custom scenarios; the remaining nine tenors are linearly interpolated and shown in gray.
  • Severity score = P(DΒ² ≀ dΒ²_scenario) under the fitted distribution.

Bootstrap Bench (Standalone Web Tool)

A single-file, client-side what-if tool for Scheme 3 bootstrapping β€” no Python, no server. Move par-rate sliders and a short-rate anchor (r0) and it redraws the implied instantaneous forward, spot, and discount curves out to 360 months live, with a round-trip fit check. Also includes a paste-in importer for Treasury.gov's daily par yield table, and an "Export 30Y forward curves" button that produces an .xlsx workbook of forward-implied par yield / spot / discount grids (quarterly, 0–30Y, with flat-forward extrapolation past 30Y).

# Open directly in a browser -- no install, no build step
open web/bootstrap-bench/index.html

See web/bootstrap-bench/README.md for details, including how to import it into CodeSandbox or another static host.

Note: the bootstrap math here is a JS port of src/cmt_bootstrap.py's Scheme 2/3 logic (verified to match to 10 decimal places at the time of writing) β€” kept in this repo specifically so it stays visible next to the Python it mirrors. If cmt_bootstrap.py's Scheme 2/3 math changes, re-verify this file against it.

Project Structure

cmt-yield-curve-bootstrap/
β”œβ”€β”€ README.md                          # This file
β”œβ”€β”€ LICENSE
β”œβ”€β”€ requirements.txt                   # Python dependencies
β”œβ”€β”€ RELEASE_NOTES.md                   # Per-release changelog
β”œβ”€β”€ INSTALLATION_GUIDE.md              # Setup + troubleshooting
β”œβ”€β”€ .gitignore
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ cmt_bootstrap.py               # Core bootstrap algorithms
β”‚   └── vol_analysis.py                # Span-weighted PCA + F(k,Ξ½) tail fit
β”‚
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ run_bootstrap.py              # Wrapper to run bootstrap on Excel file
β”‚   β”œβ”€β”€ update_short_rates.py         # Combine SOFR/Fed Funds histories
β”‚   β”œβ”€β”€ update_treasury_cmt.py        # Fetch latest CMT rates from Treasury API
β”‚   β”œβ”€β”€ update_all_data.py            # Daily entrypoint: chains the two updaters above + bootstrap
β”‚   β”œβ”€β”€ build_initial_treasury_file.py # One-time: build complete historical dataset
β”‚   β”œβ”€β”€ yield_curve_app.py            # Streamlit: curve visualization
β”‚   └── vol_analysis_app.py           # Streamlit: PCA + tail-risk / ALCO scenarios
β”‚
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ Treasury_CMT_Data_Tool.xlsx   # Main data file (generated by build script)
β”‚   β”œβ”€β”€ par-yield-curve-rates-1990-2023.csv  # Historical data (download from Treasury)
β”‚   β”œβ”€β”€ samples/                      # Ready-to-use bootstrapped NPZ files (S1/S2/S3)
β”‚   └── short_rates/
β”‚       β”œβ”€β”€ fed_funds_1954_2018.csv   # FRED DFF series (included)
β”‚       └── sofr_manual.csv           # SOFR baseline (included)
β”‚       # short_rate_combined.csv     # Generated by update_short_rates.py
β”‚
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ USER_GUIDE.md                 # Detailed usage guide
β”‚   β”œβ”€β”€ BOOTSTRAP_GUIDE.md            # Bootstrap methodology
β”‚   β”œβ”€β”€ VISUALIZATION_GUIDE.md        # Yield curve app usage guide
β”‚   β”œβ”€β”€ CURVE_RECONSTRUCTION.md       # Curve reconstruction math
β”‚   └── images/                       # Screenshots used in this README
β”‚
└── web/
    └── bootstrap-bench/
        β”œβ”€β”€ index.html                 # Standalone what-if tool (see above)
        └── README.md                  # Tool-specific docs + hosting notes

V1.1 Directory Structure:

cmt-yield-curve-bootstrap/
β”œβ”€β”€ README.md                        ← UPDATE
β”œβ”€β”€ LICENSE
β”œβ”€β”€ requirements.txt                 ← UPDATED
β”œβ”€β”€ .gitignore                      ← UPDATE
β”œβ”€β”€ RELEASE_NOTES.md                ← NEW
β”‚
β”œβ”€β”€ src/
β”‚   └── cmt_bootstrap.py
β”‚
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ yield_curve_app.py          ← NEW
β”‚   β”œβ”€β”€ curve_reconstruction.py     ← NEW
β”‚   β”œβ”€β”€ build_initial_treasury_file.py
β”‚   β”œβ”€β”€ update_treasury_cmt.py
β”‚   β”œβ”€β”€ update_short_rates.py
β”‚   └── run_bootstrap.py
β”‚
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ samples/                    ← NEW
β”‚   β”‚   β”œβ”€β”€ README.md               ← NEW
β”‚   β”‚   β”œβ”€β”€ Treasury_CMT_curves_S1_2022_2026.npz  ← NEW
β”‚   β”‚   β”œβ”€β”€ Treasury_CMT_curves_S2_2022_2026.npz  ← NEW
β”‚   β”‚   └── Treasury_CMT_curves_S3_2022_2026.npz  ← NEW
β”‚   └── short_rates/
β”‚       β”œβ”€β”€ fed_funds_1954_2018.csv
β”‚       └── sofr_manual.csv
β”‚
└── docs/
    β”œβ”€β”€ USER_GUIDE.md
    β”œβ”€β”€ BOOTSTRAP_GUIDE.md
    β”œβ”€β”€ VISUALIZATION_GUIDE.md      ← NEW
    β”œβ”€β”€ CURVE_RECONSTRUCTION.md     ← NEW
    β”œβ”€β”€ INSTALLATION_GUIDE.md       ← NEW
    └── images/                     ← NEW
        β”œβ”€β”€ yield_curves_s1.png
        β”œβ”€β”€ yield_curves_s3.png
        β”œβ”€β”€ spread_analysis.png
        └── forward_projections.png

Bootstrap Schemes

Scheme 1: Piecewise constant instantaneous forward

  • Simplest, fast
  • Discontinuous forwards

Scheme 2: Piecewise linear instantaneous forward

  • Good balance of speed and smoothness
  • Recommended for most use cases

Scheme 3: Monotone cubic instantaneous forward

  • Smoothest curves
  • Slower computation
  • Best for derivatives pricing

Output Files

NPZ File (Primary)

Compressed NumPy archive with:

  • Par rates (input)
  • Discount factors at each tenor
  • Spot (zero) rates (continuous compounding)
  • Forward rates at tenor endpoints
  • Bootstrap parameters (scheme-dependent)
  • Validation: implied par rates, round-trip errors

Excel File (Optional)

# Output to excel modifier
python scripts/run_bootstrap.py --scheme 2 --write-excel

Human-readable tables:

  • Par Rates (input)
  • Discount Factors
  • Spot Rates (cc)
  • Forward @ TenorEnd
  • Par Rates (implied)
  • Round-Trip Error (bp)
  • Bootstrap parameters

Data Sources

CMT Rates: US Treasury Department

  • Historical: CSV download (1990-2023)
  • Recent: XML API (2024-present)

Short Rates (rβ‚€ anchor):

  • SOFR: 2018-present (preferred), fetched from the NY Fed markets API by scripts/update_short_rates.py
  • Effective Fed Funds: 1954-2018 (fallback, static β€” included in data/short_rates/, no refresh needed)

Notes

  • Par rates interpreted as swap/coupon-equivalent rates
  • Missing tenors are skipped (no interpolation)
  • Payment frequency: 24 per year (semi-monthly, configurable with --nu)
  • All rates stored in decimal form (0.0555 = 5.55%)

References

License

MIT License


About

Bootstrap arbitrage-free yield curves from US Treasury CMT rates using piecewise linear or monotone cubic forward interpolation

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages