*
* @CVStabTest( options )  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.
*
* Options:
*  [PRINT]/NOPRINT
*  TITLE=title for report ["Test of Stability of Covariance Matrix"]
*
* This produces a test statistic which will have the asymptotic
* distribution shown in Table 2 in Nyblom or using the %nyblomtest
* function.
*
* Variables Defined:
*   %CDSTAT   test statistic
*   %SIGNIF   asymptotic p-value
*
* Revision Schedule:
*  02/2005 Written by Tom Doan, Estima
*  02/2006 Updated to use %NYBLOMTEST function for p-value
*  06/2010 Updated to use LOCAL REPORT, add PRINT and TITLE options, define
*          %CDSTAT and %SIGNIF
*
procedure cvstabtest start end
type integer start end
*
option string  title
option switch		print     	1
*
local vect[int] 	ulist
local vect     vvec ut fsig
local integer  vdim
local symm     v sigsig sigsiginv uu
local integer  startl endl time
local real     sstat
local equation temp
local string   ltitle
local report   sreport
*
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)
compute %cdstat=sstat
compute %signif=%nyblomtest(sstat,vdim)
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="Test for Stability of Covariance Matrix"
*
report(use=sreport,action=define,title=ltitle)
report(use=sreport,atrow=1,atcol=1,span) ltitle
report(use=sreport,atrow=2,atcol=1) "Fluctuation Statistic" sstat
report(use=sreport,atrow=3,atcol=1) "Degrees of Freedom"    vdim
report(use=sreport,atrow=4,atcol=1) "Approximate P-Value"   %signif
report(use=sreport,action=format,picture="*.###",atcol=2,align=decimal)
if print
   report(use=sreport,action=show)
end
