*
* @MVARCHTest( options ) start end
* # list of series
*
* Performs an LM test for ARCH effects in a set of series by regressing
* the crossproducts of the series (that is u(i,t) x u(j,t) for all
* combinations of i and j) on a constant and its lag(s) and testing the
* coefficients on the lags.
*
* Parameters:
*   start  end     Range to use. By default, the common range of the input
*                  series allowing for the lags in the test regression).
*
* Options:
*   LAGS=number of ARCH lags to test [1]
*
*   [PRINT]/NOPRINT
*   TITLE="title for report" ["Multivariate ARCH Test"]
*
* Variables Defined:
*   %CDSTAT    Test statistic
*   %SIGNIF    Significance level of the test as a chi-squared with degrees of
*               freedom equal to (n(n+1)/2)^2 times the number of lags.
*   %NDFTEST   Degrees of freedom of the test
*
* Revision Schedule
*   03/2018 Add TITLE option
*
procedure MVARCHTest start end
type integer start end
*
option integer lags     1
option switch  print    1
option string  title
*
local equation xeq
local integer  n i j nnsize
local model    mvgarchmodel
local vector   x
local packed[series] xx
local rect     ss ss11 ss22 ss12
local report   areport
local string   ltitle
*
dec   integer  %ndftest
*
equation xeq *
compute n=%eqnsize(xeq)
*
* Generate all series of cross products for the input variables
*
dim xx(n,n)
do i=1,n
   do j=1,i
      set xx(i,j) start end = x=%eqnxvector(xeq,t),x(i)*x(j)
   end do j
end do i
compute nnsize=n*(n+1)/2
*
* Set up a VAR model for the crossproducts on their lags
*
system(model=mvgarchmodel)
variables xx
lags 1 to lags
det constant
end(system)
*
* Compute the crossproduct matrix for the model. This will have the
* lagged interactions in the top block, followed by the CONSTANT, with
* the current interaction variables at the bottom.
*
cmom(model=mvgarchmodel) start end
*
* Do the regression by sweeping out the lags and the constant.
*
compute ss=%sweeptop(%cmom,%ncmom-nnsize)
*
* Extract the matrices needed to compute the test statistic. Note that
* this doesn't include the CONSTANT among the tested regressors.
*
compute ss22=%xsubmat(ss,%ncmom-nnsize+1,%ncmom,%ncmom-nnsize+1,%ncmom)
compute ss11=%xsubmat(ss,1,nnsize*lags,1,nnsize*lags)
compute ss12=%xsubmat(ss,1,nnsize*lags,%ncmom-nnsize+1,%ncmom)
*
* Compute the LM statistic
*
compute %cdstat=%nobs*%dot(inv(ss22),%mqform(inv(ss11),ss12))
compute %ndftest=nnsize^2*lags
compute %signif=%chisqr(%cdstat,%ndftest)
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="Multivariate ARCH Test"
*
report(use=areport,action=define,title=ltitle)
report(use=areport,atrow=1,atcol=1,span) ltitle
report(use=areport,atrow=2,atcol=1) "Statistic" "Degrees" "Signif"
report(use=areport,atrow=3,atcol=1) %cdstat %ndftest %signif
report(use=areport,atcol=1,action=format,picture="*.##")
report(use=areport,atcol=3,action=format,picture="#.#####")
if print
   report(use=areport,action=show)
end
