From 9d40921fa428330f86814bb813dfb1c524053a40 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Fri, 12 Sep 2014 15:45:36 -0400 Subject: starting to clean up the doc --- R/tranche_functions.R | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/R/tranche_functions.R b/R/tranche_functions.R index c167384..217a7b3 100644 --- a/R/tranche_functions.R +++ b/R/tranche_functions.R @@ -31,13 +31,16 @@ GHquad <- function(n){ return(result) } -#' Basic recursive algorithm of Andersen, Sidenius and Basu +#' Loss distribution of a portfolio #' #' \code{lossdistrib} computes the probability distribution of a sum #' of independent Bernouilli variables with unequal probabilities. #' +#' This uses the basic recursive algorithm of Andersen, Sidenius and Basu +#' We compute the probability distribution of S = \sum_{i=1}^n X_i +#' where X_i is Bernouilli(p_i) #' @param p Numeric vector, the vector of success probabilities -#' @return A vector such that q[k]=P(S=k) +#' @return A vector q such that q[k]=P(S=k) lossdistrib <- function(p){ ## basic recursive algorithm of Andersen, Sidenius and Basu n <- length(p) @@ -50,11 +53,19 @@ lossdistrib <- function(p){ return(q) } +#' Loss distribution of a portfolio +#' +#' \code{lossdistrib.fft} computes the probability distribution of a sum +#' of independent Bernouilli variables with unequal probabilities. +#' +#' This uses the fft. 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. +#' It is slower than the recursive algorithm in practice. +#' We compute the probability distribution of S = \sum_{i=1}^n X_i +#' where X_i is Bernouilli(p_i) +#' @param p Numeric vector, the vector of success probabilities +#' @return A vector such that q[k]=P(S=k) lossdistrib.fft <- function(p){ - ## computes loss distribution using the fft - ## 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) @@ -132,10 +143,10 @@ lossdistrib2.truncated <- function(p, w, S, N, cutoff=N){ recovdist <- function(dp, pp, w, S, N){ ## computes the recovery distribution for a sum of independent variables - ## R=\sum_{i=1}^n X_i - ## where X_i = 0 w.p 1-dp[i]-pp[i] - ## = w[i]*(1-S[i]) w.p dp[i] - ## = w[i] w.p pp[i] + ## R=\sum_{i=1}^n w[i] X_i + ## where X_i = 0 w.p 1 - dp[i] - pp[i] + ## = 1 - S[i] w.p dp[i] + ## = 1 w.p pp[i] ## each non zero value v is interpolated on the grid as ## the pair of values floor(v/lu) and ceiling(v/lu) so that ## X_i has four non zero values -- cgit v1.3