[Rquantlib-commits] r325 - in pkg/RQuantLib: . R man src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Dec 1 20:45:24 CET 2012


Author: edd
Date: 2012-12-01 20:45:22 +0100 (Sat, 01 Dec 2012)
New Revision: 325

Modified:
   pkg/RQuantLib/ChangeLog
   pkg/RQuantLib/DESCRIPTION
   pkg/RQuantLib/R/option.R
   pkg/RQuantLib/man/AmericanOption.Rd
   pkg/RQuantLib/src/Makevars.win
   pkg/RQuantLib/src/bonds.cpp
   pkg/RQuantLib/src/curves.cpp
   pkg/RQuantLib/src/utils.cpp
   pkg/RQuantLib/src/vanilla.cpp
   pkg/RQuantLib/src/zero.cpp
Log:
new option 'engine' for AmericanOption to select CrankNicolson FD scheme which can compute first three greeks


Modified: pkg/RQuantLib/ChangeLog
===================================================================
--- pkg/RQuantLib/ChangeLog	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/ChangeLog	2012-12-01 19:45:22 UTC (rev 325)
@@ -1,3 +1,23 @@
+2012-12-01  Dirk Eddelbuettel  <edd at debian.org>
+
+	* DESCRIPTION: Release 0.3.9
+
+	* src/vanilla.cpp (AmericanOption): Support engine choice, adding
+	"CrankNicolson" to the default "BaroneAdesiWhaley" as the former adds
+	delta, gamma and vega greeks -- thanks to Bryan Lewis for the
+	suggestion
+	* R/option.R: Support new the new 'engine' option
+	* man/AmericanOption.Rd: Document new 'engine' option
+
+	* src/bonds.cpp: Remove remaining std::cout use
+	* src/curve.cpp: Idem
+	* src/zero.cpp: Idem
+
+2011-12-27  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/Makevars.win: Add -I"$(BOOSTLIB)" which is what other CRAN
+	packages depending on Boost do
+
 2011-09-11  Dirk Eddelbuettel  <edd at debian.org>
 
 	* DESCRIPTION: Release 0.3.8

Modified: pkg/RQuantLib/DESCRIPTION
===================================================================
--- pkg/RQuantLib/DESCRIPTION	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/DESCRIPTION	2012-12-01 19:45:22 UTC (rev 325)
@@ -1,6 +1,6 @@
 Package: RQuantLib
 Title: R interface to the QuantLib library
-Version: 0.3.8
+Version: 0.3.9
 Date: $Date$
 Maintainer: Dirk Eddelbuettel <edd at debian.org>
 Author: Dirk Eddelbuettel <edd at debian.org> and Khanh Nguyen <knguyen at cs.umb.edu>

Modified: pkg/RQuantLib/R/option.R
===================================================================
--- pkg/RQuantLib/R/option.R	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/R/option.R	2012-12-01 19:45:22 UTC (rev 325)
@@ -44,14 +44,17 @@
 
 AmericanOption <- function(type, underlying, strike, dividendYield,
                            riskFreeRate, maturity, volatility,
-                           timeSteps=150, gridPoints=151) {
+                           timeSteps=150, gridPoints=149,
+                           engine="BaroneAdesiWhaley") {
     UseMethod("AmericanOption")
 }
 
 AmericanOption.default <- function(type, underlying, strike, dividendYield,
                                    riskFreeRate, maturity, volatility,
-                                   timeSteps=150, gridPoints=151) {
+                                   timeSteps=150, gridPoints=149,
+                                   engine="BaroneAdesiWhaley") {
     type <- match.arg(type, c("call", "put"))
+    engine <- match.arg(engine, c("BaroneAdesiWhaley", "CrankNicolson"))
     val <- .Call("AmericanOption",
                  list(type=as.character(type),
                       underlying=as.double(underlying),
@@ -61,15 +64,10 @@
                       maturity=as.double(maturity),
                       volatility=as.double(volatility),
                       timeSteps=as.integer(timeSteps),
-                      gridPoints=as.integer(gridPoints)),
+                      gridPoints=as.integer(gridPoints),
+                      engine=as.character(engine)),
                  PACKAGE="RQuantLib")
     class(val) <- c("AmericanOption","Option")
-    val[[2]] <- NA
-    val[[3]] <- NA
-    val[[4]] <- NA
-    val[[5]] <- NA
-    val[[6]] <- NA
-    val[[7]] <- NA
     val
 }
 

