From e6228dacf2fef4b77d994ec99a5886956cf6ab7b Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Thu, 8 May 2014 17:12:58 -0400 Subject: simplify function call --- R/tranche_functions.R | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'R/tranche_functions.R') diff --git a/R/tranche_functions.R b/R/tranche_functions.R index a5b99d8..7ef68e1 100644 --- a/R/tranche_functions.R +++ b/R/tranche_functions.R @@ -657,7 +657,7 @@ BClossdistC <- function(defaultprob, issuerweights, recov, rho, Z, w, return(list(L=r$L,R=r$R)) } -BCtranche.pv <- function(defaultprob, issuerweights, recov, cs, K1, K2, rho1, rho2, +BCtranche.pv <- function(index, 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 @@ -667,14 +667,14 @@ BCtranche.pv <- function(defaultprob, issuerweights, recov, cs, K1, K2, rho1, rh } } dK <- K2 - K1 - dist2 <- BClossdistC(defaultprob, issuerweights, recov, rho2, Z, w, N) + dist2 <- BClossdistC(index$defaultprob, index$issuerweights, index$recov, rho2, Z, w, N) if(rho1!=0){ - dist1 <- BClossdistC(defaultprob, issuerweights, recov, rho1, Z, w, N) + dist1 <- BClossdistC(index$defaultprob, index$issuerweights, index$recov, rho1, Z, w, N) } - 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) + cl2 <- tranche.cl(dist2$L, dist2$R, index$cs, 0, K2) + 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))) } @@ -718,26 +718,28 @@ BCtranche.delta <- function(portfolio, index, coupon, K1, K2, rho1, rho2, Z, w, return( delta ) } -EL <- function(defaultprob, issuerweights, recov, cs){ +EL <- function(index){ ## computes the expected loss of a portfolio (time discounted) ## given the default curves and recovery ## should be very close to the protection leg of the portfolio of cds - ELvec <- crossprod( issuerweights * (1-recov), defaultprob) - return( crossprod(cs$df, diff(c(0, as.numeric(ELvec)))) ) + ## index should be a list with issuerweights, recov, defaultprob and cs parameters + ELvec <- as.numeric(crossprod(index$issuerweights * (1-index$recov), index$defaultprob)) + return( sum(index$cs$df*diff(c(0, ELvec))) ) } -ELtrunc <- function(defaultprob, issuerweights, recov, cs, K, rho, Z, w, Ngrid){ +ELtrunc <- function(index, K, rho, Z, w, Ngrid){ ## computes the expected loss of a portfolio below strike K ## could be written faster by using a truncated version of lossdistrib - rho <- rep(rho, length(issuerweights)) - L <- matrix(0, Ngrid, dim(defaultprob)[2]) - L <- .C("BCloss_dist", defaultprob, dim(defaultprob)[1], dim(defaultprob)[2], - as.double(issuerweights), as.double(recov), as.double(Z), as.double(w), + ## index should be a list with issuerweights, recov, defaultprob and cs parameters + rho <- rep(rho, length(index$issuerweights)) + L <- matrix(0, Ngrid, dim(index$defaultprob)[2]) + L <- .C("BCloss_dist", index$defaultprob, dim(index$defaultprob)[1], dim(index$defaultprob)[2], + as.double(index$issuerweights), as.double(index$recov), as.double(Z), as.double(w), as.integer(length(Z)), as.double(rho), as.integer(Ngrid), FALSE, L=L)$L support <- seq(0, 1, length=Ngrid) trancheloss <- pmin(support, K) ELvec <- crossprod(trancheloss, L) - return( crossprod(cs$df, diff(c(0, as.numeric(ELvec)))) ) + return( sum(index$cs$df * diff(c(0, as.numeric(ELvec)))) ) } BCstrikes <- function(defaultprob, issuerweights, recov, cs, K, rho, Z, w, N=101) { @@ -751,18 +753,14 @@ BCstrikes <- function(defaultprob, issuerweights, recov, cs, K, rho, Z, w, N=101 return( ELvec/el ) } -skewmapping <- function(defaultprob1, issuerweights1, recov1, cs1, rhofun, - defaultprob2, issuerweights2, recov2, cs2, K2, Z, w, N=101) { - - EL1 <- EL(defaultprob1, issuerweights1, recov1, cs1) - EL2 <- EL(defaultprob2, issuerweights2, recov2, cs2) +skewmapping <- function(index1, rhofun, index2, K2, Z, w, N=101) { + EL1 <- EL(index1) + EL2 <- EL(index2) f <- function(x, ...){ - return(abs(ELtrunc(defaultprob1, issuerweights1, recov1, cs1, - 0, x, 0, rhofun(x), Z, w, N)/EL1- - ELtrunc(defaultprob2, issuerweights2, recov2, cs2, - 0, K2, 0, rhofun(x), Z, w, N)/EL2)) + return(abs(ELtrunc(index1, x, rhofun(x), Z, w, N)/EL1- + ELtrunc(index2, K2, rhofun(x), Z, w, N)/EL2)) } - return( optimize(f, interval=c(0,1)) ) + return( optimize(f, interval=c(0,1), index1, rhofun, index2, K2, Z, w, N) ) } -- cgit v1.2.3-70-g09d2