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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 17 04:45:45 CEST 2010


Author: edd
Date: 2010-06-17 04:45:45 +0200 (Thu, 17 Jun 2010)
New Revision: 256

Modified:
   pkg/RQuantLib/R/dayCounter.R
   pkg/RQuantLib/inst/ChangeLog
   pkg/RQuantLib/src/cbond.cpp
   pkg/RQuantLib/src/daycounter.cpp
   pkg/RQuantLib/src/zero.cpp
Log:
more files converted to new API


Modified: pkg/RQuantLib/R/dayCounter.R
===================================================================
--- pkg/RQuantLib/R/dayCounter.R	2010-06-16 18:46:42 UTC (rev 255)
+++ pkg/RQuantLib/R/dayCounter.R	2010-06-17 02:45:45 UTC (rev 256)
@@ -1,14 +1,12 @@
 dayCount <- function(startDates, endDates, dayCounters){
- 
+
   val <- .Call('dayCount', startDates, endDates, dayCounters,
-               PACKAGE="RQuantLib") 
-  val <- unlist(val)
+               PACKAGE="RQuantLib")
   val
 }
 
-yearFraction <- function(startDates, endDates, dayCounters){ 
+yearFraction <- function(startDates, endDates, dayCounters){
   val <- .Call('yearFraction', startDates, endDates, dayCounters,
                PACKAGE="RQuantLib")
-  val <- unlist(val)
   val
 }

Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog	2010-06-16 18:46:42 UTC (rev 255)
+++ pkg/RQuantLib/inst/ChangeLog	2010-06-17 02:45:45 UTC (rev 256)
@@ -5,8 +5,11 @@
 	* src/*cpp: Use getOptionType()
 
 	* src/asian.cpp: Converted to new API
-	* src/barrier_binary.cpp: Converted to new API
-	* src/implieds.cpp: Converted to new API
+	* src/barrier_binary.cpp: Idem
+	* src/implieds.cpp: Idem
+	* src/cbond.cpp: Idem
+	* src/daycounter.cpp: Idem
+	* src/zero.cpp: Idem
 
 2010-06-15  Dirk Eddelbuettel  <edd at debian.org>
 

Modified: pkg/RQuantLib/src/cbond.cpp
===================================================================
--- pkg/RQuantLib/src/cbond.cpp	2010-06-16 18:46:42 UTC (rev 255)
+++ pkg/RQuantLib/src/cbond.cpp	2010-06-17 02:45:45 UTC (rev 256)
@@ -1,32 +1,55 @@
-#include "rquantlib.hpp"
+// -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 
+//
+// RQuantLib -- R interface to the QuantLib libraries
+//
+// Copyright (C) 2009 - 2010  Dirk Eddelbuettel and Khanh Nguyen
+//
+// $Id$
+//
+// This file is part of the RQuantLib library for GNU R.
+// It is made available under the terms of the GNU General Public
+// License, version 2, or at your option, any later version,
+// incorporated herein by reference.
+//
+// This program is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied
+// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+// PURPOSE.  See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public
+// License along with this program; if not, write to the Free
+// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+// MA 02111-1307, USA
 
+#include <rquantlib.hpp>
+
 using namespace boost;
-RcppExport SEXP cbprice(SEXP params, SEXP dividendFrame, SEXP callFrame){
+
+RcppExport SEXP cbprice(SEXP params, SEXP dividendFrame, SEXP callFrame) {
     
-    SEXP rl=R_NilValue;
-    char* exceptionMesg=NULL;
-    try{
-        RcppParams rparams(params);
+    try {
+		Rcpp::List rparams(params);
 
-        double rff = rparams.getDoubleValue("rff");
-        double spread = rparams.getDoubleValue("spread");
-        double sigma = rparams.getDoubleValue("sigma");
-        double price = rparams.getDoubleValue("price");
-        double convRatio = rparams.getDoubleValue("convRatio");
-        double numSteps = rparams.getDoubleValue("numSteps");
-        QuantLib::Date maturity(dateFromR(rparams.getDateValue("maturity")));
-        QuantLib::Date settle(dateFromR(rparams.getDateValue("settle")));
-        QuantLib::Date issue(dateFromR(rparams.getDateValue("issue")));
-        double coupon = rparams.getDoubleValue("couponRate");
-        double p = rparams.getDoubleValue("period");
-        double basis = rparams.getDoubleValue("basis");
+        double rff = Rcpp::as<double>(rparams["rff"]);
+        double spread = Rcpp::as<double>(rparams["spread"]);
+        double sigma = Rcpp::as<double>(rparams["sigma"]);
+        double price = Rcpp::as<double>(rparams["price"]);
+        double convRatio = Rcpp::as<double>(rparams["convRatio"]);
+        double numSteps = Rcpp::as<double>(rparams["numSteps"]);
+        QuantLib::Date maturity(dateFromR(Rcpp::as<RcppDate>(rparams["maturity"])));
+        QuantLib::Date settle(dateFromR(Rcpp::as<RcppDate>(rparams["settle"])));
+        QuantLib::Date issue(dateFromR(Rcpp::as<RcppDate>(rparams["issue"])));
+        double coupon = Rcpp::as<double>(rparams["couponRate"]);
+        double p = Rcpp::as<double>(rparams["period"]);
+        double basis = Rcpp::as<double>(rparams["basis"]);
         DayCounter dayCounter = getDayCounter(basis);
         Frequency freq = getFrequency(p);
         Period period(freq);
-        //double emr = rparams.getDoubleValue("emr");
-        double callType = rparams.getDoubleValue("calltype");
-        double dividendType = rparams.getDoubleValue("dividendtype");
-        //double treeType = rparams.getDoubleValue("treeType");        
+        //double emr = Rcpp::as<double>(rparams["emr"]);
+        double callType = Rcpp::as<double>(rparams["calltype"]);
+        double dividendType = Rcpp::as<double>(rparams["dividendtype"]);
+        //double treeType = Rcpp::as<double>(rparams["treeType"]);        
 
 
         DividendSchedule dividendSchedule;
@@ -49,7 +72,7 @@
                 }
             }
         }
-        catch (std::exception& ex){}
+        catch (std::exception& ex) { }
 
         CallabilitySchedule callabilitySchedule;
         try {
@@ -76,7 +99,7 @@
         }
         catch (std::exception& ex){}
 
-        Calendar calendar=UnitedStates(UnitedStates::GovernmentBond);        
+        Calendar calendar = UnitedStates(UnitedStates::GovernmentBond);	// FIXME
         QuantLib::Integer fixingDays = 1;
         Date todayDate = calendar.advance(settle, -fixingDays, Days);
         Settings::instance().evaluationDate() = todayDate;
@@ -123,16 +146,13 @@
         
         bond.setPricingEngine(engine);
 
-        RcppResultSet rs;
-        rs.add("NPV", bond.NPV());
-        rl = rs.getReturnList();
+        return Rcpp::wrap(bond.NPV());
 
-    } catch(std::exception& ex) {
-        exceptionMesg = copyMessageToR(ex.what());
-    } catch(...) {
-        exceptionMesg = copyMessageToR("unknown reason");
-    }   
-    if(exceptionMesg != NULL)
-        Rf_error(exceptionMesg);    
-    return rl;
+    } catch(std::exception &ex) { 
+        forward_exception_to_r(ex); 
+    } catch(...) { 
+        ::Rf_error("c++ exception (unknown reason)"); 
+    }
+
+    return R_NilValue;
 }


Property changes on: pkg/RQuantLib/src/cbond.cpp
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: pkg/RQuantLib/src/daycounter.cpp
===================================================================
--- pkg/RQuantLib/src/daycounter.cpp	2010-06-16 18:46:42 UTC (rev 255)
+++ pkg/RQuantLib/src/daycounter.cpp	2010-06-17 02:45:45 UTC (rev 256)
@@ -1,76 +1,83 @@
+// -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 
+//
+// RQuantLib -- R interface to the QuantLib libraries
+//
+// Copyright (C) 2009 - 2010  Dirk Eddelbuettel and Khanh Nguyen
+//
+// $Id$
+//
+// This file is part of the RQuantLib library for GNU R.
+// It is made available under the terms of the GNU General Public
+// License, version 2, or at your option, any later version,
+// incorporated herein by reference.
+//
+// This program is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied
+// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+// PURPOSE.  See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public
+// License along with this program; if not, write to the Free
+// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+// MA 02111-1307, USA
+
 #include "rquantlib.hpp"
+
 using namespace boost;
 
+RcppExport SEXP dayCount(SEXP startDates, SEXP endDates, SEXP dayCounter){
 
-RcppExport SEXP dayCount(SEXP startDates, SEXP endDates, SEXP dayCounter){
-    SEXP rl = R_NilValue;
-    char* exceptionMesg = NULL;
     try {
         
         RcppDateVector s = RcppDateVector(startDates);
         RcppDateVector e = RcppDateVector(endDates);
         
-        RcppVector<double> rcppVec(dayCounter);
-        std::vector<double> dc(rcppVec.stlVector());
+		Rcpp::NumericVector dc(dayCounter);
         
         int n = dc.size();
         std::vector<double> result(n);
         for (int i=0; i< n; i++){
             QuantLib::Date d1( dateFromR(s(i)) );
             QuantLib::Date d2( dateFromR(e(i)) );
-
             DayCounter counter = getDayCounter(dc[i]);
-            result[i] = (double)counter.dayCount(d1, d2);            
+            result[i] = static_cast<double>(counter.dayCount(d1, d2));            
         }
+        return Rcpp::wrap(result);
         
-        RcppResultSet rs;
-        rs.add("",result);
-        rl = rs.getReturnList();
-        
-    } 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 yearFraction(SEXP startDates, SEXP endDates, SEXP dayCounter){
-    SEXP rl = R_NilValue;
-    char* exceptionMesg = NULL;
+
     try {
         
         RcppDateVector s = RcppDateVector(startDates);
         RcppDateVector e = RcppDateVector(endDates);
         
-        RcppVector<double> rcppVec(dayCounter);
-        std::vector<double> dc(rcppVec.stlVector());
-        
+		Rcpp::NumericVector dc(dayCounter);
         int n = dc.size();
         std::vector<double> result(n);
         for (int i=0; i< n; i++){
             QuantLib::Date d1( dateFromR(s(i)) );
             QuantLib::Date d2( dateFromR(e(i)) );            
-            
-            
             DayCounter counter = getDayCounter(dc[i]);
             result[i] = (double)counter.yearFraction(d1, d2);            
         }        
-        RcppResultSet rs;
-        rs.add("",result);
-        rl = rs.getReturnList();
+        return Rcpp::wrap(result);
 
-    } 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;
 }


Property changes on: pkg/RQuantLib/src/daycounter.cpp
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: pkg/RQuantLib/src/zero.cpp
===================================================================
--- pkg/RQuantLib/src/zero.cpp	2010-06-16 18:46:42 UTC (rev 255)
+++ pkg/RQuantLib/src/zero.cpp	2010-06-17 02:45:45 UTC (rev 256)
@@ -1,102 +1,111 @@
-#include "rquantlib.hpp"
+// -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 
+//
+// RQuantLib -- R interface to the QuantLib libraries
+//
+// Copyright (C) 2009 - 2010  Dirk Eddelbuettel and Khanh Nguyen
+//
+// $Id$
+//
+// This file is part of the RQuantLib library for GNU R.
+// It is made available under the terms of the GNU General Public
+// License, version 2, or at your option, any later version,
+// incorporated herein by reference.
+//
+// This program is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied
+// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+// PURPOSE.  See the GNU General Public License for more
+// details.
+//
+// You should have received a copy of the GNU General Public
+// License along with this program; if not, write to the Free
+// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+// MA 02111-1307, USA
 
-RcppExport SEXP zeroprice(SEXP params){
-    SEXP rl = R_NilValue;
-    char* exceptionMesg = NULL;
+#include <rquantlib.hpp>
+
+RcppExport SEXP zeroprice(SEXP params) {
+
     try {
-        RcppParams rparam(params);
+		Rcpp::List rparam(params);
 
-        double yield = rparam.getDoubleValue("Yield");
-        QuantLib::Date maturity(dateFromR(rparam.getDateValue("Maturity")));
-        QuantLib::Date settle(dateFromR(rparam.getDateValue("Settle")));
+        double yield = Rcpp::as<double>(rparam["Yield"]);
+        QuantLib::Date maturity(dateFromR(Rcpp::as<RcppDate>(rparam["Maturity"])));
+        QuantLib::Date settle(dateFromR(Rcpp::as<RcppDate>(rparam["Settle"])));
 
         Calendar calendar=UnitedStates(UnitedStates::GovernmentBond);
         QuantLib::Integer fixingDays = 2;
         Date todaysDate = calendar.advance(settle, -fixingDays, Days);
         Settings::instance().evaluationDate() = todaysDate;
 
-
-        double period = rparam.getDoubleValue("Period");
-        double basis = rparam.getDoubleValue("Basis");
+        double period = Rcpp::as<double>(rparam["Period"]);
+        double basis = Rcpp::as<double>(rparam["Basis"]);
         DayCounter dayCounter = getDayCounter(basis);
         Frequency freq = getFrequency(period);
         Period p(freq);
-        //double EMR = rparam.getDoubleValue("EMR");
+        //double EMR = Rcpp::as<double>(rparam["EMR");
 
-
         ZeroCouponBond bond(1, calendar, 
                             100, maturity, Unadjusted, 
                             100.0, settle);
 
         double price = bond.cleanPrice(yield, dayCounter, Compounded, freq);
         
-        RcppResultSet rs;
-        rs.add("Price", price);
-        rl = rs.getReturnList();    
+        return Rcpp::wrap(price);
 
-    } 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 zeroyield(SEXP params){
-    SEXP rl = R_NilValue;
-    char* exceptionMesg = NULL;
+
     try {
-        RcppParams rparam(params);
+		Rcpp::List rparam(params);
 
-        double price = rparam.getDoubleValue("Price");
-        QuantLib::Date maturity(dateFromR(rparam.getDateValue("Maturity")));
-        QuantLib::Date settle(dateFromR(rparam.getDateValue("Settle")));
+        double price = Rcpp::as<double>(rparam["Price"]);
+        QuantLib::Date maturity(dateFromR(Rcpp::as<RcppDate>(rparam["Maturity"])));
+        QuantLib::Date settle(dateFromR(Rcpp::as<RcppDate>(rparam["Settle"])));
 
         Calendar calendar=UnitedStates(UnitedStates::GovernmentBond);
         QuantLib::Integer fixingDays = 2;
         Date todaysDate = calendar.advance(settle, -fixingDays, Days);
         Settings::instance().evaluationDate() = todaysDate;
 
-        double period = rparam.getDoubleValue("Period");
-        double basis = rparam.getDoubleValue("Basis");
+        double period = Rcpp::as<double>(rparam["Period"]);
+        double basis = Rcpp::as<double>(rparam["Basis"]);
         DayCounter dayCounter = getDayCounter(basis);
         Frequency freq = getFrequency(period);
         Period p(freq);
-        //double EMR = rparam.getDoubleValue("EMR");
+        //double EMR = Rcpp::as<double>(rparam["EMR");
 
 
         ZeroCouponBond bond(1, calendar, 100, maturity, Unadjusted, 100.0, settle);
 
         double yield = bond.yield(price, dayCounter, Compounded, freq);
         
-        RcppResultSet rs;
-        rs.add("Yield", yield);
-        rl = rs.getReturnList();    
+        return Rcpp::wrap(yield);
 
-    } 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 zbtyield(SEXP MatVec, SEXP BondMat, 
                          SEXP yieldVec, SEXP SettlVec,
-                         SEXP cpVec, SEXP param){
-    SEXP rl = R_NilValue;
-    char* exceptionMesg = NULL;
+                         SEXP cpVec, SEXP param) {
     try {   
 
-
         RcppParams rparam(param);
-        //double oc = rparam.getDoubleValue("OC");
+        //double oc = Rcpp::as<double>(rparam["OC");
 
         RcppDateVector rmat(MatVec);
         RcppDateVector rsettle(SettlVec);
@@ -125,8 +134,6 @@
         //setting up the bonds
         const Size numberOfBonds = n;
 
-
-
         std::vector<boost::shared_ptr<RateHelper> > instruments;
 
         std::vector< boost::shared_ptr<SimpleQuote> > quote;
@@ -202,38 +209,26 @@
         colNames[0] = "date";
         colNames[1] = "zeroRates";
         
-        RcppFrame frame(colNames);
+		RcppDateVector dates(numberOfBonds);
+		Rcpp::NumericVector zeros(numberOfBonds);
+
         Date current = SettleDates[0];
         //int n1 = curve->maxDate() - SettleDates[0];
         for (unsigned int i = 0; i<numberOfBonds;i++){
-            std::vector<ColDatum> row(numCol);
             Date d = MatDates[i];
-            row[0].setDateValue(RcppDate(d.month(),
-                                         d.dayOfMonth(),
-                                         d.year()));
-            
-            double zrate = curve->zeroRate(d, ActualActual(),
-                                            Simple);
-            row[1].setDoubleValue(zrate);                        
-
-
-
-            frame.addRow(row);
-            current++;
+            dates(i) = RcppDate(d.month(), d.dayOfMonth(), d.year());
+			zeros[i] = curve->zeroRate(d, ActualActual(), Simple);
+            current++; // ?
         }
 
-	RcppResultSet rs;
-        rs.add("table", frame);
-	rl = rs.getReturnList();
+		return Rcpp::DataFrame::create(Rcpp::Named("date")=dates,
+									   Rcpp::Named("zeroRates")=zeros);
 
-        
-    } 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;
 }


Property changes on: pkg/RQuantLib/src/zero.cpp
___________________________________________________________________
Added: svn:keywords
   + Id



More information about the Rquantlib-commits mailing list