Modified: pkg/RQuantLib/man/AmericanOption.Rd
===================================================================
--- pkg/RQuantLib/man/AmericanOption.Rd	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/man/AmericanOption.Rd	2012-12-01 19:45:22 UTC (rev 325)
@@ -10,7 +10,7 @@
 \usage{
 \method{AmericanOption}{default}(type, underlying, strike,
 		dividendYield, riskFreeRate, maturity, volatility, 
-		timeSteps=150, gridPoints=151)
+		timeSteps=150, gridPoints=149, engine="BaroneAdesiWhaley")
 }
 \arguments{
   \item{type}{A string with one of the values \code{call} or \code{put}}
@@ -20,10 +20,12 @@
   \item{riskFreeRate}{Risk-free rate}
   \item{maturity}{Time to maturity (in fractional years)}
   \item{volatility}{Volatility of the underlying stock}
-  \item{timeSteps}{Time steps for the Finite Differences method, default
-  value is 150}
-  \item{gridPoints}{Grid points for the Finite Differences method,
-    default value is 151}
+  \item{timeSteps}{Time steps for the \dQuote{CrankNicolson} finite
+    differences method engine, default value is 150}
+  \item{gridPoints}{Grid points for the \dQuote{CrankNicolson} finite differences method,
+    default value is 149}
+  \item{engine}{String selecting pricing engine, currently supported are
+    \dQuote{BaroneAdesiWhaley} and \dQuote{CrankNicolson}}
 }
 \value{
   An object of class \code{AmericanOption} (which inherits from class
@@ -42,9 +44,11 @@
     dividend yield}
   \item{parameters}{List with parameters with which object was created}
 
-  Note that under the new pricing framework used in QuantLib, binary
-  pricers do not provide analytics for 'Greeks'. This is expected to be
-  addressed in future releases of QuantLib.
+  Note that under the new pricing framework used in QuantLib,
+  pricers do not provide analytics for all 'Greeks'. When
+  \dQuote{CrankNicolson} is selected, then at least delta, gamma and
+  vega are available. With the default pricing engine of
+  \dQuote{BaroneAdesiWhaley}, no greeks are returned.
 }
 \details{
   The Finite Differences method is used to value the American Option.
@@ -65,6 +69,8 @@
 AmericanOption("call", 100, 100, 0.02, 0.03, 0.5, 0.4)
 # simple call with some explicit parameters
 AmericanOption("put", strike=100, volatility=0.4, 100, 0.02, 0.03, 0.5)
+# simple call with unnamed parameters, using Crank-Nicolons
+AmericanOption("put", strike=100, volatility=0.4, 100, 0.02, 0.03, 0.5, engine="CrankNicolson")
 }
 \keyword{misc}
 

Modified: pkg/RQuantLib/src/Makevars.win
===================================================================
--- pkg/RQuantLib/src/Makevars.win	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/src/Makevars.win	2012-12-01 19:45:22 UTC (rev 325)
@@ -11,5 +11,5 @@
 
 ## The environment variable QUANTLIB_ROOT has to point to an existing build of QuantLib
 ## With R 2.12.0 and later, we also support 32 and 64 bit builds and need to differentiate
-PKG_CXXFLAGS=-I$(QUANTLIB_ROOT) -I. 
+PKG_CXXFLAGS=-I$(QUANTLIB_ROOT) -I. -I"$(BOOSTLIB)"
 PKG_LIBS=$(RCPP_LDFLAGS) -L$(QUANTLIB_ROOT)/lib${R_ARCH} -lQuantLib

Modified: pkg/RQuantLib/src/bonds.cpp
===================================================================
--- pkg/RQuantLib/src/bonds.cpp	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/src/bonds.cpp	2012-12-01 19:45:22 UTC (rev 325)
@@ -2,8 +2,8 @@
 //
 // RQuantLib -- R interface to the QuantLib libraries
 //
-// Copyright (C) 2002 - 2009 Dirk Eddelbuettel 
-// Copyright (C) 2009 - 2011 Khanh Nguyen and Dirk Eddelbuettel
+// Copyright (C) 2002 - 2012 Dirk Eddelbuettel 
+// Copyright (C) 2009 - 2012 Khanh Nguyen and Dirk Eddelbuettel
 //
 // $Id$
 //
