*
* @TseCCTest( options )
*
* Tse LM test for constant correlation. This must follow estimation of a
* constant correlation model. In estimating that model, you need to save
* the following (which are required inputs into this procedure):
*
* 1. The residuals. Save with the RVECTOR option on GARCH; input using
* the RVECTOR option on the procedure.
*
* 2. The covariance matrices. Save with the HMAT option on GARCH; input
* using the HMAT option on the procedure.
*
* 3. The derivatives of the log likelihood elements with respect to the
* parameters. Save with the DERIVES option on GARCH; input using the
* DERIVES option on the procedure.
*
* The alternative model takes the form
*
* rho(i,j)(t)=rho(i,j)+delta(i,j)*e(i)(t-1)e(j)(t-1)
*
* At delta=0, this will give the CC estimates. The test is an LM test
* for delta=0 against the alternative that it isn't.
*
* In order to apply the Tse test, the model can't use the (off-diagonal)
* covariances in any form elsewhere in the model. So, for instance, you
* can't use any "M" specification which use covariances and not just
* variances.
*
* Options:
*  RVECTOR=SERIES[VECTOR] of GARCH residuals
*  HMAT=SERIES[SYMM] of GARCH covariance matrices
*  DERIVES=VECT[SERIES] of GARCH gradient components
*    (all required as described above)
*
*  TITLE=title for test ["Tse Test for CC"]
*  [PRINT]/NOPRINT
*
* Variables Defined:
*  %CDSTAT    LM test statistic
*  %SIGNIF    Significance level as chi-square (n(n-1)/2 d.f.)
*
* Reference: Tse, Y.K.(2000), "A Test for Constant Correlations in a
* Multivariate GARCH Model", Journal of Econometrics 98, 107-127.
*
* Revision Schedule:
*  08/2010 Converted to procedure by Tom Doan, Estima
*  07/2012 Add PRINT option
*
procedure TseCCTest
option series[vect]	rvector
option series[symm]	hmat
option vect[series]  derives
option string        title		"Tse Test for CC"
option switch        print    1
*
local integer 			gstart gend
local integer 			n      nslot  time
local symm    			qcinv  uux    hx
local vect    			ux 	 ihu
local pack[series] 	xd
*
* Make sure we have everything
*
if .not.%defined(rvector).or..not.%defined(hmat).or..not.%defined(derives) {
   disp "Usage: @TseCCTest(rvector=residuals,hmat=h matrices,derives=derivatives)"
   return
}
*
* This drops the first data point since the lagged ee' for that is just
* the initial estimate.
*
compute gstart=%regstart()+1,gend=%regend()
*
* This pulls the subdiagonal of the CC matrix out of the beta vector.
* (It's always the last block of parameters).
*
compute n=%nvar
dim qcinv(n,n)
compute nslot=%rows(derives)-n*(n-1)/2
ewise qcinv(i,j)=%if(i==j,1,%beta(nslot=nslot+1))
compute qcinv=inv(qcinv)
*
* Compute the derivatives with respect to the deltas (parameterized as
* the subdiagonal on an n-1 x n-1 matrix.
*
dim xd(n-1,n-1)
do time=gstart+1,gend
   compute ux=rvector(time),uux=%outerxx(rvector(time-1)),hx=hmat(time),$
        ihu=%diag(%sqrt(%xdiag(hx)))*inv(hx)*ux
   do i=1,n-1
      do j=1,i
         set xd(i,j) time time = -qcinv(i+1,j)*uux(i+1,j)+$
             uux(i+1,j)*ihu(i+1)*ihu(j)
      end do j
   end do i
end do time
*
* Compute an OPG version of the LM test for the sum of the gradients=0.
*
mcov(opgstat=%cdstat)
# derives xd
cdf(title=title,print=print) chisqr %cdstat n*(n-1)/2
end

