aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--build_SC.R8
-rw-r--r--calibrate_tranches.R32
-rw-r--r--plot_distributions.R8
-rw-r--r--tranche_functions.R1
4 files changed, 36 insertions, 13 deletions
diff --git a/build_SC.R b/build_SC.R
index 8565edec..43cf2907 100644
--- a/build_SC.R
+++ b/build_SC.R
@@ -313,11 +313,11 @@ buildSC.portfolio <- function(dealname, global.params, startdate=today()) {
stonln1.portfolio <- buildSC.portfolio("stonln1", global.params)
A <- SPmatrix2(stonln1.portfolio$SC, getdealdata("stonln1"))
S <- 1 - sapply(stonln1.portfolio$SC, attr, "recov")
-w <- stonln1.portfolio$notional/sum(stonln1.portfolio$notional) * S
-w <- w * length(w)
+w <- stonln1.portfolio$notional/sum(stonln1.portfolio$notional)
lu <- 0.01
-test <- lossrecovdist(A$DP[,39], A$PP[,39], w, lu, useC=FALSE)
+test <- lossrecovdist(A$DP[,39], A$PP[,39], w, S, lu, useC=FALSE)
+test2 <- lossdistribprepayC.joint(A$DP[,39], A$PP[,39], w, S, lu)
+
-pomme2 <- lossdistrib3(A$DP[,39], w/length(w), lu)
defaultprob <- A$DP[,39]
prepayprob <- A$PP[,39]
diff --git a/calibrate_tranches.R b/calibrate_tranches.R
index 6f243a6a..07a07b2f 100644
--- a/calibrate_tranches.R
+++ b/calibrate_tranches.R
@@ -281,10 +281,9 @@ 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)
-Ngrid <- 501
-MFdist <- MFlossdistrib(cl, cs, w.mod, rho, defaultprob, p, issuerweights, 501)
-MFdist.orig <- MFlossdistrib(cl, cs, w, rho, defaultprob, defaultprob, issuerweights, 501)
+MFdist <- MFlossdistrib(cl, cs, w.mod, rho, defaultprob, p, issuerweights, Ngrid)
+MFdist.orig <- MFlossdistrib(cl, cs, 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")
@@ -297,6 +296,29 @@ 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, 501)
+MFdist2 <- MFlossdistrib2(cl, cs, 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)
+gc()
+#L+R<=1
+test <- apply(MFdist2[21,,], 2, rev)
+test.tri <- lower.tri(test)
+sum(test[test.tri]) #close to 1
-supportD <- outer(seq(0,1,0.01), seq(0,1, 0.01), "+")
+## D=L+R
+dist <- MFdist2[21,,]
+supportD <- outer(seq(0,1, length=Ngrid), seq(0,1, length=Ngrid), "+")
+## compute the joint density of (L/D, D)
+## u=x/(x+y)
+## v=x+y
+x <- seq(0,1, length=Ngrid) %o% seq(0,1, length=Ngrid)
+y <- sweep(-x, 2, seq(0, 1, length=Ngrid), "+")
+xgrid <- round(x/0.005)
+ygrid <- round(y/0.005)
+distchv <- matrix(0, Ngrid, Ngrid)
+for(i in 1:Ngrid){
+ for(j in 1:Ngrid){
+ distchv[i,j] <- dist[xgrid[i,j]+1, ygrid[i,j]+1]*seq(0,1, length=Ngrid)[j]
+ }
+}
diff --git a/plot_distributions.R b/plot_distributions.R
index 840a9ea0..0b369065 100644
--- a/plot_distributions.R
+++ b/plot_distributions.R
@@ -1,14 +1,14 @@
## some plots
matplot(Z, cbind(w, w.mod), type="l", ylab="probability density",
main="Factor distribution (Gaussian, and Market implied)")
-lossdist <- array(0, dim=c(1/lu+1, n.int))
+lossdist <- array(0, dim=c(Ngrid, n.int))
for(i in 1:n.int){
pshocked <- sapply(p[,ncol(p)], shockprob, rho=rho, Z=Z[i])
S <- 1 - Rstoch[i,,ncol(p)]
- lossdist[,i] <- lossrecovdist(pshocked, 0, issuerweights, S, lu)$L
+ lossdist[,i] <- lossrecovdist(pshocked, 0, issuerweights, S, Ngrid)$L
}
-lossdist.orig <- BClossdistC(SurvProb, issuerweights, recov, rho, lu)
+lossdist.orig <- BClossdistC(SurvProb, issuerweights, recov, rho, Ngrid)
matplot(seq(0,1,0.01), cbind(lossdist.orig$L[,ncol(p)], lossdist%*%w.mod), type="l", xlab="loss percentage",
ylab="probability density", main="market implied loss distribution")
#3d surface of the loss distribution
@@ -18,7 +18,7 @@ for(t in 1:ncol(SurvProb)){
Rstoch[t,,i] <- stochasticrecov(recov[i], 0, Z, w.mod, rho, defaultprob[i,t], p[i,t])
}
}
-lu <- 0.01
+
clusterExport(cl, list("p", "shockprob", "rho", "Z", "lossdistribC.joint", "Rstoch", "lu"))
parf <- function(i){
diff --git a/tranche_functions.R b/tranche_functions.R
index f410081d..59c5f721 100644
--- a/tranche_functions.R
+++ b/tranche_functions.R
@@ -294,6 +294,7 @@ lossrecovdist.joint.term <- function(defaultprob, prepayprob, w, S, N, useC=TRUE
}
}
}
+ gc()
return(Q)
}