This time, I wanted to share some fruits of woodshedding QuantLib way to build up piecewise term structure. As I was initially tackling through the example files found in QL downloadable package and the other relevant examples found with Google, there was one obvious issue : with all those rates, quotes and handles involved, a lot of administrative code writing needs to be done first, in order to get any piecewise curve up and running. Since we need valuation curves anyway for everything we do with QL, there should be a manageable way to handle this issue.
One template to rule them all
For the reason mentioned above, I came up with an idea of auxiliary piecewise curve builder class. The purpose of this class would be simple : it could give a client a chance to assemble piecewise curve by adding arbitrary amount of different types of quotes (deposit, FRA or swap) and finally a client could request handle for assembled curve. The resulting curve handle could then be directly used in other QL programs. Effectively, this class would be wrapping some of the required administrative code work away from a client.
As an example of the usage of this PiecewiseCurveBuilder template class, let us go through some parts of the example program. In order to keep this example as simple as possible, we will operate only with one rate for each category (deposit, FRA, swap). First, we create stand-alone quotes from market data. Memory for a SimpleQuote (inherits from Quote base class) will be reserved by using Boost shared pointer :
boost::shared_ptr<Quote> usd_quote_deposit_3M(new SimpleQuote(0.006127));
boost::shared_ptr<Quote> usd_quote_fra_3M6M(new SimpleQuote(0.008253));
boost::shared_ptr<Quote> usd_quote_swap_2Y(new SimpleQuote(0.011459));
Next, we create curve builder class instance for assembling USD Libor swap curve :
Date settlementDate = UnitedKingdom().advance(tradeDate, 2, Days);
PiecewiseCurveBuilder<USDLibor> USDCurveBuilder(settlementDate, UnitedKingdom(), Annual, Thirty360());
After this, we add quotes into USD curve builder :
USDCurveBuilder.AddDeposit(usd_quote_deposit_3M, 3 * Months);
USDCurveBuilder.AddFRA(usd_quote_fra_3M6M, 3 * Months, 3 * Months);
USDCurveBuilder.AddSwap(usd_quote_swap_2Y, 2 * Years);
Finally, we request relinkable curve handle from USD curve builder :
RelinkableHandle<YieldTermStructure> curveHandle = USDCurveBuilder.GetCurveHandle();
Requested RelinkableHandle (inherits from Handle base class) can then be directly used in other QL programs. One should be aware, that all the changes we made in stand-alone quotes (SimpleQuotes) will have an effect on the curve. For an example, we could modify the existing quotes by shocking their rate up by 100 bps :
boost::dynamic_pointer_cast<SimpleQuote>(usd_quote_deposit_3M)->setValue(usd_quote_deposit_3M->value() + 0.01);
boost::dynamic_pointer_cast<SimpleQuote>(usd_quote_fra_3M6M)->setValue(usd_quote_fra_3M6M->value() + 0.01);
boost::dynamic_pointer_cast<SimpleQuote>(usd_quote_swap_2Y)->setValue(usd_quote_swap_2Y->value() + 0.01);
Additional add methods for different quote types are enabling even easier way to assemble a curve. Instead of giving a rate wrapped inside Quote object, it can be given directly into curve builder. First, we create curve builder for CHF Libor swap curve :
settlementDate = UnitedKingdom().advance(tradeDate, 2, Days);
PiecewiseCurveBuilder<CHFLibor> CHFCurveBuilder(settlementDate, UnitedKingdom(), Annual, Thirty360());
Next, we add market rates directly into CHF curve builder :
CHFCurveBuilder.AddDeposit(-0.006896, 6 * Months);
CHFCurveBuilder.AddFRA(-0.007103, 6 * Months, 6 * Months);
CHFCurveBuilder.AddSwap(-0.0068355, 2 * Years);
Finally, we request relinkable curve handle from CHF curve builder :
curveHandle = CHFCurveBuilder.GetCurveHandle();
This last option would be suitable in situations, where a client has no need for any auxiliary rate
The complete example program has been presented below. The program will first create updateable USD Libor swap curve, print all the rates, modify quotes and re-prints the rates. After this, the program will create updateable CHF Libor swap curve and prints a set of discount factors. Finally, the program will create non-updateable CHF Libor swap curve by using another set of add methods and prints a set of discount factors.
Have a great start for the year 2016. Thanks for reading my blog.