eddelbuettel / rquantlib

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

BKTree option in AffineSwaption #91

Closed imunir23 closed 7 years ago

imunir23 commented 7 years ago

Hello, I'm simply trying to recreate the example included in the guide for AffineSwaption, but rather than use the G2Analytic method, I'd like to use the BKTree:

params <- list(tradeDate=as.Date('2016-2-15'), settleDate=as.Date('2016-2-17'), startDate=as.Date('2017-2-17'), maturity=as.Date('2022-2-17'), payFixed=TRUE, european=FALSE, dt=.25, strike=.06, method= "BKTree", 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)

swaptionMaturities <- c(1,2,3,4,5)

swapTenors <- c(1,2,3,4,5)

volMatrix <- matrix( c(0.1490, 0.1340, 0.1228, 0.1189, 0.1148, 0.1290, 0.1201, 0.1146, 0.1108, 0.1040, 0.1149, 0.1112, 0.1070, 0.1010, 0.0957, 0.1047, 0.1021, 0.0980, 0.0951, 0.1270, 0.1000, 0.0950, 0.0900, 0.1230, 0.1160), ncol=5, byrow=TRUE)

legparams=list(dayCounter="Thirty360", fixFreq="Annual", floatFreq="Semiannual")

setEvaluationDate(as.Date("2016-2-16"))
times<-times <- seq(0,14.5,.25) dcurve <- DiscountCurve(params, tsQuotes, times=times,legparams)

pricing <- AffineSwaption(params, dcurve,swaptionMaturities, swapTenors, volMatrix,legparams) summary(pricing)

I receive an error everytime, "Unknown method in AffineSwaption"

The guide includes BKTree as a selectable option so I'm not sure what the problem here is.

Thank you!

eddelbuettel commented 7 years ago

That was a bug!

@tleitch: Please have a look. I think these lines, and then these are a copy-and-paste double. When I remove them, the "BKTree" gets reached and we get for the input above:

Black-Karasinski (tree) calibration
-985504320x-1937198408: model 1.000000, market 5.000000, diff 0.101015
-985504320x-1937198408: model 2.000000, market 5.000000, diff 0.099098
-985504320x-1937198408: model 3.000000, market 4.000000, diff 0.100668
-985504320x-1937198408: model 4.000000, market 3.000000, diff 0.102047
-985504320x-1937198408: model 5.000000, market 2.000000, diff 0.104708

        Summary of pricing results for Affine Swaption

Price (in bp) of Affine swaption is 
Stike is  NULL (ATM strike is  0.0496033 )
Model used is: Black-Karasinski using a tree
Calibrated model parameters are:
a =  0.1574
sigma =  0.1681 

I can PR this but I would appreciate a second set of eyes.

eddelbuettel commented 7 years ago

That is now in this branch.

imunir23 commented 7 years ago

oh thank you for getting on this so quickly! I take that re-downloading RQuantLib once the build is complete should fix the issue?

tleitch commented 7 years ago

In transit so hard to check, will tomorrow.

Tree valuation is counter to affine. In the latter, you use a model which is path independent and an analytical form similar to the Black futures option model, so tree method shouldn’t be an option.

On Aug 2, 2017, at 9:57 PM, Dirk Eddelbuettel notifications@github.com wrote:

That was bug!

@tleitch https://github.com/tleitch: Please have a look. I think these lines https://github.com/eddelbuettel/rquantlib/blob/master/src/affine.cpp#L251-L301, and then these https://github.com/eddelbuettel/rquantlib/blob/master/src/affine.cpp#L319-L321 are a copy-and-paste double. When I remove them, the "BKTree" gets reached and we get for the input above:

Black-Karasinski (tree) calibration -985504320x-1937198408: model 1.000000, market 5.000000, diff 0.101015 -985504320x-1937198408: model 2.000000, market 5.000000, diff 0.099098 -985504320x-1937198408: model 3.000000, market 4.000000, diff 0.100668 -985504320x-1937198408: model 4.000000, market 3.000000, diff 0.102047 -985504320x-1937198408: model 5.000000, market 2.000000, diff 0.104708

    Summary of pricing results for Affine Swaption

