diff options
| -rw-r--r-- | calibrate_tranches.R | 108 | ||||
| -rw-r--r-- | tranche_functions.R | 4 |
2 files changed, 30 insertions, 82 deletions
diff --git a/calibrate_tranches.R b/calibrate_tranches.R index 07a07b2f..d1d390c9 100644 --- a/calibrate_tranches.R +++ b/calibrate_tranches.R @@ -152,82 +152,6 @@ for(l in 1:100){ cat(err,"\n")
}
-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))
-}
-
-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 )
-}
-
-
## computes MFdeltas
newportf <- hy19portfolio.tweaked
eps <- 1e-4
@@ -282,8 +206,8 @@ percentiles <- (seq(0, 1, length=n.scenarios+1)[-1]+ l <- matrix(0, ncol(defaultprob), n.scenarios)
r <- matrix(0, ncol(defaultprob), n.scenarios)
-MFdist <- MFlossdistrib(cl, cs, w.mod, rho, defaultprob, p, issuerweights, Ngrid)
-MFdist.orig <- MFlossdistrib(cl, cs, w, rho, defaultprob, defaultprob, issuerweights, Ngrid)
+MFdist <- MFlossdistrib(w.mod, rho, defaultprob, p, issuerweights, Ngrid)
+MFdist.orig <- MFlossdistrib(w, rho, defaultprob, defaultprob, issuerweights, Ngrid)
lossdist.orig <- BClossdistC(SurvProb, issuerweights, recov, rho, 101, 500)
for(i in 1:17){
Lfun <- splinefun(c(0, cumsum(MFdist$L[,i])),c(0, seq(0, 1, length=Ngrid)), "monoH.FC")
@@ -296,7 +220,7 @@ for(i in 1:17){ ## joint generation of scenarios for loss and recovery distribution
clusterExport(cl, list("lossrecovdist.joint.term", "lossdistribC.joint"))
-MFdist2 <- MFlossdistrib2(cl, cs, w.mod, rho, defaultprob, p, issuerweights, Ngrid)
+MFdist2 <- MFlossdistrib2(cl, w.mod, rho, defaultprob, p, issuerweights, Ngrid)
## memory never gets released by the clusters for some reasons, needs to call gc twice
clusterCall(cl, gc)
clusterCall(cl, gc)
@@ -304,7 +228,7 @@ gc() #L+R<=1
test <- apply(MFdist2[21,,], 2, rev)
test.tri <- lower.tri(test)
-sum(test[test.tri]) #close to 1
+sum(test[test.tri])#close to 1
## D=L+R
dist <- MFdist2[21,,]
@@ -322,3 +246,27 @@ for(i in 1:Ngrid){ distchv[i,j] <- dist[xgrid[i,j]+1, ygrid[i,j]+1]*seq(0,1, length=Ngrid)[j]
}
}
+
+## compute reinvesting distribution
+beta <- 1.1
+f <- function(l, r)(1-l-r)/(1-r)^beta
+test <- outer(seq(0, 1, length=Ngrid), seq(0, 1, length=Ngrid), f)
+plot(density(x=test[-Ngrid*Ngrid], weights=dist[-Ngrid*Ngrid], from=0, to=5))
+
+## two dimensional scenarios
+n.scenarios <- 100
+percentiles <- (seq(0, 1, length=n.scenarios+1)[-1]+
+ seq(0, 1, length=n.scenarios+1)[-(n.scenarios+1)])/2
+l <- matrix(0, ncol(defaultprob), n.scenarios)
+r <- matrix(0, ncol(defaultprob), n.scenarios)
+
+MFdist <- MFlossdistrib(w.mod, rho, defaultprob, p, issuerweights, Ngrid)
+i <- 21
+Lfun <- splinefun(c(0, cumsum(MFdist$L[,i])),c(0, seq(0, 1, length=Ngrid)), "monoH.FC")
+
+Rfun <- splinefun(c(0, cumsum(MFdist$R[,i])),c(0, seq(0, 1, length=Ngrid)), "monoH.FC")
+ for(j in 1:99){
+ l[i, j] <- Lfun(d1[j])
+ r[i, j] <- Rfun(d1[j])
+ }
+}
diff --git a/tranche_functions.R b/tranche_functions.R index c11b68d0..2fb11238 100644 --- a/tranche_functions.R +++ b/tranche_functions.R @@ -573,7 +573,7 @@ MFupdate.prob <- function(Z, w, rho, defaultprob){ return( p )
}
-MFlossdistrib <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
+MFlossdistrib <- function(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)
@@ -598,7 +598,7 @@ MFlossdistrib <- function(cl, cs, w, rho, defaultprob, p, issuerweights, return( list(L=L, R=R) )
}
-MFlossdistrib2 <- function(cl, cs, w, rho, defaultprob, p, issuerweights,
+MFlossdistrib2 <- function(cl, w, rho, defaultprob, p, issuerweights,
Ngrid=2*length(issuerweights)+1){
## rowSums is the loss distribution
## colSums is the recovery distribution
|
