Bootstrapping Yield Curves in Python with QuantLib and Futures
Summary
The document asks how to translate a Matlab yield curve bootstrap into Python for multiple currencies using three-month futures. An answer proposes QuantLib, outlining a workflow that sets curve dates and market quotes, applies a time-dependent convexity adjustment, creates rate helpers, and bootstraps a spline zero curve. The example also shows retrieving zero rates from the resulting curve.
The code is illustrative rather than a verified one-to-one translation. Its sample dates, quotes, day count, compounding, calendar, instrument helper, and adjustment assumptions may need to change for a real market and currency. In particular, the question concerns futures while the example builds deposit rate helpers, so instrument conventions and convexity handling should be checked before relying on the curve. No comparison against the Matlab output or numerical validation is provided.
Key ideas
- QuantLib provides Python tools for bootstrapping curves with different interpolation choices.
- The example applies a maturity-dependent convexity adjustment to market quotes before curve construction.
- A bootstrapped curve can provide zero rates and other term structure measures.
- The sample uses deposit helpers despite the question involving futures, so instrument conventions need review.
- The document does not demonstrate equivalence with the Matlab implementation or validate the output.
Tags
Full text
# Python yield curve bootstrapping equivalent to Matlab IRDataCurve.bootstrap
# Python yield curve bootstrapping equivalent to Matlab IRDataCurve.bootstrap
I want to bootstrap the yield curve of multiple currencies using 3M futures. I have it implemented in Matlab, but need to transfer to Python. I have a vector of dates and a vector of future prices.
Could somebody help me finding the python equivalent to Matlab's:
`IRDataCurve.bootstrap('Zero', quote_date, instrument_types, instruments,'InterpMethod','spline','Basis',2,'Compounding',-1,'IRBootstrapOptions', IRBootstrapOptions('ConvexityAdjustment',@(t) .5*sigma^2.*t.^2));`
Is it possible to replicate?
I found some options with QuantLib:
- PiecewiseLogLinearDiscount
- PiecewiseLogCubicDiscount
- PiecewiseLinearZero
- PiecewiseCubicZero
- PiecewiseLinearForward
- PiecewiseSplineCubicDiscount
But there is very little documentation.
Help would be very much appreciated
## Answer by Lautaro Parada (score 1)
https://quant.stackexchange.com/a/75065
You can bootstrap a yield curve using the QuantLib library in Python. First, you need to install QuantLib for Python by running:
```
pip install QuantLib-Python
```
This is an example of how to bootstrap a yield curve using QuantLib (Python equivalent code of the Matlab code you've published):
```
import QuantLib as ql
# Set up parameters
quote_date = ql.Date(28, 3, 2023)
sigma = 0.01 # Adjust based on your data
yield_curve_basis = ql.Actual365Fixed()
compounding = ql.Simple
day_count = ql.Actual365Fixed()
# Set up your dates and future prices
dates = [ql.Date(30, 6, 2023), ql.Date(30, 9, 2023), ql.Date(31, 12, 2023)] # Replace with your dates
future_prices = [0.01, 0.015, 0.02] # Replace with your future prices
# Calculate convexity adjustments
time_to_maturities = [(date - quote_date) / 365.0 for date in dates]
convexity_adjustments = [0.5 * sigma ** 2 * t ** 2 for t in time_to_maturities]
# Create instruments
instrument_types = [ql.DepositRateHelper(ql.QuoteHandle(ql.SimpleQuote(price + adjustment)),
ql.Period(3, ql.Months),
2,
ql.TARGET(),
ql.ModifiedFollowing,
False,
day_count) for price, adjustment in zip(future_prices, convexity_adjustments)]
# Bootstrap yield curve
curve = ql.PiecewiseSplineZero(quote_date, instrument_types, yield_curve_basis, compounding)
# Print zero rates
for date, t in zip(dates, time_to_maturities):
zero_rate = curve.zeroRate(t, compounding).rate()
print(f"Zero rate for {date}: {zero_rate * 100:.2f}%")
```
This code sets up the required instruments and bootstraps the yield curve using a cubic spline interpolation method. The `curve` object is a `PiecewiseSplineZero` instance representing the bootstrapped yield curve. You can use this object to obtain zero rates, discount factors, or forward rates as needed.
Please note that you might need to adjust the parameters and input data to match your specific requirements.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.