[R-SIG-Finance] dynamic copula using rmgarch package (ignore previous question, don't know how to delete it)

Sachin Kuruvithadam sacios at hotmail.it
Thu Jun 23 23:07:39 CEST 2016

Hi Robert, thank you for your reply (I apologize in advance if this is not the proper way to respond in the thread, I'm very new at this).

i) Portfolio return is computed as equal-weighted sum of the nine series. Since in the examples in the 'rmgarchtests' folder these are directly used in the GARCH-GPD-copula specification, I assume these are log returns, and therefore transform them into simple daily returns since log returns are time-additive but not "portfolio"-additive;

ii) In 'simulatedreturns' I have 570 predicted returns for 9 series simulated 5000 times. Like before, I first transform them in simple returns;

iii) the simulated portfolio returns are then computed as before, as equal-weighted sum since simple returns are additive across assets;

iv) for VaR estimation I don't using anything fancy, just the 'quantile' function. For each observation, I estimate the respective simulated quantile and subtract the actual return from it, and store the difference in vector 'a'. If 'a' is negative (meaning VaR is greater in absolute terms than returns) then 'b' equals 0, if 'a' is positive then 'b'=1.

Since I haven't fixed the random number seed, it is possible that you obtain slightly different results. But the number of violations should be the approximately the same as mine, and nowhere close to the number expected.

This is the rest of the code, with comments reported above.

You have an idea what could have gone wrong?

#comment i)
#from log to simple returns
Simple = exp(Dat)-1

#return of equally weighted portfolio as weighted sum
portfolioreturn <- (1/9)*rowSums(Simple)
#since I'm only interested in returns in the testing period, I only take the last 570 observations
portfolioreturn <- portfolioreturn[572:1141] #we're only interested in returns in the testing period

#comment ii)
#transform simulated log returns into simple returns

#comment iii)
#return series of simulated portfolio (for each observation from 1 to 570 I have 5000 portfolio returns)
portfolio <- matrix(nrow=570,ncol=5000)
for (i in 1:570){
  for (j in 1:5000){

#comment iv)
#estimation of 90%, 95% and 99% VaR as respective 10%, 5% and 1% quantiles
#a is a vector containing the difference between quantile and actual loss
#b is a vector equal to 0 when a is negative (as it should) and 1 when loss is greater than VaR (thus when a is positive)
#number of violations is then computed as sum(b), summing all ones;
a90 <- c()
for (i in 1:570){
b90 <- c()
for (i in 1:570){
  if (a90[i]>0){b90[i]=1}
  else {b90[i]=0}
sum(b90)#number of violations=171 (expected: 570*0.1=57)

a95 <- c()
for (i in 1:570){
b95 <- c()
for (i in 1:570){
  if (a95[i]>0){b95[i]=1}
  else {b95[i]=0}
sum(b95)#number of violations=127 (expected: 570*0.05=29)

a99 <- c()
for (i in 1:570){
b99 <- c()
for (i in 1:570){
  if (a99[i]>0){b99[i]=1}
  else {b99[i]=0}
sum(b99) #number of violations=69 (expected: 570*0.01=6)

Da: Robert Iquiapaza <rbali at ufmg.br>
Inviato: giovedì 23 giugno 2016 21.02
A: Sachin Kuruvithadam
Cc: r-sig-finance at r-project.org
Oggetto: Re: [R-SIG-Finance] dynamic copula using rmgarch package (ignore previous question, don't know how to delete it)

Could you show how are you computing the return of an equally-weighted portfolio and the VaR?

2016-06-22 16:36 GMT-03:00 Sachin Kuruvithadam <sacios at hotmail.it<mailto:sacios at hotmail.it>>:
(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 i[[elided Hotmail spam]]


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),
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<http://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){
    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]))

        [[alternative HTML version deleted]]

R-SIG-Finance at r-project.org<mailto:R-SIG-Finance at r-project.org> mailing list
-- 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.

	[[alternative HTML version deleted]]

More information about the R-SIG-Finance mailing list