%% Extract Estimation Results from Estimated Model
% Estimate several models by passing an EGARCH model template and data to estimate. Vary the number of ARCH and GARCH lags among the models. Extract the AIC from the estimation results, and choose the model that minimizes the fit statistic.
% Simulate data from an EGARCH(0,1) model with known parameter values.

Mdl0 = egarch('Constant',0.01,'ARCH',0.75,'Leverage',-0.1);
rng(2); % For reproducibility
[~,Y] = simulate(Mdl0,100);

%
% To determine the number of ARCH and GARCH lags, create and estimate multiple EGARCH models.  Vary the number of GARCH and ARCH lags (p and q, respectively) among the models from 0 to 1 lag. Exclude the case where p = 1 and q = 0 because the presence of GARCH lags requires the presence of ARCH lags.  Suppress all estimation displays.  Extract the AIC from the estimation results structure.  The field AIC stores the AIC.

pq = [0 0; 0 1; 1 1];
AIC = zeros(size(pq,1),1); % Preallocation

for j = 1:size(pq,1)
    Mdl = egarch(pq(j,1),pq(j,2));
    EstMdl = estimate(Mdl,Y,'Display','off');
    results = summarize(EstMdl);
    AIC(j) = results.AIC;
end

%
% Compare the AIC values among the models.

[minAIC,bestidx] = min(AIC,[],1);
bestPQ = pq(bestidx,:)

% The best fitting model is the EGARCH(0,1) model because its corresponding AIC is the lowest.  This model also has the structure of the model used to simulate the data.
% Copyright 2017 The MathWorks, Inc.
%