Hi all,
Iam trying to compute the Expected Shortfall from a GARCH(1,1) with sged
innovations created via the great rugarch package. The problem is that the
range of values compared to the VaR(99) is totally different and I dont
know where I have made the mistake.
Here is my code:
library(quantmod)
library(rugarch)
library(parallel)
library(PerformanceAnalytics)
# get Data
mmm <- getSymbols("MMM", from = "2005-01-01", to = "2013-05-31")
mmm <- Ad(get(mmm))
ldr_mmm <- Return.calculate(mmm, method = "log")
# remove NA observations
ldr_mmm <- na.omit(ldr_mmm)
ctrl = list(rho = 1, delta = 1e-9, outer.iter = 1000, tol = 1e-7) # options
for solver
cl = makePSOCKcluster(10) # Create a Parallel Socket Cluster
# Choosing estimation and test window
n_all_mmm = nrow(mmm)
n_test_mmm <- nrow(as.xts(ldr_mmm)["2007-01-04/2013-05-31"]) # testing
window
n_est_mmm <- n_all_mmm - n_test_mmm # estimation window
# Fitting a GARCH(1,1) Model with skewed generalized error distribution
innovations
fit_MMM_def = ugarchspec(variance.model = list(model = "sGARCH", garchOrder
= c(1,1)),
mean.model = list(armaOrder =
c(0,0), include.mean = TRUE),
distribution.model = "sged")
# Calcualte Backtest
MMM.backtest = ugarchroll(fit_MMM_def, data = ldr_mmm, n.ahead = 1,
forecast.length =
n_test_mmm, refit.every = 20, refit.window = "moving",
solver = "hybrid",
fit.control = list(), solver.control = ctrl,
calculate.VaR = TRUE,
VaR.alpha = c(0.01), # Compute VaR = TRUE
cluster = cl)
# Calculate the VaR(99) by your own if calculate.VaR = FALSE @ ugarchroll
df1_var <- as.data.frame(MMM.backtest, which = "density")
f = function(x, skew, shape) qdist("sged", p = x, mu = 0, sigma = 1, skew =
skew, shape = shape)
test_var = df1_var[, 'Mu'] + qdist("sged", 0.01, 0, 1, skew = df1_var[,
'Skew'],
shape =
df1_var[, 'Shape']) * df1_var[, 'Sigma']
# Lets compare it with the results from the ugarchroll function
MMM_GARCH <- MMM.backtest@forecast
head(cbind(test_var, as.data.frame(MMM_GARCH[["VaR"]]))) # exactly the
same, thats good!
# Calcualte the Expected Shortfall (99)
test_es = apply(df1_var, 1, function(x) x['Mu'] + x['Sigma'] * integrate(f,
0, 0.01, skew = x['Skew'], shape = x['Shape'])$value)
test_es <- as.zoo(as.xts(test_es))
test_es <- aggregate(test_es, function(tt) as.Date(tt, tz = "")) #convert
to date
# Lets compare the VaR(99) and the ES(99)
layout(1:2)
plot(test_es) # ES(99)
plot(as.zoo(MMM.backtest@forecast$VaR[1])) # VaR(99)
The most of the ideas are from http://www.unstarched.net (rugarch). My clue
is that the integration is wrong but Iam not sure...
Thanks in advance
Daniel
[[alternative HTML version deleted]]
_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should
go.