From 6f4c38840d9fc2ebc7c909eeff418f97ba4a58a2 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Mon, 16 Jun 2014 17:51:49 -0400 Subject: fix the deltas computation when non constant tranche spread code is quite ugly, need to make it better --- R/tranche_functions.R | 59 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 14 deletions(-) (limited to 'R/tranche_functions.R') diff --git a/R/tranche_functions.R b/R/tranche_functions.R index b21094f..2743efd 100644 --- a/R/tranche_functions.R +++ b/R/tranche_functions.R @@ -658,7 +658,7 @@ BClossdistC <- function(defaultprob, issuerweights, recov, rho, Z, w, } BCtranche.pv <- function(index, K1, K2, rho1, rho2, - Z, w, N=length(index$issuerweights)+1){ + Z, w, N=length(index$issuerweights)+1, protection=FALSE){ ## computes the protection leg, couponleg, and bond price of a tranche ## in the base correlation setting if(K1==0){ @@ -675,8 +675,13 @@ BCtranche.pv <- function(index, K1, K2, rho1, rho2, cl1 <- tranche.cl(dist1$L, dist1$R, index$cs, 0, K1) pl2 <- tranche.pl(dist2$L, index$cs, 0, K2) pl1 <- tranche.pl(dist1$L, index$cs, 0, K1) - return(list(pl=(pl2-pl1)/dK, cl=(cl2-cl1)/dK, - bp=100*(1+(pl2-pl1+cl2-cl1)/dK))) + + if(protection){ + bp <- (pl1-pl2+cl1-cl2)/dK + }else{ + bp <- 100*(1-(pl2-pl1+cl2-cl1)/dK) + } + return(list(pl=(pl2-pl1)/dK, cl=(cl2-cl1)/dK, bp=bp)) } BCtranche.delta <- function(index, Z, w, N=length(index$portolio)+1, tradedate = Sys.Date()){ @@ -706,22 +711,48 @@ BCtranche.delta <- function(index, Z, w, N=length(index$portolio)+1, tradedate = indexpv(index, tradedate = tradedate, clean = FALSE)$bp dPVindex1 <- indexpv(indexplus, tradedate = tradedate, clean = FALSE)$bp- indexpv(index, tradedate = tradedate, clean = FALSE)$bp - bpplus <- 0 - bpminus <- 0 - bp <- 0 - bp2plus <- 0 + clplus <- 0 + clminus <- 0 + cl <- 0 + cl2plus <- 0 + plplus <- 0 + plminus <- 0 + pl <- 0 + pl2plus <- 0 + bp <- c() + bpplus <- c() + bpminus <- c() + bp2plus <- c() + index$cs$coupons <- index$cs$coupons/index$spreadref for(i in 1:(length(index$rho)-2)){ dist <- BClossdistC(index$defaultprob, index$issuerweights, index$recov, index$rho[i+1], Z, w, N) distplus <- BClossdistC(indexplus$defaultprob, index$issuerweights, index$recov, index$rho[i+1], Z, w, N) distminus <- BClossdistC(indexminus$defaultprob, index$issuerweights, index$recov, index$rho[i+1], Z, w, N) dist2plus <- BClossdistC(index2plus$defaultprob, index$issuerweights, index$recov, index$rho[i+1], Z, w, N) - bp <- c(bp, tranche.pv(dist$L, dist$R, index$cs, 0, index$K[i+1])) - bpplus <- c(bpplus, tranche.pv(distplus$L, distplus$R, index$cs, 0, index$K[i+1])) - bpminus <- c(bpminus, tranche.pv(distminus$L, distminus$R, index$cs, 0, index$K[i+1])) - bp2plus <- c(bp2plus, tranche.pv(dist2plus$L, dist2plus$R, index$cs, 0, index$K[i+1])) - } - dPVtranche <- 1/diff(index$K[-length(index$K)])*(diff(bpplus) - diff(bpminus)) - dPVtranche2 <- 1/diff(index$K[-length(index$K)])*(diff(bp2plus) - diff(bp)) + #shock all the coupon legs + cl <- c(cl, tranche.cl(dist$L, dist$R, index$cs, 0, index$K[i+1])) + clplus <- c(clplus, tranche.cl(distplus$L, distplus$R, index$cs, 0, index$K[i+1])) + clminus <- c(clminus, tranche.cl(distminus$L, distminus$R, index$cs, 0, index$K[i+1])) + cl2plus <- c(cl, tranche.cl(dist2plus$L, dist2plus$R, index$cs, 0, index$K[i+1])) + #shock all the protection legs + pl <- c(pl, tranche.pl(dist$L, index$cs, 0, index$K[i+1])) + plplus <- c(plplus, tranche.pl(distplus$L, index$cs, 0, index$K[i+1])) + plminus <- c(plminus, tranche.pl(distminus$L, index$cs, 0, index$K[i+1])) + pl2plus <- c(pl2plus, tranche.pl(dist2plus$L, index$cs, 0, index$K[i+1])) + } + for(i in 2:(length(cl))){ + dK <- index$K[i]-index$K[i-1] + bp <- c(bp, (pl[i]+cl[i]*index$tranche.running[i]-(pl[i-1]+cl[i-1]*index$tranche.running[i]))/dK) + bpplus <- c(bpplus, (plplus[i]+clplus[i]*index$tranche.running[i]- + (plplus[i-1]+clplus[i-1]*index$tranche.running[i]))/dK) + bpminus <- c(bpminus, (plminus[i]+clminus[i]*index$tranche.running[i]- + (plminus[i-1]+clminus[i-1]*index$tranche.running[i]))/dK) + bp2plus <- c(bp2plus, (pl2plus[i]+cl2plus[i]*index$tranche.running[i]- + (pl2plus[i-1]+cl2plus[i-1]*index$tranche.running[i]))/dK) + + } + dPVtranche <- bpplus-bpminus + dPVtranche2 <- bp2plus - bp deltas <- dPVtranche/dPVindex ##might need to check that in case of non 1 factor -- cgit v1.2.3-70-g09d2