aboutsummaryrefslogtreecommitdiffstats
path: root/tranche_functions.R
diff options
context:
space:
mode:
Diffstat (limited to 'tranche_functions.R')
-rw-r--r--tranche_functions.R38
1 files changed, 24 insertions, 14 deletions
diff --git a/tranche_functions.R b/tranche_functions.R
index 159158fb..27ab8ec7 100644
--- a/tranche_functions.R
+++ b/tranche_functions.R
@@ -55,7 +55,7 @@ recovdist <- function(dp, pp, w, S, lu){
## = w[i]*(1-S[i]) w.p dp[i]
## = w[i] w.p pp[i]
## each non zero value v is interpolated on the grid as
- ## two values floor(v/lu) and ceiling(v/lu) so that
+ ## the pair of values floor(v/lu) and ceiling(v/lu) so that
## X_i has four non zero values
n <- length(dp)
N <- 1/lu + 1
@@ -85,10 +85,15 @@ recovdist <- function(dp, pp, w, S, lu){
lossdistrib.joint <- function(p, w, S, lu){
## recursive algorithm with first order correction
## to compute the joint probability distribution of the loss and recovery
- ## p vector of default probabilities
- ## w vector of issuer weights
- ## S vector of severities
- ## lu loss unit
+ ## inputs:
+ ## p: vector of default probabilities
+ ## w: vector of issuer weights
+ ## S: vector of severities
+ ## lu: loss unit
+ ## output:
+ ## q: matrix of joint loss, recovery probability
+ ## colSums(q) is the recovery distribution marginal
+ ## rowSums(q) is the loss distribution marginal
n <- length(p)
N <- ceiling(1/lu + 1)
q <- matrix(0, N, N)
@@ -114,11 +119,16 @@ lossdistrib.joint <- function(p, w, S, lu){
lossdistribprepay.joint <- function(dp, pp, w, S, lu){
## recursive algorithm with first order correction
## to compute the joint probability distribition of the loss and recovery
- ## dp vector of default probabilities
- ## pp vector of prepay probabilities
- ## w vector of issuer weights
- ## S vector of severities
- ## lu loss unit
+ ## inputs:
+ ## dp: vector of default probabilities
+ ## pp: vector of prepay probabilities
+ ## w: vector of issuer weights
+ ## S: vector of severities
+ ## lu: loss unit
+ ## outputs
+ ## q: matrix of joint loss and recovery probability
+ ## colSums(q) is the recovery distribution marginal
+ ## rowSums(q) is the loss distribution marginal
n <- length(dp)
N <- ceiling(1/lu + 1)
q <- matrix(0, N, N)
@@ -178,7 +188,7 @@ lossdistribprepayC.joint <- function(dp, pp, w, S, lu){
}
lossrecovdist <- function(defaultprob, prepayprob, w, S, lu, useC=TRUE){
- if(prepayprob==0){
+ if(all(!prepayprob)){
if(useC){
L <- lossdistribC(defaultprob, w, S, lu)
R <- lossdistribC(defaultprob, w, 1-S, lu)
@@ -197,7 +207,7 @@ lossrecovdist.term <- function(defaultprob, prepayprob, w, S, lu, useC=TRUE){
## computes the loss and recovery distribution over time
L <- array(0, dim=c(1/lu+1, ncol(defaultprob)))
R <- array(0, dim=c(1/lu+1, ncol(defaultprob)))
- if(prepayprob==0){
+ if(all(!prepayprob)){
for(t in 1:ncol(defaultprob)){
temp <- lossrecovdist(defaultprob[,t], 0, w, S[,t], lu, useC)
L[,t] <- temp$L
@@ -226,8 +236,8 @@ dqnorm <- function(x){
}
fit.prob <- function(Z, w, rho, p0){
- #if the weights are not perfectly gaussian, find the probability p such
- # E_w(shockprob(p, rho, Z)) = p0
+ ## if the weights are not perfectly gaussian, find the probability p such
+ ## E_w(shockprob(p, rho, Z)) = p0
eps <- 1e-12
dp <- (crossprod(shockprob(p0,rho,Z),w)-p0)/crossprod(dshockprob(p0,rho,Z),w)
p <- p0