Educational Python library for interest rate term structure estimation
Project description
PyTermStructure
Educational Python library for interest rate term structure estimation
Inspired by Damir Filipović's "Interest Rate Models"
École Polytechnique Fédérale de Lausanne (EPFL)
Course Link
Version 0.1.0
NEW in v0.1.0: Bootstrap accuracy improvements
- Exact calendar date support with ACT/360 day-count
- Automatic curve densification with interpolated swap rates
- Three-phase bootstrap algorithm
- Accuracy: <1 bps on benchmark data (improved from ~13 bps)
Status: Educational implementation suitable for learning and research.
Recommendation: Use for educational purposes, research, and prototyping. For production systems requiring high precision, consider QuantLib or FinancePy.
Quick Start
Installation
pip install pytermstructure
Your First Term Structure (NEW: with exact dates!)
import pytermstructure as pts
from pytermstructure.core import MarketInstrument, InstrumentType
from datetime import datetime
# Create bootstrap with spot date
spot = datetime(2024, 1, 15)
bootstrap = pts.BootstrapMethod(verbose=True, spot_date=spot)
# Add LIBOR with exact date
bootstrap.add_instrument(MarketInstrument(
instrument_type=InstrumentType.LIBOR,
maturity=datetime(2024, 4, 15), # 3 months
quote=0.15, # 0.15%
spot_date=spot
))
# Add swap with exact date
bootstrap.add_instrument(MarketInstrument(
instrument_type=InstrumentType.SWAP,
maturity=datetime(2026, 1, 15), # 2 years
quote=0.50, # 0.50%
spot_date=spot
))
# Fit discount curve (automatic densification!)
discount_curve = bootstrap.fit()
# Get zero rates
zero_rates = bootstrap.get_zero_rates()
Getting Help
import pytermstructure as pts
# General help
pts.help()
# Method-specific help
pts.help("bootstrap")
pts.help("lorimier")
pts.help("accuracy") # NEW!
Features
Methods Implemented
| Method | Type | Accuracy | Use Case |
|---|---|---|---|
| Bootstrap | Exact | <1 bps | Educational, curve construction |
| Pseudoinverse | Exact | <1 bps | Smooth + exact pricing |
| Lorimier | Smooth | ±3 bps | Smooth forward curves |
| PCA | Analysis | N/A | Risk management |
| Nelson-Siegel | Parametric | N/A | Quick approximation |
Educational Quality
- Built-in help system with comprehensive documentation
- Based on academic course materials (Filipović, EPFL)
- Full typing support with type hints
- Validated with academic benchmark data
- Free Software (GNU GPLv3 license)
- Professional structure ready for contributions
Methods Overview
1. Bootstrap Method
Sequential construction from LIBOR → Futures → Swaps.
NEW in v0.1.0: Sub-basis-point accuracy with exact dates!
from datetime import datetime
spot = datetime(2024, 10, 3)
bootstrap = pts.BootstrapMethod(verbose=True, spot_date=spot)
bootstrap.add_instrument(pts.MarketInstrument(
pts.InstrumentType.LIBOR,
datetime(2025, 1, 3),
0.15,
spot_date=spot
))
bootstrap.add_instrument(pts.MarketInstrument(
pts.InstrumentType.SWAP,
datetime(2026, 10, 3),
0.50,
spot_date=spot
))
discount_curve = bootstrap.fit()
Accuracy: <1 bps on benchmark data (v0.1.0)
2. Lorimier Method
Smoothing splines with parameter α for smooth forward curves.
import numpy as np
# Swiss government bond yields
maturities = np.array([2, 3, 4, 5, 7, 10, 20, 30])
yields = np.array([-0.79, -0.73, -0.65, -0.55,
-0.33, -0.04, 0.54, 0.73]) / 100
lorimier = pts.LorimierMethod(alpha=0.1)
discount_curve = lorimier.fit(yields, maturities)
# Interpolate at 6 years
y_6y = lorimier.get_yield_at(6.0)
Accuracy: ±3 bps on benchmark data
3. PCA Analysis
Principal component analysis of yield curve movements.
pca = pts.PCAAnalysis()
eigenvalues, eigenvectors, explained_var = pca.fit(yield_changes)
print(f"Level: {explained_var[0]:.1f}%")
print(f"Slope: {explained_var[1]:.1f}%")
print(f"Curvature: {explained_var[2]:.1f}%")
Installation
From PyPI
pip install pytermstructure
From Source
git clone https://github.com/MarcoGigante/pytermstructure.git
cd pytermstructure
pip install -e .
Test Installation
python -c "import pytermstructure as pts; pts.version(); pts.help()"
Expected output:
PyTermStructure 0.1.0 loaded. For help: pts.help()
PyTermStructure 0.1.0
Author: Marco Gigante
License: GPLv3
Running Examples
# Example 1: Bootstrap US market
python examples/example_bootstrap.py
# Example 2: Pseudoinverse
python examples/example_pseudoinverse.py
# Example 3: Lorimier Swiss bonds
python examples/example_lorimier.py
# Run all examples
python examples/practical_examples.py
What's New in v0.1.0
Bootstrap Enhancements
-
Exact Date Support
Use real calendar dates instead of year fractions -
Automatic Densification
Automatically adds intermediate points with interpolated swap rates -
Three-Phase Algorithm
- Phase 1: Bootstrap market instruments (LIBOR, Futures, Swaps ≤20Y)
- Phase 2: Add intermediate dates (21-29Y) with interpolated rates
- Phase 3: Calculate long swaps (30Y+) using densified curve
-
ACT/360 Day-Count
Exact day-count calculations for all periods
Technical Improvements
- Added
python-dateutildependency for date handling - Enhanced
MarketInstrumentwithmaturity_datesupport - Implemented
_get_exact_swap_schedule()for ACT/360 - Removed unused dependencies (pandas, openpyxl)
Academic Reference
This library implements methods from:
Filipović, D. (2009). Term-Structure Models: A Graduate Course. Springer Finance.
Online Course: Interest Rate Models
École Polytechnique Fédérale de Lausanne (EPFL)
License
GNU General Public License v3.0 or later.
This ensures PyTermStructure remains free software forever.
See LICENSE for details.
Contributing
Contributions are welcome. Areas for improvement:
- Enhanced numerical methods
- Additional day-count conventions
- Business day calendars
- Curve analytics
- Additional validation tests
Please ensure GPL-compatible contributions.
See CONTRIBUTING.md for guidelines.
Author
Marco Gigante
MSc in Quantitative Finance, University of Siena
Inspired by Prof. Damir Filipović's course at EPFL.
Acknowledgments
- Prof. Damir Filipović (EPFL)
- École Polytechnique Fédérale de Lausanne
- NumPy, SciPy communities
- Free Software Foundation
Support
- Built-in Help:
pts.help() - Issues: GitHub Issues
- Documentation: pytermstructure.readthedocs.io
Changelog
v0.1.0 (November 17, 2025)
- Bootstrap accuracy improvements (<1 bps on benchmark data)
- Exact calendar date support with ACT/360
- Automatic curve densification
- Three-phase bootstrap algorithm
- Enhanced documentation and help system
v0.0.1 (Initial Release)
- Bootstrap, Lorimier, PCA, Nelson-Siegel methods
- Built-in help system
- Educational implementation
See CHANGELOG.md for complete history.)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytermstructure-0.1.0.tar.gz.
File metadata
- Download URL: pytermstructure-0.1.0.tar.gz
- Upload date:
- Size: 34.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6845adcf2282c0453f62f3a05f3e176992a28bec8810f54862215cfa9dc3e4d1
|
|
| MD5 |
01e2c0a646cc3f919a22c07d5440fa2a
|
|
| BLAKE2b-256 |
e8fe006ca5339a8adfebeaba3da58e535f7f32b7dfdbeeaee242921f14c9c250
|
Provenance
The following attestation bundles were made for pytermstructure-0.1.0.tar.gz:
Publisher:
publish.yml on MarcoGigante/pytermstructure
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytermstructure-0.1.0.tar.gz -
Subject digest:
6845adcf2282c0453f62f3a05f3e176992a28bec8810f54862215cfa9dc3e4d1 - Sigstore transparency entry: 705451502
- Sigstore integration time:
-
Permalink:
MarcoGigante/pytermstructure@4a87609c72408a80da40fcb20718c3d691b5cd30 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/MarcoGigante
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4a87609c72408a80da40fcb20718c3d691b5cd30 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytermstructure-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytermstructure-0.1.0-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b5f6b23a67779311ab5455ce8794f2eef718f4ce50ac6005c18785cac425b64
|
|
| MD5 |
b08bcd906ffa9cb12e9579af2bf33f6f
|
|
| BLAKE2b-256 |
dec170e0a6ff98166846ad63f315a54b1145faeb7b17dc7db8b42cd36a081ccc
|
Provenance
The following attestation bundles were made for pytermstructure-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on MarcoGigante/pytermstructure
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytermstructure-0.1.0-py3-none-any.whl -
Subject digest:
9b5f6b23a67779311ab5455ce8794f2eef718f4ce50ac6005c18785cac425b64 - Sigstore transparency entry: 705451508
- Sigstore integration time:
-
Permalink:
MarcoGigante/pytermstructure@4a87609c72408a80da40fcb20718c3d691b5cd30 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/MarcoGigante
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4a87609c72408a80da40fcb20718c3d691b5cd30 -
Trigger Event:
release
-
Statement type: