diff options
Diffstat (limited to 'cds_functions_generic.R')
| -rw-r--r-- | cds_functions_generic.R | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/cds_functions_generic.R b/cds_functions_generic.R index d3f980a5..45482212 100644 --- a/cds_functions_generic.R +++ b/cds_functions_generic.R @@ -556,11 +556,11 @@ survivalProbability.exact <- function(credit.curve, date) { return( exp(as.numeric(logprob)) )
}
-PD <- function(sc){
- ## computes the default probability associated with the survival curve
+SP <- function(sc){
+ ## computes the survival probability associated with the survival curve
T <- c(0, yearFrac(today(), sc@dates))
dT <- diff(T)
- return( 1-cumprod(exp(-sc@hazardrates * dT)) )
+ return( cumprod(exp(-sc@hazardrates * dT)) )
}
@@ -568,7 +568,37 @@ SPmatrix <- function(portfolio, index){ cs <- couponSchedule(nextIMMDate(today()), index$maturity, "Q", "FIXED", index$coupon)
SP <- matrix(0, length(portfolio), length(cs$dates))
for(i in 1:length(portfolio)){
- SP[i,] <- 1 - PD(portfolio[[i]]@curve)[1:length(cs$dates)]
+ SP[i,] <- PD(portfolio[[i]]@curve)[1:length(cs$dates)]
}
return( SP )
}
+
+DP2 <- function(sc){
+ ## computes the default probability and prepay probability associated
+ ## with the survival curve
+ T <- c(0, yearFrac(today(), sc@dates))
+ dT <- diff(T)
+ Q <- cumprod(exp(- (sc@hazardrates[1:length(dT)]+sc@prepayrates[1:length(dT)]) * dT))
+ Qmid <- 1/2 * (c(1, Q[-length(Q)]) + Q)
+ list(dates=T[-1],
+ defaultprob= cumsum(sc@hazardrates[1:length(dT)] * Qmid * dT),
+ prepayprob = cumsum(sc@prepayrates[1:length(dT)] * Qmid * dT))
+}
+
+SPmatrix2 <- function(portfolio, dealdata){
+ ## computes the default probability and prepay matrix of a portfolio
+ ## at the dates specified from dealdata
+ dates <- seq(dealdata$"Deal Next Pay Date", dealdata$maturity, by="3 months")
+ T <- yearFrac(today(), dates)
+ DP <- matrix(0, length(portfolio), length(dates))
+ PP <- matrix(0, length(portfolio), length(dates))
+ for(i in 1:length(portfolio)){
+ temp <- DP2(portfolio[[i]]@curve)
+ ## linear interpolation of the default and prepay prob
+ ## need to figure out a better way to do this
+ DP[i,] <- approxfun(temp$dates, temp$defaultprob, rule=2)(T)
+ PP[i,] <- approxfun(temp$dates, temp$prepayprob, rule=2)(T)
+ }
+ return(list(DP, PP))
+}
+
|
