aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tranche_functions.R39
1 files changed, 39 insertions, 0 deletions
diff --git a/tranche_functions.R b/tranche_functions.R
index 27ab8ec7..1a437f43 100644
--- a/tranche_functions.R
+++ b/tranche_functions.R
@@ -352,3 +352,42 @@ BClossdistC <- function(SurvProb, issuerweights, recov, rho, lu, n.int=100){
r <- .C("BClossdist", SurvProb, as.integer(dim(SurvProb)[1]), as.integer(dim(SurvProb)[2]), as.double(issuerweights), as.double(recov), as.double(Z), as.double(w), as.integer(n.int), as.double(rho), as.double(lu), L=L, R=R)
return(list(L=r$L,R=r$R))
}
+
+BCtranche.pv <- function(portfolio, index, coupon, K1, K2, rho1, rho2, lu=0.01){
+ ## computes the protection leg, couponleg, and bond price of a tranche
+ ## in the base correlation setting
+ if(K1==0){
+ if(rho1!=0){
+ stop("equity tranche must have 0 lower correlation")
+ }
+ }
+ SurvProb <- SPmatrix(portfolio, index)
+ cs <- couponSchedule(nextIMMDate(Sys.Date()), index$maturity,"Q", "FIXED", coupon, 0)
+ recov <- sapply(portfolio, attr, "recovery")
+ issuerweights <- rep(1/length(portfolio), length(portfolio))
+ K <- adjust.attachments(c(K1,K2), index$loss, index$factor)
+ dist2 <- BClossdistC(SurvProb, issuerweights, recov, rho2, lu)
+ if(rho1!=0){
+ dist1 <- BClossdistC(SurvProb, issuerweights, recov, rho1, lu)
+ }
+ cl2 <- tranche.cl(dist2$L, dist2$R, cs, 0, K[2]) * K[2]
+ cl1 <- tranche.cl(dist1$L, dist1$R, cs, 0, K[1]) * K[1]
+ pl2 <- tranche.pl(dist2$L, dist2$R, cs, 0, K[2]) * K[2]
+ pl1 <- tranche.pl(dist1$L, dist1$R, cs, 0, K[1]) * K[1]
+ return(list(pl=(pl2-pl1)/(K[2]-K[1]), cl=(cl2-cl1)/(K[2]-K[1]),
+ bp=100*(1+(pl2-pl1+cl2-cl1)/(K[2]-K[1]))))
+}
+
+BCtranche.delta <- function(portfolio, index, coupon, K1, K2, rho1, rho2, lu=0.01){
+ eps <- 1e-4
+ portfolioplus <- portfolio
+ portfoliominus <- portfolio
+ for(i in 1:length(portfolio)){
+ portfolioplus[[i]]@curve@hazardrates <- portfolioplus[[i]]@curve@hazardrates + eps
+ portfoliominus[[i]]@curve@hazardrates <- portfoliominus[[i]]@curve@hazardrates - eps
+ }
+ dPVtranche <- BCtranche.pv(portfolioplus, index, coupon, K1, K2, rho1, rho2, lu)$bp -
+ BCtranche.pv(portfoliominus, index, coupon, K1, K2, rho1, rho2, lu)$bp
+ dPVindex <- indexpv(portfolioplus, index)-indexpv(portfoliominus, index)
+ return( dPVtranche/dPVindex)
+}