aboutsummaryrefslogtreecommitdiffstats
path: root/tranche_functions.R
diff options
context:
space:
mode:
Diffstat (limited to 'tranche_functions.R')
-rw-r--r--tranche_functions.R33
1 files changed, 26 insertions, 7 deletions
diff --git a/tranche_functions.R b/tranche_functions.R
index b553332f..89ad5e77 100644
--- a/tranche_functions.R
+++ b/tranche_functions.R
@@ -212,7 +212,7 @@ lossdistribC <- function(p, w, S, N){
lossdistribC.truncated <- function(p, w, S, N, T=N){
## C version of lossdistrib2, roughly 50 times faster
dyn.load("lossdistrib.dll")
- .C("lossdistrib_fast", as.double(p), as.integer(length(p)),
+ .C("lossdistrib_truncated", as.double(p), as.integer(length(p)),
as.double(w), as.double(S), as.integer(N), as.integer(T), q = double(T))$q
}
@@ -267,6 +267,25 @@ lossrecovdist.term <- function(defaultprob, prepayprob, w, S, N, useC=TRUE){
return(list(L=L, R=R))
}
+lossrecovdist.joint.term <- function(defaultprob, prepayprob, w, S, N, useC=TRUE){
+ ## computes the joint loss and recovery distribution over time
+ Q <- array(0, dim=c(ncol(defaultprob), N, N))
+ if(useC){
+ if(all(!prepayprob)){
+ for(t in 1:ncol(defaultprob)){
+ Q[t,,] <- lossdistribC.joint(defaultprob[,t], w, S[,t], N)
+ }
+ }
+ }else{
+ if(all(!prepayprob)){
+ for(t in 1:ncol(defaultprob)){
+ Q[t,,] <- lossdistrib.joint(defaultprob[,t], w, S[,t], N)
+ }
+ }
+ }
+ return(Q)
+}
+
shockprob <- function(p, rho, Z, log.p=F){
## computes the shocked default probability as a function of the copula factor
pnorm((qnorm(p)-sqrt(rho)*Z)/sqrt(1-rho), log.p=log.p)
@@ -391,7 +410,7 @@ tranche.pl.scenarios <- function(l, cs, K1, K2, scaled=FALSE){
}
tranche.pv <- function(L, R, cs, K1, K2, Ngrid=nrow(L)){
- return( tranche.pl(L, cs, K1, K2, Ngrid, TRUE) + tranche.cl(L, R, cs, K1, K2, Ngrid, TRUE))
+ return( tranche.pl(L, cs, K1, K2, Ngrid) + tranche.cl(L, R, cs, K1, K2, Ngrid))
}
tranche.pv.scenarios <- function(l, r, cs, K1, K2){
@@ -490,15 +509,15 @@ BCtranche.delta <- function(portfolio, index, coupon, K1, K2, rho1, rho2, N=leng
portfolioplus[[i]]@curve@hazardrates <- portfolioplus[[i]]@curve@hazardrates * (1 + eps)
portfoliominus[[i]]@curve@hazardrates <- portfoliominus[[i]]@curve@hazardrates * (1- eps)
}
- dPVindex <- indexpv(portfolioplus, index) - indexpv(portfoliominus, index)
+ dPVindex <- indexpv(portfolioplus, index)$bp - indexpv(portfoliominus, index)$bp
if(K2==1){
- dPVtranche <- BCtranche.pv(portfolioplus, index, coupon, 0, K1, 0, rho1, lu)$bp -
- BCtranche.pv(portfoliominus, index, coupon, 0, K1, 0, rho1, lu)$bp
+ 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)
delta <- (1 - dPVtranche/(100*dPVindex) * K1adj)/(1-K1adj)
}else{
- dPVtranche <- BCtranche.pv(portfolioplus, index, coupon, K1, K2, rho1, rho2, lu)$bp -
- BCtranche.pv(portfoliominus, index, coupon, K1, K2, rho1, rho2, lu)$bp
+ dPVtranche <- BCtranche.pv(portfolioplus, index, coupon, K1, K2, rho1, rho2, N)$bp -
+ BCtranche.pv(portfoliominus, index, coupon, K1, K2, rho1, rho2, N)$bp
delta <- dPVtranche/(100*dPVindex)
}
## dPVindex <- BCtranche.pv(portfolioplus, index, coupon, 0, 1, 0, 0.5, lu)$bp-