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

Dirk Eddelbuettel edd at debian.org
Wed Oct 29 15:49:19 CET 2014


On 29 October 2014 at 15:22, Benjamin J. J. Voigt wrote:
| I have tried a few things on the segmentation fault. 
| 
| It's a little hard to understand what is really going on because I am not shown
| much information about where the error occurred. So far I have identified a few
| conditions when things apparently do cause an error:
| 1. No call to setEvaluationDate() supplied 
| 2. The setEvaluationDate() date is not exactly the same date as the trade date
| 3. The tradeDate, and therefore the setEvaluationDate(), are set to a
| non-business day
| 4. The times argument goes right up to the time for which rates are provided
| e.g., a 15y swap rate is provided and times is defined as seq(0,15,0.1). It
| seems a save way to do this is to provide _at least_ one more rate beyond the
| times interval, i.e., seq(0,14,0.1) with a 15y swap rate defined

Excellent work. 
 
| I am catching these conditions now.

Yes, we surely need to in the R interface, and for the more fundamental
questions of what is computed we can go to the quantlib-dev list.

This is really exciting.  With your help, and Michele's help, we are now finally
overhauling the fixed income parts. This was overdue.

Dirk
 
| Regards,
| Ben
| 
| On Wed, Oct 29, 2014 at 4:12 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
| 
|    
|     On 29 October 2014 at 03:52, Benjamin J. J. Voigt wrote:
|     | Hi,
|     |
|     | the code is take from here: http://stackoverflow.com/questions/22226195/
|     | discountcurve-example-not-working-on-rquantlib 
|     | and I ran with recent (2h old as per time of writing) GH. It results in a
|     | segfault all the time no matter wether its in RStudio, command line R or
|     the
|     | Mac R GUI.
| 
|     Ok. I am in the middle of something else so I can help you with this ...
|    
|     | But, the good thing is, the example(DiscountCurve) on the GH sources
|     worked. So
|     | I will look at that code instead. Thank you!
| 
|     ... that is exactly what I would do.
| 
|     My first instinct always is to a) get something in 'pure' QuantLib working
|     and b) once that is done add the Rcpp/RQuantLib layer.  By separating the
|     two
|     steps I find it easier to make progress.
|    
|     | Regards,
|     | Ben
|     |
|     | P.S.: Happy to test some more, but do you rather take findings on the
|     list or
|     | as tickets?
| 
|     I am old school and tend to favour the list, but I am also getting used to
|     having to respond to issue tickets at GH.  Most folks seem to prefer the
|     latter. I guess overall I don;t have a strong preference either.  More
|     important to get progress on code...
|    
|     Dirk
|    
| 
|     |
|     | On Wed, Oct 29, 2014 at 3:32 AM, Dirk Eddelbuettel <edd at debian.org>
|     wrote:
|     |
|     |
|     |     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
|     |
|     |
|     |
|     |
|     | --
|     | Ben
| 
|     --
|     http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
| 
| 
| 

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


More information about the Rquantlib-devel mailing list