Provides an example of the concept of introspection in QTK
Last couple of months, I have been working on the infrastructure of QTK
. I annouced the first point release a couple of weeks back after I implemented the core internals. Recently, I implemented the workflow paradigm for qtk
. I will highlight some of the core concepts here. The Template
in QTK
is like a blueprint for what you want to accomplish. Let us see how to create a flat forward curve using some of the introspection concepts introduced in 0.1.3 onwards.
from qtk import Template as T, Controller
The above statement is used to import the template. Now you can use the tab
functionality in ipython to get all the different types of templates. Now if you want to get the description of how to create this template, then all you do is execute that template as shown below. This is going to produce a rather nice output for you to view with the required variables and such.
T.TS_YIELD_FLAT
Or if you use a simple python console, then you can use the help to print the info in Markdown
format. If you just need the help string, then you can use the info
method.
T.TS_YIELD_FLAT.help()
In order to create a yield curve, you can use the sample_data
method attached to the template to get a sample dictionary object. This is what it looks like.
T.TS_YIELD_FLAT.sample_data()
{'AsOfDate': 'Optional (Date)', 'Compounding': 'Optional (Compounding)', 'CompoundingFrequency': 'Optional (Frequency)', 'Currency': 'Required (String)', 'DiscountBasis': 'Optional (DayCount)', 'Extrapolation': 'Optional (Boolean)', 'ForwardRate': 'Required (Float)', 'ObjectId': 'Optional (String)', 'SettlementCalendar': 'Optional (Calendar)', 'SettlementDays': 'Optional (Integer)', 'Template': 'TermStructure.Yield.FlatCurve'}
You can prune the optional fields to just pass the required items. It is worth pointing out that sometimes when one of two keywords need to be provided, both would be marked as optional. Looking at the documentation as listed by info
or help
methods will clarify the usage. For example, the fixed rate bond template INSTRUMENT_BOND_TBOND
has Coupon
and ListOfCoupon
both marked as optional fields. But this is because you can provide either one of the two as inputs, but atleast one of the two has to be provided. This is clarified in the documentation.
zero_curve = {"Template": T.TS_YIELD_FLAT.id, "ForwardRate": 0.02, "Currency": "USD", "ObjectId": "FlatCurve.USD"}
data = [zero_curve]
c = Controller(data)
out = c.process("1/15/2016")
ts = c.object("FlatCurve.USD")
ts.referenceDate()
Date(20,1,2016)
import QuantLib as ql
time_grid = range(30)
zero = [ts.zeroRate(t, ql.Compounded).rate() for t in time_grid]
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(time_grid, zero)
plt.xlabel("Time", size=12)
plt.ylabel("Zero Rate", size=12)
<matplotlib.text.Text at 0x7f1f95768690>