[R-SIG-Finance] dynamic copula using rmgarch package (ignore previous question, don't know how to delete it)
Sachin Kuruvithadam
sacios at hotmail.it
Wed Jun 22 21:36:20 CEST 2016
(there were some mistakes in the previous code, I've corrected it and I'm reposting)
I'm trying to simulate 1-step ahead returns 5000 times for a portfolio of 9 variables. After specifying their GJR-GARCH specifications with SPD marginals, I use the following code (which was inspired by the examples in the 'rmgarch.tests' folder). I use the first 571x9 observations for fitting and the last 570 for out-of-sample analysis. Moreover, I use a moving-window approach, in the sense that I always use the last 571 observations in the cgarchfilter rather than [1:(T+i)] as in the example. Once the simulated returns 'simx' are stored in 'simulatedreturns', I compute the return of an equally-weighted portfolio as a 570x5000 matrix, i.e. one series of return of each observation for each simulation. Last, I compute VaR (90%, 95%, 99%) as the respective quantile (0.10, 0.05, 0.01).
My problem is that, contrary to the literature I've found on the topic, VaR is severely underestimated, I obtain a number of violations which is way more than expected (should be 57 for 90%VaR, 28 for 95%VaR, 6 for 99%VaR). Is there any error in the code I have below? I've followed the example by the letter and cannot understand what went wrong. Anyone can help? Any hint is GREATLY appreciated!
library(rugarch)
library(rmgarch)
data(dji30retw)
Dat = dji30retw[, 1:9, drop = FALSE]
model <- ugarchspec(variance.model=list(model="gjrGARCH", garchOrder=c(1,1), variance.targeting=TRUE),
mean.model=list(armaOrder=c(0,0), include.mean=F, archm=F, archpow=1),
distribution.model="norm")
spec = cgarchspec(uspec = multispec(replicate(9, model)), VAR = FALSE, robust = FALSE,
lag.criterion = "AIC", external.regressors = NULL,
dccOrder = c(1,1), asymmetric = TRUE, distribution.model = list(copula = "mvt",
method = "ML", time.varying = TRUE,
transformation = "spd"))
fit = cgarchfit(spec, data = Dat, out.sample = 570, cluster = NULL, spd.control = list(lower = 0.1, upper = 0.9, type = "mle", kernel = "normal"),
fit.control = list(eval.se=FALSE))
T = dim(Dat)[1]-570
simMu = simS = filtMu = filtS = matrix(NA, ncol = 9, nrow = 570)
simCor = simC = filtC = filtCor = array(NA, dim = c(9,9,570))
colSd = function(x) apply(x, 2, "sd")
specx = spec
for(i in 1:9) specx at umodel$fixed.pars[[i]] = as.list(fit at model$mpars[fit at model$midx[,i]==1,i])
setfixed(specx)<-as.list(fit at model$mpars[fit at model$midx[,10]==1,10])
simulatedreturns <- array(dim=c(570,9,5000))
for(i in 1:570){
if(i==1){
presigma = matrix(tail(sigma(fit), 1), ncol = 9)
prereturns = matrix(unlist(Dat[T, ]), ncol = 9, nrow = 1)
preresiduals = matrix(tail(residuals(fit),1), ncol = 9, nrow = 1)
preR = last(rcor(fit))[,,1]
diag(preR) = 1
preQ = fit at mfit$Qt[[length(fit at mfit$Qt)]]
preZ = tail(fit at mfit$Z, 1)
tmp = cgarchfilter(specx, Dat[2:(T+1), ], filter.control = list(n.old = T))
filtMu[i,] = tail(fitted(tmp), 1)
filtS[i,] = tail(sigma(tmp), 1)
filtC[,,i] = last(rcov(tmp))[,,1]
filtCor[,,i] = last(rcor(tmp))[,,1]
} else{
presigma = matrix(tail(sigma(tmp), 1), ncol = 9)
prereturns = matrix(unlist(Dat[(T+i-1), ]), ncol = 9, nrow = 1)
preresiduals = matrix(tail(residuals(tmp),1), ncol = 9, nrow = 1)
preR = last(rcor(tmp))[,,1]
diag(preR) = 1
preQ = tmp at mfilter$Qt[[length(tmp at mfilter$Qt)]]
preZ = tail(tmp at mfilter$Z, 1)
tmp = cgarchfilter(specx, Dat[(i+1):(T+i), ], filter.control = list(n.old = T))
filtMu[i,] = tail(fitted(tmp), 1)
filtS[i,] = tail(sigma(tmp), 1)
filtC[,,i] = last(rcov(tmp))[,,1]
filtCor[,,i] = last(rcor(tmp))[,,1]
}
sim = cgarchsim(fit, n.sim = 1, m.sim = 5000, startMethod = "sample", preR = preR, preQ = preQ, preZ = preZ,
prereturns = prereturns, presigma = presigma, preresiduals = preresiduals, cluster = NULL)
simx = t(sapply(sim at msim$simX, FUN = function(x) x[1,]))
simMu[i,] = colMeans(simx)
simC[,,i] = sim at msim$simH[[1]][,,1]
simCor[,,i] = sim at msim$simR[[1]][,,1]
simS[i,] = sqrt(diag(simC[,,i]))
simulatedreturns[i,,]=simx
}
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list