[Rquantlib-commits] r175 - in pkg: R man src tests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Feb 13 05:31:17 CET 2010


Author: knguyen
Date: 2010-02-13 05:31:17 +0100 (Sat, 13 Feb 2010)
New Revision: 175

Modified:
   pkg/R/bond.R
   pkg/man/FixedRateBond.Rd
   pkg/src/bonds.cpp
   pkg/tests/RQuantlib.R
   pkg/tests/RQuantlib.Rout.save
Log:
code cleaning. Remove depreciated FixedRateBond codes

Modified: pkg/R/bond.R
===================================================================
--- pkg/R/bond.R	2010-02-13 04:12:52 UTC (rev 174)
+++ pkg/R/bond.R	2010-02-13 04:31:17 UTC (rev 175)
@@ -152,26 +152,12 @@
     if (is.null(dateparams$fixingDays)){dateparams$fixingDays=2}
     
     dateparams <- matchParams(dateparams)
-    if (class(discountCurve)=="DiscountCurve"){
-         val <- .Call("QL_FixedRateWithRebuiltCurve",
-                   bond, rates, c(discountCurve$table$date), 
-                  discountCurve$table$zeroRates, dateparams,
-                  PACKAGE="RQuantLib")         
-    }
-    else {
-         if (length(discountCurve)==2){
-             val <- .Call("QL_FixedRateBond1", 
-                     bond, rates, discountCurve, dateparams,
-                     PACKAGE="RQuantLib")
-         }
-         if (length(discountCurve)==3){
-             val <- .Call("QL_FixedRateBond2", 
-                    bond, rates, discountCurve[[1]], 
-                    discountCurve[[2]], discountCurve[[3]],
-                    dateparams,
-                    PACKAGE="RQuantLib")      
-         }
-    }
+    
+    val <- .Call("QL_FixedRateWithRebuiltCurve",
+                 bond, rates, c(discountCurve$table$date), 
+                 discountCurve$table$zeroRates, dateparams,
+                 PACKAGE="RQuantLib")  
+
     val$cashFlow <- as.data.frame(val$cashFlow)
     class(val) <- c("FixedRateBond", "Bond")    
     val

