3
$\begingroup$

anyone who could help me interpreting and modifying this code?

I have a dataset and want to reserve the last 100 returns for out-of-sample analysis. After specifying and fitting the garch-spd-copula, in the for loop I fix a condition for $i=1$ and another for $i=2,...,100$, and compute these first calculations (presigma, preR, preQ, ...). Now, I want R to do the 1-step ahead forecast simulated 1000 times (n.sim=1 and m.sim=1000 in cgarchsim command) while incrementally consider the "extended" dataset (consider T for forecasting T+1, consider T+1 for forecasting T+2, ...). I would like the end result to be a 100x3x1000 array (100: observations, 3: assets, 1000: simulations) of 1-step ahead forecasts.

install.packages("rmgarch")
library(rmgarch)

data(dji30retw)
Dat = dji30retw[, 1:3, drop = FALSE]

uspec17 = ugarchspec(mean.model = list(armaOrder = c(0,0)), 
             variance.model = list(garchOrder = c(1,1), model = "sGARCH", variance.targeting=FALSE), 
             distribution.model = "norm")
spec17 = cgarchspec(uspec = multispec( replicate(3, uspec17) ), asymmetric = FALSE, 
             distribution.model = list(copula = "mvnorm", method = "Kendall", 
                                       time.varying = TRUE, transformation = "spd"))
fit17 <- cgarchfit(spec17, Dat, out.sample=100, spd.control=list(upper=0.95, lower=0.05, type="mle", kernel="normal"),
         cluster=NULL, fit.control=list(eval.se=FALSE))

T = dim(Dat)[1]-100
simMu = simS = filtMu = filtS = matrix(NA, ncol = 3, nrow = 100)
simCor = simC = filtC = filtCor = array(NA, dim = c(3,3,100))
colSd = function(x) apply(x, 2, "sd")
specx17 = spec17
for(i in 1:3) specx17@umodel$fixed.pars[[i]] = as.list(fit17@model$mpars[fit17@model$midx[,i]==1,i])
setfixed(specx17)<-as.list(fit17@model$mpars[fit17@model$midx[,4]==1,4])

for(i in 1:100){
 if(i==1){
 presigma = matrix(tail(sigma(fit17), 1), ncol = 3)
 prereturns = matrix(unlist(Dat[T, ]), ncol = 3, nrow = 1)
 preresiduals = matrix(tail(residuals(fit17),1), ncol = 3, nrow = 1)
 preR = last(rcor(fit17))[,,1]
 diag(preR) = 1
 preQ = fit17@mfit$Qt[[length(fit17@mfit$Qt)]]
 preZ = tail(fit17@mfit$Z, 1)
 tmp = cgarchfilter(specx17, Dat[1:(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 = 3)
 prereturns = matrix(unlist(Dat[(T+i-1), ]), ncol = 3, nrow = 1)
 preresiduals = matrix(tail(residuals(tmp),1), ncol = 3, nrow = 1)
 preR = last(rcor(tmp))[,,1]
 diag(preR) = 1
 preQ = tmp@mfilter$Qt[[length(tmp@mfilter$Qt)]]
 preZ = tail(tmp@mfilter$Z, 1) 
 tmp = cgarchfilter(specx17, Dat[1:(T+i-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]
 }
 sim17 = cgarchsim(fit17, n.sim = 1, m.sim = 1000, startMethod = "sample", preR = preR, preQ = preQ, preZ = preZ,
                prereturns = prereturns, presigma = presigma, preresiduals = preresiduals, cluster = NULL)
 simx = t(sapply(sim17@msim$simX, FUN = function(x) x[1,]))
 simMu[i,] = colMeans(simx)
 simC[,,i] = sim17@msim$simH[[1]][,,1]
 simCor[,,i] = sim17@msim$simR[[1]][,,1]
 simS[i,] = sqrt(diag(simC[,,i]))
}

However, if I run this, as conditional returns (sim17@msim$simX) I obtain three 1-step ahead forecasts simulated 1000 times, no trace of the remaining 99 values to be estimated. Any idea why, and how to modify the code in order to have 100 estimates?

$\endgroup$

1 Answer 1

1
$\begingroup$

In the line:

sim17 = cgarchsim(fit17, n.sim = 1, m.sim = 1000, startMethod = "sample", preR = preR, preQ = preQ, preZ = preZ,
            prereturns = prereturns, presigma = presigma, preresiduals = preresiduals, cluster = NULL)

, you should add n.start=0:

sim17 = cgarchsim(fit17, n.sim = 1, n.start=0, m.sim = 1000, startMethod = "sample", preR = preR, preQ = preQ, preZ = preZ,
            prereturns = prereturns, presigma = presigma, preresiduals = preresiduals, cluster = NULL)

It should work fine then.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.