@@ -1269,7 +1269,7 @@
         // RcppFrame frame(colNames);
         QuantLib::Date current = curve->referenceDate();;
         int n = curve->maxDate() - curve->referenceDate();
-        std::cout << curve->maxDate() << " " << curve->referenceDate() << " " << n << std::endl;
+        //std::cout << curve->maxDate() << " " << curve->referenceDate() << " " << n << std::endl;
 
         Rcpp::DateVector dates(n);
         Rcpp::NumericVector zr(n);

Modified: pkg/RQuantLib/src/curves.cpp
===================================================================
--- pkg/RQuantLib/src/curves.cpp	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/src/curves.cpp	2012-12-01 19:45:22 UTC (rev 325)
@@ -3,7 +3,7 @@
 // RQuantLib helper functions for term structure construction
 //
 // Copyright (C) 2005 - 2007  Dominick Samperi
-// Copyright (C) 2007 - 2010  Dirk Eddelbuettel
+// Copyright (C) 2007 - 2012  Dirk Eddelbuettel
 //
 // $Id$
 //
@@ -101,7 +101,7 @@
         QuantLib::Date imm = QuantLib::IMM::nextDate(settlementDate);
         for(int i = 1; i < n1; i++)
             imm = QuantLib::IMM::nextDate(imm+1);
-        std::cout << "Curves: IMM Date is " << imm << std::endl;
+        //Rcpp::Rcout << "Curves: IMM Date is " << imm << std::endl;
         boost::shared_ptr<QuantLib::Quote> quote(new QuantLib::SimpleQuote(r));
         boost::shared_ptr<QuantLib::RateHelper> future(new QuantLib::FuturesRateHelper(
 	    	QuantLib::Handle<QuantLib::Quote>(quote),

Modified: pkg/RQuantLib/src/utils.cpp
===================================================================
--- pkg/RQuantLib/src/utils.cpp	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/src/utils.cpp	2012-12-01 19:45:22 UTC (rev 325)
@@ -2,9 +2,9 @@
 //
 // RQuantLib -- R interface to the QuantLib libraries
 //
-// Copyright (C) 2002 - 2011  Dirk Eddelbuettel 
+// Copyright (C) 2002 - 2012  Dirk Eddelbuettel 
 // Copyright (C) 2005 - 2006  Dominick Samperi
-// Copyright (C) 2009 - 2011  Dirk Eddelbuettel and Khanh Nguyen
+// Copyright (C) 2009 - 2012  Dirk Eddelbuettel and Khanh Nguyen
 //
 // $Id$
 //
@@ -126,11 +126,9 @@
         //double dt = rparam.getDoubleValue("dt");
         
         std::string interpWhat, interpHow;
-        bool flatQuotes = true;
         if (firstQuoteName.compare("flat") != 0) { // Get interpolation method (not needed for "flat" case)
             interpWhat = Rcpp::as<std::string>(rparam["interpWhat"]);
             interpHow  = Rcpp::as<std::string>(rparam["interpHow"]);
-            flatQuotes = false;
         }
         
         // initialise from the singleton instance

Modified: pkg/RQuantLib/src/vanilla.cpp
===================================================================
--- pkg/RQuantLib/src/vanilla.cpp	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/src/vanilla.cpp	2012-12-01 19:45:22 UTC (rev 325)
@@ -92,7 +92,10 @@
         QuantLib::Time maturity = Rcpp::as<double>(rparam["maturity"]);
         int length = int(maturity*360 + 0.5); // FIXME: this could be better
         double volatility = Rcpp::as<double>(rparam["volatility"]);
-        
+        int timeSteps = Rcpp::as<int>(rparam["timeSteps"]);
+        int gridPoints = Rcpp::as<int>(rparam["gridPoints"]);
+        std::string engine = Rcpp::as<std::string>(rparam["engine"]);
+
         QuantLib::Option::Type optionType = getOptionType(type);
 
         // new framework as per QuantLib 0.3.5, updated for 0.3.7
@@ -118,22 +121,38 @@
                                                                  QuantLib::Handle<QuantLib::YieldTermStructure>(qTS),
                                                                  QuantLib::Handle<QuantLib::YieldTermStructure>(rTS),
                                                                  QuantLib::Handle<QuantLib::BlackVolTermStructure>(volTS)));