Price (in bp) of Affine swaption is Stike is NULL (ATM strike is 0.0496033 ) Model used is: Black-Karasinski using a tree Calibrated model parameters are: a = 0.1574 sigma = 0.1681 I can PR this but I would appreciate a second set of eyes.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eddelbuettel/rquantlib/issues/91#issuecomment-319846550, or mute the thread https://github.com/notifications/unsubscribe-auth/AHBCorIq5fIe94G1n2gKbOTDPIipxyytks5sUSkcgaJpZM4Orx_f.

imunir23 commented 7 years ago

Are there other functions that support trees in RQuantLib? I saw the "BermudanSwaption can be considered Deprecated" thread which is what pointed me to the AffineSwaption function, seeing that the BermudanSwaption function seemed to be not recommended for use.

tleitch commented 7 years ago

BermudanSwaption was implemented to handle path dependent swaptions. It’s a tree method. For european options, you don’t need a lattice as there’s no path dependency.

BermudanSwaption calibrates to a european swaption surface and then runs a path dependent lattice through each adaption expiration/tenor combo up to final expiration.

On Aug 2, 2017, at 11:46 PM, imunir23 notifications@github.com wrote:

oh thank you for getting on this so quickly! I take that re-downloading RQuantLib once the build is complete should fix the issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eddelbuettel/rquantlib/issues/91#issuecomment-319860158, or mute the thread https://github.com/notifications/unsubscribe-auth/AHBCojYkm8PP_u2zRzrLm5iXxbhwUBQIks5sUUKwgaJpZM4Orx_f.

imunir23 commented 7 years ago

I was previously using BermudanSwaptions before I updated to the 0.4.3 version of RQuantLib. The BermudanSwaption example presented in the guide for the updated version had me confused:

This data replicates sample code shipped with QuantLib 0.3.10 results ... pricing <- BermudanSwaption(params, ts=.05, swaptionMaturities, swapTenors, volMatrix)

It looks to me that to recreate the results from Quantlib 0.3.10, you need to use a flat curve (equaling 0.05) to recreate results previously gotten from a curve that was not flat. Was the old BermudanSwaption function buggy or am I missing something here?

Thanks for all your help!

eddelbuettel commented 7 years ago

Yes, you will need to rebuild from source once merged (or from the current branch).

As for "motivation", I let @tleitch answer this. I think it is simply a better/newer/richer model... and he needed it in a particular (large scale) application.

tleitch commented 7 years ago

sabrswaption is state of the art implementation for path dependent swaptions with early exercise.

On Aug 3, 2017, at 6:20 AM, Dirk Eddelbuettel notifications@github.com wrote:

Yes, you will need to rebuild from source once merged (or from the current branch).

As for "motivation", I let @tleitch https://github.com/tleitch answer this. I think it is simply a better/newer/richer model... and he needed it in a particular (large scale) application.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eddelbuettel/rquantlib/issues/91#issuecomment-319929912, or mute the thread https://github.com/notifications/unsubscribe-auth/AHBCorpgfAtbIuajOfzOEJVkW-o97qiiks5sUZ8IgaJpZM4Orx_f.

tleitch commented 7 years ago

Also, BermudanSwaption only calculates the the example in the documentation (i.e. the swap tenors and adaption expirations in the example). It mirrors the example in rquantlib. AffineSwaption and sabrSwaption take generalized adaption expirations and swap tenors provided you have the the data points to support the valuation. They interpolate the implied volatility for the relevant expiration/tenor pairs from the provided vol surface.

On Aug 3, 2017, at 6:20 AM, Dirk Eddelbuettel notifications@github.com wrote:

Yes, you will need to rebuild from source once merged (or from the current branch).

As for "motivation", I let @tleitch https://github.com/tleitch answer this. I think it is simply a better/newer/richer model... and he needed it in a particular (large scale) application.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eddelbuettel/rquantlib/issues/91#issuecomment-319929912, or mute the thread https://github.com/notifications/unsubscribe-auth/AHBCorpgfAtbIuajOfzOEJVkW-o97qiiks5sUZ8IgaJpZM4Orx_f.

