aboutsummaryrefslogtreecommitdiffstats
path: root/R
diff options
context:
space:
mode:
Diffstat (limited to 'R')
-rw-r--r--R/build_scenarios.R2
-rw-r--r--R/tranche_functions.R49
2 files changed, 2 insertions, 49 deletions
diff --git a/R/build_scenarios.R b/R/build_scenarios.R
index cb9ce0ee..7a250a28 100644
--- a/R/build_scenarios.R
+++ b/R/build_scenarios.R
@@ -102,7 +102,7 @@ for(deal.name in dealnames){
pp <- A$PP
dpmod <- MFupdate.probC(Z, w, rho, dp)
ppmod <- MFupdate.probC(-Z, w, rho, pp)
- dist.joint <- MFlossdist.prepay.joint2(w, Z, rho, dp, dpmod, pp, ppmod,
+ dist.joint <- MFlossdist.prepay.joint(w, Z, rho, dp, dpmod, pp, ppmod,
deal.weights, 1-S, Ngrid)
distDR <- dist.transform(dist.joint)
diff --git a/R/tranche_functions.R b/R/tranche_functions.R
index 66afac89..f08dd6c9 100644
--- a/R/tranche_functions.R
+++ b/R/tranche_functions.R
@@ -874,54 +874,7 @@ MFlossdist.joint <- function(cl, w, Z, rho, defaultprob, defaultprobmod, issuerw
return( Q )
}
-MFlossdist.prepay.joint <- function(cl, w, Z, rho, defaultprob, defaultprobmod,
- prepayprob, prepayprobmod, issuerweights, recov,
- 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
- ## 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)
- n.int <- length(w)
- 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, defaultflag)
- return(dist)
- }
- Q <- array(0, dim=c(ncol(defaultprob), Ngrid, Ngrid))
- ## 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){
- if(!is.null(cl)){
- 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+chunksize*(chunk-1)]*array(temp[,i], dim=c(ncol(defaultprob), Ngrid, Ngrid))
- }
- ## same here
- gc()
- gc()
- }
- return( Q )
-}
-
-MFlossdist.prepay.joint2 <- function(w, Z, rho, defaultprob, defaultprobmod,
+MFlossdist.prepay.joint <- function(w, Z, rho, defaultprob, defaultprobmod,
prepayprob, prepayprobmod, issuerweights, recov,
Ngrid=2*length(issuerweights)+1, defaultflag=FALSE){
## rowSums is the loss distribution