From 6f01cad69f962f7f2d74c56896a36ea50c2b7fda Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Mon, 2 Jun 2014 18:30:00 -0400 Subject: compute deltas for all attach/detach in one go --- R/tranche_functions.R | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'R') diff --git a/R/tranche_functions.R b/R/tranche_functions.R index 8d7ab04..8b8b263 100644 --- a/R/tranche_functions.R +++ b/R/tranche_functions.R @@ -679,8 +679,7 @@ BCtranche.pv <- function(index, K1, K2, rho1, rho2, bp=100*(1+(pl2-pl1+cl2-cl1)/dK))) } -BCtranche.delta <- function(index, K1, K2, rho1, rho2, Z, w, - N=length(portolio)+1, tradedate = Sys.Date()){ +BCtranche.delta <- function(index, Z, w, N=length(portolio)+1, tradedate = Sys.Date()){ ## computes the tranche delta (on current notional) by doing a proportional ## blip of all the curves ## if K2==1, then computes the delta using the lower attachment only @@ -697,20 +696,20 @@ BCtranche.delta <- function(index, K1, K2, rho1, rho2, Z, w, indexminus$defaultprob <- 1 - SPmatrix(Portf.minus, length(index$cs$dates)) dPVindex <- indexpv(indexplus, tradedate = tradedate, clean = FALSE)$bp - indexpv(indexminus, tradedate = tradedate, clean = FALSE)$bp - if(K2==1){ - K1adj <- adjust.attachments(K1, index$loss, index$factor) - dPVtranche <- BCtranche.pv(indexplus, 0, K1adj, 0, rho1, Z, w, N)$bp - - BCtranche.pv(indexminus, 0, K1adj, 0, rho1, Z, w, N)$bp - delta <- (1 - dPVtranche/(100*dPVindex) * K1adj)/(1-K1adj) - }else{ - Kmod <- adjust.attachments(c(K1, K2), index$loss, index$factor) - dPVtranche <- BCtranche.pv(indexplus, Kmod[1], Kmod[2], rho1, rho2, Z, w, N)$bp - - BCtranche.pv(indexminus, Kmod[1], Kmod[2], rho1, rho2, Z, w, N)$bp - delta <- dPVtranche/(100*dPVindex) - } - ## dPVindex <- BCtranche.pv(portfolioplus, index, coupon, 0, 1, 0, 0.5, lu)$bp- - ## BCtranche.pv(portfoliominus, index, coupon, 0, 1, 0, 0.5, lu)$bp - return( delta ) + deltas <- rep(0, length(index$K)-1) + bpplus <- c(0) + bpminus <- c(0) + for(i in 1:(length(index$rho)-2)){ + 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) + 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])) + } + dPVtranche <- 1/diff(index$K[-length(index$K)])*(diff(bpplus) - diff(bpminus)) + deltas <- dPVtranche/dPVindex + deltas <- c(deltas, + (1-(bpplus[length(bpplus)]-bpminus[length(bpminus)])/dPVindex)/(1-index$K[length(index$K)-1])) + return( deltas ) } EL <- function(index){ -- cgit v1.2.3-70-g09d2