******************************************************************************
* This procedure is a modification of Stock's procedure to do Stock-Watson   *
* and Dickey-Fuller Unit Root and Time Trend tests.                          *
*
******************************************************************************
*
*  @STOCKWAT( options )  series   start  end
*
*    Options:
*     ARCORR =  Number of autoregressive corrections
*
PROC STOCKWAT SERIES START END
TYPE SERIES SERIES
TYPE INTEGER START END
*
OPTION INTEGER ARCORR 1
*
LOCAL INTEGER START0 START01 STARTL ENDL STARTM ENDM I J K
LOCAL RECT CV
LOCAL VECTOR QFF QTT
LOCAL EQUATION EQ99
LOCAL VECT[SERIES] BASE
*
DIM CV(3,6) QFF(10) QTT(10)
*
INQUIRE(SERIES=SERIES) STARTM>>START ENDM>>END
INQUIRE(SERIES=SERIES) STARTL
IF .NOT.%DEFINED(START)
   COMPUTE STARTM=STARTL+ARCORR+3

IF STARTM<=STARTL+ARCORR+2
   {
   DIS '*** WARNING ***'
   DIS 'START does not allow for enough lags: START should be chosen'
   DIS 'so that (START-ARCORR-2) >= second date for which SERIES is defined'
   COMPUTE STARTM=STARTL+ARCORR+3
   }

COMPUTE START0 =STARTM-ARCORR-1
COMPUTE START01=START0-1

DIM BASE(8)
SET BASE(1) START01 ENDM = T
SET BASE(2) START01 ENDM = T*T
COMPUTE CV=||.01 , -20.6 ,  -29.2 ,  -35.5 ,  -3.43 , -3.96 | $
             .05 , -14.1 ,  -21.7 ,  -27.9 ,  -2.86 , -3.41 | $
             .10 , -11.2 ,  -18.2 ,  -24.1 ,  -2.57 , -3.12 ||
DIS ' '
DIS '-------------------------------------------------------------------------'
DIS ' Levels: '
CORR(NUMBER=2*ARCORR) SERIES STARTM ENDM
DIFF SERIES START01 ENDM BASE(3)
LABEL BASE(3)
# 'DIFF'+%LABEL(SERIES)
DIS ' Differences: '
CORR(NUMBER=2*ARCORR) BASE(3) STARTM ENDM
DIS '     DICKEY-FULLER / STOCK-WATSON UNIT ROOT / TIME TREND TESTS'
DIS '         Number of autoregressive corrections = ' ARCORR
DO I=0,1
   COMPUTE J=5*I
   IF I==0
      {
      SET BASE(4) START01 ENDM = SERIES
      LABEL BASE(4)
      # %LABEL(SERIES)
      }
   ELSE
      {
      SET BASE(4) START01 ENDM = BASE(3)
      LABEL BASE(4)
      # %LABEL(BASE(3))
      }
* Stock-Watson statistics
* First detrend the data
    DO K=1,3
       IF K==1
          {
          LINREG(NOPRINT) BASE(4) START01 ENDM BASE(5)
          # CONSTANT
          }
       ELSE IF K==2
          {
          LINREG(NOPRINT) BASE(4) START01 ENDM BASE(5)
          # CONSTANT BASE(1)
          }
       ELSE
          {
          LINREG(NOPRINT) BASE(4) START01 ENDM BASE(5)
          # CONSTANT BASE(1) BASE(2)
          }
* Construct test statistics
       LINREG(NOPRINT) BASE(5) START0 ENDM BASE(6)
       # CONSTANT BASE(5){1}
       LINREG(NOPRINT,DEF=EQ99) BASE(6) STARTM ENDM
       # CONSTANT BASE(6){1 TO ARCORR}
       FILTER(EQU=EQ99) BASE(5) STARTM-1 ENDM BASE(7)
       LINREG(NOPRINT) BASE(7) STARTM ENDM
       # CONSTANT BASE(7){1}
       COMPUTE QFF(K+J)=%NOBS*(%BETA(2)-1)
    END DO K
* Dickey-Fuller tests
    DIFF BASE(4) START0 ENDM BASE(8)
    LINREG(NOPRINT) BASE(8) STARTM ENDM
    # CONSTANT BASE(4){1} BASE(8){1 TO ARCORR} BASE(1)
    COMPUTE QFF(4+J)=%BETA(2)*(%SEESQ*%XX(2,2))**-.5
    LINREG(NOPRINT) BASE(8) STARTM ENDM
    # CONSTANT BASE(4){1} BASE(8){1 TO ARCORR}
    COMPUTE QFF(5+J)=%BETA(2)*(%SEESQ*%XX(2,2))**-.5
* Tests for deterministic trends
    LINREG(NOPRINT) BASE(4) STARTM ENDM
    # CONSTANT BASE(4){1 TO ARCORR}
    COMPUTE QTT(1+J)=%BETA(1)*(%SEESQ*%XX(1,1))**-.5
    LINREG(NOPRINT) BASE(4) STARTM ENDM
    # CONSTANT BASE(1) BASE(4){1 TO ARCORR}
    COMPUTE QTT(2+J)=%BETA(2)*(%SEESQ*%XX(2,2))**-.5
END DO I
DIS ' '
DIS '       TEST     Statistic     |   1% value      5% value      10% value '
DIS ' '
DIS '   qf-tausq(z) ' qff(3)  ' | ' cv(1,4) cv(2,4) cv(3,4)
DIS '     qf-tau(z) ' qff(2)  ' | ' cv(1,3) cv(2,3) cv(3,3)
DIS '      qf-mu(z) ' qff(1)  ' | ' cv(1,2) cv(2,2) cv(3,2)
DIS '    qf-tau(dz) ' qff(7)  ' | ' cv(1,3) cv(2,3) cv(3,3)
DIS '     qf-mu(dz) ' qff(6)  ' | ' cv(1,2) cv(2,2) cv(3,2)
DIS '  DF: t-tau(z) ' qff(4)  ' | ' cv(1,6) cv(2,6) cv(3,6)
DIS '  DF:  t-mu(z) ' qff(5)  ' | ' cv(1,5) cv(2,5) cv(3,5)
DIS '  DF:t-tau(dz) ' qff(9)  ' | ' cv(1,6) cv(2,6) cv(3,6)
DIS '  DF: t-mu(dz) ' qff(10) ' | ' cv(1,5) cv(2,5) cv(3,5)
DIS '       time(z) ' qtt(2)  ' \ '
DIS '   constant(z) ' qtt(1)  '  }     Tests for deterministic '
DIS '      time(dz) ' qtt(7)  '  }  terms in levels and differences '
DIS '  constant(dz) ' qtt(6)  ' / '
DIS ' '
END

