* BLSUNIT2.SRC
*
* - by Vincent Labhard, October 28, 1998.
*
* - procedure for rolling minimum unit root tests following:
*   BANERJEE, A.; LUMSDAINE, R.L.; STOCK, J.H. "Recursive
*     and sequential tests of the unit-root and trend-break
*     hypothesis: theory and international evidence".
*     JBES, vol.10, 1992; pp.271-287.
*   DICKEY, D.A.; FULLER, W.A. "Distribution of the Estimators
*     for Time Series Regressions with a Unit Root".
*     JASA, vol.74, 1979; pp 427-431.
*   FULLER, W.A. Introduction to Statistical Time Series.
*     New York, John Wiley, 1976; pp..
*
* - call of procedure:
*   blsunit2 series start end
*
* - options:
*   ALL/[NOALL]        with ALL the procedure reports values of all
*                       tests, by default only the minimum value.
*   FRACTION=[3**(-1)] fraction of total sample for the test.
*   LAGS=[0]           number of additional lags (i.e. the degree of AR
*                       polynomial on the differences) to include.
*   TREND/[NOTREND]    with TREND the estimated model includes
*                       1 and t. by default, only 1 is included.
*   TTEST/[NOTTEST]    computes the regression t test,
*                       as opposed to the T(rho-1) test.

procedure blsunit2 series start end

type series SERIES
type integer START END

option integer LAGS  0
option real FRACTION 3**(-1)
option switch ALL 0
option switch TREND 0
option switch TTEST 0

local integer STARTQ ENDQ STARTL ENDL ENDM I1
local real FIDDLE TESTSTAT MINSTAT R1
local series SDIFF STREND

inquire(series=SERIES) STARTQ>>START ENDQ>>END

set SDIFF  STARTQ+1 ENDQ = SERIES-SERIES{1}
set STREND STARTQ+1 ENDQ = t
compute R1 = FRACTION*(ENDQ-STARTQ), I1 = fix(R1)

do STARTL = STARTQ,ENDQ-I1
   compute ENDL = STARTL+I1
   if (.not.TREND)
      {
       if (LAGS)
          {
           linreg(noprint) SERIES STARTL+LAGS+1 ENDL
           # SERIES{1} CONSTANT SDIFF{1 to LAGS}
           summarize(noprint)
           # SDIFF{1 to LAGS}
          }
       else
          linreg(noprint) SERIES STARTL+1 ENDL
          # SERIES{1} CONSTANT
      }
   else
      {
       if (LAGS)
          {
           linreg(noprint) SERIES STARTL+LAGS+1 ENDL
           # SERIES{1} CONSTANT STREND SDIFF{1 to LAGS}
           summarize(noprint)
           # SDIFF{1 to LAGS}
          }
       else
          linreg(noprint) SERIES STARTL+1 ENDL
          # SERIES{1} CONSTANT STREND
      }
   if (LAGS)
      compute FIDDLE = 1.0/(1.0-%sumlc)
   else
      compute FIDDLE = 1.0
   if (TTEST)
      compute TESTSTAT = (%beta(1)-1)/sqrt(%seesq*%xx(1,1))*(ENDL-STARTL)/(ENDQ-STARTQ)
   else
      compute TESTSTAT = %nobs*FIDDLE*(%beta(1)-1)*(ENDL-STARTL)/(ENDQ-STARTQ)
   if (ALL)
      display 'Dickey-Fuller Test (' LAGS 'lags and sample ending at' ENDL ') =' #####.##### TESTSTAT
   if ENDL==STARTL+I1.or.TESTSTAT<MINSTAT
      compute MINSTAT = TESTSTAT , ENDM = ENDL
end do ENDL

display 'Rolling minimum ADF Test (' LAGS 'lags and sample ending at' ENDM ') =' #####.##### MINSTAT

end
