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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Apr 5 19:15:42 CEST 2010


Author: knguyen
Date: 2010-04-05 19:15:41 +0200 (Mon, 05 Apr 2010)
New Revision: 215

Modified:
   pkg/RQuantLib/R/hullWhiteCalibration.R
   pkg/RQuantLib/src/hullwhite.cpp
Log:
hull-white calibration using swap, yet tested

Modified: pkg/RQuantLib/R/hullWhiteCalibration.R
===================================================================
--- pkg/RQuantLib/R/hullWhiteCalibration.R	2010-04-05 16:33:13 UTC (rev 214)
+++ pkg/RQuantLib/R/hullWhiteCalibration.R	2010-04-05 17:15:41 UTC (rev 215)
@@ -1,12 +1,11 @@
-
-hullWhiteCalibrate <- function(termStrc, capHelpers,
+hullWhiteCalibrateUsingCap <- function(termStrc, capHelpers,
                                index, evaluationDate){
   capData <- capHelpers$data
 
   indexparams <- list(type=index$type);
   ibor <- index$term
 
-  val <- .Call("QL_HullWhiteCalibration",
+  val <- .Call("QL_HullWhiteCalibrationUsingCap",
                c(termStrc$table$date),
                termStrc$table$zeroRates,
                capData, 
@@ -14,3 +13,19 @@
                ibor$table$zeroRates,
                indexparams, evaluationDate)
 }
+
+hullWhiteCalibrateUsingSwap <- function(termStrc, swapHelpers,
+                               index, evaluationDate){
+  swapData <- swapHelpers$data
+
+  indexparams <- list(type=index$type);
+  ibor <- index$term
+
+  val <- .Call("QL_HullWhiteCalibrationUsingSwap",
+               c(termStrc$table$date),
+               termStrc$table$zeroRates,
+               swapData, 
+               c(ibor$table$date),
+               ibor$table$zeroRates,
+               indexparams, evaluationDate)
+}

Modified: pkg/RQuantLib/src/hullwhite.cpp
===================================================================
--- pkg/RQuantLib/src/hullwhite.cpp	2010-04-05 16:33:13 UTC (rev 214)
+++ pkg/RQuantLib/src/hullwhite.cpp	2010-04-05 17:15:41 UTC (rev 215)
@@ -3,7 +3,7 @@
 using namespace boost;
 
 
-RcppExport SEXP QL_HullWhiteCalibration(SEXP termStrcDateVec,
+RcppExport SEXP QL_HullWhiteCalibrationUsingCap(SEXP termStrcDateVec,
                                         SEXP termStrcZeroVec,
                                         SEXP capDataDF,
                                         SEXP iborDateVec,
@@ -81,3 +81,96 @@
         Rf_error(exceptionMesg);    
     return rl;    
 }
+
+
+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;
+
+    try {
+        RcppDateVector dv(evaluationDate);
+        QuantLib::Date evalDate(dateFromR(dv(0)));
+        Settings::instance().evaluationDate() = evalDate;
+
+        //set up the HullWhite model       
+        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);
+        //process capDataDF
+        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();
+            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 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<CalibrationHelper> 
+                    helper(new SwaptionHelper(maturity, length, 
+                                              Handle<Quote>(vol), 
+                                              index, 
+                                              fixedLegTenor, 
+                                              fixedLegDayCounter,
+                                              floatingLegDayCounter,                                              
+                                              term));
+                helper->setPricingEngine(engine);                
+                swaps.push_back(helper);
+            }
+        }
+        catch(std::exception& 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);
+        model->calibrate(swaps, optimizationMethod, endCriteria);
+        EndCriteria::Type ecType = model->endCriteria();
+        //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");
+    }
+    
+    if(exceptionMesg != NULL)
+        Rf_error(exceptionMesg);    
+    return rl;    
+}



More information about the Rquantlib-commits mailing list