#' Value at Risk and Conditional Value at risk.
#'
#' Estimates losses distribution, VaR amd CVaR.
#' @param series returns series.
#' @param quant Quantile.
#' @param normal Indicator if returns are normaly distributed.
#' @return Losses distribution, covar matrix, volatility, VaR and CVaR.
#' @export
series_var_cvar <- function(series, quant, normal = FALSE) {
if(nrow(series) > 1){
neg_rets <- -1 * series
vol <- sd(series)
if(normal){
var <- qnorm(quant) * vol
cvar <- dnorm(qnorm(quant))/(1-quant) * vol
}else{
var <- quantile(neg_rets, probs = quant)
cvar <- sapply(var, function(x) mean(neg_rets[neg_rets > x]))
}
}else{
neg_rets <- vol <- var <- cvar <- NA
}
return(list(neg_rets = neg_rets, vol = vol, var = var, cvar = cvar))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.