[Rquantlib-devel] Is DiscountCurve() supposed to work?

Dirk Eddelbuettel edd at debian.org
Wed Oct 29 03:32:51 CET 2014


On 29 October 2014 at 02:50, Benjamin J. J. Voigt wrote:
| Hi,
| 
| running the following example (mac 10.9.*) I get a crash, both on stable and
| github sources. Is there a known reason for this and how do I prevent the
| error?
| 
| R-Code:
| savepar <- par(mfrow = c(3, 3))
| 
| params <- list(tradeDate = c(2, 15, 2002), settleDate = c(2,
|     19, 2002), dt = 0.01, interpWhat = "discount", interpHow = "loglinear")
| 
| tsQuotes <- list(d1w = 0.0382, d1m = 0.0372, fut1 = 96.2875,
|     fut2 = 96.7875, fut3 = 96.9875, fut4 = 96.6875, fut5 = 96.4875,
|     fut6 = 96.3875, fut7 = 96.2875, fut8 = 96.0875, s3y = 0.0398,
|     s5y = 0.0443, s10y = 0.05165, s15y = 0.055175)
| 
| times <- seq(0, 10, 0.1)
| 
| DiscountCurve(params, tsQuotes, times)

Can you detail where you got this from / what you changed?  Because Gmail
insists on inserting garbage into text this is a little tedious for me.

When I run your code against current GH sources, I get an error on input:
R> params <- list(tradeDate = c(2, 15, 2002),
+                settleDate = c(2, 19, 2002),
+                dt = 0.01,
+                interpWhat = "discount",
+                interpHow = "loglinear")
R> tsQuotes <- list(d1w = 0.0382, d1m = 0.0372, fut1 = 96.2875,
+                  fut2 = 96.7875, fut3 = 96.9875, fut4 = 96.6875, fut5 = 96.4875,
+                  fut6 = 96.3875, fut7 = 96.2875, fut8 = 96.0875, s3y = 0.0398,
+                  s5y = 0.0443, s10y = 0.05165, s15y = 0.055175)
R> times <- seq(0, 10, 0.1)
R> DiscountCurve(params, tsQuotes, times)
Error: expecting a single value
R> 


Now, there was a (very recent) conversation with Luigi (upstream at QL) which
we had, as I recall, mostly in an issue ticket at GH.  That revealed two
sources of trouble: I had the wrong date, and a bad tenor -- removing the s2y
point leads to better / stabler results.

With that I (finally!!) reenable the discount curve examples. So from current
GitHub sources you once again get a meaning 3 x 3 grid of plots. An example
is below, this of course does not include the chart (which I could email
you...) but you get a hint of the final values of curves via str() below.

R> library(RQuantLib)
R> example(DiscountCurve)

DscntCR> savepar <- par(mfrow=c(3,3), mar=c(4,4,2,0.5))

DscntCR> ## This data is taken from sample code shipped with QuantLib 0.9.7
DscntCR> ## from the file Examples/Swap/swapvaluation
DscntCR> params <- list(tradeDate=as.Date('2004-09-20'),
DscntC+                settleDate=as.Date('2004-09-22'),
DscntC+                dt=.25,
DscntC+                interpWhat="discount",
DscntC+                interpHow="loglinear")

DscntCR> setEvaluationDate(as.Date("2004-09-20"))
[1] TRUE

DscntCR> ## We get numerical issue for the spline interpolation if we add
DscntCR> ## any on of these three extra futures -- the original example
DscntCR> ## creates different curves based on different deposit, fra, futures
DscntCR> ## and swap data
DscntCR> ## Removing s2y helps, as kindly pointed out by Luigi Ballabio
DscntCR> tsQuotes <- list(d1w = 0.0382,
DscntC+                  d1m = 0.0372,
DscntC+                  d3m = 0.0363,
DscntC+                  d6m = 0.0353,
DscntC+                  d9m = 0.0348,
DscntC+                  d1y = 0.0345,
DscntC+                  fut1=96.2875,
DscntC+                  fut2=96.7875,
DscntC+                  fut3=96.9875,
DscntC+                  fut4=96.6875,
DscntC+                  fut5=96.4875,
DscntC+                  fut6=96.3875,
DscntC+                  fut7=96.2875,
DscntC+                  fut8=96.0875,
DscntC+ #                 s2y = 0.037125,
DscntC+                  s3y = 0.0398,
DscntC+                  s5y = 0.0443,
DscntC+                  s10y = 0.05165,
DscntC+                  s15y = 0.055175)

DscntCR> times <- seq(0,10,.1)

DscntCR> # Loglinear interpolation of discount factors
DscntCR> curves <- DiscountCurve(params, tsQuotes, times)

DscntCR> plot(curves,setpar=FALSE)
Hit <Return> to see next plot: 

DscntCR> # Linear interpolation of discount factors
DscntCR> params$interpHow="linear"

DscntCR> curves <- DiscountCurve(params, tsQuotes, times)

DscntCR> plot(curves,setpar=FALSE)

DscntCR> # Spline interpolation of discount factors
DscntCR> params$interpHow="spline"

DscntCR> curves <- DiscountCurve(params, tsQuotes, times)

DscntCR> plot(curves,setpar=FALSE)

DscntCR> par(savepar)
R> str(curves)
List of 7
 $ times     : num [1:101] 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 ...
 $ discounts : num [1:101] 1 0.996 0.993 0.989 0.984 ...
 $ forwards  : num [1:101] 0.0367 0.0404 0.0404 0.0287 0.0275 ...
 $ zerorates : num [1:101] 0.0392 0.0376 0.0368 0.0379 0.0402 ...
 $ flatQuotes: logi FALSE
 $ params    :List of 5
  ..$ tradeDate : Date[1:1], format: "2004-09-20"
  ..$ settleDate: Date[1:1], format: "2004-09-22"
  ..$ dt        : num 0.25
  ..$ interpWhat: chr "discount"
  ..$ interpHow : chr "spline"
 $ table     :'data.frame':	5479 obs. of  2 variables:
  ..$ date     : Date[1:5479], format: "2004-09-22" "2004-09-23" "2004-09-24" "2004-09-25" ...
  ..$ zeroRates: num [1:5479] 0.0392 0.0392 0.0392 0.0392 0.0392 ...
 - attr(*, "class")= chr "DiscountCurve"
R> 


So very much no segfault here on the default example.
 

| 
| 
| R-Error:
|  *** caught segfault ***
| address 0x20, cause 'memory not mapped'
| 
| Traceback:
|  1: .Call("RQuantLib_discountCurveEngine", PACKAGE = "RQuantLib",     rparams,
| tslist, times)
|  2: discountCurveEngine(params, tsQuotes, times)
|  3: DiscountCurve.default(params, tsQuotes, times)
|  4: DiscountCurve(params, tsQuotes, times)
| 
| 
| I am not in a hurry, just might need to think of doing this on my own, but
| pricing bonds on a flat curve is not quite what regular users would like to do
| when thinking about QuantLib.

We've just overcome this.

RQuantLib is coming together. I did a bunch of work in the summer, then got
sidetracked with travel and conferences and getting back to it. Michele
helped with two excellent pull requests, and I think we are getting ready for
a new release.  The testing you are doing here is very welcome.

Cheers, Dirk

| 
| Happy to provide more details if required.
| 
| Regards,
| Ben
| 
| --
| Ben
| _______________________________________________
| Rquantlib-devel mailing list
| Rquantlib-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rquantlib-devel

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rquantlib-devel mailing list