diff options
| -rw-r--r-- | tranche_functions.R | 87 |
1 files changed, 83 insertions, 4 deletions
diff --git a/tranche_functions.R b/tranche_functions.R index 59c5f721..c11b68d0 100644 --- a/tranche_functions.R +++ b/tranche_functions.R @@ -118,7 +118,6 @@ recovdist <- function(dp, pp, w, S, N){ dp2 <- dp[i] - dp1
pp1 <- pp[i] * (d2u - d2)
pp2 <- pp[i] - pp1
- cat(dp1, dp2, pp1, pp2, "\n")
q1 <- c(rep(0, d1l), dp1 * q[1:(N-d1l)])
q2 <- c(rep(0, d1u), dp2 * q[1:(N-d1u)])
q3 <- c(rep(0, d2l), pp1 * q[1:(N-d2l)])
@@ -258,8 +257,13 @@ lossrecovdist <- function(defaultprob, prepayprob, w, S, N, useC=TRUE){ R <- lossdistrib2(defaultprob, w, 1-S, N)
}
}else{
- L <- lossdistribC(defaultprob, w, S, N)
- R <- recovdistC(defaultprob, prepayprob, w, S, N)
+ if(useC){
+ L <- lossdistribC(defaultprob, w, S, N)
+ R <- recovdistC(defaultprob, prepayprob, w, S, N)
+ }else{
+ L <- lossdistrib2(defaultprob, w, S, N)
+ R <- recovdist(defaultprob, prepayprob, w, S, N)
+ }
}
return(list(L=L, R=R))
}
@@ -563,8 +567,83 @@ MFupdate.prob <- function(Z, w, rho, defaultprob){ p <- matrix(0, nrow(defaultprob), ncol(defaultprob))
for(i in 1:nrow(defaultprob)){
for(j in 1:ncol(defaultprob)){
- p[i,j] <- fit.prob(Z, program$weight, rho, defaultprob[i,j])
+ p[i,j] <- fit.prob(Z, w, rho, defaultprob[i,j])
}
}
return( p )
}
+
+MFlossdistrib <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
+ Ngrid=2*length(issuerweights)+1){
+ ## computes the loss and recovery distribution using the modified factor distribution
+ n.credit <- length(issuerweights)
+ Rstoch <- array(0, dim=c(n.int, n.credit, ncol(defaultprob)))
+ for(t in 1:ncol(defaultprob)){
+ for(i in 1:n.credit){
+ Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w, rho, defaultprob[i,t], p[i,t])
+ }
+ }
+ parf <- function(i){
+ pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
+ S <- 1 - Rstoch[i,,]
+ dist <- lossrecovdist.term(pshocked, 0, issuerweights, S, Ngrid)
+ }
+ L <- matrix(0, Ngrid, ncol(defaultprob))
+ R <- matrix(0, Ngrid, ncol(defaultprob))
+ for(i in 1:length(w)){
+ dist <- parf(i)
+ L <- L + dist$L * w[i]
+ R <- R + dist$R * w[i]
+ }
+ return( list(L=L, R=R) )
+}
+
+MFlossdistrib2 <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
+ Ngrid=2*length(issuerweights)+1){
+ ## rowSums is the loss distribution
+ ## colSums is the recovery distribution
+ ## so that recovery is the y axis and L is the x axis
+ ## if we use the persp function, losses is the axes facing us,
+ ## and R is the axis going away from us.
+ n.credit <- length(issuerweights)
+ Rstoch <- array(0, dim=c(n.int, n.credit, ncol(defaultprob)))
+ for(t in 1:ncol(defaultprob)){
+ for(i in 1:n.credit){
+ Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w, rho, defaultprob[i,t], p[i,t])
+ }
+ }
+ parf <- function(i){
+ pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
+ S <- 1 - Rstoch[i,,]
+ dist <- lossrecovdist.joint.term(pshocked, 0, issuerweights, S, Ngrid)
+ return(dist)
+ }
+ Q <- array(0, dim=c(ncol(defaultprob), Ngrid, Ngrid))
+ temp <- parSapply(cl, 1:length(w), parf)
+ for(i in 1:length(w)){
+ Q <- Q + w[i]*array(temp[,i], dim=c(ncol(defaultprob), Ngrid, Ngrid))
+ }
+ return( Q )
+}
+
+MFtranche.pv <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
+ Kmodified, Ngrid=length(issuerweights)+1){
+ ## computes the tranches pv using the modified factor distribution
+ ## p is the modified probability so that
+ n.credit <- length(issuerweights)
+ Rstoch <- array(0, dim=c(n.int, n.credit, ncol(defaultprob)))
+ for(t in 1:ncol(defaultprob)){
+ for(i in 1:n.credit){
+ Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w, rho, defaultprob[i,t], p[i,t])
+ }
+ }
+ parf <- function(i){
+ pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
+ S <- 1 - Rstoch[i,,]
+ dist <- lossrecovdist.term(pshocked, 0, issuerweights, S, Ngrid)
+ return( tranche.pvvec(Kmodified, dist$L, dist$R, cs))
+ }
+ clusterExport(cl, list("Rstoch", "p"))
+ result <- parSapply(cl, 1:length(w), parf)
+ return( list(pv=100*(1+result%*%w), pv.w=result))
+}
|
