Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/sandbox/symposium2013/R/chart.VaRSensitivity.R
1433 views
1
#' show the sensitivity of Value-at-Risk or Expected Shortfall estimates
2
#'
3
#' Creates a chart of Value-at-Risk and/or Expected Shortfall estimates by
4
#' confidence interval for multiple methods.
5
#'
6
#' This chart shows estimated VaR along a series of confidence intervals for
7
#' selected calculation methods. Useful for comparing a method to the
8
#' historical VaR calculation.
9
#'
10
#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
11
#' asset returns
12
#' @param methods one or more calculation methods indicated "GaussianVaR",
13
#' "ModifiedVaR", "HistoricalVaR", "GaussianES", "ModifiedES", "HistoricalES".
14
#' See \code{\link{VaR}} or \code{\link{ES}} for more detail.
15
#' @param clean method for data cleaning through \code{\link{Return.clean}}.
16
#' Current options are "none" or "boudt" or "geltner".
17
#' @param elementcolor the color used to draw chart elements. The default is
18
#' "darkgray"
19
#' @param reference.grid if true, draws a grid aligned with the points on the x
20
#' and y axes
21
#' @param ylab set the y-axis label, same as in \code{\link{plot}}
22
#' @param xlab set the x-axis label, same as in \code{\link{plot}}
23
#' @param ylim set the y-axis dimensions, same as in \code{\link{plot}}
24
#' @param type set the chart type, same as in \code{\link{plot}}
25
#' @param lty set the line type, same as in \code{\link{plot}}
26
#' @param lwd set the line width, same as in \code{\link{plot}}
27
#' @param colorset color palette to use, set by default to rational choices
28
#' @param pch symbols to use, see also \code{\link{plot}}
29
#' @param legend.loc places a legend into one of nine locations on the chart:
30
#' bottomright, bottom, bottomleft, left, topleft, top, topright, right, or
31
#' center.
32
#' @param cex.legend The magnification to be used for sizing the legend
33
#' relative to the current setting of 'cex'.
34
#' @param main set the chart title, same as in \code{\link{plot}}
35
#' @param \dots any other passthru parameters
36
#' @author Peter Carl
37
#' @seealso \code{\link{VaR}} \cr \code{\link{ES}}
38
#' @references Boudt, K., Peterson, B. G., Croux, C., 2008. Estimation and
39
#' Decomposition of Downside Risk for Portfolios with Non-Normal Returns.
40
#' Journal of Risk, forthcoming.
41
#' @keywords ts multivariate distribution
42
#' @examples
43
#'
44
#' data(managers)
45
#' chart.VaRSensitivity(managers[,1,drop=FALSE],
46
#' methods=c("HistoricalVaR", "ModifiedVaR", "GaussianVaR"),
47
#' colorset=bluefocus, lwd=2)
48
#'
49
#' @export
50
chart.VaRSensitivity <-
51
function (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, ...)
52
{ # @author Peter Carl
53
54
R = checkData(R)
55
columnnames = colnames(R)
56
clean = clean[1]
57
legend.txt = NULL
58
if(length(methods) > 1){
59
columns=1
60
}
61
p = seq(0.99,0.89,by=-0.005)
62
risk = matrix(nrow=length(p), ncol=length(methods),dimnames=list(p,methods))
63
64
for(column in 1:columns) {
65
for(j in 1:length(methods)) {
66
for(i in 1:length(p)){
67
switch(methods[j],
68
GaussianVaR = {
69
risk[i, j] = as.numeric(VaR(na.omit(R[,column,drop=FALSE]), p = p[i], method="gaussian", clean=clean))
70
if(i==1)
71
legend.txt = c(legend.txt, "Gaussian VaR")
72
73
},
74
ModifiedVaR = {
75
risk[i, j] = as.numeric(VaR(na.omit(R[,column,drop=FALSE]), p = p[i], method="modified", clean=clean))
76
if(i==1)
77
legend.txt = c(legend.txt, "Modified VaR")
78
},
79
HistoricalVaR = {
80
risk[i,j] = as.numeric(VaR(na.omit(R[,column,drop=FALSE]), p = p[i], method="historical", clean=clean)) #hVaR = quantile(x,probs=.01)
81
if(i==1)
82
legend.txt = c(legend.txt, "Historical VaR")
83
84
},
85
GaussianES = {
86
risk[i, j] = as.numeric(ES(na.omit(R[,column,drop=FALSE]), p = p[i], method="gaussian", clean=clean))
87
if(i==1)
88
legend.txt = c(legend.txt, "Gaussian ES")
89
90
},
91
ModifiedES = {
92
risk[i, j] = as.numeric(ES(na.omit(R[,column,drop=FALSE]), p = p[i], method="modified", clean=clean))
93
if(i==1)
94
legend.txt = c(legend.txt, "Modified ES")
95
},
96
HistoricalES = {
97
risk[i,j] = as.numeric(ES(na.omit(R[,column,drop=FALSE]), p = p[i], method="historical", clean=clean))
98
if(i==1)
99
legend.txt = c(legend.txt, "Historical ES")
100
101
}
102
) # end switch
103
}
104
105
} # end method loop
106
} # end column loop
107
# print(risk)
108
109
risk.columns = ncol(risk)
110
if(is.null(ylim))
111
ylim=c(min(risk),max(risk))
112
xlim=c(min(p), max(p))
113
if(is.null(main))
114
main=paste("Risk Confidence Sensitivity of ", columnnames[1], sep="")
115
if(length(lwd) < risk.columns)
116
lwd = rep(lwd,risk.columns)
117
if(length(lty) < risk.columns)
118
lty = rep(lty,risk.columns)
119
if(length(pch) < risk.columns)
120
pch = rep(pch,risk.columns)
121
plot.new()
122
plot.window(xlim, ylim, xaxs = "r")
123
if (reference.grid) {
124
grid(col = elementcolor)
125
}
126
for(risk.column in risk.columns:1) {
127
lines(p,risk[,risk.column], col = colorset[risk.column], lwd = lwd[risk.column], pch = pch[risk.column], lty = lty[risk.column], type = type, ...)
128
}
129
130
# draw x-axis
131
axis(1, labels=p, at=p, col = elementcolor) # at= 1/(1:length(p))
132
title(xlab = xlab)
133
134
# set up y-axis
135
axis(2, col=elementcolor)
136
box(col = elementcolor)
137
138
if(!is.null(legend.loc)){
139
# There's no good place to put this automatically, except under the graph.
140
# That requires a different solution, but here's the quick fix
141
legend(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)
142
}
143
144
# Add the other titles
145
if(is.null(main))
146
main=columnnames[1]
147
title(ylab = ylab)
148
title(main = main)
149
}
150
151
###############################################################################
152
# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
153
#
154
# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
155
#
156
# This R package is distributed under the terms of the GNU Public License (GPL)
157
# for full details see the file COPYING
158
#
159
# $Id: chart.VaRSensitivity.R 3191 2013-09-26 15:42:06Z peter_carl $
160
#
161
###############################################################################
162
163