I have been trying to measure VaR using rolling window and estimate VaR using GARCH.
To do so I have implemented below loop. However, I am uncertain below method is correctly impplemented. Due to my lack of knoweldge in Matlab.
Would someone please guide me through this?
for t = startIdx:numReturns
% Get the window of returns
windowReturns = returns((t - rollingWindowSize):(t - 1));
% Calculate the parametric VaR using the normal distribution
windowMean = mean(windowReturns);
windowStd = std(windowReturns);
parametricVaR(t) = windowMean - zScore * windowStd;
% Calculate the parametric VaR using the t-distribution
pd = fitdist(windowReturns, 'tLocationScale');
tQuantile = tinv(confidenceLevel, pd.nu);
parametricTVaR(t) = windowMean - tQuantile * windowStd; % Negative sign for loss
% Fit the GARCH(1,1) model and forecast the next period's variance
garchModel = garch('GARCHLags',1,'ARCHLags',1);
[estGarchModel,~,~,~] = estimate(garchModel, windowReturns, 'Display', 'off', 'Options', options);
[condVar, ~] = infer(estGarchModel, windowReturns);
garchVaR(t) = windowMean - zScore * sqrt(condVar(end));
% Fit the GARCH(1,1) with tdistribution model and forecast the next period's variance
garchModelt = garch('GARCHLags',1,'ARCHLags',1,'Distribution','t');
[estGarchModelt,~,~,~] = estimate(garchModelt, windowReturns, 'Display', 'off', 'Options', options);
[condVargarcht, ~] = infer(estGarchModelt, windowReturns);
dofgarcht = estGarchModelt.Distribution.DoF;
tQuantilegarch = tinv(confidenceLevel, dofgarcht);
garchTVaR(t) = windowMean - tQuantilegarch * sqrt(condVargarcht(end));
% Fit the EGARCH(1,1) model and forecast the next period's variance
egarchModel = egarch('GARCHLags',1,'ARCHLags',1,'Distribution','t');
[estEgarchModel,~,~,~] = estimate(egarchModel, windowReturns, 'Display', 'off', 'Options', options);
[condVarEgarch, ~] = infer(estEgarchModel, windowReturns);
dofEgarch = estEgarchModel.Distribution.DoF;
tQuantileEgarch = tinv(confidenceLevel, dofEgarch);
egarchTVaR(t) = windowMean - tQuantileEgarch * sqrt(condVarEgarch(end));
end