R/bootstrap.R

##' @title Bootstrap the nonparametric VaR or ES estimator for all alpha
##' @param x vector of losses
##' @param B number of bootstrap replications
##' @param level confidence level
##' @param method risk measure used
##' @return (length(alpha), B)-matrix where the bth column contains the estimated
##'         risk measure at each alpha based on the bth bootstrap sample of the
##'         losses
##' @author Marius Hofert
##' @note vectorized in x and level
bootstrap <- function(x, B, level, method = c("VaR", "ES"))
{
  stopifnot(is.vector(x), (n <- length(x)) >= 1, B >= 1) # sanity checks
  ## Define the risk measure (as a function of x, level)
  method <- match.arg(method) # check and match 'method'
  rm <- if(method == "VaR") {
    VaR_np # see qrmtools; essentially quantile(, type = 1)
  } else {
    function(x, level) ES_np(x, level = level, verbose = TRUE) # see qrmtools; uses '>' and 'verbose'
  }
  ## Construct the bootstrap samples (by drawing with replacement)
  ## from the underlying empirical distribution function
  x.boot <- matrix(sample(x, size = n * B, replace = TRUE), ncol = B) # (n, B)-matrix
  ## For each bootstrap sample, estimate the risk measure
  apply(x.boot, 2, rm, level = level) # (length(level), B)-matrix
}
3schwartz/SpecialeScrAndFun documentation built on May 4, 2019, 6:29 a.m.