* BLSUNIT3.SRC
*
* - by Vincent Labhard, October 28, 1998.
*
* - procedure for sequential 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..

* Revision History:
*  12/03/03 -- Re-instated the TEST and related commands for computing the F-test--they
*              appear to be correct as written.
*   7/18/02 -- set ENDM = to BREAK, not ENDL, report ENDM (not BREAK) as
*           --  computed breakpoint.
*           -- Also, comment out TEST and related commands as these seemed incorrect.
*
* - call of procedure:
*   blsunit3 series start end
*
* - options:
*   ALL/[NOALL]       with ALL the procedure reports values of all
*                      tests, by default only the minimum value.
*   FRACTION=[.15]    fraction at beginning and end of the total
*                      sample not considered for the test.
*   LAGS=[0]          number of additional lags (i.e. the degree of AR
*                       polynomial on the differences) to include.
*   SLOPE/[INTERCEPT] with SLOPE the shift in trend is tested,
*                      by default the shift in mean is tested.
*   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 blsunit3 series start end

type series SERIES
type integer START END

option integer LAGS  0
option real FRACTION .15
option switch ALL 0
option switch SLOPE 0
option switch TREND 0
option switch TTEST 0

local integer STARTL ENDL ENDM BREAK BREAKM I1
local real FIDDLE TESTSTAT MINSTAT FSTAT FSIGN R1
local series SDIFF STREND DUMMY

inquire(series=SERIES) STARTL>>START ENDL>>END

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

do BREAK = STARTL+I1,ENDL-I1
   if (SLOPE)
      {
       set(scratch) DUMMY STARTL BREAK = 0
       set(scratch) DUMMY BREAK+1 ENDL = t-BREAK
      }
   else
      {
       set(scratch) DUMMY STARTL BREAK = 0
       set(scratch) DUMMY BREAK+1 ENDL = 1
      }
   if (.not.TREND)
      {
       if (LAGS)
          {
           linreg(noprint) SERIES STARTL+LAGS+1 ENDL
           # SERIES{1} DUMMY CONSTANT SDIFF{1 to LAGS}
           summarize(noprint)
           # SDIFF{1 to LAGS}
          }
       else
          linreg(noprint) SERIES STARTL+1 ENDL
          # SERIES{1} DUMMY CONSTANT
      }
   else
      {
       if (LAGS)
          {
           linreg(noprint) SERIES STARTL+LAGS+1 ENDL
           # SERIES{1} DUMMY CONSTANT STREND SDIFF{1 to LAGS}
           summarize(noprint)
           # SDIFF{1 to LAGS}
          }
       else
          linreg(noprint) SERIES STARTL+1 ENDL
          # SERIES{1} DUMMY 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))*(BREAK-STARTL)/(ENDL-STARTL)
      }
   else
      compute TESTSTAT = %nobs*FIDDLE*(%beta(1)-1)*(BREAK-STARTL)/(ENDL-STARTL)
   if (ALL)
      display 'Dickey-Fuller Test (' LAGS 'lags and break at' BREAK ') =' #####.##### TESTSTAT
   if BREAK==STARTL+I1.or.TESTSTAT<MINSTAT
     {
      test(zeroes,noprint)
      # 1 2
      compute FSTAT = %cdstat , FSIGN = %signif
      compute MINSTAT = TESTSTAT , ENDM = BREAK
     }
end do BREAK

display 'Sequential minimum ADF Test (' LAGS 'lags and break at' ENDM ') =' #####.##### MINSTAT
display 'F Test = ' #####.##### FSTAT 'with significance level' #####.##### FSIGN

end




