%% Estimate Conditional Mean and Variance Model
% This example shows how to estimate a composite conditional mean and variance model using docid:econ_ug#bs607ka.
%% Load and Preprocess Data
% Load the NASDAQ data included with Econometrics Toolbox™. Convert the daily close composite index series to a return series. For numerical stability, convert the returns to percentage returns.

load Data_EquityIdx
nasdaq = DataTable.NASDAQ;
r = 100*price2ret(nasdaq);
T = length(r);

%
%% Create Model Template
% Create an AR(1) and GARCH(1,1) composite model, which has the form
% {r_t} = c + {\phi _1}{r_{t - 1}} + {\varepsilon _t},
% \sigma _t^2 = \kappa  + {\gamma _1}\sigma _{t - 1}^2 + {\alpha _1}\varepsilon _{t - 1}^2,
% where {\varepsilon _t} = {\sigma _t}{z_t} and z_t is an iid standardized Gaussian process.

VarMdl = garch(1,1)
Mdl = arima('ARLags',1,'Variance',VarMdl)

% Mdl is an arima model template for estimation. NaN-valued properties of Mdl and VarMdl correspond to unknown, estimable coefficients and variance parameters of the composite model.
%
%% Estimate Model Parameters
% Fit the model to the return series r by using estimate.

EstMdl = estimate(Mdl,r);

% EstMdl is a fully specified arima model.
% The estimation display shows the five estimated parameters and their corresponding standard errors (the AR(1) conditional mean model has two parameters, and the GARCH(1,1) conditional variance model has three parameters).
% The fitted model (EstMdl) is
% {r_t} = 0.073 + 0.138{r_{t - 1}} + {\varepsilon _t},
% \sigma _t^2 = 0.022 + 0.873\sigma _{t - 1}^2 + 0.119\varepsilon _{t - 1}^2.
% All t statistics are greater than 2, which suggests that all parameters are statistically significant.
% Dynamic models require presample observations, with which to initialize the model. If you do not specify presample observations, estimate generates them by default.
%
%% Infer Conditional Variances and Residuals
% Infer and plot the conditional variances and standardized residuals. Output the loglikelihood objective function value.

[res,v,logL] = infer(EstMdl,r);

figure
subplot(2,1,1)
plot(v)
xlim([0,T])
title('Conditional Variances')

subplot(2,1,2)
plot(res./sqrt(v))
xlim([0,T])
title('Standardized Residuals')

% The conditional variances increase after observation 2000. This result corresponds to the increased volatility seen in the original return series.
% The standardized residuals have more large values (larger than 2 or 3 in absolute value) than expected under a standard normal distribution. This result suggests a Student's t distribution can be more appropriate for the innovation distribution.
%
%% Fit Model With t Innovation Distribution
% Create a model template from Mdl, and specify that its innovations have a Student's t distribution.

MdlT = Mdl;
MdlT.Distribution = 't';

% MdlT has one additional parameter estimate: the t distribution degrees of freedom.
% Fit the new model to the NASDAQ return series. Specify an initial value for the variance model constant term.

Variance0 = {'Constant0',0.001};
EstMdlT = estimate(MdlT,r,'Variance0',Variance0);

% The coefficient estimates between EstMdl and EstMdlT are slightly different. The degrees of freedom estimate is relatively small (about 8), which indicates a significant departure from normality.
%
%% Compare Model Fits
% Summarize the estimated models.  From the summaries, obtain the number of estimated parameters and the loglikelihood objective function value from the second fit.

Summary = summarize(EstMdl);
SummaryT = summarize(EstMdlT);

numparams = Summary.NumEstimatedParameters;
numparamsT = SummaryT.NumEstimatedParameters;
logLT = SummaryT.LogLikelihood;

% Compare the two model fits (Gaussian and t innovation distribution) using the Akaike information criterion (AIC) and Bayesian information criterion (BIC).

[numparams numparamsT]
[aic,bic] = aicbic([logL logLT],[numparams numparamsT],T)

% The first model has six fitted parameters, whereas the second model has six (because it contains the t distribution degrees of freedom). Despite this difference, both information criteria favor the model with the Student's t innovation distribution because it yields smaller AIC and BIC values than the model with Gaussian innovations.
% Copyright 2020 The MathWorks, Inc.