imunir23 commented 7 years ago

I see thank you for your answers; the sabrswaption doesn't particularly help me (I think); what I'm trying to do is get the volatility and mean reversion constants (sigma and a) to build an interest rate tree following the Black-Karasinski model.

I've never actually used GitHib before; I'm seeing in the pull requests that the request attached to my bug has a green check, so is it good to redownload the package (via dratt)? I can close the issue after that, thank you so much for taking care of this so fast!

tleitch commented 7 years ago

If you look at affine.cpp you can see the calls to rquantlib methods. At the bottom is BKTree. It returns a and sigma to the AffineSwaption, so should be available for your use. Alternatively, you could build R functions that do calibration w/o valuation by lifting the .cpp and R code.

On Aug 3, 2017, at 1:21 PM, imunir23 notifications@github.com wrote:

I see thank you for your answers; the sabrswaption doesn't particularly help me (I think); what I'm trying to do is get the volatility and mean reversion constants (sigma and a) to build an interest rate tree following the Black-Karasinski model.

I've never actually used GitHib before; I'm seeing in the pull requests that the request attached to my bug has a green check, so is it good to redownload the package (via dratt)? I can close the issue after that, thank you so much for taking care of this so fast!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/eddelbuettel/rquantlib/issues/91#issuecomment-320034894, or mute the thread https://github.com/notifications/unsubscribe-auth/AHBCosykAiHC4bLeXEWNpVLJgdqog1vtks5sUgGggaJpZM4Orx_f.

eddelbuettel commented 7 years ago

Regarding GitHub: Green checks just mean that the continuous integration tests passed. You can, as I recall, "download" an entire repo as a zipfile, especially now that I merged this pull request. A more common approach is via git clone .... and there are dozens of tutorials.

If you are really stuck, though, I can make a new tarball available too.

Thanks again for the bugreport. This was an oversight on our part.

imunir23 commented 7 years ago

Hi Thank you so much for getting this fixed up! So I downloaded the repository and tried to build it (using the build function) and it outputted a tar.gz file which I assume would be the tarball needed to build the package.

I'm a little stuck after that, I've tried install.package on the tarball file:

install.packages("RQuantLib", repos = "C:/Users/imuni/Documents/R/win-library/3.3/RQuantLib_0.4.3.3.tar.gz", type = "source")

It errors out with "RQuantLib is not available (for R Version 3.3.1)"

Having never assembled a package in this manner (I've only downloaded from CRAN or dratt in particular for this), could any pointers be given on how to assemble the package from the downloaded repository? I'm using Windows, also.

Thanks again!

UPDATE:

I've used this code which I think is more correct on the locally saved tarball:

install.packages("C:/Users/imuni/Documents/R/win-library/3.3/RQuantLib_0.4.3.3.tar.gz", repos = NULL, type="source")

I have a new error now as well:

ERROR: Unix-only package

Is the package actually Unix only??

eddelbuettel commented 7 years ago

Well if you are on Windows you have a whole uphill struggle ahead of you as you need to build QuantLib locally.

imunir23 commented 7 years ago

Ah... thanks for point me in the right direction! The case is fully closed.

eddelbuettel commented 7 years ago

It's a little complicated. 10-15 years ago CRAN kept a QuantLib library on Windows allowing me to build against it. Now they don't, and the only way to tell CRAN not to build on Windows is that flag.

If you actually have QuantLib built (using R's compiler, this matters) then you can just override that. I could walk you through this but it is still a lot of work....

imunir23 commented 7 years ago

So I don't have QuantLib built using any compilers. I found this blog posting and was basically planning on running through the steps it described (substituting to current versions of the given software):

https://www.r-bloggers.com/build-rquantlib-on-32-bit-windows/

I downloaded the Boost, QuantLib, RTools, and MinGW and was planning on running through the steps tomorrow.

eddelbuettel commented 7 years ago

Yes that is the way to do. It is sadly a fair amount of work.

imunir23 commented 7 years ago

At least I have the assurance that it will work, it would be even more sad if I ended up doing all that and it didn't work. Thanks for all your help!