diff options
| -rw-r--r-- | R/tranche_functions.R | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/R/tranche_functions.R b/R/tranche_functions.R index ea3c2e32..cecf24b8 100644 --- a/R/tranche_functions.R +++ b/R/tranche_functions.R @@ -706,14 +706,15 @@ BClossdistC <- function(defaultprob, issuerweights, recov, rho, Z, w, }
L <- matrix(0, N, dim(defaultprob)[2])
R <- matrix(0, N, dim(defaultprob)[2])
+ rho <- rep(rho, length(issuerweights))
r <- .C("BClossdist", defaultprob, dim(defaultprob)[1], dim(defaultprob)[2],
as.double(issuerweights), as.double(recov), as.double(Z), as.double(w),
as.integer(length(Z)), as.double(rho), as.integer(N), as.logical(defaultflag), L=L, R=R)
return(list(L=r$L,R=r$R))
}
-BCtranche.pv <- function(portfolio, index, coupon, K1, K2, rho1, rho2, N=length(portfolio)+1,
- tradedate=Sys.Date()){
+BCtranche.pv <- function(defaultprob, issuerweights, recov, cs, K1, K2, rho1, rho2,
+ Z, w, N=length(issuerweights)+1){
## computes the protection leg, couponleg, and bond price of a tranche
## in the base correlation setting
if(K1==0){
@@ -721,25 +722,21 @@ BCtranche.pv <- function(portfolio, index, coupon, K1, K2, rho1, rho2, N=length( stop("equity tranche must have 0 lower correlation")
}
}
- cs <- couponSchedule(IMMDate(tradedate), index$maturity, "Q", "FIXED", coupon, 0, tradedate)
- SurvProb <- SPmatrix(portfolio, length(cs$dates))
- recov <- sapply(portfolio, attr, "recovery")
- issuerweights <- rep(1/length(portfolio), length(portfolio))
- K <- adjust.attachments(c(K1,K2), index$loss, index$factor)
- dK <- K[2] - K[1]
- dist2 <- BClossdistC(1-SurvProb, issuerweights, recov, rho2, N)
+ dK <- K2 - K1
+ dist2 <- BClossdistC(defaultprob, issuerweights, recov, rho2, Z, w, N)
if(rho1!=0){
- dist1 <- BClossdistC(1-SurvProb, issuerweights, recov, rho1, N)
+ dist1 <- BClossdistC(defaultprob, issuerweights, recov, rho1, Z, w, N)
}
- cl2 <- tranche.cl(dist2$L, dist2$R, cs, 0, K[2])
- cl1 <- tranche.cl(dist1$L, dist1$R, cs, 0, K[1])
- pl2 <- tranche.pl(dist2$L, cs, 0, K[2])
- pl1 <- tranche.pl(dist1$L, cs, 0, K[1])
+ cl2 <- tranche.cl(dist2$L, dist2$R, cs, 0, K2)
+ cl1 <- tranche.cl(dist1$L, dist1$R, cs, 0, K1)
+ pl2 <- tranche.pl(dist2$L, cs, 0, K2)
+ pl1 <- tranche.pl(dist1$L, cs, 0, K1)
return(list(pl=(pl2-pl1)/dK, cl=(cl2-cl1)/dK,
bp=100*(1+(pl2-pl1+cl2-cl1)/dK)))
}
-BCtranche.delta <- function(portfolio, index, coupon, K1, K2, rho1, rho2, N=length(portolio)+1){
+BCtranche.delta <- function(portfolio, index, coupon, K1, K2, rho1, rho2, 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
@@ -747,19 +744,29 @@ BCtranche.delta <- function(portfolio, index, coupon, K1, K2, rho1, rho2, N=leng eps <- 1e-4
portfolioplus <- portfolio
portfoliominus <- portfolio
+ cs <- couponSchedule(IMMDate(tradedate), index$maturity,"Q", "FIXED", coupon, 0, tradedate,
+ IMMDate(tradedate, "prev"))
for(i in 1:length(portfolio)){
portfolioplus[[i]]@curve@hazardrates <- portfolioplus[[i]]@curve@hazardrates * (1 + eps)
portfoliominus[[i]]@curve@hazardrates <- portfoliominus[[i]]@curve@hazardrates * (1- eps)
}
- dPVindex <- indexpv(portfolioplus, index)$bp - indexpv(portfoliominus, index)$bp
+ dPVindex <- indexpv(portfolioplus, index, tradedate = tradedate, clean = FALSE)$bp -
+ indexpv(portfoliominus, index, tradedate = tradedate, clean = FALSE)$bp
+ defaultprobplus <- 1 - SPmatrix(portfolioplus, length(cs$dates))
+ defaultprobminus <- 1 - SPmatrix(portfoliominus, length(cs$dates))
if(K2==1){
- dPVtranche <- BCtranche.pv(portfolioplus, index, coupon, 0, K1, 0, rho1, N)$bp -
- BCtranche.pv(portfoliominus, index, coupon, 0, K1, 0, rho1, N)$bp
K1adj <- adjust.attachments(K1, index$loss, index$factor)
+ dPVtranche <- BCtranche.pv(defaultprobplus, issuerweights, recov, cs, 0, K1adj, 0, rho1,
+ Z, w, N)$bp -
+ BCtranche.pv(defaultprobminus, issuerweights, recov, cs, 0, K1adj, 0, rho1,
+ Z, w, N)$bp
delta <- (1 - dPVtranche/(100*dPVindex) * K1adj)/(1-K1adj)
}else{
- dPVtranche <- BCtranche.pv(portfolioplus, index, coupon, K1, K2, rho1, rho2, N)$bp -
- BCtranche.pv(portfoliominus, index, coupon, K1, K2, rho1, rho2, N)$bp
+ Kmod <- adjust.attachments(c(K1, K2), index$loss, index$factor)
+ dPVtranche <- BCtranche.pv(defaultprobplus, issuerweights, recov, cs, Kmod[1], Kmod[2], rho1, rho2,
+ Z, w, N)$bp -
+ BCtranche.pv(defaultprobminus, issuerweights, recov, cs, 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-
|
