*
* @McLeodLi( options)  x start end
* Performs a McLeod-Li test for 2nd order dependence
*
* Parameters:
*   x           series to analyze
*   start end   range to analyze [defaults to full range of x]
*
* Options:
*   NUMBER=number of autocorrelations on the squares [10]
*   DFC=degrees of freedom correction [0]. For residuals from a GARCH
*     model, this should be the number of estimated "GARCH" parameters
*    (those on lagged variance and lagged squared residuals).
*   TITLE=title of report ["McLeod-Li Test for Series x"]
*   [PRINT]/NOPRINT
*

* Variables Defined:
*   %CDSTAT  Test statistic
*   %SIGNIF  Significance level of %CDSTAT as a chi-squared with degrees
*            of freedom NUMBER-DFC.
*   %NOBS    Number of observations
*
* Reference:
*   McLeod and Li(1983), "Diagnostic Checking of ARMA Time Series Models
*    Using Squared Residual Autocorrelations", J. of Time Series Analysis,
*    vol 4, pp 269-273.
*
* Revision Schedule:
*   05/2008 Written by Tom Doan, Estima
*   09/2010 Revised to use local report and add title option.
*   05/2014 Improve table by adding column headers
*
procedure McLeodLi x start end
type series x
type integer start end
*
option integer number 10
option integer dfc    0
option switch  print  1
option string  title
*
local integer startl endl
local series  xsq
local string  ltitle
local report  mreport
*
if .not.%defined(x) {
   display "Syntax: @McLeodLi(options) >>x<< start end"
   return
}
inquire(series=x) startl<<start endl<<end
set xsq startl endl = x^2
corr(number=number,method=yule,noprint,qstats,dfc=dfc) xsq startl endl
compute %cdstat=%qstat,%signif=%qsignif
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="McLeod-Li Test for Series "+%l(x)
*
report(use=mreport,action=define,title=ltitle)
report(use=mreport,row=new,atcol=1,span) ltitle
report(use=mreport,row=new,atcol=1,span) "Using "+%nobs+" Observations from "+%datelabel(startl)+" to "+%datelabel(endl)
report(use=mreport,row=new,atcol=1,align=center) "" "Test Stat" "Signif"
report(use=mreport,row=new,atcol=1) "McLeod-Li("+number+"-"+dfc+")" %cdstat %signif
report(use=mreport,action=format,atcol=2,width=10)
report(use=mreport,action=format,atcol=3,picture='#.#####')
if print
   report(use=mreport,action=show)
end
