diff options
Diffstat (limited to 'tranche_functions.R')
| -rw-r--r-- | tranche_functions.R | 93 |
1 files changed, 78 insertions, 15 deletions
diff --git a/tranche_functions.R b/tranche_functions.R index 2fb11238..45662584 100644 --- a/tranche_functions.R +++ b/tranche_functions.R @@ -272,12 +272,10 @@ lossrecovdist.term <- function(defaultprob, prepayprob, w, S, N, useC=TRUE){ ## computes the loss and recovery distribution over time
L <- array(0, dim=c(N, ncol(defaultprob)))
R <- array(0, dim=c(N, ncol(defaultprob)))
- if(all(!prepayprob)){
- for(t in 1:ncol(defaultprob)){
- temp <- lossrecovdist(defaultprob[,t], 0, w, S[,t], N, useC)
- L[,t] <- temp$L
- R[,t] <- temp$R
- }
+ for(t in 1:ncol(defaultprob)){
+ temp <- lossrecovdist(defaultprob[,t], prepayprob[,t], w, S[,t], N, useC)
+ L[,t] <- temp$L
+ R[,t] <- temp$R
}
return(list(L=L, R=R))
}
@@ -290,12 +288,20 @@ lossrecovdist.joint.term <- function(defaultprob, prepayprob, w, S, N, useC=TRUE for(t in 1:ncol(defaultprob)){
Q[t,,] <- lossdistribC.joint(defaultprob[,t], w, S[,t], N)
}
+ }else{
+ for(t in 1:ncol(defaultprob)){
+ Q[t,,] <- lossdistribprepayC.joint(defaultprob[,t], prepayprob[,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)
}
+ }else{
+ for(t in 1:ncol(defaultprob)){
+ Q[t,,] <- lossdistribprepay.joint(defaultprob[,t], prepayprob[,t], w, S[,t], N)
+ }
}
}
gc()
@@ -573,18 +579,18 @@ MFupdate.prob <- function(Z, w, rho, defaultprob){ return( p )
}
-MFlossdistrib <- function(w, rho, defaultprob, p, issuerweights,
+MFlossdistrib <- function(w, Z, rho, defaultprob, defaultprobmod, issuerweights, recov,
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])
+ Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w, rho, defaultprob[i,t], defaultprobmod[i,t])
}
}
parf <- function(i){
- pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
+ pshocked <- apply(defaultprobmod, 2, shockprob, rho=rho, Z=Z[i])
S <- 1 - Rstoch[i,,]
dist <- lossrecovdist.term(pshocked, 0, issuerweights, S, Ngrid)
}
@@ -598,7 +604,33 @@ MFlossdistrib <- function(w, rho, defaultprob, p, issuerweights, return( list(L=L, R=R) )
}
-MFlossdistrib2 <- function(cl, w, rho, defaultprob, p, issuerweights,
+MFlossrecovdist <- function(w, Z, rho, defaultprob, defaultprobmod, prepayprob, prepayprobmod,
+ issuerweights, recov, 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], defaultprobmod[i,t])
+ }
+ }
+ parf <- function(i){
+ dpshocked <- apply(defaultprobmod, 2, shockprob, rho=rho, Z=Z[i])
+ ppshocked <- apply(prepayprobmod, 2, shockprob, rho=rho, Z=-Z[i])
+ S <- 1 - Rstoch[i,,]
+ dist <- lossrecovdist.term(dpshocked, ppshocked, 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, w, Z, rho, defaultprob, defaultprobmod, issuerweights, recov,
Ngrid=2*length(issuerweights)+1){
## rowSums is the loss distribution
## colSums is the recovery distribution
@@ -609,11 +641,11 @@ MFlossdistrib2 <- function(cl, w, rho, defaultprob, p, 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])
+ Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w, rho, defaultprob[i,t], defaultprobmod[i,t])
}
}
parf <- function(i){
- pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
+ pshocked <- apply(defaultprobmod, 2, shockprob, rho=rho, Z=Z[i])
S <- 1 - Rstoch[i,,]
dist <- lossrecovdist.joint.term(pshocked, 0, issuerweights, S, Ngrid)
return(dist)
@@ -626,7 +658,38 @@ MFlossdistrib2 <- function(cl, w, rho, defaultprob, p, issuerweights, return( Q )
}
-MFtranche.pv <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
+MFlossrecovdist2 <- function(cl, w, Z, rho, defaultprob, defaultprobmod,
+ prepayprob, prepayprobmod, issuerweights, recov,
+ 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], defaultprobmod[i,t])
+ }
+ }
+ parf <- function(i){
+ dpshocked <- apply(defaultprobmod, 2, shockprob, rho=rho, Z=Z[i])
+ ppshocked <- apply(prepayprobmod, 2, shockprob, rho=rho, Z=-Z[i])
+ S <- 1 - Rstoch[i,,]
+ dist <- lossrecovdist.joint.term(dpshocked, ppshocked, issuerweights, S, Ngrid)
+ return(dist)
+ }
+ Q <- array(0, dim=c(ncol(defaultprob), Ngrid, Ngrid))
+ ## temp <- parSapply(cl, 1:length(w), parf)
+ temp <- sapply(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, defaultprobmod, issuerweights, recov,
Kmodified, Ngrid=length(issuerweights)+1){
## computes the tranches pv using the modified factor distribution
## p is the modified probability so that
@@ -634,11 +697,11 @@ MFtranche.pv <- function(cl, cs, w, rho, defaultprob, p, 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])
+ Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w, rho, defaultprob[i,t], defaultprobmod[i,t])
}
}
parf <- function(i){
- pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
+ pshocked <- apply(defaultprobmod, 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))
|
