diff options
Diffstat (limited to 'R/distrib.R')
| -rw-r--r-- | R/distrib.R | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/R/distrib.R b/R/distrib.R index 536ed20..d941bd2 100644 --- a/R/distrib.R +++ b/R/distrib.R @@ -16,7 +16,7 @@ #' Gaussian distribution.
#'
#' if f is a function, then with(GHquad(100), crossprod(f(Z), w))
-#'
+#' will compute \eqn{\frac{1}{\sqrt{2\pi}}\int_-\infty^\infty f(x)e^{-\frac{x^2}{2}}\,dx}.
#' @param n Integer, the number of nodes
#' @return A list with two components:
#' \item{Z}{the list of nodes}
@@ -106,20 +106,19 @@ lossdistrib2 <- function(p, w, S, N, defaultflag=FALSE){ return(q)
}
+#' recursive algorithm with first order correction truncated version
+#' this is actually slower than lossdistrib2. But in C this is
+#' twice as fast.
+#' For high severities, M can become bigger than N, and there is
+#' some probability mass escaping.
+#'
+#' @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 cutoff Integer, where to stop computing the exact probabilities
+#' @return a Numeric vector of size \code{N} computing the loss distribution
lossdistrib2.truncated <- function(p, w, S, N, cutoff=N){
- ## 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 (for best accuracy should
- ## be a multiple of the number of issuers)
- ## cutoff where to stop computing the exact probabilities
- ## (useful for tranche computations)
-
- ## this is actually slower than lossdistrib2. But in C this is
- ## twice as fast.
- ## for high severities, M can become bigger than N, and there is
- ## some probability mass escaping.
n <- length(p)
lu <- 1/(N-1)
q <- rep(0, cutoff)
@@ -174,19 +173,20 @@ recovdist <- function(dp, pp, w, S, N){ return(q)
}
+#' recursive algorithm with first order correction to compute the joint
+#' probability distribution of the loss and recovery.
+#' For high severities, M can become bigger than N, and there is
+#' some probability mass escaping.
+#'
+#' @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 cutoff Integer, where to stop computing the exact probabilities
+#' @return q Matrix of joint loss, recovery probability distribution
+#' colSums(q) is the recovery distribution marginal
+#' rowSums(q) is the loss distribution marginal
lossdist.joint <- function(p, w, S, N, defaultflag=FALSE){
- ## recursive algorithm with first order correction
- ## to compute the joint probability distribution of the loss and recovery
- ## inputs:
- ## p: vector of default probabilities
- ## w: vector of issuer weights
- ## S: vector of severities
- ## N: number of tick sizes on the grid
- ## defaultflag: if true computes the default distribution
- ## 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)
lu <- 1/(N-1)
q <- matrix(0, N, N)
|
