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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 18 03:00:08 CEST 2010


Author: edd
Date: 2010-06-18 03:00:08 +0200 (Fri, 18 Jun 2010)
New Revision: 259

Modified:
   pkg/RQuantLib/R/hullWhiteCalibration.R
   pkg/RQuantLib/inst/ChangeLog
   pkg/RQuantLib/src/hullwhite.cpp
Log:
more files converted to new API


Modified: pkg/RQuantLib/R/hullWhiteCalibration.R
===================================================================
--- pkg/RQuantLib/R/hullWhiteCalibration.R	2010-06-17 11:52:54 UTC (rev 258)
+++ pkg/RQuantLib/R/hullWhiteCalibration.R	2010-06-18 01:00:08 UTC (rev 259)
@@ -1,17 +1,40 @@
+## RQuantLib -- R interface to the QuantLib libraries
+##
+## Copyright (C) 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
+
 hullWhiteCalibrateUsingCap <- function(termStrc, capHelpers,
-                               index, evaluationDate){
+                                       index, evaluationDate) {
   capData <- capHelpers$data
 
   indexparams <- list(type=index$type);
   ibor <- index$term
 
   val <- .Call("QL_HullWhiteCalibrationUsingCap",
-               c(termStrc$table$date),
+               termStrc$table$date,
                termStrc$table$zeroRates,
-               capData, 
-               c(ibor$table$date),
+               capData,
+               ibor$table$date,
                ibor$table$zeroRates,
-               indexparams, evaluationDate)
+               indexparams,
+               evaluationDate)
 }
 
 hullWhiteCalibrateUsingSwap <- function(termStrc, swapHelpers,
@@ -22,10 +45,11 @@
   ibor <- index$term
 
   val <- .Call("QL_HullWhiteCalibrationUsingSwap",
-               c(termStrc$table$date),
+               termStrc$table$date,
                termStrc$table$zeroRates,
-               swapData, 
-               c(ibor$table$date),
+               swapData,
+               ibor$table$date,
                ibor$table$zeroRates,
-               indexparams, evaluationDate)
+               indexparams,
+               evaluationDate)
 }


Property changes on: pkg/RQuantLib/R/hullWhiteCalibration.R
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog	2010-06-17 11:52:54 UTC (rev 258)
+++ pkg/RQuantLib/inst/ChangeLog	2010-06-18 01:00:08 UTC (rev 259)
@@ -2,6 +2,7 @@
 
 	* src/curves.cpp: Converted to new API
 	* src/discount.cpp: Idem
+	* src/hullwhite.cpp: Idem
 
 2010-06-16  Dirk Eddelbuettel  <edd at debian.org>
 


