[Rquantlib-commits] r251 - in pkg/RQuantLib: inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 16 04:38:15 CEST 2010


Author: edd
Date: 2010-06-16 04:38:14 +0200 (Wed, 16 Jun 2010)
New Revision: 251

Modified:
   pkg/RQuantLib/inst/ChangeLog
   pkg/RQuantLib/src/calendars.cpp
   pkg/RQuantLib/src/vanilla.cpp
Log:
more new API conversion


Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog	2010-06-15 22:50:33 UTC (rev 250)
+++ pkg/RQuantLib/inst/ChangeLog	2010-06-16 02:38:14 UTC (rev 251)
@@ -1,3 +1,7 @@
+2010-06-15  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/vanilla.cpp: Converted to new API
+
 2010-06-14  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/calendars.cpp: Yet more simplification from "new" Rcpp API

Modified: pkg/RQuantLib/src/calendars.cpp
===================================================================
--- pkg/RQuantLib/src/calendars.cpp	2010-06-15 22:50:33 UTC (rev 250)
+++ pkg/RQuantLib/src/calendars.cpp	2010-06-16 02:38:14 UTC (rev 251)
@@ -243,10 +243,10 @@
     try {
         boost::shared_ptr<Calendar> pcal( getCalendar(Rcpp::as<std::string>(calSexp)) );
         Rcpp::List rparam(params);        
-        BusinessDayConvention bdc = getBusinessDayConvention(rparam["bdc"]);
-        double emr = rparam["emr"];
-        double amount = rparam["amount"];
-        double unit = rparam["unit"];
+        BusinessDayConvention bdc = getBusinessDayConvention( Rcpp::as<double>(rparam["bdc"]) );
+        double emr = Rcpp::as<double>(rparam["emr"]);
+        double amount = Rcpp::as<double>(rparam["amount"]);
+        double unit = Rcpp::as<double>(rparam["unit"]);
 
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
@@ -276,9 +276,9 @@
     try {
         boost::shared_ptr<Calendar> pcal( getCalendar(Rcpp::as<std::string>(calSexp)) );
         Rcpp::List rparam(param);        
-        BusinessDayConvention bdc = getBusinessDayConvention(rparam["bdc"]);
-        double emr = rparam["emr"];
-        double period = rparam["period"];
+        BusinessDayConvention bdc = getBusinessDayConvention( Rcpp::as<double>(rparam["bdc"]) );
+        double emr = Rcpp::as<double>(rparam["emr"]);
+        double period = Rcpp::as<double>(rparam["period"]);
 
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
@@ -310,8 +310,8 @@
     try {
         boost::shared_ptr<Calendar> pcal( getCalendar(Rcpp::as<std::string>(calSexp)) );
         Rcpp::List rparam(params);
-        double ifirst = rparam["includeFirst"];
-        double ilast = rparam["includeLast"];
+        double ifirst = Rcpp::as<double>(rparam["includeFirst"]);
+        double ilast = Rcpp::as<double>(rparam["includeLast"]);
 
         RcppDateVector dates1  = RcppDateVector(from);
         RcppDateVector dates2  = RcppDateVector(to);
@@ -343,9 +343,9 @@
     try {
         boost::shared_ptr<Calendar> pcal( getCalendar(Rcpp::as<std::string>(calSexp)) );
         Rcpp::List rparam(params);
-        double iw = rparam["includeWeekends"];
-        RcppDate d1 = RcppDate( (int) rparam["from"] );
-        RcppDate d2 = RcppDate( (int) rparam["to"] );
+        double iw = Rcpp::as<double>(rparam["includeWeekends"]);
+        RcppDate d1 = RcppDate( Rcpp::as<int>( rparam["from"] ));
+        RcppDate d2 = RcppDate( Rcpp::as<int>( rparam["to"] ));
 
         std::vector<QuantLib::Date> 
             holidays = QuantLib::Calendar::holidayList(*pcal,

Modified: pkg/RQuantLib/src/vanilla.cpp
===================================================================
--- pkg/RQuantLib/src/vanilla.cpp	2010-06-15 22:50:33 UTC (rev 250)
+++ pkg/RQuantLib/src/vanilla.cpp	2010-06-16 02:38:14 UTC (rev 251)
@@ -22,27 +22,22 @@
 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 // MA 02111-1307, USA
 
-// NB can be build standalone as   PKG_LIBS=-lQuantLib R CMD SHLIB RQuantLib.cc
+#include <rquantlib.hpp>
 
-#include "rquantlib.hpp"
-
 RcppExport  SEXP QL_EuropeanOption(SEXP optionParameters) {
 
-    SEXP rl=R_NilValue;
-    char* exceptionMesg=NULL;
-  
     try {
 
-        RcppParams rparam(optionParameters);    	// Parameter wrapper class
+        Rcpp::List rparam(optionParameters);    	// Parameters via list
 
-        std::string type = rparam.getStringValue("type");
-        double underlying = rparam.getDoubleValue("underlying");
-        double strike = rparam.getDoubleValue("strike");
-        Spread dividendYield = rparam.getDoubleValue("dividendYield");
-        Rate riskFreeRate = rparam.getDoubleValue("riskFreeRate");
-        Time maturity = rparam.getDoubleValue("maturity");
+        std::string type = Rcpp::as<std::string>(rparam["type"]);
+        double underlying = Rcpp::as<double>(rparam["underlying"]);
+        double strike = Rcpp::as<double>(rparam["strike"]);
+        Spread dividendYield = Rcpp::as<double>(rparam["dividendYield"]);
+        Rate riskFreeRate = Rcpp::as<double>(rparam["riskFreeRate"]);
+        Time maturity = Rcpp::as<double>(rparam["maturity"]);
         int length = int(maturity*360 + 0.5); // FIXME: this could be better
-        double volatility = rparam.getDoubleValue("volatility");
+        double volatility = Rcpp::as<double>(rparam["volatility"]);
     
         Option::Type optionType=Option::Call;
         if (type=="call") {
@@ -71,49 +66,40 @@
         boost::shared_ptr<StrikedTypePayoff> payoff(new PlainVanillaPayoff(optionType, strike));
         boost::shared_ptr<VanillaOption> option = makeOption(payoff, exercise, spot, qTS, rTS, volTS);
 
-        RcppResultSet rs;
-        rs.add("value", option->NPV());
-        rs.add("delta", option->delta());
-        rs.add("gamma", option->gamma());
-        rs.add("vega", option->vega());
-        rs.add("theta", option->theta());
-        rs.add("rho", option->rho());
-        rs.add("divRho", option->dividendRho());
-        rs.add("parameters", optionParameters, false);
-        rl = rs.getReturnList();
+        Rcpp::List rl = Rcpp::List::create(Rcpp::Named("value") = option->NPV(),
+                                           Rcpp::Named("delta") = option->delta(),
+                                           Rcpp::Named("gamma") = option->gamma(),
+                                           Rcpp::Named("vega") = option->vega(),
+                                           Rcpp::Named("theta") = option->theta(),
+                                           Rcpp::Named("rho") = option->rho(),
+                                           Rcpp::Named("divRho") = option->dividendRho(),
+                                           Rcpp::Named("parameters") = optionParameters);
+        return rl;
 
-    } catch(std::exception& ex) {
-        exceptionMesg = copyMessageToR(ex.what());
-    } catch(...) {
-        exceptionMesg = copyMessageToR("unknown reason");
+    } catch(std::exception &ex) { 
+        forward_exception_to_r(ex); 
+    } catch(...) { 
+        ::Rf_error("c++ exception (unknown reason)"); 
     }
-  
-    if(exceptionMesg != NULL)
-        Rf_error(exceptionMesg);
-    
-    return rl;
+
+    return R_NilValue;
 }
 
 RcppExport  SEXP QL_AmericanOption(SEXP optionParameters) {
 
-    SEXP rl=R_NilValue;
-    char* exceptionMesg=NULL;
-  
     try {
+        Rcpp::List rparam(optionParameters);
 
-        // Parameter wrapper classes.
-        RcppParams rparam(optionParameters);
-
-        std::string type = rparam.getStringValue("type");
-        double underlying = rparam.getDoubleValue("underlying");
-        double strike = rparam.getDoubleValue("strike");
-        Spread dividendYield = rparam.getDoubleValue("dividendYield");
-        Rate riskFreeRate = rparam.getDoubleValue("riskFreeRate");
-        Time maturity = rparam.getDoubleValue("maturity");
+        std::string type = Rcpp::as<std::string>(rparam["type"]);
+        double underlying = Rcpp::as<double>(rparam["underlying"]);
+        double strike = Rcpp::as<double>(rparam["strike"]);
+        Spread dividendYield = Rcpp::as<double>(rparam["dividendYield"]);
+        Rate riskFreeRate = Rcpp::as<double>(rparam["riskFreeRate"]);
+        Time maturity = Rcpp::as<double>(rparam["maturity"]);
         int length = int(maturity*360 + 0.5); // FIXME: this could be better
-        double volatility = rparam.getDoubleValue("volatility");
+        double volatility = Rcpp::as<double>(rparam["volatility"]);
         
-        Option::Type optionType=Option::Call;
+        Option::Type optionType = Option::Call;
         if (type=="call") {
             optionType = Option::Call;
         } else if (type=="put") {
@@ -122,7 +108,6 @@
             throw std::range_error("Unknown option " + type);
         }
 
-
         // new framework as per QuantLib 0.3.5, updated for 0.3.7
         // updated again for 0.9.0, see eg test-suite/americanoption.cpp
         Date today = Date::todaysDate();
@@ -151,26 +136,22 @@
         VanillaOption option(payoff, exercise);
         option.setPricingEngine(engine);
                                                 
-        RcppResultSet rs;
-        rs.add("value", option.NPV());
-        rs.add("delta", R_NaN);
-        rs.add("gamma", R_NaN);
-        rs.add("vega", R_NaN);
-        rs.add("theta", R_NaN);
-        rs.add("rho", R_NaN);
-        rs.add("divRho", R_NaN);
-        rs.add("parameters", optionParameters, false);
-        rl = rs.getReturnList();
+        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;
 
-    } catch(std::exception& ex) {
-        exceptionMesg = copyMessageToR(ex.what());
-    } catch(...) {
-        exceptionMesg = copyMessageToR("unknown reason");
+    } catch(std::exception &ex) { 
+        forward_exception_to_r(ex); 
+    } catch(...) { 
+        ::Rf_error("c++ exception (unknown reason)"); 
     }
-  
-    if(exceptionMesg != NULL)
-        Rf_error(exceptionMesg);
-    
-  return rl;
+
+    return R_NilValue;
 }
 



More information about the Rquantlib-commits mailing list