%% Conduct Wald Test
% This example shows how to calculate the required inputs for conducting a Wald test with waldtest. The Wald test compares the fit of a restricted model against an unrestricted model by testing whether the restriction function, evaluated at the unrestricted maximum likelihood estimates (MLEs), is significantly different from zero.
% The required inputs for waldtest are a restriction function, the Jacobian of the restriction function evaluated at the unrestricted MLEs, and an estimate of the variance-covariance matrix evaluated at the unrestricted MLEs. This example compares the fit of an AR(1) model against an AR(2) model.
%% Compute Unrestricted MLE
% Obtain the unrestricted MLEs by fitting an AR(2) model (with a Gaussian innovation distribution) to the given data. Assume you have presample observations (y_{-1},y_0) = (9.6249,9.6396)

Y = [10.1591; 10.1675; 10.1957; 10.6558; 10.2243; 10.4429;
     10.5965; 10.3848; 10.3972;  9.9478;  9.6402;  9.7761;
     10.0357; 10.8202; 10.3668; 10.3980; 10.2892;  9.6310;
      9.6318;  9.1378;  9.6318;  9.1378];
Y0 = [9.6249; 9.6396];

Mdl = arima(2,0,0);
[EstMdl,V] = estimate(Mdl,Y,'Y0',Y0);

% When conducting a Wald test, only the unrestricted model needs to be fit. estimate returns the estimated variance-covariance matrix as an optional output.
%
%% Compute Jacobian Matrix
% Define the restriction function, and calculate its Jacobian matrix.
% For comparing an AR(1) model to an AR(2) model, the restriction function is
% r(c,\phi_1,\phi_2,\sigma_\varepsilon^2) = \phi_2 - 0 = 0.
% The Jacobian of the restriction function is
% \left[\matrix{\frac{\partial r}{\partial c} & \frac{\partial r}{\partial \phi_1} & \frac{\partial r}{\partial \phi_2} & \frac{\partial r}{\partial \sigma_\varepsilon^2}}\right] = \left[\matrix{0 &0 &1 &0}\right]
% Evaluate the restriction function and Jacobian at the unrestricted MLEs.

r = EstMdl.AR{2};
R = [0 0 1 0];

%
%% Conduct Wald Test
% Conduct a Wald test to compare the restricted AR(1) model against the unrestricted AR(2) model.

[h,p,Wstat,crit] = waldtest(r,R,V)

% The restricted AR(1) model is not rejected in favor of the AR(2) model (h = 0).
% Copyright 2015 The MathWorks, Inc.