rquantlib icon indicating copy to clipboard operation
rquantlib copied to clipboard

Add vectorised version of cashflow functions

Open DavideMagno opened this issue 6 years ago • 1 comments

Hi, first of all I want to say that I am big fun of the toolbox.

As per the title of the issue, would it be possible to vectorise the cashflow related functions? I have done a microbenchmark test with another

today <- Sys.Date()
test <- seq(from = 3, to = 50*12, by = 3)
cashflows <- today + months(test)

RQuantLib::yearFraction(today, cashflows, 2)

returns [1] 0 ie. only the year_fraction for the first element.

microbenchmark::microbenchmark(
  "Rquantlib" = mapply(function(x) RQuantLib::yearFraction(today, x, 2), cashflows),
  "fmdates" = fmdates::year_frac(today, cashflows, "act/365")
)

The difference is quite material

Unit: microseconds
      expr      min       lq      mean   median        uq       max neval
 Rquantlib 2863.532 3044.524 3625.3488 3158.520 3390.6060 11575.490   100
   fmdates  203.336  229.387  292.9247  304.839  340.7695   488.034   100

On the other hand, the RQuantLib::advance function works just much better than the equivalent fmdates one.

microbenchmark::microbenchmark(
    "Rquantlib" = mapply(function(x) RQuantLib::advance(dates = today, x, timeUnit = 2, calendar = "TARGET"), test),
    "fmdates" = mapply(function(x) fmdates::shift(today, months(x)), test)
)

     expr        min         lq       mean     median         uq       max neval
 Rquantlib   14.80745   15.37617   18.37219   15.79076   16.54245  123.6318   100
   fmdates 1726.72736 1765.48300 1881.30645 1784.04107 1867.18817 3772.2047   100

Hence I think that having a vectorised version of the cashflow related functions (advance and yearFraction could be the best of the two worlds.

DavideMagno avatar Jul 09 '19 19:07 DavideMagno

Sorry that this fell to the side -- had some traveling coming.

In general I am in favour -- vectorising is good, it is not something we did 15 or more years ago with the first variants, so we should look into adding it.

Any thoughts, @tleitch ?

eddelbuettel avatar Jul 22 '19 19:07 eddelbuettel