aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/cds_functions_generic.R37
1 files changed, 28 insertions, 9 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R
index 1527c367..581f7d3c 100644
--- a/R/cds_functions_generic.R
+++ b/R/cds_functions_generic.R
@@ -192,6 +192,25 @@ setMethod("cdsduration", signature("abstractcurve", "Date"),
return( couponleg(cs, sc, startdate, accruedondefault=accruedondefault) - acc)
})
+## define cdsduration generic
+setGeneric("cdsspread", function(sc, recovery, maturity, ...) {
+ standardGeneric("cdsspread")
+})
+
+## duration is based on the standard IMM schedule
+setMethod("cdsspread", signature("abstractcurve", "numeric", "Date"),
+ ## computes the risky PV01, also called risky annuity of a cds
+ function(sc, recovery, maturity, tradedate=Sys.Date(), accruedondefault=TRUE){
+ stopifnot(is.logical(accruedondefault))
+ cs <- couponSchedule(IMMDate(tradedate, noadj=TRUE), maturity,"Q", "FIXED", 1,
+ 0, tradedate, IMMDate(tradedate, "prev"))
+ startdate <- tradedate+1
+ acc <- cdsAccrued(tradedate, 1)
+
+ return( defaultleg(cs, sc, recovery, startdate)/
+ couponleg(cs, sc, startdate, accruedondefault=accruedondefault) - acc)
+ })
+
## define defaultleg generic
setGeneric("defaultleg", function(cs, sc, recovery, ...) {
standardGeneric("defaultleg")
@@ -353,13 +372,13 @@ setMethod("contingentleg", signature("data.frame", "shapedcurve", "numeric"),
})
## define dcontingentleg generic
-setGeneric("dcontingentleg", function(cs, sc, recovery, index, ...) {
+setGeneric("dcontingentleg", function(cs, sc, recovery, startdate, index, ...) {
standardGeneric("dcontingentleg")
})
-setMethod("dcontingentleg", signature("data.frame", "defaultcurve", "numeric", "numeric"),
+setMethod("dcontingentleg", signature("data.frame", "defaultcurve", "numeric", "Date", "numeric"),
## derivative of contingentleg with respect to hazardrate
- function(cs, sc, recovery, index, startdate=Sys.Date()){
+ function(cs, sc, recovery, startdate=Sys.Date(), index){
T <- yearFrac(startdate, cs$dates)
dT <- diff(c(0,T))
Q <- cumprod(exp(-sc@hazardrates[1:length(dT)] * dT)) * cs$df
@@ -371,7 +390,7 @@ setMethod("dcontingentleg", signature("data.frame", "defaultcurve", "numeric", "
return( as.numeric(dr) )
})
-setMethod("dcontingentleg", signature("data.frame", "defaultcurve", "numeric", "missing"),
+setMethod("dcontingentleg", signature("data.frame", "defaultcurve", "numeric", "Date", "missing"),
## derivative of contingentleg with respect to hazardrate
function(cs, sc, recovery, startdate=Sys.Date()){
## derivative of contingentleg with respect to hazardrate
@@ -385,7 +404,7 @@ setMethod("dcontingentleg", signature("data.frame", "defaultcurve", "numeric", "
return( as.numeric(dr) )
})
-setMethod("dcontingentleg", signature("data.frame", "shapedcurve", "numeric", "missing"),
+setMethod("dcontingentleg", signature("data.frame", "shapedcurve", "numeric", "Date", "missing"),
## Computes the pv of the contingent leg of a cds based on a given
## coupon schedule, hazard rates curve, prepay curve, and recovery.
function(cs, sc, recovery, startdate=Sys.Date()){
@@ -455,7 +474,7 @@ dbondpv <- function(cs, sc, recovery, startdate, index){
if(missing(index)){
return( dcontingentleg(cs, sc, recovery, startdate) + dcouponleg(cs, sc, startdate))
}else{
- return( dcontingentleg(cs, sc, recovery, startdate, index)+dcouponleg(cs, sc, startdate, index) )
+ return( dcontingentleg(cs, sc, recovery, startdate, index) + dcouponleg(cs, sc, startdate, index) )
}
}
@@ -637,7 +656,7 @@ indextheta <- function(index, tradedate=Sys.Date()){
}
-portfoliospread <- function(portfolio, maturity){
+portfoliospread <- function(portfolio, maturity, tradedate=Sys.Date()){
## computes the spread of a portfolio defined by notionals and survivalcurves
## for a given maturity.
## if maturity is missing, we use the intrinsic maturity for each curve
@@ -652,8 +671,8 @@ portfoliospread <- function(portfolio, maturity){
when(length(it$creditcurve@curve@hazardrates)!=0 && it$maturity > it$creditcurve@startdate
&& it$creditcurve@curve@hazardrates[1]<=10)
} %dopar% {
- d <- cdsduration(it$creditcurve@curve, it$maturity)
- S <- cdsspread(it$creditcurve@curve, it$maturity, it$creditcurve@recovery)
+ d <- cdsduration(it$creditcurve@curve, it$maturity, tradedate)
+ S <- cdsspread(it$creditcurve@curve, it$creditcurve@recovery, it$maturity, tradedate)
c(d * S * it$notional, d * it$notional)
}
if(is.null(dim(data))){