-        // new from 0.3.7 BaroneAdesiWhaley
-        boost::shared_ptr<QuantLib::PricingEngine> engine(new QuantLib::BaroneAdesiWhaleyApproximationEngine(stochProcess));
 
         QuantLib::VanillaOption option(payoff, exercise);
-        option.setPricingEngine(engine);
-                                                
-        Rcpp::List rl = Rcpp::List::create(Rcpp::Named("value") = option.NPV(),
-                                           Rcpp::Named("delta") = R_NaN,
-                                           Rcpp::Named("gamma") = R_NaN,
-                                           Rcpp::Named("vega") = R_NaN,
-                                           Rcpp::Named("theta") = R_NaN,
-                                           Rcpp::Named("rho") = R_NaN,
-                                           Rcpp::Named("divRho") = R_NaN,
-                                           Rcpp::Named("parameters") = optionParameters);
-        return rl;
-
+        if (engine=="BaroneAdesiWhaley") {
+            // new from 0.3.7 BaroneAdesiWhaley
+            boost::shared_ptr<QuantLib::PricingEngine> engine(new QuantLib::BaroneAdesiWhaleyApproximationEngine(stochProcess));
+            option.setPricingEngine(engine);
+            Rcpp::List rl = Rcpp::List::create(Rcpp::Named("value") = option.NPV(),
+                                               Rcpp::Named("delta") = R_NaReal,
+                                               Rcpp::Named("gamma") = R_NaReal,
+                                               Rcpp::Named("vega") = R_NaReal,
+                                               Rcpp::Named("theta") = R_NaReal,
+                                               Rcpp::Named("rho") = R_NaReal,
+                                               Rcpp::Named("divRho") = R_NaReal,
+                                               Rcpp::Named("parameters") = optionParameters);
+            return rl;
+        } else if (engine=="CrankNicolson") {
+            // suggestion by Bryan Lewis: use CrankNicolson for greeks
+            boost::shared_ptr<QuantLib::PricingEngine> 
+                fdcnengine(new QuantLib::FDAmericanEngine<QuantLib::CrankNicolson>(stochProcess, timeSteps, gridPoints));
+            option.setPricingEngine(fdcnengine);
+            Rcpp::List rl = Rcpp::List::create(Rcpp::Named("value") = option.NPV(),
+                                               Rcpp::Named("delta") = option.delta(),
+                                               Rcpp::Named("gamma") = option.gamma(),
+                                               Rcpp::Named("vega") = R_NaReal,
+                                               Rcpp::Named("theta") = R_NaReal,
+                                               Rcpp::Named("rho") = R_NaReal,
+                                               Rcpp::Named("divRho") = R_NaReal,
+                                               Rcpp::Named("parameters") = optionParameters);
+            return rl;
+        } else {
+            throw std::range_error("Unknown engine " + engine);
+        }
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
     } catch(...) { 

Modified: pkg/RQuantLib/src/zero.cpp
===================================================================
--- pkg/RQuantLib/src/zero.cpp	2011-09-12 02:24:54 UTC (rev 324)
+++ pkg/RQuantLib/src/zero.cpp	2012-12-01 19:45:22 UTC (rev 325)
@@ -2,7 +2,7 @@
 //
 // RQuantLib -- R interface to the QuantLib libraries
 //
-// Copyright (C) 2009 - 2010  Dirk Eddelbuettel and Khanh Nguyen
+// Copyright (C) 2009 - 2012  Dirk Eddelbuettel and Khanh Nguyen
 //
 // $Id$
 //
@@ -115,7 +115,6 @@
         //std::vector<double> yields(ryields.stlVector());
         //std::vector<std::vector<double> > bondparam(rbondmat.stlMatrix());
 
-        std::cout << "read done";
         int n = rmat.size();
         std::vector<QuantLib::Date> MatDates(rmat.size());
         for (int i = 0;i<n;i++){
@@ -192,7 +191,6 @@
 
         boost::shared_ptr<QuantLib::YieldTermStructure> 
             curve(new QuantLib::PiecewiseYieldCurve<QuantLib::ZeroYield, QuantLib::Cubic>(1, calendar, instruments, dayCounter));
-        std::cout << "here";
         int numCol = 2;
         std::vector<std::string> colNames(numCol);
         colNames[0] = "date";



More information about the Rquantlib-commits mailing list