From fe0dacb151528f14683c74b456a581375ef78bf4 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 10 Sep 2014 16:25:33 -0400 Subject: start documenting the code properly --- R/tranche_functions.R | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/R/tranche_functions.R b/R/tranche_functions.R index 4d5abab..c167384 100644 --- a/R/tranche_functions.R +++ b/R/tranche_functions.R @@ -10,6 +10,18 @@ ## - do the correlation adjustments when computing the deltas since it seems to be ## the market standard +#' Gauss-Hermite quadrature weights +#' +#' \code{GHquad} computes the quadrature weights for integrating against a +#' Gaussian distribution. +#' +#' if f is a function, then with(GHquad(100), crossprod(f(Z), w)) +#' +#' @param n Integer, the number of nodes +#' @return A list with two components: +#' \item{Z}{the list of nodes} +#' \item{w}{the corresponding weights} +#' GHquad <- function(n){ n <- as.integer(n) Z <- double(n) @@ -19,6 +31,13 @@ GHquad <- function(n){ return(result) } +#' Basic recursive algorithm of Andersen, Sidenius and Basu +#' +#' \code{lossdistrib} computes the probability distribution of a sum +#' of independent Bernouilli variables with unequal probabilities. +#' +#' @param p Numeric vector, the vector of success probabilities +#' @return A vector such that q[k]=P(S=k) lossdistrib <- function(p){ ## basic recursive algorithm of Andersen, Sidenius and Basu n <- length(p) @@ -36,19 +55,24 @@ lossdistrib.fft <- function(p){ ## complexity is of order O(n*m)+O(m*log(m)) ## where m is the size of the grid and n the number of probabilities. ## this is slower than the recursive algorithm + n <- length(p) theta <- 2*pi*1i*(0:n)/(n+1) Phi <- 1 - p + p%o%exp(theta) v <- apply(Phi, 2, prod) return(1/(n+1)*Re(fft(v))) } +#' recursive algorithm with first order correction +#' +#' @param p Numeric, vector of default probabilities +#' @param w Numeric, vector of weights +#' @param S Numeric, vector of severities +#' @param N Integer, number of ticks in the grid +#' @param defaultflag Boolean, if True, we compute the default distribution +#' (instead of the loss distribution). +#' @return a Numeric vector of size \code{N} computing the loss (resp. +#' default) distribution if \code{defaultflag} is FALSE (resp. TRUE). lossdistrib2 <- function(p, w, S, N, defaultflag=FALSE){ - ## recursive algorithm with first order correction - ## p vector of default probabilities - ## w vector of weigths - ## S vector of severities - ## N number of ticks in the grid - ## defaultflag if true computes the default distribution n <- length(p) lu <- 1/(N-1) q <- rep(0, N) @@ -366,11 +390,11 @@ lossrecovdist.joint.term <- function(defaultprob, prepayprob, w, S, N, defaultfl if(useC){ if(missing(prepayprob)){ for(t in 1:ncol(defaultprob)){ - Q[t,,] <- lossdistC.joint(defaultprob[,t], w, S[,t], N, defaultflag) + Q[t,,] <- lossdistC.jointblas(defaultprob[,t], w, S[,t], N, defaultflag) } }else{ for(t in 1:ncol(defaultprob)){ - Q[t,,] <- lossdistC.prepay.joint(defaultprob[,t], prepayprob[,t], w, S[,t], N, defaultflag) + Q[t,,] <- lossdistC.prepay.jointblas(defaultprob[,t], prepayprob[,t], w, S[,t], N, defaultflag) } } }else{ @@ -913,7 +937,7 @@ Probtrunc <- function(index, K, rho){ } -BCstrikes <- function(defaultprob, issuerweights, recov, cs, K, rho, Z, w, N=101) { +BCstrikes <- function(index, K, rho) { ## computes the strikes as a percentage of expected loss ## Kmodified is the current attachment points (adjusted for losses) el <- EL(index) -- cgit v1.2.3-70-g09d2