*
* @CVStabTest  start  end
* # list of series
*
* Performs the special case of the test in Jukka Nyblom, "Testing for Constancy of
* Parameters Over Time", JASA 1989, vol 84, pp 223-230 for the case of a complete
* covariance matrix. Note that while this should have the correct asymptotic
* distribution under the null (under fairly typical assumptions in addition to
* stability), the argument for it being UMP against an alternative of martingale
* behavior won't hold because the covariance matrix is constrained to be p.d.
*
* Parameters:
*   start  end     Range of data to analyze. By default, the defined range of
*                  the listed series.
*
* This produces a test statistic which will have the asymptotic distribution shown
* in Table 2 in Nyblom or using the %nyblomtest function.
*
* Revision Schedule:
*  02/2005 Written by Tom Doan, Estima
*  02/2006 Updated to use %NYBLOMTEST function for p-value
*
procedure cvstabtest start end
type integer start end
*
local vect[int] ulist
local vect vvec ut fsig
local int vdim
local symm v sigsig sigsiginv uu
local integer startl endl time
local real sstat
local equation temp
*
enter(varying) ulist
equation temp *
# ulist
inquire(reglist) startl<<start endl<<end
# ulist
vcv(noprint,matrix=v) startl endl
# ulist
*
compute vvec=%vec(v)
compute vdim=%rows(vvec)
compute sigsig=%zeros(vdim,vdim)
do time=startl,endl
   compute ut=%eqnxvector(temp,time)
   compute uu=%outerxx(ut)
   compute sigsig=sigsig+%outerxx(%vec(uu)-vvec)
end do time
compute sigsig=sigsig/%nobs
compute sigsiginv=inv(sigsig)
*
compute fsig=%zeros(vdim,1)
compute sstat=0.0
do time=startl,endl
   compute ut=%eqnxvector(temp,time)
   compute uu=%outerxx(ut)
   compute fsig=fsig+(%vec(uu)-vvec)
   compute sstat=sstat+%qform(sigsiginv,fsig)
end do time
compute sstat=sstat/(%nobs**2)
*
disp "Test for Stability of Covariance Matrix"
disp "Fluctuation Statistic" @25 *.### sstat
disp "Degrees of Freedom"    @25 vdim
disp "Approximate P-Value"   @25 *.### %nyblomtest(sstat,vdim)
end

