diff options
Diffstat (limited to 'R/cds_functions_generic.R')
| -rw-r--r-- | R/cds_functions_generic.R | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/R/cds_functions_generic.R b/R/cds_functions_generic.R index 671d1c0f..32f6559b 100644 --- a/R/cds_functions_generic.R +++ b/R/cds_functions_generic.R @@ -733,7 +733,7 @@ survivalProbability1 <- function(startdate, date, survival.curve) { }
}
}
- ## if date is greater than last survival.curve date, keep the hazard rate flate
+ ## if date is greater than last survival.curve date, keep the hazard rate flat
logprob <- - yearFrac(startdate, date) * survival.curve$hazardrates[i]
return( exp(as.numeric(logprob)) )
}
@@ -749,17 +749,21 @@ survivalProbability.exact <- function(credit.curve, date) { if ( date > curve@dates[i] ) {
logprob <- logprob - curve@hazardrates[i] * dT[i]
}else{
- if( i > 1 ){
- logprob <- logprob - curve@hazardrates[i] * (Tmat - T[i])
- }else{
- logprob <- logprob - curve@hazardrates[1] * Tmat
- }
+ logprob <- logprob - curve@hazardrates[i] * (Tmat - T[i])
break
}
}
return( exp(as.numeric(logprob)) )
}
+FEP <- function(index, exerciseDate) {
+ ## computes the front end protection for a credit index at a given date
+ DP <- 1-unlist(lapply(index$portfolio, survivalProbability.exact, date=exerciseDate))
+ R <- unlist(lapply(index$portfolio, function(x)x@recovery))
+ fep <- crossprod(index$issuerweights, (1-R)*DP)
+ return( fep )
+}
+
SP <- function(sc, startdate=Sys.Date()){
## computes the survival probability associated with the survival curve
T <- c(0, yearFrac(startdate, sc@dates))
@@ -829,3 +833,29 @@ creditcurve.maturity <- function(creditcurve){ stop("not of class creditcurve")
}
}
+
+forward.cl <- function(index, exerciseDate){
+ aux <- function(creditCurve){
+ cs <- cds.cs$cs[cds.cs$cs$unadj.dates<=index$maturity,]
+ couponleg(cs, creditCurve@curve, startdate=exerciseDate)
+ }
+ return( crossprod(index$issuerweights, unlist(lapply(index$portfolio, aux))) )
+}
+
+forward.prot <- function(index, exerciseDate){
+ cs <- cds.cs$cs[cds.cs$cs$unadj.dates<=index$maturity,]
+ prot <- numeric(length(index$portfolio))
+ for(i in seq_along(index$portfolio)){
+ creditCurve <- index$portfolio[[i]]
+ prot[i] <- defaultleg(cs, creditCurve@curve, creditCurve@recovery, startdate=exerciseDate)
+ }
+ return( crossprod(prot, index$issuerweights) )
+}
+
+defaultAdjustedForwardIndexPrice <- function(index, exerciseDate){
+ tes <- addBusDay(exerciseDate)
+ df <- DiscountCurve(L3m$params, L3m$tsQuotes, yearFrac(tradedate, tes))$discounts
+ price <- 1 - FEP(index, exerciseDate) + forward.cl(index, exerciseDate)*0.05 -
+ forward.prot(index, exerciseDate)
+ return( as.numeric(df*price) )
+}
|
