eddelbuettel / rquantlib

R interface to the QuantLib library
117 stars 50 forks source link

Upgrading BermudanSwaption to take discount curve #37

Closed tleitch closed 8 years ago

tleitch commented 8 years ago

Currently, BermudanSwaption has an input for rate quotes, tsQuotes. It would be more efficient to pass in a DiscountCurve object when you are valuing multiple options. It is also more ih line with the current form for bond function FixedBondWithRebiltCurve:

Bonds.cpp:

399 Rcpp::List FixedRateWithRebuiltCurve(Rcpp::List bondparam,  
400                                      std::vector<double> ratesVec, 
401                                      Rcpp::List scheduleparam, 
402                                      Rcpp::List calcparam, 
403                                      std::vector<QuantLib::Date> dateVec,  
404                                      std::vector<double> zeroVec) { 

The current model takes a tsQuotes in Bermudan.R and it is called tslist in Bermudan.Cpp:

52 Rcpp::List bermudanSwaptionEngine(Rcpp::List rparam, 
53                                   Rcpp::List tslist, 
54                                   Rcpp::NumericVector swaptionMat, 
55                                   Rcpp::NumericVector swapLengths, 
56                                   Rcpp::NumericMatrix swaptionVols) { 

Unfortunately, while tslist is used to create a curve, it is never used because at line 131, a hard coded flat rate is used to generate a separate valuation curve.

131     boost::shared_ptr<QuantLib::Quote> flatRate(new QuantLib::SimpleQuote(0.04875825));  // FIXME: hardcoded? 
132     QuantLib::Handle<QuantLib::YieldTermStructure>  
133         rhTermStructure(boost::shared_ptr<QuantLib::FlatForward>(new QuantLib::FlatForward(settlementDate,  
134                                                                                            QuantLib::Handle<QuantLib::Quote>(flatRate), 
135                                                                                            QuantLib::Actual365Fixed()))); 

So, I propose removing the tsQuotes argument, since it cannot actually be used anywhere anyway, and replace it with a DiscountCurve object, and a flat term structure option similar to that handled by Bond.R:

91     FixedRateBond <- function(bond, rates, schedule, calc, discountCurve, yield, price){

150     if (!is.null(discountCurve)) { 
151         val <- FixedRateWithRebuiltCurve( 
152             bond, rates, schedule, calc, c(discountCurve$table$date), discountCurve$table$zeroRates) 
153        
154     } else if (!is.na(yield)) { 
155       val <- FixedRateWithYield(bond, rates, schedule, calc, yield) 

This would brind Bermudan in line with the bond functions with a single yield option or a yield curve (pre-stripped).

The other option is to have a single arguement and split on class("Discount Curve") with a check for a valid numeric value if the class fails.

eddelbuettel commented 8 years ago

Sounds reasonable. While I tend to lean against interface changes, the Bermudan code is so old, and has seen so little love since DS initially put it in a decade ago that I can hardly object.

One question, though: do we have 'reference' examples from either QL tests, or QL examples, or some other places? That would be good for demos or examples --- and even more importantly for unit tests.

tleitch commented 8 years ago

I rewrote bermudanSwaption but decided to create a parallel affineSwaption model that does both a European and a Bermudan. Default is European. I did not want to call it EuropeanSwaption as I am reserving that for the fit and valuation routines based on SABR and I thought an overloaded version would confuse the initiate. The code is in my repository and I will do a pull as soon as I have finished a unit test for the swaption models that includes some yield curve tests.

I preserved the flat yield curve option in bermudanSwaption but I abandoned it in affineSwaption as DiscountCurve provides a flat yield curve option and I think the model process should be 1) strip curve, 2) value instrument using DiscountCurve object. Intertwining the curve stripping and valuation creates a lot of redundant code that has to be maintained or restructured. This is simple for swaps/bonds, but when you get to options life is getting very complex very fast.

Thoughts?

eddelbuettel commented 8 years ago

Sounds good!

If anything though, and given the birthing problems we had previously, it may prudent to start small with limited changes, get those booked and reconciled and then move on from there.

tleitch commented 8 years ago

Addressed in #43