%% Compare Models Using AIC and BIC
% Compare the in-sample fits of three competing models using the AIC and BIC. Their loglikelihood values logL and corresponding number of estimated parameters numParam are in the following table. Suppose the effective sample size is 1500.

logL = [-681.4724; -663.4615; -632.3158];
numParam = [12; 18; 27];
numObs = 1500;
Tbl = table(logL,numParam,'RowNames',"Model"+string(1:3))

%
%% Compute AIC
% Calculate the AIC of each estimated model.

aic = aicbic(logL,numParam)

% The model with the lowest AIC has the best in-sample fit. Identify the model with the lowest AIC.

[~,idxmin] = min(aic);
bestFitAIC = Tbl.Properties.RowNames{idxmin}

% The AIC suggests that Model3 has the best, most parsimonious fit, despite being the most complex of the three models.
%
%% Compute BIC
% Calculate the BIC of each estimated model. Specify the sample size numObs, which is required for computing the BIC.

[~,bic] = aicbic(logL,numParam,numObs)

% As is the case with the AIC, the model with the lowest BIC has the best in-sample fit. Identify the model with the lowest BIC.

[~,idxmin] = min(bic);
bestFitBIC = Tbl.Properties.RowNames{idxmin}

% The BIC suggests Model1, the simplest of the three models. The results show that when the sample size is large, the BIC imposes a greater penalty on complex models than the AIC.
% Copyright 2020 The MathWorks, Inc.