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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 19 23:33:46 CEST 2010


Author: edd
Date: 2010-06-19 23:33:44 +0200 (Sat, 19 Jun 2010)
New Revision: 261

Modified:
   pkg/RQuantLib/R/bond.R
   pkg/RQuantLib/inst/ChangeLog
   pkg/RQuantLib/src/bonds.cpp
   pkg/RQuantLib/src/rquantlib.hpp
   pkg/RQuantLib/src/utils.cpp
Log:
converted utils.cpp to new API
factored-out three utility functions from bonds.cpp into utils.cpp


Modified: pkg/RQuantLib/R/bond.R
===================================================================
--- pkg/RQuantLib/R/bond.R	2010-06-19 02:15:54 UTC (rev 260)
+++ pkg/RQuantLib/R/bond.R	2010-06-19 21:33:44 UTC (rev 261)
@@ -239,18 +239,17 @@
                                      floors=c(),
                                      index,
                                      curve,
-                                     dateparams=list(
-                                       refDate=bond$issueDate-2,
-                                       settlementDays=1,
-                                       calendar='us',
-                                       businessDayConvention='Following',
-                                       terminationDateConvention='Following',
-                                       dayCounter='Thirty360',
-                                       period='Semiannual',
-                                       dateGeneration='Backward',
-                                       endOfMonth=0,
-                                       fixingDays=2)
-                                     ){
+                                     dateparams=list(refDate=bond$issueDate-2,
+                                                     settlementDays=1,
+                                                     calendar='us',
+                                                     businessDayConvention='Following',
+                                                     terminationDateConvention='Following',
+                                                     dayCounter='Thirty360',
+                                                     period='Semiannual',
+                                                     dateGeneration='Backward',
+                                                     endOfMonth=0,
+                                                     fixingDays=2)
+                                     ) {
     val <- 0
 
     if (is.null(bond$faceAmount)){bond$faceAmount=100}

Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog	2010-06-19 02:15:54 UTC (rev 260)
+++ pkg/RQuantLib/inst/ChangeLog	2010-06-19 21:33:44 UTC (rev 261)
@@ -1,3 +1,11 @@
+2010-06-19  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/bonds.cpp: Converted to new API
+
+	* src/utils.cpp: Factored-out utility functions from bonds.cpp
+	* src/rquantlib.hpp: Declarations for new utility functions
+	* src/bonds.cpp: Some refactoring
+
 2010-06-18  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/bonds.cpp: Converted to new API

Modified: pkg/RQuantLib/src/bonds.cpp
===================================================================
--- pkg/RQuantLib/src/bonds.cpp	2010-06-19 02:15:54 UTC (rev 260)
+++ pkg/RQuantLib/src/bonds.cpp	2010-06-19 21:33:44 UTC (rev 261)
@@ -44,7 +44,7 @@
         QuantLib::Date issueDate(dateFromR(iDate));
         //setup bond
         QuantLib::Integer fixingDays = 2;
-        Calendar calendar=UnitedStates(UnitedStates::GovernmentBond);
+        Calendar calendar = UnitedStates(UnitedStates::GovernmentBond);
         Date todaysDate = calendar.advance(issueDate, -fixingDays, Days);
         Settings::instance().evaluationDate() = todaysDate;
         Natural settlementDays = 1;
@@ -71,7 +71,7 @@
 }
 
 