Modified: pkg/man/FixedRateBond.Rd
===================================================================
--- pkg/man/FixedRateBond.Rd	2010-02-13 04:12:52 UTC (rev 174)
+++ pkg/man/FixedRateBond.Rd	2010-02-13 04:31:17 UTC (rev 175)
@@ -103,24 +103,29 @@
 
 
 \examples{
-
-bond <- list(faceAmount=100, issueDate=as.Date("2004-11-30"),
-             maturityDate=as.Date("2008-11-30"), redemption=100, 
+#Simple call with a flat curve
+bond <- list(faceAmount=100,
+             issueDate=as.Date("2004-11-30"),
+             maturityDate=as.Date("2008-11-30"),
+             redemption=100, 
              effectiveDate=as.Date("2004-11-30"))
-dateparams <- list(settlementDays=1, calendar="us", dayCounter = 1, period=3, 
+dateparams <- list(settlementDays=1,
+                   calendar="us", dayCounter = 'Thirty360', period=2, 
                    businessDayConvention = 4, terminationDateConvention=4,
                    dateGeneration=1, endOfMonth=1)
-curve <- list(todayDate=as.Date("2004-11-04"), riskFreeRate=0.03)
-rates <- c(0.02875)
+coupon.rate <- c(0.02875)
                        
-FixedRateBond(bond, rates, curve, dateparams)
-
 params <- list(tradeDate=as.Date('2002-2-15'),
                settleDate=as.Date('2002-2-19'),
                dt=.25,
                interpWhat="discount",
                interpHow="loglinear")
 
+discountCurve.flat <- DiscountCurve(params, list(flat=0.05))
+FixedRateBond(bond, coupon.rate, discountCurve.flat, dateparams)
+
+
+#Same bond with a discount curve constructed from market quotes
 tsQuotes <- list(d1w  =0.0382,
                  d1m  =0.0372,
                  fut1=96.2875,
@@ -136,27 +141,20 @@
                  s10y =0.05165,
                  s15y =0.055175)
 
-times <- seq(0,10,.1)
-curve <- list(params, tsQuotes, times)
-FixedRateBond(bond, rates, curve, dateparams)
+discountCurve <- DiscountCurve(params, tsQuotes)
+FixedRateBond(bond, coupon.rate, discountCurve, dateparams)
 
-curve <- DiscountCurve(params, tsQuotes, times)
-dateparams <- list(settlementDays=1, calendar="us", dayCounter = "Thirty360", 
-                   period="Annual", businessDayConvention = "Preceding", 
-                   terminationDateConvention="Preceding",
-                   dateGeneration="Forward", endOfMonth=1)
-FixedRateBond(bond, rates, curve, dateparams)
 
-
 #example with default dateparams
-FixedRateBond(bond, rates, curve)
+FixedRateBond(bond, coupon.rate, discountCurve)
 
 ##exampe with defaul bond parameter and dateparams
 bond <- list(issueDate=as.Date("2004-11-30"),
-maturityDate=as.Date("2008-11-30"))
-dateparams <- list(calendar="us", dayCounter = "ActualActual", 
+             maturityDate=as.Date("2008-11-30"))
+dateparams <- list(calendar="us",
+                   dayCounter = "ActualActual", 
                    period="Annual")
-FixedRateBond(bond, rates, curve, dateparams)
+FixedRateBond(bond, coupon.rate, discountCurve, dateparams)
 
 }
 \keyword{misc}

Modified: pkg/src/bonds.cpp
===================================================================
--- pkg/src/bonds.cpp	2010-02-13 04:12:52 UTC (rev 174)
+++ pkg/src/bonds.cpp	2010-02-13 04:31:17 UTC (rev 175)
@@ -315,52 +315,9 @@
     return rl;
 }
 
-RcppExport SEXP QL_FixedRateBond1(SEXP bondparam, SEXP ratesVec,
-                                  SEXP discountCurve, SEXP dateparams){
-    SEXP rl = R_NilValue;
-    char *exceptionMesg = NULL;
-    try{
-        RcppParams curve(discountCurve);
-        Rate riskFreeRate = curve.getDoubleValue("riskFreeRate");
-        RcppDate today_Date = curve.getDateValue("todayDate");       
-        QuantLib::Date today(dateFromR(today_Date));
-        
-        boost::shared_ptr<SimpleQuote> rRate(new SimpleQuote(riskFreeRate));
-        Settings::instance().evaluationDate() = today;
-        Handle<YieldTermStructure> discountCurve(flatRate(today,rRate,Actual360()));
 
-        rl = FixedBond(bondparam, ratesVec, discountCurve, dateparams);
-    } catch(std::exception& ex) {
-        exceptionMesg = copyMessageToR(ex.what());
-    } catch(...) {
-        exceptionMesg = copyMessageToR("unknown reason");
-    }    
-    if(exceptionMesg != NULL)
-        Rf_error(exceptionMesg);
-    
-    return rl;
-}
 
-RcppExport SEXP QL_FixedRateBond2(SEXP bondparam, SEXP ratesVec, 
-                                  SEXP params, SEXP tsQuotes, 
-                                  SEXP times, SEXP dateparams){
-    SEXP rl = R_NilValue;
-    char *exceptionMesg = NULL;
-    try{
-        
-        Handle<YieldTermStructure> discountCurve(
-                                                 buildTermStructure(params, tsQuotes, times));
-        
-        rl = FixedBond(bondparam, ratesVec, discountCurve, dateparams);
-    } catch(std::exception& ex) {
-        exceptionMesg = copyMessageToR(ex.what());
-    } catch(...) {
-        exceptionMesg = copyMessageToR("unknown reason");
-    }    
-    if(exceptionMesg != NULL)
-        Rf_error(exceptionMesg);   
-    return rl;
-}
+
     
 
 RcppExport  SEXP QL_FixedRateBondYield(SEXP optionParameters, SEXP ratesVec) {

Modified: pkg/tests/RQuantlib.R
===================================================================
--- pkg/tests/RQuantlib.R	2010-02-13 04:12:52 UTC (rev 174)
+++ pkg/tests/RQuantlib.R	2010-02-13 04:31:17 UTC (rev 175)
@@ -92,17 +92,27 @@
 FixedRateBondPriceByYield(,0.0307, 100000, as.Date("2004-11-30"), as.Date("2008-11-30"), 3, , c(0.02875), , , , ,as.Date("2004-11-30"))
 
 ## bond.cpp
-bond <- list(faceAmount=100, issueDate=as.Date("2004-11-30"),
-             maturityDate=as.Date("2008-11-30"), redemption=100, 
+
+#Simple call with a flat curve
+bond <- list(faceAmount=100,
+             issueDate=as.Date("2004-11-30"),
+             maturityDate=as.Date("2008-11-30"),
+             redemption=100, 
              effectiveDate=as.Date("2004-11-30"))
-dateparams <- list(settlementDays=1, calendar="us", dayCounter = 1, period=3, 
+dateparams <- list(settlementDays=1,
+                   calendar="us", dayCounter = 'Thirty360', period=2, 
                    businessDayConvention = 4, terminationDateConvention=4,
                    dateGeneration=1, endOfMonth=1)
-curve <- list(todayDate=as.Date("2004-11-04"), riskFreeRate=0.03)
-rates <- c(0.02875)
+coupon.rate <- c(0.02875)
                        
-print(FixedRateBond(bond, rates, curve, dateparams))
+params <- list(tradeDate=as.Date('2002-2-15'),
+               settleDate=as.Date('2002-2-19'),
+               dt=.25,
+               interpWhat="discount",
+               interpHow="loglinear")
 
+discountCurve.flat <- DiscountCurve(params, list(flat=0.05))
+FixedRateBond(bond, coupon.rate, discountCurve.flat, dateparams)
 ## bond.cpp FloatingRateBond, following test-suite/bonds.cpp
 bond <- list(faceAmount=100, issueDate=as.Date("2004-11-30"),
              maturityDate=as.Date("2008-11-30"), redemption=100, 

Modified: pkg/tests/RQuantlib.Rout.save
===================================================================
--- pkg/tests/RQuantlib.Rout.save	2010-02-13 04:12:52 UTC (rev 174)
+++ pkg/tests/RQuantlib.Rout.save	2010-02-13 04:31:17 UTC (rev 175)
@@ -182,38 +182,44 @@
 [1] "FixedRateBondPriceByYield"
 > 
 > ## bond.cpp
-> bond <- list(faceAmount=100, issueDate=as.Date("2004-11-30"),
-+              maturityDate=as.Date("2008-11-30"), redemption=100, 
+> 
+> #Simple call with a flat curve
+> bond <- list(faceAmount=100,
++              issueDate=as.Date("2004-11-30"),
++              maturityDate=as.Date("2008-11-30"),
++              redemption=100, 
 +              effectiveDate=as.Date("2004-11-30"))
-> dateparams <- list(settlementDays=1, calendar="us", dayCounter = 1, period=3, 
+> dateparams <- list(settlementDays=1,
++                    calendar="us", dayCounter = 'Thirty360', period=2, 
 +                    businessDayConvention = 4, terminationDateConvention=4,
 +                    dateGeneration=1, endOfMonth=1)
-> curve <- list(todayDate=as.Date("2004-11-04"), riskFreeRate=0.03)
-> rates <- c(0.02875)
+> coupon.rate <- c(0.02875)
 >                        
-> print(FixedRateBond(bond, rates, curve, dateparams))
+> params <- list(tradeDate=as.Date('2002-2-15'),
++                settleDate=as.Date('2002-2-19'),
++                dt=.25,
++                interpWhat="discount",
++                interpHow="loglinear")
+> 
+> discountCurve.flat <- DiscountCurve(params, list(flat=0.05))
+> FixedRateBond(bond, coupon.rate, discountCurve.flat, dateparams)
 Concise summary of valuation for FixedRateBond 
- Net present value :  99.10206 
-       clean price :  99.317 
-       dirty price :  99.317 
+ Net present value :  80.2129 
+       clean price :  92.167 
+       dirty price :  92.167 
     accrued coupon :  0 
-             yield :  0.030455 
+             yield :  0.050517 
         cash flows : 
-       Date    Amount
- 2005-03-31   0.95308
- 2005-07-29   0.94521
- 2005-11-30   0.97671
- 2006-03-31   0.95308
- 2006-07-31   0.96096
- 2006-11-30   0.96096
- 2007-03-30   0.94521
- 2007-07-31   0.96884
- 2007-11-30   0.96096
- 2008-03-31   0.96096
- 2008-07-31   0.96096
- 2008-11-30   0.96096
- 2008-11-30 100.00000
-> 
+       Date   Amount
+ 2005-05-31   1.4375
+ 2005-11-30   1.4375
+ 2006-05-31   1.4375
+ 2006-11-30   1.4375
+ 2007-05-31   1.4375
+ 2007-11-30   1.4375
+ 2008-05-30   1.4375
+ 2008-11-30   1.4375
+ 2008-11-30 100.0000
 > ## bond.cpp FloatingRateBond, following test-suite/bonds.cpp
 > bond <- list(faceAmount=100, issueDate=as.Date("2004-11-30"),
 +              maturityDate=as.Date("2008-11-30"), redemption=100, 



More information about the Rquantlib-commits mailing list