As I already mentioned, IKB, Quaternion and d-fine are organizing a two-days QuantLib workshop in Düsseldorf on November 13th and 14th. The agenda is here; if you’re interested, send an email to the contacts listed in there. Registration is free; there are just 35 places, though.
Example: adding z-spread to an interest-rate curve
This example (a lot simpler than the previous one) shows how to build a term-structure based on another one. We’ll take an existing risk-free curve and modify it to include credit risk. The risk is expressed as a z-spread, i.e., a constant spread to be added to the zero-yield rates. For the pattern-savvy, this is an application of the Decorator pattern; we’ll wrap an existing object, adding some behavior and delegating the rest to the original instance.
Listing 3.12: Implementation of the
The implementation of the
ZeroSpreadedTermStructure is shown in
listing 3.12. As previously mentioned, it is based on zero-yield
rates; therefore, it inherits from the
described here and will have to
implement the required
zeroYieldImpl method. Not surprisingly, its
constructor takes as arguments the risk-free curve to be modified and
the z-spread to be applied; to allow switching data sources, both are
passed as handles. The arguments are stored in the corresponding data
members, and the curve registers with both of them as an observer. The
update method inherited from the base class will take care of
forwarding any received notifications.
Note that none of the base-class constructors was called
explicitly. As you might remember from this post, this means that
instances of our curve store no data that can be used by the
TermStructure machinery; therefore, the class must provide its own
implementation of the methods related to reference-date
calculation. In true Decorator fashion, this is done by delegating
behavior to the wrapped object; each of the
forwards to the corresponding method in the risk-free curve.
Finally, we can implement our own specific behavior—namely,
adding the z-spread. This is done in the
zeroYieldImpl method; we
ask the risk-free curve for the zero-yield rate at the required time
(continuously compounded, since that’s what our method must return),
add the value of the z-spread, and return the result as the new
zero-yield rate. The machinery of the
will take care of the rest, giving us the desired risky curve.