%% Assess Conditional Heteroscedasticity Using the Likelihood Ratio Test
% Test whether there are significant ARCH effects in a simulated response series using lratiotest. The parameter values in this example are arbitrary.
% Specify the AR(1) model with an ARCH(1) variance:
% y_t = 0.9y_{t-1} + \varepsilon_t,
% where
% \varepsilon_t = w_t\sqrt{h_t}.
% h_t = 1 + 0.5\varepsilon_{t-1}^2.
% w_t is Gaussian with mean 0 and variance 1.

VarMdl = garch('ARCH',0.5,'Constant',1);
Mdl = arima('Constant',0,'Variance',VarMdl,'AR',0.9);

% Mdl is a fully specified AR(1) model with an ARCH(1) variance.
%
% Simulate presample and effective sample responses from Mdl.

T = 100;
rng(1);  % For reproducibility
n = 2;   % Number of presample observations required for the gradient
[y,epsilon,condVariance] = simulate(Mdl,T + n);

psI = 1:n;             % Presample indices
esI = (n + 1):(T + n); % Estimation sample indices

% epsilon is the random path of innovations from VarMdl. The software filters epsilon through Mdl to yield the random response path y.
%
% Specify the unrestricted model assuming that the conditional mean model constant is 0:
% y_t = \phi_1 y_{t-1} + \varepsilon_t,
% where h_t = \alpha_0 + \alpha_1\varepsilon_{t-1}^2. Fit the simulated data (y) to the unrestricted model using the presample observations.

UVarMdl = garch(0,1);
UMdl = arima('ARLags',1,'Constant',0,'Variance',UVarMdl);
[~,~,uLogL] = estimate(UMdl,y(esI),'Y0',y(psI),'E0',epsilon(psI),...
    'V0',condVariance(psI),'Display','off');

% uLogL is the maximum value of the unrestricted loglikelihood function.
%
% Specify the restricted model assuming that the conditional mean model constant is 0:
% y_t = \phi_1 y_{t-1} + \varepsilon_t,
% where h_t = \alpha_0. Fit the simulated data (y) to the restricted model using the presample observations.

RVarMdl = garch(0,1);
RVarMdl.ARCH{1} = 0;
RMdl = arima('ARLags',1,'Constant',0,'Variance',RVarMdl);
[~,~,rLogL] = estimate(RMdl,y(esI),'Y0',y(psI),'E0',epsilon(psI),...
    'V0',condVariance(psI),'Display','off');

% The structure of RMdl is the same as UMdl. However, every parameter is unknown, except for the restriction. These are equality constraints during estimation. You can interpret RMdl as an AR(1) model with the Gaussian innovations that have mean 0 and constant variance.
%
% Test the null hypothesis that \alpha_1 = 0 at the default 5% significance level using lratoitest.

dof = (UMdl.P + UMdl.Q + UVarMdl.P + UVarMdl.Q) ...
    - (RMdl.P + RMdl.Q + RVarMdl.P + RVarMdl.Q);
[h,pValue,stat,cValue] = lratiotest(uLogL,rLogL,dof)

% h = 1 indicates that the null, restricted model should be rejected in favor of the alternative, unrestricted model.  pValue is close to 0, suggesting that there is strong evidence for the rejection. stat is the value of the chi-square test statistic, and cValue is the critical value for the test.
% Copyright 2012 The MathWorks, Inc.