0

I am trying to use RQuantLib to price bonds but the version I am using will not work with negative interest rates. See example below. Does anyone know a work around? I thought that QuantLib was able to accept negative rates?

require(RQuantLib)

today <- as.Date("2014-10-27")
setEvaluationDate(today)

times <- seq(today+2,as.Date("2024-12-30"),by=1)
maturity <- yearFraction(rep(today,length(times)),times,rep(2,length(times)))

zerorates <- seq(-.0001,.01,length.out=length(maturity))

curve <- list(table = data.frame(date=times, zeroRates=zerorates))
attr(curve,"class") <- "DiscountCurve"

FixedRateBond(
bond =list(issueDate=as.Date("2005-11-24"), maturityDate=as.Date("2016-01-03")),
rates=.035, discountCurve= curve,
dateparams=list(settlementDays=2,dayCounter=2,period=1))

Error: invalid value (-0.0001) at index 0

sessionInfo()


R version 3.1.1 (2014-07-10)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252     LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RQuantLib_0.3.12 rJava_0.9-6     

loaded via a namespace (and not attached):
[1] Rcpp_0.11.3 tools_3.1.1
rnorthcott
  • 77
  • 1
  • 5
  • QuantLib allows negative rates by default since version 1.2.1 (Sept. 2012). However, grepping the sources for the error message seems to indicate that you're trying to use log interpolation between the rates, which obviously can't work if they're negative. I don't know how to change that in RQuantLib, so I'll let others chime in. – Luigi Ballabio Oct 28 '14 at 11:02
  • Actually I think that answers my question Luigi. The windows version of RQuantLib includes compiled QuantLib 1.01 so that is most likely why it won't work. – rnorthcott Oct 28 '14 at 18:09
  • Except that the error message matches the one from the log interpolation code. Ok, I'll summarize the thing in an answer for future reference (and to suggest a thing or two you can try). – Luigi Ballabio Oct 28 '14 at 21:16
  • Can you please post on rquantlib-devel so that Michele sees it? The interface to `FixedRateBond()` was changed recently to be more inline with what QuantLib does, and I can't even run your example... – Dirk Eddelbuettel Oct 28 '14 at 21:57

1 Answers1

1

There might be two different issues at play.

Up to and including version 1.2, QuantLib used to reject negative rates by default. In version 1.2.1, the behavior changed and the default is now to accept negative rates.

If this were the problem, the only way to allow them in your RQuantLib installation would be to recompile the library. All versions of QuantLib since 1.0 are backward-compatible, so you can download a more recent version and drop it in as a replacement for the one used in RQuantLib (possibly getting a few bug fixes out of the deal, too). Otherwise, you can keep version 1.0.1 and enable negative rates; in an autotools-based build (Linux, Mac OS X and Windows using MinGW) you do it by running

./configure --enable-negative-rates

while on other Windows compilers (Visual C++, Dev-C++) you'll have to edit ql/userconfig.hpp and uncomment the relevant #define.

However, the error you're getting (not very informative, but fortunately not very common either, so grep will locate it) comes from a part of the code that deals with log interpolation instead. This suggests that the library was already compiled with negative rates enabled (although someone else will have to confirm this) and that the rates weren't rejected as such. This might be good news: it means that, if RQuantLib allows you to choose a different interpolation, you'll be able to make it work without having to recompile. Again, I have no idea how this can be done; if you find out, please answer your own question and accept your answer as the correct one.

Luigi Ballabio
  • 4,128
  • 21
  • 29
  • This seems likely. The courtesy build of QuantLib used for the Windows binaries is rather likely to have been built with default values, hence no negative rates. – Dirk Eddelbuettel Oct 28 '14 at 22:04
  • Hmm. Then I'm puzzled about how the curve constructor got as far as trying to interpolate. Can one change the interpolation method? I'd be curious to see if that changes things. – Luigi Ballabio Oct 28 '14 at 22:15
  • Me too but then I have not worked all that much with the discount curve stuff. Our layer is at https://github.com/eddelbuettel/rquantlib/blob/master/src/curves.cpp and the sibbling discount.cpp --- that is the current code which is now much, much closer to what you are used as the mapping to/from `SEXP` objects for R happens by an intermediating layer (in `RcppExports.{h,cpp}`). – Dirk Eddelbuettel Oct 28 '14 at 22:18
  • Looking closer what happens here is a shortcut: `curve` is just a container with the two columns. Not computation is invoked. That is not entirely standard. – Dirk Eddelbuettel Oct 28 '14 at 22:26
  • Yes this is the most simplified version of my actual code I could get to. As far as I can tell the FixedRateBond() function only uses the table entry in the curve object. The function doesn't work if it's not there and does work even if nothing else is there. In my actual code I do things properly and create a fully specified DiscountCurve object including InterpWhat and InterpHow but it doesn't seem to make any difference. If there are negative values in the zeroRates col of the table entry the function fails with the error message shown in the example. – rnorthcott Oct 29 '14 at 11:42