1
$\begingroup$

Is it possible to create yield curve object in Quantlib given some function of time? For example, given Nelson-Siegel parameters, create yield curve which can compute zero yield for any date >= reference date.

$\endgroup$

1 Answer 1

1
$\begingroup$

Yes, it is possible. You can feed bond instruments with prices and fit Nelson-Siegel parameters.

Try this simple example:

import QuantLib as ql
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

today = ql.Date(30,12,2019)
ql.Settings.instance().evaluationDate = today

bonds = pd.DataFrame({
    'maturity': ['30-03-2020','30-12-2020', '15-06-2021', '17-10-2022', '17-04-2023'],
    'coupon': [0,1.25, 3.40, 6.40, 2.25],
    'price': [99.88, 99.75, 101.98, 108.70, 96.46]
})

calendar = ql.TARGET()
bondSettlementDays = 2
bondSettlementDate = calendar.advance(today, ql.Period(2, ql.Days))
frequency = ql.Annual
dc = ql.ActualActual(ql.ActualActual.ISMA)
accrualConvention = ql.ModifiedFollowing
convention = ql.ModifiedFollowing
redemption = 100.0

instruments = []
for idx, row in bonds.iterrows():
    schedule = ql.Schedule(
        bondSettlementDate,
        ql.Date(row.maturity, '%d-%m-%Y'),
        ql.Period(frequency),
        calendar,
        accrualConvention,
        accrualConvention,
        ql.DateGeneration.Backward,
        False)
    helper = ql.FixedRateBondHelper(
            ql.QuoteHandle(ql.SimpleQuote(row.price)),
            bondSettlementDays,
            100.0,
            schedule,
            [row.coupon / 100],
            dc,
            convention,
            redemption)

    instruments.append(helper)    

curveSettlementDays = 2
curveFittingMethod = ql.NelsonSiegelFitting()
tolerance = 1.0e-5
iterations = 1000

curve_ns = ql.FittedBondDiscountCurve(curveSettlementDays, calendar, instruments,
                                          dc, curveFittingMethod, tolerance, iterations)
t = np.linspace(1,3.25)
rates_ns = [curve_ns.zeroRate(n, ql.Annual).rate() for n in t]
plt.figure(figsize=(15,5)),
plt.plot(t, rates_ns);
$\endgroup$

Your Answer

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

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