%% Assess Conditional Heteroscedasticity Using the Wald Test
% Test whether there are significant ARCH effects in a simulated response series using waldtest.
% Suppose that the model for the simulated data is AR(1) with an ARCH(1) variance. Symbolically, the model is
% 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.
% Specify the model for the simulated data.

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 Jacobian
[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 is
% y_t = c + \phi_1y_{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,'Variance',UVarMdl);
[UEstMdl,UEstParamCov] = estimate(UMdl,y(esI),'Y0',y(psI),...
    'E0',epsilon(psI),'V0',condVariance(psI),'Display','off');

% UEstMdl is the fitted, unrestricted model, and UEstParamCov is the estimated parameter covariance of the unrestricted model parameters.
%
% The null hypothesis is that \alpha_1 = 0, i.e., the restricted model is AR(1) with Gaussian innovations that have mean 0 and constant variance. Therefore, the restriction function is r(\theta)=\alpha_1, where \theta = [c,\phi_1,\alpha_0,\alpha_1]^\prime. The components of the Wald test are:
% The restriction function evaluated at the unrestricted parameter estimates is r = \hat\alpha_1.
% The Jacobian of r evaluated at the unrestricted model parameters is R = \left[\matrix{0&0&0&1}\right].
% The unrestricted model estimated parameter covariance matrix is UEstParamCov.
% Specify r and R.

r = UEstMdl.Variance.ARCH{1};
R = [0, 0, 0, 1];

%
% Test the null hypothesis that \alpha_1 = 0 at the 1% significance level using waldtest.

[h,pValue,stat,cValue] = waldtest(r,R,UEstParamCov,0.01)

% h = 0 indicates that the null, restricted model should not be rejected in favor of the alternative, unrestricted model. This result is consistent with the model for the simulated data.
% Copyright 2012 The MathWorks, Inc.