2
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Students sometimes ask for project ideaa. Publishing lots of QuantLib reference implementations such as this would make gteat student projects. $\endgroup$ Commented Mar 28, 2023 at 14:34
  • $\begingroup$ I just released a python library called 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$
    – Attack68
    Commented May 1, 2023 at 9:33

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for your help. It seems that QuantLib does not have the attribute 'PiecewiseSplineZero' Also, trying the same code with 'PiecewiseCubicZero' will result in the errer "Wrong number or type of arguments for overloaded function". (I ran the code as is, without changing inputs) $\endgroup$ Commented Mar 30, 2023 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.