In this exercise, the author will use QuantLib to construct a curve based on short term rates and par bond yields. Lets assume we have the following curve represented as python dictionary:
These two curves have to be defined. At the very minimum, we need to know the day count convention and the accrual convention for each curve. Let defined them using QuantLib. For st_curve, we will need to use ql.DepositRateHelper, while the lt_curve will be using qlFixedRateHelper. Before using these classes we need to import QuantLib.
We also need to define the value date of these curves. Lets assumed a single value date of June 17, 2020. In the python module add the following:
Note: ql.Date is overloaded as most other classes in QuantLib. ql.Date is documented here.
Lets generate our discount factor curve from the short term curve prior to combining the two curves. To do so, we need to use ql.DepositRateHelper (documentation is here) which requires several parameters namely:
ql.DepositRateHelper basically defines a point on the short term curve with information listed above. Since we have 4 points in our st_curve, we will need to create a list.
The above code is a list comprehension which iterates through our dictionary to create a new list called st_helpers. ql.SimpleQuote(st_curve[key]/100.0) convert our rate which was in percentage to a simple float while ql.Period(key) convert the key of our st_curve dictionary to a ql.Period object. The iteration is done by for key in st_curve.
Now we have all that we need to transform our data to QuantLib Yield Term Structure object generally called Piecewise. There are several method available (documentation is here) and for our exercise we will be using ql.PiecewiseLogCubicDiscount which requires:
Unfortunately, that is not the end of it. We cant actually see the result and we are interested in seeing the discount factors it generated. Dont worry. ql.PiecewiseLogCubicDiscount returns a ql.YieldTermStructure class which has several public member functions including:
Take not of "date": x.ISO() in the above code. .ISO() is used to convert ql.Date to ISO standard date string ('yyyy-mm-dd' format). This allow passing of the value as JSON.
Now we can use pandas to see the result in DataFrame, print to your console/terminal or send as JSON as a response to a request. For interpolation purposes, however, a list of dictionary with string type date is not the best format. Instead, we have to create two lists containing time as a fraction of year and the discount factors. Conversion of dates to time as a fraction of a year is available but not documented in the python version but available in the C++ version. It is a public member function of ql.DayCounter class named as 'yearFraction'.
The following codes use the ql.DayCounter class to calculate time as fraction of a year (yearfrac in the code) and retrieve the discount factors from the ql.YieldTermStructure class:
Now suppose we want to find the discount factor for November 25, 2020. We should convert the date to time as a fraction of a year. The interpolation can be done using either python or QuantLib library. There several methods in QuantLib and the references are here. For illustartion we use ql.LogCubicNaturalSpline as shown below:
Using QuantLib's interpolation is similar to interpolation in scipy.interpolate packages. ql.LogCubicNaturalSpline returns a class or function which we assigned a variable named ipolate. ipolate is then fed with the relevant parameters - itime, the time for the rate to be interpolated and allowExtrapolation which is self explanatory. You can print irate to the console/terminal to see the result.
We have only worked with st_curve which is our short term rates. The long term rates lt_curve will need to be merged with st_curve - an endeavour we will undertake in the next part of our exercise.