%% Information Criteria Statistics for Simulated Data
% Compare information criteria statistics for several model fits.
% Specify the model
% {y_t} =  - 4 + 0.2{y_{t - 1}} + 0.5{y_{t - 2}} + {\varepsilon
% _t},
% where \varepsilon_{t} is Gaussian with mean 0 and variance 2. Simulate data from this model.

rng(1);  % For random data reproducibility
T = 100; % Sample size
DGP = arima('Constant',-4,'AR',[0.2, 0.5], ...
    'Variance',2);
y = simulate(DGP,T);

%
% Define three competing models to fit to the data.

EstMdl1 = arima('ARLags',1);
EstMdl2 = arima('ARLags',1:2);
EstMdl3 = arima('ARLags',1:3);

%
% Fit the models to the data.

logL = zeros(3,1); % Preallocate loglikelihood vector
[~,~,logL(1)] = estimate(EstMdl1,y,'Display','off');
[~,~,logL(2)] = estimate(EstMdl2,y,'Display','off');
[~,~,logL(3)] = estimate(EstMdl3,y,'Display','off');

%
% Compute the AIC and BIC for each model.

[aic,bic] = aicbic(logL, [3; 4; 5], T*ones(3,1))

% The model containing two autoregressive lag parameters fits best since it yields the lowest information criteria. The structure of the best fitting model matches the model structure that simulated the data.
% Copyright 2017 The MathWorks, Inc.