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.
rateslib
. The documentation for it is available here (rateslib.readthedocs.io/en/latest/g_curves.html). I expect it can accomplish what you want. $\endgroup$