-RcppExport  SEXP QL_ZeroYield(SEXP optionParameters) {
+RcppExport SEXP QL_ZeroYield(SEXP optionParameters) {
 
     try {
         Rcpp::List rparam(optionParameters);
@@ -114,9 +114,9 @@
 }
 
 
-SEXP ZeroBond(SEXP bondparam, 
-              Handle<YieldTermStructure> &discountCurve,
-              SEXP dateparams) {
+RcppExport SEXP QL_ZeroBond(SEXP bondparam, 
+                            Handle<YieldTermStructure> &discountCurve,
+                            SEXP dateparams) {
 
     try {
         Rcpp::List rparam(bondparam);
@@ -159,34 +159,12 @@
         boost::shared_ptr<PricingEngine> bondEngine(new DiscountingBondEngine(discountCurve));
         bond.setPricingEngine(bondEngine);
 
-        //cashflow
-        //int numCol = 2;
-        //std::vector<std::string> colNames(numCol);
-        //colNames[0] = "Date";
-        //colNames[1] = "Amount";
-        //RcppFrame frame(colNames);
-        Leg bondCashFlow = bond.cashflows();
-        RcppDateVector dates(bondCashFlow.size());
-        Rcpp::NumericVector amount(bondCashFlow.size());
-
-        for (unsigned int i = 0; i< bondCashFlow.size(); i++){
-            //std::vector<ColDatum> row(numCol);
-            Date d = bondCashFlow[i]->date();
-            //row[0].setDateValue(RcppDate(d.month(), d.dayOfMonth(), d.year()));
-            //row[1].setDoubleValue(bondCashFlow[i]->amount());
-            //frame.addRow(row);
-            dates(i) = RcppDate(d.month(), d.dayOfMonth(), d.year());
-            amount[i] = bondCashFlow[i]->amount();
-        }
-        Rcpp::DataFrame df = Rcpp::DataFrame::create(Rcpp::Named("Date") = dates,
-                                                     Rcpp::Named("Amount") = amount);
-
         return Rcpp::List::create(Rcpp::Named("NPV") = bond.NPV(),
                                   Rcpp::Named("cleanPrice") = bond.cleanPrice(),
                                   Rcpp::Named("dirtyPrice") = bond.dirtyPrice(),
                                   Rcpp::Named("accruedCoupon") = bond.accruedAmount(),
                                   Rcpp::Named("yield") = bond.yield(Actual360(), Compounded, Annual),
-                                  Rcpp::Named("cashFlow") = df);
+                                  Rcpp::Named("cashFlow") = getCashFlowDataFrame(bond.cashflows()));
 
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -198,9 +176,9 @@
 }
 
 
-SEXP FixedBond(SEXP bondparam, SEXP ratesVec,
-               Handle<YieldTermStructure> &discountCurve,
-               SEXP dateparams){
+RcppExport SEXP QL_FixedBond(SEXP bondparam, SEXP ratesVec,
+                             Handle<YieldTermStructure> &discountCurve,
+                             SEXP dateparams){
 
     try {
         Rcpp::List rparam(bondparam);
@@ -254,34 +232,12 @@
         boost::shared_ptr<PricingEngine> bondEngine(new DiscountingBondEngine(discountCurve));
         bond.setPricingEngine(bondEngine);   
         
-        //cashflow
-        //int numCol = 2;
-        //std::vector<std::string> colNames(numCol);
-        //colNames[0] = "Date";
-        //colNames[1] = "Amount";
-        //RcppFrame frame(colNames);
-        Leg bondCashFlow = bond.cashflows();
-        RcppDateVector dates(bondCashFlow.size());
-        Rcpp::NumericVector amount(bondCashFlow.size());
-        
-        for (unsigned int i = 0; i< bondCashFlow.size(); i++){
-            //std::vector<ColDatum> row(numCol);
-            Date d = bondCashFlow[i]->date();
-            //row[0].setDateValue(RcppDate(d.month(), d.dayOfMonth(), d.year()));
-            //row[1].setDoubleValue(bondCashFlow[i]->amount());
-            //frame.addRow(row);
-            dates(i) = RcppDate(d.month(), d.dayOfMonth(), d.year());
-            amount[i] = bondCashFlow[i]->amount();
-        }
-        Rcpp::DataFrame df = Rcpp::DataFrame::create(Rcpp::Named("Date") = dates,
-                                                     Rcpp::Named("Amount") = amount);
-
         return Rcpp::List::create(Rcpp::Named("NPV") = bond.NPV(),
                                   Rcpp::Named("cleanPrice") = bond.cleanPrice(),
                                   Rcpp::Named("dirtyPrice") = bond.dirtyPrice(),
                                   Rcpp::Named("accruedCoupon") = bond.accruedAmount(),
                                   Rcpp::Named("yield") = bond.yield(Actual360(), Compounded, Annual),
-                                  Rcpp::Named("cashFlow") = df);
+                                  Rcpp::Named("cashFlow") = getCashFlowDataFrame(bond.cashflows()));
 
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -351,7 +307,7 @@
 }
 
  
-RcppExport  SEXP QL_FixedRateBondPriceByYield(SEXP optionParameters, SEXP ratesVec) {
+RcppExport SEXP QL_FixedRateBondPriceByYield(SEXP optionParameters, SEXP ratesVec) {
   
     try {
         Rcpp::List rparam(optionParameters);
@@ -409,12 +365,12 @@
 }
 
 
-SEXP FloatingBond(SEXP bondparam, SEXP gearingsVec, SEXP spreadsVec,
-                  SEXP capsVec, SEXP floorsVec, 
-                  Handle<YieldTermStructure> &index,
-                  SEXP indexparams,
-                  Handle<YieldTermStructure> &discountCurve,
-                  SEXP dateparams) 
+SEXP QL_FloatingBond(SEXP bondparam, SEXP gearingsVec, SEXP spreadsVec,
+                     SEXP capsVec, SEXP floorsVec, 
+                     Handle<YieldTermStructure> &index,
+                     SEXP indexparams,
+                     Handle<YieldTermStructure> &discountCurve,
+                     SEXP dateparams) 
 {
   
     try {
@@ -470,7 +426,7 @@
 
         boost::shared_ptr<IborIndex> iborindex(new USDLibor(6 * Months, index));
         if (type=="USDLibor") {
-            if (inTermOf=="Months"){               
+            if (inTermOf=="Months") {               
                 boost::shared_ptr<IborIndex> temp(new USDLibor(length * Months, index));
                 iborindex = temp;
             } else if (inTermOf=="Years") {
@@ -492,33 +448,13 @@
         boost::shared_ptr<IborCouponPricer> pricer(new BlackIborCouponPricer(Handle<OptionletVolatilityStructure>()));
         setCouponPricer(bond.cashflows(),pricer);
 
-        // int numCol = 2;
-        // std::vector<std::string> colNames(numCol);
-        // colNames[0] = "Date";
-        // colNames[1] = "Amount";
-        // RcppFrame frame(colNames);
-        Leg bondCashFlow = bond.cashflows();
-        RcppDateVector dates(bondCashFlow.size());
-        Rcpp::NumericVector amount(bondCashFlow.size());
-
-        for (unsigned int i = 0; i< bondCashFlow.size(); i++) {
-            //std::vector<ColDatum> row(numCol);
-            Date d = bondCashFlow[i]->date();
-            //row[0].setDateValue(RcppDate(d.month(), d.dayOfMonth(), d.year()));
-            //row[1].setDoubleValue(bondCashFlow[i]->amount());
-            //frame.addRow(row);
-            dates(i) = RcppDate(d.month(), d.dayOfMonth(), d.year());
-            amount[i] = bondCashFlow[i]->amount();
-        }
-        Rcpp::DataFrame df = Rcpp::DataFrame::create(Rcpp::Named("Date") = dates,
-                                                     Rcpp::Named("Amount") = amount);
-
         return Rcpp::List::create(Rcpp::Named("NPV") = bond.NPV(),
                                   Rcpp::Named("cleanPrice") = bond.cleanPrice(),
                                   Rcpp::Named("dirtyPrice") = bond.dirtyPrice(),
                                   Rcpp::Named("accruedCoupon") = bond.accruedAmount(),
                                   Rcpp::Named("yield") = bond.yield(Actual360(), Compounded, Annual),
-                                  Rcpp::Named("cashFlow") = df);
+                                  Rcpp::Named("cashFlow") = getCashFlowDataFrame(bond.cashflows()));
+
         
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -539,9 +475,9 @@
 
         Handle<YieldTermStructure> discount_curve(getFlatCurve(discountCurve));
         Handle<YieldTermStructure> ibor_curve(getFlatCurve(index));
-        return Rcpp::wrap(FloatingBond(bond, gearings, caps, spreads,
-                                       floors, ibor_curve, indexparams,
-                                       discount_curve, dateparams));       
+        return Rcpp::wrap(QL_FloatingBond(bond, gearings, caps, spreads,
+                                          floors, ibor_curve, indexparams,
+                                          discount_curve, dateparams));       
         
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -566,9 +502,9 @@
         Handle<YieldTermStructure> ibor_curve(buildTermStructure(index_params,
                                                                  index_tsQuotes,
                                                                  index_times));
-        return Rcpp::wrap(FloatingBond(bond, gearings, caps, spreads,
-                                       floors, ibor_curve, indexparams,
-                                       discount_curve, dateparams));       
+        return Rcpp::wrap(QL_FloatingBond(bond, gearings, caps, spreads,
+                                          floors, ibor_curve, indexparams,
+                                          discount_curve, dateparams));       
         
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -593,9 +529,9 @@
         Handle<YieldTermStructure> discount_curve(buildTermStructure(discount_params,
                                                                      discount_tsQuotes,
                                                                      discount_times));
-        return Rcpp::wrap(FloatingBond(bond, gearings, caps, spreads,
-                                       floors, ibor_curve, indexparams,
-                                       discount_curve, dateparams));       
+        return Rcpp::wrap(QL_FloatingBond(bond, gearings, caps, spreads,
+                                          floors, ibor_curve, indexparams,
+                                          discount_curve, dateparams));       
         
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -624,9 +560,9 @@
         Handle<YieldTermStructure> discount_curve(buildTermStructure(discount_params,
                                                                  discount_tsQuotes,
                                                                  discount_times));
-        return Rcpp::wrap(FloatingBond(bond, gearings, caps, spreads,
-                                       floors, ibor_curve, indexparams,
-                                       discount_curve, dateparams));       
+        return Rcpp::wrap(QL_FloatingBond(bond, gearings, caps, spreads,
+                                          floors, ibor_curve, indexparams,
+                                          discount_curve, dateparams));       
         
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -637,21 +573,21 @@
     return R_NilValue;
 }
 
-RcppExport SEXP QL_FloatingWithRebuiltCurve(SEXP bond, SEXP gearings,
+RcppExport SEXP QL_FloatingWithRebuiltCurve(SEXP bondparams, SEXP gearings,
                                             SEXP spreads, SEXP caps,
                                             SEXP floors, SEXP indexparams,
                                             SEXP iborDateSexp, SEXP iborzeroSexp,
                                             SEXP dateSexp, SEXP zeroSexp,
-                                            SEXP dateparams){
+                                            SEXP dateparams) {
 
     try {
-        
         Handle<YieldTermStructure> ibor_curve(rebuildCurveFromZeroRates(iborDateSexp, iborzeroSexp));       
         Handle<YieldTermStructure> curve(rebuildCurveFromZeroRates(dateSexp, zeroSexp));       
         
-        return Rcpp::wrap(FloatingBond(bond, gearings, caps, spreads,
-                                       floors, ibor_curve, indexparams,
-                                       curve, dateparams));
+        SEXP flrtbond = QL_FloatingBond(bondparams, gearings, caps, spreads,
+                                        floors, ibor_curve, indexparams,
+                                        curve, dateparams);
+        return flrtbond;
 
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -668,7 +604,7 @@
                                              SEXP dateparams){
     try {
         Handle<YieldTermStructure> curve(rebuildCurveFromZeroRates(dateSexp, zeroSexp));
-        return Rcpp::wrap(FixedBond(bondparam, ratesVec, curve, dateparams));
+        return Rcpp::wrap(QL_FixedBond(bondparam, ratesVec, curve, dateparams));
         
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -686,7 +622,7 @@
     try {
 
         Handle<YieldTermStructure> curve(rebuildCurveFromZeroRates(dateSexp, zeroSexp));
-        return Rcpp::wrap(ZeroBond(bond, curve, dateparams));
+        return Rcpp::wrap(QL_ZeroBond(bond, curve, dateparams));
 
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -709,65 +645,9 @@
 
     try {
 
-        DividendSchedule dividendSchedule;
-        CallabilitySchedule callabilitySchedule;
+        DividendSchedule dividendSchedule = getDividendSchedule(dividendScheduleFrame);
+        CallabilitySchedule callabilitySchedule = getCallabilitySchedule(callabilityScheduleFrame);
 
-        try {
-            //RcppFrame rcppDividendSchedule(dividendScheduleFrame);        
-            //std::vector<std::vector<ColDatum> > table = rcppDividendSchedule.getTableData();
-            //int nrow = table.size();
-            //int ncol = table[0].size();
-            Rcpp::DataFrame divScheDF(dividendScheduleFrame);
-            Rcpp::CharacterVector s0v = divScheDF[0];
-            Rcpp::NumericVector n1v = divScheDF[1];
-            Rcpp::NumericVector n2v = divScheDF[2];
-            Rcpp::NumericVector n3v = divScheDF[3];
-            int nrow = s0v.size();
-
-            for (int row=0; row<nrow; row++){          
-                int type = (s0v[row] == "Fixed") ? 1 : 0; //  (table[row][0].getStringValue()=="Fixed") ? 1 : 0;
-                double amount = n1v[row]; // table[row][1].getDoubleValue();
-                double rate = n2v[row]; // table[row][2].getDoubleValue();
-                QuantLib::Date d(dateFromR(n3v[row])); //table[row][3].getDateValue()));            
-                if (type==1) {
-                    dividendSchedule.push_back(boost::shared_ptr<Dividend>(new FixedDividend(amount, d)));
-                } else {
-                    dividendSchedule.push_back(boost::shared_ptr<Dividend>(new FractionalDividend(rate, amount, d)));
-                }
-            }
-        } catch (std::exception& ex) { 
-            forward_exception_to_r(ex); 
-        }
-
-        try {
-            // RcppFrame rcppCallabilitySchedule(callabilityScheduleFrame);
-            // std::vector<std::vector<ColDatum> > table = rcppCallabilitySchedule.getTableData();
-            // int nrow = table.size();
-            Rcpp::DataFrame callScheDF(callabilityScheduleFrame);
-            Rcpp::NumericVector n0v = callScheDF[0];
-            Rcpp::CharacterVector s1v = callScheDF[1];
-            Rcpp::NumericVector n2v = callScheDF[2];
-            int nrow = n0v.size();
-            for (int row=0; row<nrow; row++) {
-                double price = n0v[row]; //table[row][0].getDoubleValue();
-                int type = (s1v[row]=="P") ? 1 : 0;
-                QuantLib::Date d(dateFromR(n2v[row]));
-                if (type==1){
-                    callabilitySchedule.push_back(boost::shared_ptr<Callability>
-                                                  (new Callability(Callability::Price(price, 
-                                                                                      Callability::Price::Clean),
-                                                                   Callability::Put,d )));
-            } else {
-                    callabilitySchedule.push_back(boost::shared_ptr<Callability>
-                                                  (new Callability(Callability::Price(price, 
-                                                                                      Callability::Price::Clean),
-                                                                   Callability::Call,d )));
-                }            
-            }
-        } catch (std::exception& ex){
-            forward_exception_to_r(ex); 
-        }
-
         Rcpp::List rparam(bondparams);
         
         //double faceAmount = Rcpp::as<double>(rparam["faceAmount");        
@@ -847,34 +727,12 @@
                                        redemption);
         bond.setPricingEngine(engine);
 
-        //cashflow
-        // int numCol = 2;
-        // std::vector<std::string> colNames(numCol);
-        // colNames[0] = "Date";
-        // colNames[1] = "Amount";
-        // RcppFrame frame(colNames);
-        Leg bondCashFlow = bond.cashflows();
-        RcppDateVector dates(bondCashFlow.size());
-        Rcpp::NumericVector amount(bondCashFlow.size());
-        
-        for (unsigned int i = 0; i< bondCashFlow.size(); i++){
-            //std::vector<ColDatum> row(numCol);
-            Date d = bondCashFlow[i]->date();
-            //row[0].setDateValue(RcppDate(d.month(), d.dayOfMonth(), d.year()));
-            //row[1].setDoubleValue(bondCashFlow[i]->amount());
-            //frame.addRow(row);
-            dates(i) = RcppDate(d.month(), d.dayOfMonth(), d.year());
-            amount[i] = bondCashFlow[i]->amount();
-        }
-        Rcpp::DataFrame df = Rcpp::DataFrame::create(Rcpp::Named("Date") = dates,
-                                                     Rcpp::Named("Amount") = amount);
-        
         return Rcpp::List::create(Rcpp::Named("NPV") = bond.NPV(),
                                   Rcpp::Named("cleanPrice") = bond.cleanPrice(),
                                   Rcpp::Named("dirtyPrice") = bond.dirtyPrice(),
                                   Rcpp::Named("accruedCoupon") = bond.accruedAmount(),
                                   Rcpp::Named("yield") = bond.yield(Actual360(), Compounded, Annual),
-                                  Rcpp::Named("cashFlow") = df);
+                                  Rcpp::Named("cashFlow") = getCashFlowDataFrame(bond.cashflows()));
 
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -896,65 +754,9 @@
 
     try {
 
-        DividendSchedule dividendSchedule;
-        CallabilitySchedule callabilitySchedule;
+        DividendSchedule dividendSchedule = getDividendSchedule(dividendScheduleFrame);
+        CallabilitySchedule callabilitySchedule = getCallabilitySchedule(callabilityScheduleFrame);
 
-        try {
-            //RcppFrame rcppDividendSchedule(dividendScheduleFrame);        
-            //std::vector<std::vector<ColDatum> > table = rcppDividendSchedule.getTableData();
-            //int nrow = table.size();
-            //int ncol = table[0].size();
-            Rcpp::DataFrame divScheDF(dividendScheduleFrame);
-            Rcpp::CharacterVector s0v = divScheDF[0];
-            Rcpp::NumericVector n1v = divScheDF[1];
-            Rcpp::NumericVector n2v = divScheDF[2];
-            Rcpp::NumericVector n3v = divScheDF[3];
-            int nrow = s0v.size();
-
-            for (int row=0; row<nrow; row++){          
-                int type = (s0v[row]=="Fixed") ? 1 : 0;
-                double amount = n1v[row];
-                double rate = n2v[row];
-                QuantLib::Date d(dateFromR(n3v[row]));            
-                if (type==1){
-                    dividendSchedule.push_back(boost::shared_ptr<Dividend>(new FixedDividend(amount, d)));
-                } else {
-                    dividendSchedule.push_back(boost::shared_ptr<Dividend>(new FractionalDividend(rate,amount,d)));
-                }
-            }
-        } catch (std::exception& ex) { 
-            forward_exception_to_r(ex); 
-        }
-
-        try {
-            // RcppFrame rcppCallabilitySchedule(callabilityScheduleFrame);
-            // std::vector<std::vector<ColDatum> > table = rcppCallabilitySchedule.getTableData();
-            // int nrow = table.size();
-            Rcpp::DataFrame callScheDF(callabilityScheduleFrame);
-            Rcpp::NumericVector n0v = callScheDF[0];
-            Rcpp::CharacterVector s1v = callScheDF[1];
-            Rcpp::NumericVector n2v = callScheDF[2];
-            int nrow = n0v.size();
-            for (int row=0; row<nrow; row++) {
-                double price = n0v[row]; //table[row][0].getDoubleValue();
-                int type = (s1v[row]=="P") ? 1 : 0;
-                QuantLib::Date d(dateFromR(n2v[row]));
-                if (type==1){
-                    callabilitySchedule.push_back(boost::shared_ptr<Callability>
-                                                  (new Callability(Callability::Price(price, 
-                                                                                      Callability::Price::Clean),
-                                                                   Callability::Put,d )));
-            } else {
-                    callabilitySchedule.push_back(boost::shared_ptr<Callability>
-                                                  (new Callability(Callability::Price(price, 
-                                                                                      Callability::Price::Clean),
-                                                                   Callability::Call,d )));
-                }            
-            }
-        } catch (std::exception& ex){
-            forward_exception_to_r(ex); 
-        }
-
         Rcpp::List rparam(bondparams);
         //double faceAmount = Rcpp::as<double>(rparam["faceAmount");        
         RcppDate mDate = RcppDate(Rcpp::as<int>(rparam["maturityDate"]));
@@ -1040,36 +842,13 @@
                                         Rcpp::as<std::vector <double> >(rates), 
                                         dc, sch, redemption);
         bond.setPricingEngine(engine);
-
-
-        //cashflow
-        // int numCol = 2;
-        // std::vector<std::string> colNames(numCol);
-        // colNames[0] = "Date";
-        // colNames[1] = "Amount";
-        // RcppFrame frame(colNames);
-        Leg bondCashFlow = bond.cashflows();
-        RcppDateVector dates(bondCashFlow.size());
-        Rcpp::NumericVector amount(bondCashFlow.size());
         
-        for (unsigned int i = 0; i< bondCashFlow.size(); i++){
-            //std::vector<ColDatum> row(numCol);
-            Date d = bondCashFlow[i]->date();
-            //row[0].setDateValue(RcppDate(d.month(), d.dayOfMonth(), d.year()));
-            //row[1].setDoubleValue(bondCashFlow[i]->amount());
-            //frame.addRow(row);
-            dates(i) = RcppDate(d.month(), d.dayOfMonth(), d.year());
-            amount[i] = bondCashFlow[i]->amount();
-        }
-        Rcpp::DataFrame df = Rcpp::DataFrame::create(Rcpp::Named("Date") = dates,
-                                                     Rcpp::Named("Amount") = amount);
-        
         return Rcpp::List::create(Rcpp::Named("NPV") = bond.NPV(),
                                   Rcpp::Named("cleanPrice") = bond.cleanPrice(),
                                   Rcpp::Named("dirtyPrice") = bond.dirtyPrice(),
                                   Rcpp::Named("accruedCoupon") = bond.accruedAmount(),
                                   Rcpp::Named("yield") = bond.yield(Actual360(), Compounded, Annual),
-                                  Rcpp::Named("cashFlow") = df);
+                                  Rcpp::Named("cashFlow") = getCashFlowDataFrame(bond.cashflows()));
 
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -1093,66 +872,9 @@
 
     try {
 
-        DividendSchedule dividendSchedule;
-        CallabilitySchedule callabilitySchedule;
+        DividendSchedule dividendSchedule = getDividendSchedule(dividendScheduleFrame);
+        CallabilitySchedule callabilitySchedule = getCallabilitySchedule(callabilityScheduleFrame);
 
-        try {
-            //RcppFrame rcppDividendSchedule(dividendScheduleFrame);        
-            //std::vector<std::vector<ColDatum> > table = rcppDividendSchedule.getTableData();
-            //int nrow = table.size();
-            //int ncol = table[0].size();
-            Rcpp::DataFrame divScheDF(dividendScheduleFrame);
-            Rcpp::CharacterVector s0v = divScheDF[0];
-            Rcpp::NumericVector n1v = divScheDF[1];
-            Rcpp::NumericVector n2v = divScheDF[2];
-            Rcpp::NumericVector n3v = divScheDF[3];
-            int nrow = s0v.size();
-
-            for (int row=0; row<nrow; row++){          
-                int type = (s0v[row]=="Fixed") ? 1 : 0;
-                double amount = n1v[row];
-                double rate = n2v[row];
-                QuantLib::Date d(dateFromR(n3v[row]));            
-                if (type==1){
-                    dividendSchedule.push_back(boost::shared_ptr<Dividend>(new FixedDividend(amount, d)));
-                } else {
-                    dividendSchedule.push_back(boost::shared_ptr<Dividend>(new FractionalDividend(rate,amount,d)));
-                }
-            }
-        } catch (std::exception& ex) { 
-            forward_exception_to_r(ex); 
-        }
-
-        try {
-            // RcppFrame rcppCallabilitySchedule(callabilityScheduleFrame);
-            // std::vector<std::vector<ColDatum> > table = rcppCallabilitySchedule.getTableData();
-            // int nrow = table.size();
-            Rcpp::DataFrame callScheDF(callabilityScheduleFrame);
-            Rcpp::NumericVector n0v = callScheDF[0];
-            Rcpp::CharacterVector s1v = callScheDF[1];
-            Rcpp::NumericVector n2v = callScheDF[2];
-            int nrow = n0v.size();
-            for (int row=0; row<nrow; row++) {
-                double price = n0v[row]; //table[row][0].getDoubleValue();
-                int type = (s1v[row]=="P") ? 1 : 0;
-                QuantLib::Date d(dateFromR(n2v[row]));
-                if (type==1){
-                    callabilitySchedule.push_back(boost::shared_ptr<Callability>
-                                                  (new Callability(Callability::Price(price, 
-                                                                                      Callability::Price::Clean),
-                                                                   Callability::Put,d )));
-            } else {
-                    callabilitySchedule.push_back(boost::shared_ptr<Callability>
-                                                  (new Callability(Callability::Price(price, 
-                                                                                      Callability::Price::Clean),
-                                                                   Callability::Call,d )));
-                }            
-            }
-        } catch (std::exception& ex){
-            forward_exception_to_r(ex); 
-        }
-
-
         Rcpp::List rparam(bondparams);
         
         //double faceAmount = Rcpp::as<double>(rparam["faceAmount");        
@@ -1255,34 +977,12 @@
                                          dc, sch, redemption);
         bond.setPricingEngine(engine);
 
-        //cashflow
-        // int numCol = 2;
-        // std::vector<std::string> colNames(numCol);
-        // colNames[0] = "Date";
-        // colNames[1] = "Amount";
-        // RcppFrame frame(colNames);
-        Leg bondCashFlow = bond.cashflows();
-        RcppDateVector dates(bondCashFlow.size());
-        Rcpp::NumericVector amount(bondCashFlow.size());
-        
-        for (unsigned int i = 0; i< bondCashFlow.size(); i++){
-            //std::vector<ColDatum> row(numCol);
-            Date d = bondCashFlow[i]->date();
-            //row[0].setDateValue(RcppDate(d.month(), d.dayOfMonth(), d.year()));
-            //row[1].setDoubleValue(bondCashFlow[i]->amount());
-            //frame.addRow(row);
-            dates(i) = RcppDate(d.month(), d.dayOfMonth(), d.year());
-            amount[i] = bondCashFlow[i]->amount();
-        }
-        Rcpp::DataFrame df = Rcpp::DataFrame::create(Rcpp::Named("Date") = dates,
-                                                     Rcpp::Named("Amount") = amount);
-   
         return Rcpp::List::create(Rcpp::Named("NPV") = bond.NPV(),
                                   Rcpp::Named("cleanPrice") = bond.cleanPrice(),
                                   Rcpp::Named("dirtyPrice") = bond.dirtyPrice(),
                                   Rcpp::Named("accruedCoupon") = bond.accruedAmount(),
                                   Rcpp::Named("yield") = bond.yield(Actual360(), Compounded, Annual),
-                                  Rcpp::Named("cashFlow") = df);
+                                  Rcpp::Named("cashFlow") = getCashFlowDataFrame(bond.cashflows()));
         
     } catch(std::exception &ex) { 
         forward_exception_to_r(ex); 
@@ -1298,37 +998,9 @@
                                 SEXP callabilityScheduleFrame,
                                 SEXP dateparams) {
 
-    try{
+    try {
 
-        CallabilitySchedule callabilitySchedule;
-        try {
-            // RcppFrame rcppCallabilitySchedule(callabilityScheduleFrame);
-            // std::vector<std::vector<ColDatum> > table = rcppCallabilitySchedule.getTableData();
-            // int nrow = table.size();
-            Rcpp::DataFrame callScheDF(callabilityScheduleFrame);
-            Rcpp::NumericVector n0v = callScheDF[0];
-            Rcpp::CharacterVector s1v = callScheDF[1];
-            Rcpp::NumericVector n2v = callScheDF[2];
-            int nrow = n0v.size();
-            for (int row=0; row<nrow; row++) {
-                double price = n0v[row]; //table[row][0].getDoubleValue();
-                int type = (s1v[row]=="P") ? 1 : 0;
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rquantlib -r 261


More information about the Rquantlib-commits mailing list