Property changes on: pkg/RQuantLib/inst/ChangeLog
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: pkg/RQuantLib/src/hullwhite.cpp
===================================================================
--- pkg/RQuantLib/src/hullwhite.cpp	2010-06-17 11:52:54 UTC (rev 258)
+++ pkg/RQuantLib/src/hullwhite.cpp	2010-06-18 01:00:08 UTC (rev 259)
@@ -1,60 +1,99 @@
-#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) 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 QL_HullWhiteCalibrationUsingCap(SEXP termStrcDateVec,
-                                        SEXP termStrcZeroVec,
-                                        SEXP capDataDF,
-                                        SEXP iborDateVec,
-                                        SEXP iborZeroVec,
-                                        SEXP iborparams,
-                                        SEXP evaluationDate){
-    SEXP rl = R_NilValue; 	// Use this when there is nothing to be returned.
-    char *exceptionMesg = NULL;
-
+												SEXP termStrcZeroVec,
+												SEXP capDataDF,
+												SEXP iborDateVec,
+												SEXP iborZeroVec,
+												SEXP iborparams,
+												SEXP evaluationDate) {
+	
     try {
         RcppDateVector dv(evaluationDate);
         QuantLib::Date evalDate(dateFromR(dv(0)));
         Settings::instance().evaluationDate() = evalDate;
+
+        Handle<YieldTermStructure> 
+			term(rebuildCurveFromZeroRates(termStrcDateVec,
+										   termStrcZeroVec));
         
-        Handle<YieldTermStructure> term(rebuildCurveFromZeroRates(termStrcDateVec,
-                                                                  termStrcZeroVec));
-        
         //set up ibor index
-        Handle<YieldTermStructure> indexStrc(rebuildCurveFromZeroRates(iborDateVec,
-                                                                       iborZeroVec));    
-        RcppParams param(iborparams);
-        std::string iborType = param.getStringValue("type");
-        boost::shared_ptr<IborIndex> index = buildIborIndex(iborType,
-                                                            indexStrc);
+        Handle<YieldTermStructure> 
+			indexStrc(rebuildCurveFromZeroRates(iborDateVec,
+												iborZeroVec));    
+		Rcpp::List param(iborparams);
+        std::string iborType = Rcpp::as<std::string>(param["type"]);
+        boost::shared_ptr<IborIndex> index = buildIborIndex(iborType, indexStrc);
         //process capDataDF
         std::vector<boost::shared_ptr <CalibrationHelper> > caps;
         try {
-            RcppFrame capDF(capDataDF);
-            std::vector<std::vector<ColDatum> > table = capDF.getTableData();
-            int nrow = table.size();
-            for (int row=0; row<nrow;row++){
-                Period p = periodByTimeUnit(table[row][0].getIntValue(),
-                                            table[row][1].getStringValue());
+			Rcpp::DataFrame capDF(capDataDF);
+			Rcpp::NumericVector i0v = capDF[0];
+			Rcpp::CharacterVector  s1v = capDF[1];
+			Rcpp::NumericVector d2v = capDF[2];
+			Rcpp::NumericVector i3v = capDF[3];
+			Rcpp::NumericVector i4v = capDF[4];
+			Rcpp::NumericVector i5v = capDF[5];
+            //std::vector<std::vector<ColDatum> > table = capDF.getTableData();
+            //int nrow = table.size();
+			int nrow = i0v.size();
+            for (int row=0; row<nrow;row++) {
+                //Period p = periodByTimeUnit(table[row][0].getIntValue(),
+                //                            table[row][1].getStringValue());
+                Period p = periodByTimeUnit(i0v[row], 
+											Rcpp::as<std::string>(s1v[row]));
                 
-                boost::shared_ptr<Quote> vol(new SimpleQuote(table[row][2].getDoubleValue()));
-                DayCounter dc = getDayCounter(table[row][4].getIntValue());
+                boost::shared_ptr<Quote> 
+					//vol(new SimpleQuote(table[row][2].getDoubleValue()));
+					vol(new SimpleQuote(d2v[row]));
+				//DayCounter dc = getDayCounter(table[row][4].getIntValue());
+				DayCounter dc = getDayCounter(i4v[row]);
                 boost::shared_ptr<CalibrationHelper> 
                     helper(new CapHelper(p, Handle<Quote>(vol), index, 
-                                         getFrequency(table[row][3].getIntValue()),
+                                         //getFrequency(table[row][3].getIntValue()),
+										 getFrequency(i3v[row]),
                                          dc,
-                                         (table[row][5].getIntValue()==1)? true:false,
+                                         //(table[row][5].getIntValue()==1)? true:false,>(
+										 (i5v[row]==1) ? true : false,
                                          term));
-                boost::shared_ptr<BlackCapFloorEngine> engine(new BlackCapFloorEngine(term, table[row][2].getDoubleValue()));
+                boost::shared_ptr<BlackCapFloorEngine> 
+					engine(new BlackCapFloorEngine(term, 
+												   //table[row][2].getDoubleValue()));
+												   d2v[row]));
                 
                 helper->setPricingEngine(engine);                
                 caps.push_back(helper);
-            }
-        }
-        catch(std::exception& ex){}
+			}
+        } catch(std::exception& ex) { 
+			forward_exception_to_r(ex); 
+		}
         
-        
         //set up the HullWhite model
         boost::shared_ptr<HullWhite> model(new HullWhite(term));
         
@@ -66,32 +105,25 @@
         //return the result
         Array xMinCalculated = model->params();
 
-        RcppResultSet rs;
-        rs.add("alpha", xMinCalculated[0]);
-        rs.add("sigma", xMinCalculated[1]);
-        rl = rs.getReturnList();
-        
-    } catch(std::exception& ex) {
-        exceptionMesg = copyMessageToR(ex.what());
-    } catch(...) {
-        exceptionMesg = copyMessageToR("unknown reason");
+        return Rcpp::List::create(Rcpp::Named("alpha") = xMinCalculated[0],
+								  Rcpp::Named("sigma") = xMinCalculated[1]);
+
+    } 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_HullWhiteCalibrationUsingSwap(SEXP termStrcDateVec,
-                                        SEXP termStrcZeroVec,
-                                        SEXP swapDataDF,
-                                        SEXP iborDateVec,
-                                        SEXP iborZeroVec,
-                                        SEXP iborparams,
-                                        SEXP evaluationDate){
-    SEXP rl = R_NilValue; 	// Use this when there is nothing to be returned.
-    char *exceptionMesg = NULL;
+												 SEXP termStrcZeroVec,
+												 SEXP swapDataDF,
+												 SEXP iborDateVec,
+												 SEXP iborZeroVec,
+												 SEXP iborparams,
+												 SEXP evaluationDate){
 
     try {
         RcppDateVector dv(evaluationDate);
@@ -99,40 +131,61 @@
         Settings::instance().evaluationDate() = evalDate;
 
         //set up the HullWhite model       
-        Handle<YieldTermStructure> term(rebuildCurveFromZeroRates(termStrcDateVec,
-                                                                  termStrcZeroVec));
+        Handle<YieldTermStructure> 
+			term(rebuildCurveFromZeroRates(termStrcDateVec,
+										   termStrcZeroVec));
         
         boost::shared_ptr<HullWhite> model(new HullWhite(term));        
         
         //set up ibor index
-        Handle<YieldTermStructure> indexStrc(rebuildCurveFromZeroRates(iborDateVec,
-                                                                       iborZeroVec));    
-        RcppParams param(iborparams);
-        std::string iborType = param.getStringValue("type");
-        boost::shared_ptr<IborIndex> index = buildIborIndex(iborType,
-                                                            indexStrc);
+        Handle<YieldTermStructure> 
+			indexStrc(rebuildCurveFromZeroRates(iborDateVec,
+												iborZeroVec));    
+		Rcpp::List param(iborparams);
+        std::string iborType = Rcpp::as<std::string>(param["type"]);
+        boost::shared_ptr<IborIndex> index = buildIborIndex(iborType, indexStrc);
         //process capDataDF
-        boost::shared_ptr<PricingEngine> engine(
-                                                new JamshidianSwaptionEngine(model));
+        boost::shared_ptr<PricingEngine> 
+			engine(new JamshidianSwaptionEngine(model));
         std::vector<boost::shared_ptr <CalibrationHelper> > swaps;
+
         try {
-            RcppFrame swapDF(swapDataDF);
-            std::vector<std::vector<ColDatum> > table = swapDF.getTableData();
-            int nrow = table.size();
+            //RcppFrame swapDF(swapDataDF);
+			Rcpp::DataFrame swapDF(swapDataDF);
+			Rcpp::NumericVector i0v = swapDF[0];
+			Rcpp::CharacterVector  s1v = swapDF[1];
+			Rcpp::NumericVector i2v = swapDF[2];
+			Rcpp::CharacterVector s3v = swapDF[3];
+			Rcpp::NumericVector d4v = swapDF[4];
+			Rcpp::NumericVector i5v = swapDF[5];
+			Rcpp::CharacterVector s6v = swapDF[6];
+			Rcpp::NumericVector i7v = swapDF[7];
+			Rcpp::NumericVector i8v = swapDF[8];
+            //std::vector<std::vector<ColDatum> > table = swapDF.getTableData();
+            //int nrow = table.size();
+			int nrow = i0v.size();
             for (int row=0; row<nrow;row++){
 
-                Period maturity = periodByTimeUnit(table[row][0].getIntValue(),
-                                                   table[row][1].getStringValue());
-                Period length = periodByTimeUnit(table[row][2].getIntValue(),
-                                            table[row][3].getStringValue());                                
-                boost::shared_ptr<Quote> vol(new SimpleQuote(table[row][4].getDoubleValue()));
+                //Period maturity = periodByTimeUnit(table[row][0].getIntValue(),
+                //                                   table[row][1].getStringValue());
+                Period maturity = periodByTimeUnit(i0v[row], 
+												   Rcpp::as<std::string>(s1v[row]));
+                //Period length = periodByTimeUnit(table[row][2].getIntValue(),
+                //                            table[row][3].getStringValue());                                
+                Period length = periodByTimeUnit(i0v[row], 
+												 Rcpp::as<std::string>(s3v[row]));
 
-                Period fixedLegTenor = periodByTimeUnit(table[row][5].getIntValue(),
-                                            table[row][6].getStringValue());
-                                
-                DayCounter fixedLegDayCounter = getDayCounter(table[row][7].getIntValue());
-                DayCounter floatingLegDayCounter = getDayCounter(table[row][8].getIntValue());
+                //boost::shared_ptr<Quote> vol(new SimpleQuote(table[row][4].getDoubleValue()));
+                boost::shared_ptr<Quote> vol(new SimpleQuote(d4v[row]));
 
+                //Period fixedLegTenor = periodByTimeUnit(table[row][5].getIntValue(),
+                //                            table[row][6].getStringValue());
+                Period fixedLegTenor = periodByTimeUnit(i5v[row], 
+														Rcpp::as<std::string>(s6v[row]));                                
+				//DayCounter fixedLegDayCounter = getDayCounter(table[row][7].getIntValue());
+                DayCounter fixedLegDayCounter = getDayCounter(i7v[row]);
+                //DayCounter floatingLegDayCounter = getDayCounter(table[row][8].getIntValue());
+				DayCounter floatingLegDayCounter = getDayCounter(i8v[row]);
 
                 boost::shared_ptr<CalibrationHelper> 
                     helper(new SwaptionHelper(maturity, length, 
@@ -140,17 +193,15 @@
                                               index, 
                                               fixedLegTenor, 
                                               fixedLegDayCounter,
-                                              floatingLegDayCounter,                                              
+                                              floatingLegDayCounter,
                                               term));
                 helper->setPricingEngine(engine);                
                 swaps.push_back(helper);
             }
-        }
-        catch(std::exception& ex){}
+        } catch(std::exception& ex) { 
+			forward_exception_to_r(ex); 
+		}
         
-        
-  
-        
         //calibrate the data
         LevenbergMarquardt optimizationMethod(1.0e-8,1.0e-8,1.0e-8);
         EndCriteria endCriteria(10000, 100, 1e-6, 1e-8, 1e-8);
@@ -159,18 +210,14 @@
         //return the result
         Array xMinCalculated = model->params();
 
-        RcppResultSet rs;
-        rs.add("alpha", xMinCalculated[0]);
-        rs.add("sigma", xMinCalculated[1]);
-        rl = rs.getReturnList();
-        
-    } catch(std::exception& ex) {
-        exceptionMesg = copyMessageToR(ex.what());
-    } catch(...) {
-        exceptionMesg = copyMessageToR("unknown reason");
+        return Rcpp::List::create(Rcpp::Named("alpha") = xMinCalculated[0],
+								  Rcpp::Named("sigma") = xMinCalculated[1]);
+
+    } 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/hullwhite.cpp
___________________________________________________________________
Added: svn:keywords
   + Id



More information about the Rquantlib-commits mailing list