Path: blob/master/sandbox/symposium2013/R/chart.VaRSensitivity.R
1433 views
#' show the sensitivity of Value-at-Risk or Expected Shortfall estimates1#'2#' Creates a chart of Value-at-Risk and/or Expected Shortfall estimates by3#' confidence interval for multiple methods.4#'5#' This chart shows estimated VaR along a series of confidence intervals for6#' selected calculation methods. Useful for comparing a method to the7#' historical VaR calculation.8#'9#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of10#' asset returns11#' @param methods one or more calculation methods indicated "GaussianVaR",12#' "ModifiedVaR", "HistoricalVaR", "GaussianES", "ModifiedES", "HistoricalES".13#' See \code{\link{VaR}} or \code{\link{ES}} for more detail.14#' @param clean method for data cleaning through \code{\link{Return.clean}}.15#' Current options are "none" or "boudt" or "geltner".16#' @param elementcolor the color used to draw chart elements. The default is17#' "darkgray"18#' @param reference.grid if true, draws a grid aligned with the points on the x19#' and y axes20#' @param ylab set the y-axis label, same as in \code{\link{plot}}21#' @param xlab set the x-axis label, same as in \code{\link{plot}}22#' @param ylim set the y-axis dimensions, same as in \code{\link{plot}}23#' @param type set the chart type, same as in \code{\link{plot}}24#' @param lty set the line type, same as in \code{\link{plot}}25#' @param lwd set the line width, same as in \code{\link{plot}}26#' @param colorset color palette to use, set by default to rational choices27#' @param pch symbols to use, see also \code{\link{plot}}28#' @param legend.loc places a legend into one of nine locations on the chart:29#' bottomright, bottom, bottomleft, left, topleft, top, topright, right, or30#' center.31#' @param cex.legend The magnification to be used for sizing the legend32#' relative to the current setting of 'cex'.33#' @param main set the chart title, same as in \code{\link{plot}}34#' @param \dots any other passthru parameters35#' @author Peter Carl36#' @seealso \code{\link{VaR}} \cr \code{\link{ES}}37#' @references Boudt, K., Peterson, B. G., Croux, C., 2008. Estimation and38#' Decomposition of Downside Risk for Portfolios with Non-Normal Returns.39#' Journal of Risk, forthcoming.40#' @keywords ts multivariate distribution41#' @examples42#'43#' data(managers)44#' chart.VaRSensitivity(managers[,1,drop=FALSE],45#' methods=c("HistoricalVaR", "ModifiedVaR", "GaussianVaR"),46#' colorset=bluefocus, lwd=2)47#'48#' @export49chart.VaRSensitivity <-50function (R, methods = c("GaussianVaR", "ModifiedVaR", "HistoricalVaR","GaussianES", "ModifiedES", "HistoricalES"), clean=c("none", "boudt", "geltner"), elementcolor="darkgray", reference.grid=TRUE, xlab = "Confidence Level", ylab="Value at Risk", type = "l", lty = c(1,2,4), lwd = 1, colorset = (1:12), pch = (1:12), legend.loc = "bottomleft", cex.legend = 0.8, main=NULL, ylim=NULL, ...)51{ # @author Peter Carl5253R = checkData(R)54columnnames = colnames(R)55clean = clean[1]56legend.txt = NULL57if(length(methods) > 1){58columns=159}60p = seq(0.99,0.89,by=-0.005)61risk = matrix(nrow=length(p), ncol=length(methods),dimnames=list(p,methods))6263for(column in 1:columns) {64for(j in 1:length(methods)) {65for(i in 1:length(p)){66switch(methods[j],67GaussianVaR = {68risk[i, j] = as.numeric(VaR(na.omit(R[,column,drop=FALSE]), p = p[i], method="gaussian", clean=clean))69if(i==1)70legend.txt = c(legend.txt, "Gaussian VaR")7172},73ModifiedVaR = {74risk[i, j] = as.numeric(VaR(na.omit(R[,column,drop=FALSE]), p = p[i], method="modified", clean=clean))75if(i==1)76legend.txt = c(legend.txt, "Modified VaR")77},78HistoricalVaR = {79risk[i,j] = as.numeric(VaR(na.omit(R[,column,drop=FALSE]), p = p[i], method="historical", clean=clean)) #hVaR = quantile(x,probs=.01)80if(i==1)81legend.txt = c(legend.txt, "Historical VaR")8283},84GaussianES = {85risk[i, j] = as.numeric(ES(na.omit(R[,column,drop=FALSE]), p = p[i], method="gaussian", clean=clean))86if(i==1)87legend.txt = c(legend.txt, "Gaussian ES")8889},90ModifiedES = {91risk[i, j] = as.numeric(ES(na.omit(R[,column,drop=FALSE]), p = p[i], method="modified", clean=clean))92if(i==1)93legend.txt = c(legend.txt, "Modified ES")94},95HistoricalES = {96risk[i,j] = as.numeric(ES(na.omit(R[,column,drop=FALSE]), p = p[i], method="historical", clean=clean))97if(i==1)98legend.txt = c(legend.txt, "Historical ES")99100}101) # end switch102}103104} # end method loop105} # end column loop106# print(risk)107108risk.columns = ncol(risk)109if(is.null(ylim))110ylim=c(min(risk),max(risk))111xlim=c(min(p), max(p))112if(is.null(main))113main=paste("Risk Confidence Sensitivity of ", columnnames[1], sep="")114if(length(lwd) < risk.columns)115lwd = rep(lwd,risk.columns)116if(length(lty) < risk.columns)117lty = rep(lty,risk.columns)118if(length(pch) < risk.columns)119pch = rep(pch,risk.columns)120plot.new()121plot.window(xlim, ylim, xaxs = "r")122if (reference.grid) {123grid(col = elementcolor)124}125for(risk.column in risk.columns:1) {126lines(p,risk[,risk.column], col = colorset[risk.column], lwd = lwd[risk.column], pch = pch[risk.column], lty = lty[risk.column], type = type, ...)127}128129# draw x-axis130axis(1, labels=p, at=p, col = elementcolor) # at= 1/(1:length(p))131title(xlab = xlab)132133# set up y-axis134axis(2, col=elementcolor)135box(col = elementcolor)136137if(!is.null(legend.loc)){138# There's no good place to put this automatically, except under the graph.139# That requires a different solution, but here's the quick fix140legend(legend.loc, inset = 0.02, text.col = colorset, col = colorset, cex = cex.legend, border.col = elementcolor, lty = lty, lwd = 2, bg = "white", legend = legend.txt)141}142143# Add the other titles144if(is.null(main))145main=columnnames[1]146title(ylab = ylab)147title(main = main)148}149150###############################################################################151# R (http://r-project.org/) Econometrics for Performance and Risk Analysis152#153# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson154#155# This R package is distributed under the terms of the GNU Public License (GPL)156# for full details see the file COPYING157#158# $Id: chart.VaRSensitivity.R 3191 2013-09-26 15:42:06Z peter_carl $159#160###############################################################################161162163