aboutsummaryrefslogtreecommitdiffstats
path: root/tranche_functions.R
diff options
context:
space:
mode:
Diffstat (limited to 'tranche_functions.R')
-rw-r--r--tranche_functions.R26
1 files changed, 15 insertions, 11 deletions
diff --git a/tranche_functions.R b/tranche_functions.R
index 51a0cd87..b5b5805a 100644
--- a/tranche_functions.R
+++ b/tranche_functions.R
@@ -335,11 +335,9 @@ lossrecovdist.joint.term <- function(defaultprob, prepayprob, w, S, N, defaultfl
}
}
}
- gc()
return(Q)
}
-
dist.transform <- function(dist.joint){
## compute the joint (D, R) distribution
## from the (L, R) distribution using D = L+R
@@ -714,7 +712,7 @@ MFlossdist.joint <- function(cl, w, Z, rho, defaultprob, defaultprobmod, issuerw
MFlossdist.prepay.joint <- function(cl, w, Z, rho, defaultprob, defaultprobmod,
prepayprob, prepayprobmod, issuerweights, recov,
- Ngrid=2*length(issuerweights)+1, defaultflag=FALSE){
+ Ngrid=2*length(issuerweights)+1, defaultflag=FALSE, n.chunks=4){
## rowSums is the loss distribution
## colSums is the recovery distribution
## so that recovery is the y axis and L is the x axis
@@ -732,21 +730,27 @@ MFlossdist.prepay.joint <- function(cl, w, Z, rho, defaultprob, defaultprobmod,
ppshocked <- apply(prepayprobmod, 2, shockprob, rho=rho, Z=-Z[i])
S <- 1 - Rstoch[i,,]
dist <- lossrecovdist.joint.term(dpshocked, ppshocked, issuerweights, S, Ngrid, defaultflag)
- gc()
return(dist)
}
Q <- array(0, dim=c(ncol(defaultprob), Ngrid, Ngrid))
- n.chunks <- 4
+ ## when using the cluster this takes a crazy amount of memory
+ ## we cut it in chunks to keep it manageable
chunksize <- length(w)/n.chunks
for(chunk in 1:n.chunks){
- temp <- sapply((1+chunksize*(chunk-1)):chunksize*chunk, parf)
- ## clusterExport(cl, list("Rstoch", "defaultprobmod", "prepayprobmod"), envir=environment())
- ## temp <- parSapply(cl, 1:length(w), parf)
- ##gc()
- ## clusterCall(cl, gc)
+ if(length(cl)>0){
+ clusterExport(cl, list("Rstoch", "defaultprobmod", "prepayprobmod"), envir=environment())
+ temp <- parSapply(cl, (1+chunksize*(chunk-1)):(chunksize*chunk), parf)
+ ## need to call gc twice for some reason, otherwise doesn't release everything
+ clusterCall(cl, gc)
+ clusterCall(cl, gc)
+ }else{
+ temp <- sapply((1+chunksize*(chunk-1)):chunksize*chunk, parf)
+ }
for(i in 1:chunksize){
- Q <- Q + w[i+chunkzise*(chunk-1)]*array(temp[,i], dim=c(ncol(defaultprob), Ngrid, Ngrid))
+ Q <- Q + w[i+chunksize*(chunk-1)]*array(temp[,i], dim=c(ncol(defaultprob), Ngrid, Ngrid))
}
+ ## same here
+ gc()
gc()
}
return( Q )