diff options
Diffstat (limited to 'cds_functions_generic.R')
| -rw-r--r-- | cds_functions_generic.R | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/cds_functions_generic.R b/cds_functions_generic.R index 57886b77..e858ffe8 100644 --- a/cds_functions_generic.R +++ b/cds_functions_generic.R @@ -1,5 +1,10 @@ source("cds_utils.R")
+## TODO:
+## - switch hazard rates and prepay curves to functions
+## - allow to compute forward quantities (works for couponleg and defauleg now,
+## if provide startdate
+
setClass("abstractcurve")
## flat hazard rate curve
setClass("flatcurve", contains="abstractcurve", representation(h="numeric"))
@@ -43,8 +48,11 @@ setMethod("couponleg", signature("data.frame", "defaultcurve"), ## computes the pv of the risky coupon leg based on a given coupon schedule
## and a survival curve. Also called premium leg or fixed leg.
function(cs, sc, accruedondefault=TRUE){
- dT <- diff(c(0, yearFrac(today(), cs$dates)))
- PD <- cumprod(exp(-sc@hazardrates[1:length(dT)] * dT))
+ x1T <- yearFrac(today(), sc@dates)
+ x2T <- yearFrac(today(), cs@dates)
+ dT <- diff(c(0, x2T))
+ hfun <- approxfun(x1T, sc@hazardrates, method="constant", rule=2)
+ PD <- cumprod(exp(- hfun(x2T) * dT))
if(accruedondefault){
PDadj <- 0.5 * (c(1, PD[-length(PD)]) + PD)
}else{
@@ -59,9 +67,13 @@ setMethod("couponleg", signature("data.frame", "defaultprepaycurve"), ## a hazard rate curve, and a prepay curve. We assume the poisson processes driving
## default and prepayment are independent, so the intensity of either event
## happenning is the sum of the two.
- function(cs, sc, accruedondefault=TRUE){
- dT <- diff(c(0, yearFrac(today(), cs$dates)))
- SP <- cumprod(exp( - (sc@hazardrates[1:length(dT)] + sc@prepayrates[1:length(dT)] ) * dT))
+ function(cs, sc, accruedondefault=TRUE, startdate=today()){
+ x1T <- yearFrac(today(), sc@dates)
+ x2T <- yearFrac(startdate, cs$dates)
+ dT <- diff(c(0, x2T))
+ hfun <- approxfun(x1T, sc@hazardrates, method="constant", rule=2)
+ pfun <- approxfun(x1T, sc@prepayrates, method="constant", rule=2)
+ SP <- cumprod(exp( - (hfun(x2T) + pfun(x2T)) * dT))
if(accruedondefault){
SPadj <- 0.5 * (c(1, SP[-length(SP)]) + SP)
}else{
@@ -82,8 +94,8 @@ setMethod("couponleg", signature("data.frame", "shapedcurve"), ## a hazard rate curve, and a prepay curve. We assume the poisson processes driving
## default and prepayment are independent, so the intensity of either event
## happenning is the sum of the two.
- function(cs, sc, accruedondefault=TRUE){
- return( couponleg(cs, shapedtodpc(cs, sc), accruedondefault) )
+ function(cs, sc, accruedondefault=TRUE, startdate){
+ return( couponleg(cs, shapedtodpc(cs, sc, startdate), accruedondefault, startdate) )
})
## define dcouponleg generic
@@ -172,7 +184,7 @@ setMethod("cdsduration", signature("abstractcurve", "Date"), })
## define defaultleg generic
-setGeneric("defaultleg", function(cs, sc, recovery) {
+setGeneric("defaultleg", function(cs, sc, recovery, ...) {
standardGeneric("defaultleg")
})
@@ -204,12 +216,15 @@ setMethod("defaultleg", signature("data.frame", "defaultcurve", "numeric"), setMethod("defaultleg", signature("data.frame", "defaultprepaycurve", "numeric"),
## Computes the pv of the default leg of a cds based on a given
## coupon schedule, hazard rates curve, prepay curves, and recovery.
- function(cs, sc, recovery){
- T <- yearFrac(today(), cs$dates)
- dT <- diff(c(0, T))
- Q <- cumprod(exp(- (sc@hazardrates[1:length(dT)]+sc@prepayrates[1:length(dT)]) * dT)) * cs$df
+ function(cs, sc, recovery, startdate=today()){
+ x2T <- yearFrac(startdate, cs$dates)
+ x1T <- yearFrac(today(), sc@dates)
+ dT <- diff(c(0, x2T))
+ hfun <- approxfun(x1T, sc@hazardrates, method = "constant", rule=2)
+ pfun <- approxfun(x1T, sc@prepayrates, method = "constant", rule=2)
+ Q <- cumprod(exp(- (hfun(x2T)+pfun(x2T)) * dT)) * cs$df
Qmid <- 1/2 * (c(1, Q[-length(Q)]) + Q)
- r <- (1 - recovery) * crossprod(sc@hazardrates[1:length(dT)] * Qmid, dT)
+ r <- (1 - recovery) * crossprod(hfun(x2T) * Qmid, dT)
return( as.numeric(r) )
})
@@ -250,7 +265,6 @@ setMethod("ddefaultleg", signature("data.frame", "defaultcurve", "numeric", "num return( as.numeric(dr) )
})
-
setMethod("ddefaultleg", signature("data.frame", "shapedcurve", "numeric", "missing"),
function(cs, sc, recovery) {
T <- yearFrac(today(), cs$dates)
|
