[Rquantlib-commits] r214 - in pkg/RQuantLib: R src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Apr 5 18:33:13 CEST 2010
Author: knguyen
Date: 2010-04-05 18:33:13 +0200 (Mon, 05 Apr 2010)
New Revision: 214
Added:
pkg/RQuantLib/R/hullWhiteCalibration.R
pkg/RQuantLib/src/hullwhite.cpp
Modified:
pkg/RQuantLib/src/rquantlib.hpp
pkg/RQuantLib/src/utils.cpp
Log:
hull white calibration, yet tested
Added: pkg/RQuantLib/R/hullWhiteCalibration.R
===================================================================
--- pkg/RQuantLib/R/hullWhiteCalibration.R (rev 0)
+++ pkg/RQuantLib/R/hullWhiteCalibration.R 2010-04-05 16:33:13 UTC (rev 214)
@@ -0,0 +1,16 @@
+
+hullWhiteCalibrate <- function(termStrc, capHelpers,
+ index, evaluationDate){
+ capData <- capHelpers$data
+
+ indexparams <- list(type=index$type);
+ ibor <- index$term
+
+ val <- .Call("QL_HullWhiteCalibration",
+ c(termStrc$table$date),
+ termStrc$table$zeroRates,
+ capData,
+ c(ibor$table$date),
+ ibor$table$zeroRates,
+ indexparams, evaluationDate)
+}
Added: pkg/RQuantLib/src/hullwhite.cpp
===================================================================
--- pkg/RQuantLib/src/hullwhite.cpp (rev 0)
+++ pkg/RQuantLib/src/hullwhite.cpp 2010-04-05 16:33:13 UTC (rev 214)
@@ -0,0 +1,83 @@
+#include "rquantlib.hpp";
+
+using namespace boost;
+
+
+RcppExport SEXP QL_HullWhiteCalibration(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;
+
+ try {
+ RcppDateVector dv(evaluationDate);
+ QuantLib::Date evalDate(dateFromR(dv(0)));
+ Settings::instance().evaluationDate() = evalDate;
+
+ 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);
+ //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());
+
+ boost::shared_ptr<Quote> vol(new SimpleQuote(table[row][2].getDoubleValue()));
+ DayCounter dc = getDayCounter(table[row][4].getIntValue());
+ boost::shared_ptr<CalibrationHelper>
+ helper(new CapHelper(p, Handle<Quote>(vol), index,
+ getFrequency(table[row][3].getIntValue()),
+ dc,
+ (table[row][5].getIntValue()==1)? true:false,
+ term));
+ boost::shared_ptr<BlackCapFloorEngine> engine(new BlackCapFloorEngine(term, table[row][2].getDoubleValue()));
+
+ helper->setPricingEngine(engine);
+ caps.push_back(helper);
+ }
+ }
+ catch(std::exception& ex){}
+
+
+ //set up the HullWhite model
+ boost::shared_ptr<HullWhite> model(new HullWhite(term));
+
+ //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(caps, 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;
+}
Modified: pkg/RQuantLib/src/rquantlib.hpp
===================================================================
--- pkg/RQuantLib/src/rquantlib.hpp 2010-04-04 16:50:02 UTC (rev 213)
+++ pkg/RQuantLib/src/rquantlib.hpp 2010-04-05 16:33:13 UTC (rev 214)
@@ -158,5 +158,8 @@
boost::shared_ptr<YieldTermStructure> rebuildCurveFromZeroRates(
SEXP dateSexp,
SEXP zeroSexp);
+boost::shared_ptr<IborIndex> buildIborIndex(std::string type,
+ const Handle<YieldTermStructure>& iborStrc);
Calendar* getCalendar(SEXP calParameters);
+Period periodByTimeUnit(int length, std::string unit);
#endif
Modified: pkg/RQuantLib/src/utils.cpp
===================================================================
--- pkg/RQuantLib/src/utils.cpp 2010-04-04 16:50:02 UTC (rev 213)
+++ pkg/RQuantLib/src/utils.cpp 2010-04-05 16:33:13 UTC (rev 214)
@@ -361,6 +361,16 @@
}
+
+Period periodByTimeUnit(int length, std::string unit){
+ TimeUnit tu = Years;
+ if (unit=="Days") tu=Days;
+ if (unit=="Weeks") tu=Weeks;
+ if (unit=="Months") tu=Months;
+
+ return Period(length, tu);
+
+}
TimeUnit getTimeUnit(const double n){
if (n==0) return Days;
else if (n==1) return Weeks;
@@ -424,3 +434,41 @@
return pcal;
}
+
+
+boost::shared_ptr<IborIndex> buildIborIndex(std::string type,
+ const Handle<YieldTermStructure>& iborStrc){
+
+ if (type == "Euribor10M")
+ return boost::shared_ptr<IborIndex>(new Euribor10M(iborStrc));
+ if (type == "Euribor11M")
+ return boost::shared_ptr<IborIndex>(new Euribor11M(iborStrc));
+ if (type == "Euribor1M")
+ return boost::shared_ptr<IborIndex>(new Euribor1M(iborStrc));
+ if (type == "Euribor1Y")
+ return boost::shared_ptr<IborIndex>(new Euribor1Y(iborStrc));
+ if (type == "Euribor2M")
+ return boost::shared_ptr<IborIndex>(new Euribor2M(iborStrc));
+ if (type == "Euribor2W")
+ return boost::shared_ptr<IborIndex>(new Euribor2W(iborStrc));
+ if (type == "Euribor3M")
+ return boost::shared_ptr<IborIndex>(new Euribor3M(iborStrc));
+ if (type == "Euribor3W")
+ return boost::shared_ptr<IborIndex>(new Euribor3W(iborStrc));
+ if (type == "Euribor4M")
+ return boost::shared_ptr<IborIndex>(new Euribor4M(iborStrc));
+ if (type == "Euribor5M")
+ return boost::shared_ptr<IborIndex>(new Euribor5M(iborStrc));
+ if (type == "Euribor6M")
+ return boost::shared_ptr<IborIndex>(new Euribor6M(iborStrc));
+ if (type == "Euribor7M")
+ return boost::shared_ptr<IborIndex>(new Euribor7M(iborStrc));
+ if (type == "Euribor8M")
+ return boost::shared_ptr<IborIndex>(new Euribor8M(iborStrc));
+ if (type == "Euribor9M")
+ return boost::shared_ptr<IborIndex>(new Euribor9M(iborStrc));
+ if (type == "EuriborSW")
+ return boost::shared_ptr<IborIndex>(new EuriborSW(iborStrc));
+ return boost::shared_ptr<IborIndex>();
+}
+
More information about the Rquantlib-commits
mailing list