aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--R/script_calibrate_tranches.R31
1 files changed, 13 insertions, 18 deletions
diff --git a/R/script_calibrate_tranches.R b/R/script_calibrate_tranches.R
index 99dc1527..c01d1123 100644
--- a/R/script_calibrate_tranches.R
+++ b/R/script_calibrate_tranches.R
@@ -89,29 +89,24 @@ defaultprob <- 1 - SurvProb
p <- defaultprob
rho <- 0.45
-clusterExport(cl, list("root.dir", "shockprob", "issuerweights", "rho", "Z", "lossrecovdist.term",
- "lossrecovdist", "lossdistC", "Ngrid",
- "tranche.pvvec", "tranche.pv", "tranche.pl", "tranche.cl",
- "trancheloss", "trancherecov", "pos", "Kmodified", "cs"))
-
-## TODO: investigate if this is the right thing w.r.t recovery
-parf <- function(i){
- pshocked <- apply(p, 2, shockprob, rho=rho, Z=Z[i])
- S <- 1 - Rstoch[i,,]
- dist <- lossrecovdist.term(pshocked, , issuerweights, S, Ngrid)
- return( tranche.pvvec(Kmodified, dist$L, dist$R, cs))
-}
-
+result <- matrix(0, 4, n.int)
for(l in 1:100){
- Rstoch <- array(0, dim=c(n.int, n.credit, ncol(SurvProb)))
+ Rstoch <- array(0, dim=c(n.credit, n.int, ncol(SurvProb)))
for(t in 1:ncol(SurvProb)){
for(i in 1:n.credit){
- Rstoch[,i,t] <- stochasticrecov(recov[i], 0, Z, w.mod, rho, defaultprob[i,t], p[i,t])
+ Rstoch[i,,t] <- stochasticrecovC(recov[i], 0, Z, w.mod, rho, defaultprob[i,t], p[i,t])
}
}
-
- clusterExport(cl, list("Rstoch", "p"))
- result <- parSapply(cl, 1:n.int, parf)
+ L <- array(0, dim=c(n.int, Ngrid, ncol(defaultprob)))
+ R <- array(0, dim=c(n.int, Ngrid, ncol(defaultprob)))
+ for(t in 1:ncol(defaultprob)){
+ S <- 1 - Rstoch[,,t]
+ L[,,t] <- t(lossdistCZ(p[,t], issuerweights, S, Ngrid, 0, rho, Z, w))
+ R[,,t] <- t(lossdistCZ(p[,t], issuerweights, 1-S, Ngrid, 0, rho, Z, w))
+ }
+ for(i in 1:n.int){
+ result[,i] <- tranche.pvvec(Kmodified, L[i,,], R[i,,], cs)
+ }
## solve the optimization problem
program <- KLfit(100*(result[bottomup,]+1), w, tranche.upf[bottomup])