%% Normalize Information Criteria
% Fit several models to simulated data, specify a presample for estimation, and then compare the model fits using normalized AIC.
% Simulate a random path of length 50 from the DGP
% y_t=1+0.2y_{t-1}-0.4y_{t-2}+\varepsilon_t,
% where \varepsilon_t is a random Gaussian series with mean 0 and variance 1.

rng(1)  % For reproducibility
T = 50;
DGP = arima('Constant',1,'AR',[0.2,-0.4],'Variance',1);
y = simulate(DGP,T);

% Create an arima model template for each competing model.

Mdl(1) = arima(1,0,0);
Mdl(2) = arima(2,0,0);
Mdl(3) = arima(3,0,0);

% Fit each model to the simulated data y, and specify the required number of presample observations for each fit. Compute the loglikelihood, and suppress the estimation display.

numMdl = numel(Mdl);
logL = zeros(numMdl,1);      % Preallocate
numParam = zeros(numMdl,1);
numObs = zeros(numMdl,1);

for j = 1:numMdl
    y0 = y(1:Mdl(j).P);             % Presample
    yest = y((Mdl(j).P+1):end);     % Estimation sample
    [EstMdl,~,logL(j)] = estimate(Mdl(j),yest,'Y0',y0,...
        'Display','off');
    results = summarize(EstMdl);
    numParam(j) = results.NumEstimatedParameters;
    numObs(j) = results.SampleSize; 
end
    

% For each model, compute the normalized AIC.

aic = aicbic(logL,numParam,numObs,'Normalize',true)

% Determine the model that yields the minimum AIC.

[~,minIdx] = min(aic);
Mdl(minIdx).Description

% Copyright 2020 The MathWorks, Inc.