*
* Computes the Hosking variant on the multivariate Q statistic
*
* Syntax:
*
*  @MVQStat( options )  start end
*  # list of series
*
* Options:
*   LAGS=# of lags [12]
*   DFC=degrees of freedom correction [0]
*
* Revision Schedule:
*  05/05 Written by Tom Doan, Estima
*
procedure MVQStat start end
type integer start end
*
option integer lags 12
option integer dfc  0
*
local vect[integer] list
local integer startl endl
local integer i j l n
local vect[rect] mcorr
local series ccorr
local symm ginv
local real qstat
*
* Get the list of variables
*
enter(varying,entries=n) list
*
* Prepare the matrices for the correlations, one nxn matrix
* of each lag.
*
dim mcorr(lags+1)
ewise mcorr(i)=%zeros(n,n)
*
* Set the sample range
*
inquire(reglist) startl<<start endl<<end
# list
*
* Use CROSS to compute the pairwise autocorrelations, and stuff
* them into the proper slots in the MCORR matrices.
*
do i=1,n
   do j=1,n
      cross(noprint) list(i) list(j) startl endl 0 lags ccorr
      do l=0,lags
         compute mcorr(l+1)(i,j)=ccorr(l+1)
      end do l
   end do j
end do i
*
* Compute the test statistic
*
compute ginv=inv(mcorr(1))
compute qstat=0.0
do l=1,lags
   compute qstat=qstat+%dot(%mqform(ginv,mcorr(l+1)),ginv)/(%nobs-l)
end do li
compute qstat=qstat*%nobs**2
compute %cdstat=qstat
compute %ndfq  =lags*n*n-dfc
compute %signif=%chisqr(qstat,%ndfq)
disp 'Multivariate Q('+lags+')=' qstat
disp 'Significance Level as Chi-Squared('+%ndfq+')=' %signif
end

