%% Compute Information Criteria Using aicbic
% This example shows how to use aicbic to compute information criteria for several competing GARCH models fit to simulated data. Although this example uses aicbic, some Statistics and Machine Learning Toolbox™ and Econometrics Toolbox™ model fitting functions also return information criteria in their estimation summaries.
%
%% Simulate Data
% Simulate a random path of length 50 from the ARCH(1) data generating process (DGP)
% \begin{array}{*{20}{rcl}}
% {y_t} &=& {\varepsilon _t}\\
% \varepsilon _t^2 &=& 0.5 + 0.1\varepsilon _{t - 1}^2,
% \end{array}
% where \varepsilon_t is a random Gaussian series of innovations.

rng(1)  % For reproducibility
DGP = garch('ARCH',{0.1},'Constant',0.5);
T = 50;
y = simulate(DGP,T);

plot(y)
ylabel('Innovation')
xlabel('Time')

%
%% Create Competing Models
% Assume that the DGP is unknown, and that the ARCH(1), GARCH(1,1), ARCH(2), and GARCH(1,2) models are appropriate for describing the DGP.
% For each competing model, create a garch model template for estimation.

Mdl(1) = garch(0,1);
Mdl(2) = garch(1,1);
Mdl(3) = garch(0,2);
Mdl(4) = garch(1,2);

%
%% Estimate Models
% Fit each model to the simulated data y, compute the loglikelihood, and suppress the estimation display.

numMdl = numel(Mdl);
logL = zeros(numMdl,1);      % Preallocate
numParam = zeros(numMdl,1);
for j = 1:numMdl
    [EstMdl,~,logL(j)] = estimate(Mdl(j),y,'Display','off');
    results = summarize(EstMdl);
    numParam(j) = results.NumEstimatedParameters;
end

%
%% Compute and Compare Information Criteria
% For each model, compute all available information criteria. Normalize the results by the sample size T.

[~,~,ic] = aicbic(logL,numParam,T,'Normalize',true)

% ic is a 1-D structure array with a field for each information criterion.  Each field contains a vector of measurements; element j corresponds to the model yielding loglikelihood logL(j).
% For each criterion, determine the model that yields the minimum value.

[~,minIdx] = structfun(@min,ic);
[Mdl(minIdx).Description]'

% The model that minimizes all criteria is the ARCH(1) model, which has the same structure as the DGP.
% Copyright 2020 The MathWorks, Inc.