*
* @ChowDenning(options) series start end
* Computes the multiple variance ratio statistic from Chow, K.V.
* and Denning, K. (1993) "A simple multiple variance ratio test",
* Journal of Econometrics, 58, 385-401
*
* Parameters:
*     series       The series to be tested. By default, this is the series
*                  being tested for independence, but it can be (with the DIFF option)
*                  the series which is hypothesized to be a random walk.
*     start end    The range of entries to use [defaults to full range]
*
* Options:
*     [PRINT]/NOPRINT
*     TITLE=title of report ["Chow-Denning Test, Series xxx"]
*
*     LAGS=number of lags examined [2]
*     ROBUST/[NOROBUST]
*        If ROBUST, computes heteroscedasticity robust standard errors
*     [CORRECT]/NOCORRECT
*        If CORRECT, corrects for bias in the variance.
*     [CENTER]/NOCENTER
*        If CENTER, subtracts a fixed mean from the differenced data.
*     DIFF/[NODIFF]
*        If DIFF, difference the data, which means that the null is that
*        the input series is a (possibly drifting) random walk rather than
*        a series of independent increments.
*
* Variables Defined:
*     %CDSTAT          Test statistic
*
* Revision Schedule:
*  02/2008 Adapted from VRATIO.SRC by adding a loop over the number of lags
*  03/2018 Update to use REPORT
*
******************************************************************************
procedure ChowDenning series start end
type series series
type integer start end
*
option switch  print    1
option string  title
option integer lags     2
option switch  robust   0
option switch  correct  1
option switch  center   1
option switch  diff     0
*
local integer  startp endp nobs i lag maxlag
local series   xdiff xseries devsqr
local real     meandif varone varlag
local real     ssqlag ssqone
local vector   lwform
local real     maxstat vratio
local report   creport
*
if %defined(series)==0 {
   display "Syntax: @ChowDenning( options ) series start end"
   return
}
*
* xseries is the series which should behave as a RW under the null.
* xdiff is its increments
*
inquire(series=series)  startp<<start endp<<end
if diff {
   set xdiff startp+1 endp = series - series{1}
   set xseries startp endp = series
}
else {
   set xdiff startp endp = series
   acc series startp endp xseries
}
*
statistics(noprint) xdiff startp+1 endp
compute nobs = %nobs
*
* Check for missing values
*
if (nobs < endp-startp) {
   display "@ChowDenning cannot be applied because the series contains missing values."
   return
}

if center
   compute meandif=%mean,varone=%variance,ssqone=varone*(nobs-1)
else {
   compute meandif=0.0
   sstats startp+1 endp xdiff^2>>ssqone
   compute varone=ssqone/nobs
}

compute maxstat=0.0
do lag=2,lags
   sstats startp+lag endp (xseries-xseries{lag}-(lag*meandif))^2>>ssqlag
   *
   * This does the bias correction for the variance as in Campbell, Lo and
   * MacKinlay, 2.4.36 with nobs=nq (in their notation).
   *
   if center.and.correct
      compute varlag  = ssqlag*nobs/(lag*(nobs-lag+1.0)*(nobs-lag))
   else
      compute varone  = ssqone/nobs , varlag = ssqlag/(nobs*lag)

   compute vratio = varlag/varone
   *
   * If ROBUST, this does the heteroscedasticity robust asymptotic
   * distribution. This uses MCOV with a non-standard window
   * applied to the square of the deviation between xdiff and its
   * sample mean.
   *
   if robust {
      set devsqr startp+1 endp = (xdiff-meandif)^2
      dim lwform(2*lag-1)
      ewise lwform(i)=%if(i==lag,0.0,2.0*(%min(i,2.0*lag-i)/lag)^2)
      mcov(lwform=lwform,lags=lag-1)
      # devsqr
      compute %cdstat=(vratio-1)/sqrt(%cmom(1,1)/ssqone^2)
   }
   else {
      compute %cdstat=(vratio-1)*sqrt(nobs)/sqrt(2.0*(2*lag-1)*(lag-1)/(3.0*lag))
   }
   if abs(%cdstat)>maxstat
      compute maxstat=abs(%cdstat),maxlag=lag
end do lag
*
compute %cdstat=maxstat
if print {
   if %defined(title)
      compute ltitle=title
   else
      compute ltitle="Chow-Denning Test on series "+%l(series)

   report(use=creport,action=define,title=ltitle)
   report(use=creport,atrow=1,atcol=1,span) ltitle
   report(use=creport,atrow=2,atcol=1) "Lags Examined"   lags
   report(use=creport,atrow=3,atcol=1) "Maximizing Lag"  maxlag
   report(use=creport,atrow=4,atcol=1) "Maximum Z"       maxstat
   report(use=creport,action=show)
}
end
