*
* @WestChoTest( options ) series start end
*
* Computes the modified Ljung-Box test, robust to heteroscedasticity,
* proposed in West and Cho(1995),  "The predictive ability of several
* models of exchange rate volatility," Journal of Econometrics, vol. 69,
* no 2, 367-391.
*
* Parameters:
*   series     series to analyze
*   start end  range of <<series>> to use. By default, the defined range
*               of <<series>>
*
* Options:
*   NUMBER=# of lags to use [roughly 2 sqrt(# of observations]
*   DFC=degrees of freedom correction for Q [0]
*
*   TITLE=Title for output ["West-Cho Modified Q Test, Series xxxx"]
*   [PRINT]/NOPRINT
*     NOPRINT suppresses the output
*
* Defines:
*   %NOBS    = number of observations
*   %CDSTAT  = test statistic
*   %QSTAT   = test statistic
*   %SIGNIF  = marginal significance level of Q-statistic
*   %QSIGNIF = marginal significance level of Q-statistic
*   %NDFQ    = degrees of freedom for the test statistic
*
* Revision Schedule:
*  03/2012 Written by Tom Doan, Estima
*  08/2012 Add TITLE option
*
procedure WestChoTest series start end
type series series
type integer start end
*
option integer number
option integer dfc     0
option switch  print   1
option string  title
*
local integer startl endl lnumber k nobs
local vector  kappa sigma
local series  u
local string  ltitle qlabel
local report  wreport
*
inquire(series=series) startl<<start endl<<end
compute nobs=endl-startl+1
if %defined(number)
   compute lnumber=number
else
   compute lnumber=fix(2.0*sqrt(nobs))
*
* We just use raw sums rather than using T divisors (P in the paper)
* (which eliminates one factor of T in the final formula) and the
* sigma0^2 factors in West and Cho's cancel.
*
diff(center) series startl endl u
dim sigma(lnumber) kappa(lnumber)
do k=1,lnumber
   sstats startl+k endl u*u{k}>>sigma(k) (u*u{k})^2>>kappa(k)
end do k
*
sstats 1 lnumber (1.0/(nobs-t))*sigma(t)^2/kappa(t)>>%cdstat
compute %cdstat=%cdstat*(nobs+2)
compute %nobs=nobs
compute %signif=%chisqr(%cdstat,lnumber-dfc)
*
if %defined(title)
   compute ltitle=title
else
   compute ltitle="West-Cho Modified Q Test, Series "+%l(series)
*
report(use=wreport,action=define,title=ltitle)
report(use=wreport,atrow=1,atcol=1,span) ltitle
if dfc>0
   compute qlabel="Q("+lnumber+"-"+dfc+")"
else
   compute qlabel="Q("+lnumber+")"
report(use=wreport,atrow=2,atcol=1) qlabel %cdstat
report(use=wreport,atrow=3,atcol=1) "Signif." %signif
report(use=wreport,atrow=2,torow=2,action=format,picture="*.##")
report(use=wreport,atrow=3,torow=3,action=format,picture="*.####")
if print
   report(use=wreport,action=show)
*
compute %nobs   =nobs
compute %qstat  =%cdstat
compute %qsignif=%signif
compute %ndfq   =lnumber-dfc
end

