aboutsummaryrefslogtreecommitdiffstats
path: root/R/tranche_functions.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/tranche_functions.R')
-rw-r--r--R/tranche_functions.R77
1 files changed, 74 insertions, 3 deletions
diff --git a/R/tranche_functions.R b/R/tranche_functions.R
index 6bea5143..306947e4 100644
--- a/R/tranche_functions.R
+++ b/R/tranche_functions.R
@@ -274,12 +274,22 @@ lossdistC.prepay.joint <- function(dp, pp, w, S, N, defaultflag=FALSE){
if(!is.loaded("lossdistrib_prepay_joint")){
dyn.load(file.path(root.dir, "code", "R", paste0("lossdistrib", .Platform$dynlib.ext)))
}
- r <- .C("lossdistrib_prepay_joint", as.double(dp), as.double(pp), as.integer(length(dp)),
- as.double(w), as.double(S), as.integer(N), as.logical(defaultflag), q=matrix(0, N, N))$q
- gc()
+ r <- .C("lossdistrib_prepay_joint_Z", as.double(dp), as.double(pp), as.integer(length(dp)),
+ as.double(w), as.double(S), as.integer(N), as.logical(defaultflag), as.double(rho),
+ as.double(Z), as.double(wZ), as.integer(length(Z)), q=matrix(0, N, N))$q
+
return(r)
}
+lossdistC.prepay.jointZ <- function(dp, pp, w, S, N, defaultflag = FALSE, rho, Z, wZ){
+ if(!is.loaded("lossdistrib_prepay_joint_Z")){
+ dyn.load(file.path(root.dir, "code", "R", paste0("lossdistrib", .Platform$dynlib.ext)))
+ }
+ r <- .C("lossdistrib_prepay_joint_Z", as.double(dp), as.double(pp), as.integer(length(dp)),
+ as.double(w), as.double(S), as.integer(N), as.logical(defaultflag), as.double(rho),
+ as.double(Z), as.double(wZ), as.integer(length(Z)), q = matrix(0, N, N))$q
+}
+
lossrecovdist <- function(defaultprob, prepayprob, w, S, N, defaultflag=FALSE, useC=TRUE){
if(missing(prepayprob)){
if(useC){
@@ -408,6 +418,15 @@ fit.prob <- function(Z, w, rho, p0){
}
}
+fit.probC <- function(Z, w, rho, p0){
+ if(!is.loaded("fitprob")){
+ dyn.load(file.path(root.dir, "code", "R", paste0("lossdistrib", .Platform$dynlib.ext)))
+ }
+ r <- .C("fitprob", as.double(Z), as.double(w), as.integer(length(Z)),
+ as.double(rho), as.double(p0), q = double(1))
+ return(r$q)
+}
+
stochasticrecov <- function(R, Rtilde, Z, w, rho, porig, pmod){
## if porig == 0 (probably matured asset) then return orginal recovery
## it shouldn't matter anyway since we never default that asset
@@ -419,6 +438,16 @@ stochasticrecov <- function(R, Rtilde, Z, w, rho, porig, pmod){
}
}
+stochasticrecovC <- function(R, Rtilde, Z, w, rho, porig, pmod){
+ if(!is.loaded("stochasticrecov")){
+ dyn.load(file.path(root.dir, "code", "R", paste0("lossdistrib", .Platform$dynlib.ext)))
+ }
+ r <- .C("stochasticrecov", as.double(R), as.double(Rtilde), as.double(Z),
+ as.double(w), as.integer(length(Z)), as.double(rho), as.double(porig),
+ as.double(pmod), q = double(length(Z)))
+ return(r$q)
+}
+
pos <- function(x){
pmax(x, 0)
}
@@ -650,6 +679,23 @@ MFupdate.prob <- function(Z, w, rho, defaultprob){
return( p )
}
+MFupdate.probC <- function(Z, w, rho, defaultprob){
+ ## update the probabilites based on a non gaussian factor
+ ## distribution so that the pv of the cds stays the same.
+ p <- matrix(0, nrow(defaultprob), ncol(defaultprob))
+ if(!is.loaded("fitprob")){
+ dyn.load(file.path(root.dir, "code", "R", paste0("lossdistrib", .Platform$dynlib.ext)))
+ }
+ for(i in 1:nrow(defaultprob)){
+ for(j in 1:ncol(defaultprob)){
+ p[i,j] <- .C("fitprob", as.double(Z), as.double(w), as.integer(length(Z)),
+ as.double(rho), as.double(defaultprob[i,j]), q = double(1))$q
+ }
+ }
+ return( p )
+}
+
+
MFlossrecovdist <- function(w, Z, rho, defaultprob, defaultprobmod, issuerweights, recov,
Ngrid=2*length(issuerweights)+1, defaultflag=FALSE){
## computes the loss and recovery distribution using the modified factor distribution
@@ -781,6 +827,31 @@ MFlossdist.prepay.joint <- function(cl, w, Z, rho, defaultprob, defaultprobmod,
return( Q )
}
+MFlossdist.prepay.joint2 <- function(w, Z, rho, defaultprob, defaultprobmod,
+ prepayprob, prepayprobmod, issuerweights, recov,
+ Ngrid=2*length(issuerweights)+1, defaultflag=FALSE){
+ ## 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.credit, n.int, ncol(defaultprob)))
+
+ for(t in 1:ncol(defaultprob)){
+ for(i in 1:n.credit){
+ Rstoch[i,,t] <- stochasticrecovC(recov[i], 0, Z, w, rho, defaultprob[i,t], defaultprobmod[i,t])
+ }
+ }
+ Q <- array(0, dim=c(ncol(defaultprob), Ngrid, Ngrid))
+ for(t in 1:ncol(defaultprob)){
+ S <- 1 - Rstoch[,,t]
+ Q[t,,] <- lossdistC.prepay.jointZ(dp[,t], pp[,t], issuerweights, S, Ngrid, defaultflag, rho, Z, w)
+ }
+ return( Q )
+}
+
MFtranche.pv <- function(cl, cs, w, rho, defaultprob, defaultprobmod, issuerweights, recov,
Kmodified, Ngrid=length(issuerweights)+1){
## computes the tranches pv using the modified factor distribution