aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tranche_functions.R48
1 files changed, 44 insertions, 4 deletions
diff --git a/tranche_functions.R b/tranche_functions.R
index b1d11b13..b553332f 100644
--- a/tranche_functions.R
+++ b/tranche_functions.R
@@ -339,7 +339,25 @@ tranche.cl <- function(L, R, cs, K1, K2, Ngrid=nrow(L), scaled=FALSE){
}
}
-tranche.pl <- function(L, R, cs, K1, K2, Ngrid=nrow(L), scaled=FALSE){
+tranche.cl.scenarios <- function(l, r, cs, K1, K2, scaled=FALSE){
+ ## computes the couponleg of a tranche for one scenario
+ ## if scaled is TRUE, scale it by the size of the tranche (K2-K1)
+ ## can make use of the fact that the loss and recov distribution are
+ ## truncated (in that case nrow(L) != Ngrid
+ if(K1==K2){
+ return( 0 )
+ }else{
+ size <- K2 - K1 - trancheloss(l, K1, K2) - trancherecov(r, K1, K2)
+ sizeadj <- as.numeric(0.5 * (size + c(K2-K1, size[-length(size)])))
+ if(scaled){
+ return( 1/(K2-K1) * crossprod(sizeadj * cs$coupons, cs$df) )
+ }else{
+ return( crossprod(sizeadj * cs$coupons, cs$df) )
+ }
+ }
+}
+
+tranche.pl <- function(L, cs, K1, K2, Ngrid=nrow(L), scaled=FALSE){
## computes the protection leg of a tranche
## if scaled
if(K1==K2){
@@ -356,10 +374,32 @@ tranche.pl <- function(L, R, cs, K1, K2, Ngrid=nrow(L), scaled=FALSE){
}
}
+tranche.pl.scenarios <- function(l, cs, K1, K2, scaled=FALSE){
+ ## computes the protection leg of a tranche
+ ## if scaled
+ if(K1==K2){
+ return(0)
+ }else{
+ cf <- K2 - K1 - trancheloss(l, K1, K2)
+ cf <- c(K2 - K1, cf)
+ if(scaled){
+ return( 1/(K2-K1) * as.numeric(crossprod(diff(cf), cs$df)))
+ }else{
+ return( as.numeric(crossprod(diff(cf), cs$df)))
+ }
+ }
+}
+
tranche.pv <- function(L, R, cs, K1, K2, Ngrid=nrow(L)){
- return( tranche.pl(L, R, cs, K1, K2, Ngrid) + tranche.cl(L, R, cs, K1, K2, Ngrid))
+ return( tranche.pl(L, cs, K1, K2, Ngrid, TRUE) + tranche.cl(L, R, cs, K1, K2, Ngrid, TRUE))
}
+tranche.pv.scenarios <- function(l, r, cs, K1, K2){
+ return( tranche.pl.scenarios(l, cs, K1, K2, TRUE) +
+ tranche.cl.scenarios(l, r, cs, K1, K2, TRUE))
+}
+
+
adjust.attachments <- function(K, losstodate, factor){
## computes the attachments adjusted for losses
## on current notional
@@ -432,8 +472,8 @@ BCtranche.pv <- function(portfolio, index, coupon, K1, K2, rho1, rho2, N=length(
}
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, dist2$R, cs, 0, K[2])
- pl1 <- tranche.pl(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])
return(list(pl=(pl2-pl1)/dK, cl=(cl2-cl1)/dK,
bp=100*(1+(pl2-pl1+cl2-cl1)/dK)))
}