*
*   URAUTO.SRC
*   Runs a set of unit root tests in sequence attempting to classify
*   the series based upon trend and unit root properties.
*
*   Revision Schedule
*     12/90 Written by Paco Goerlich
*     07/93 Revised by Paco Goerlich for RATS v 4
*     01/94 Documentation added by Tom Doan
*
*
*   @URAUTO  series  start   end
*
*   Options:
*     SIZE=ONE/TWO5/[FIVE]/TEN  Size to use for all tests
*     TRACE/[NOTRACE]  Print all regressions and test statistics
*     LAG=number of lags for autoregressive or non-parametric corrections [4]
*     [PARAM]/NOPARAM  Which type of correction: parametric or not?
*      Parametric tests are of the Dickey-Fuller type, non-parametric are
*      Phillips-Perron type.
*
PROC URAUTO SERIES TBEGP TENDP
TYPE SERIES SERIES
*
LOCAL  SERIES  DSERIES TIME RESID
LOCAL  REAL    TRHO PSI3 PSI2 PSI1 SIGMA DX
LOCAL  RECTANG CV
LOCAL  INTEGER TBEG TEND
LOCAL  VECTOR[STRINGS] CONCLUDE
LOCAL  INTEGER CONCLUSION
*
OPTION CHOICE  SIZE  3 ONE TWO5 FIVE TEN
OPTION INTEGER LAG   4
OPTION SWITCH  TRACE 0
OPTION SWITCH  PARAM 1
*
INQUIRE(SERIES=SERIES) TBEG>>TBEGP  TEND>>TENDP
DIM CV(7,4)
COM CV = || 0.01,  0.025,  0.05,   0.10 |   $
           -3.96, -3.66,  -3.41,  -3.12 |   $
            8.27,  7.16,   6.25,   5.34 |   $
            6.09,  5.31,   4.68,   4.03 |   $
           -3.43, -3.12,  -2.86,  -2.57 |   $
            6.43,  5.38,   4.59,   3.78 |   $
           -2.58, -2.23,  -1.95,  -1.62 ||

DIM CONCLUDE(7)
ENTER CONCLUDE
# 'Series stationary around a zero mean' $
  'Series stationary around a non-zero mean' $
  'Series stationary around a linear trend' $
  'Series contains a unit root with zero drift' $
  'Series contains a unit root with drift' $
  'Cannot reject unit root. Try quadratic trend alternative' $
  'Series has no unit root'

CLEAR TIME DSERIES
SET   TIME    TBEG   TEND = T - TBEG - (TEND-TBEG+1)/2.
SET   DSERIES TBEG+1 TEND = SERIES - SERIES{1}

LABELS DSERIES TIME
# %CONCAT('D',%LABEL(SERIES)) 'TREND'

DIS 'URAUTO Procedure by Paco Goerlich'
DIS 'TESTING SERIES:' %LABEL(SERIES) $
     @31 'SAMPLE' %DATELABEL(TBEG) 'TO' %DATELABEL(TEND)
IF PARAM.EQ.0
   DIS 'NON-PARAMETRIC CORRECTIONS. WINDOW SIZE:' LAG
ELSE
   DIS 'AUTOREGRESSIVE CORRECTIONS:' LAG 'LAGS'
DIS 'WORKING AT' ##.# CV(1,SIZE)*100 '%  SIGNIFICANCE LEVEL'
DIS 'ALL TESTS OF UNIT ROOT ARE ONE-SIDED'

*
*  Regressions with constant and trend
*

DIS
DIS 'REGRESSIONS WITH CONSTANT,TREND'
IF PARAM.EQ.0
   {
   CLEAR RESID
   LIN(PRINT=TRACE) DSERIES TBEG+1 TEND RESID
   # CONSTANT TIME SERIES{1}
   COM  DX = 1./%DET(%XX)
   MCOV(PRINT=TRACE,LAGS=LAG,DAMP=1) TBEG+1 TEND RESID
   # CONSTANT
   COM SIGMA = %CMOM(1,1)/%NDF
   COM TRHO  = %BETA(3)/SQRT(SIGMA*%XX(3,3)) - $
               %NOBS**3*(SIGMA - %SEESQ)/(4*SQRT(3*DX*SIGMA))
   }
ELSE
   {
    if lag>0
    {
    dis 'lag > 0 true, lags = ' lag
    LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
    # CONSTANT TIME SERIES{1} DSERIES{1 TO LAG}
    COM TRHO = %BETA(3)/SQRT(%SEESQ*%XX(3,3))
    }
    else
    {
    dis 'lag > 0 false, lags = ' lag
    LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
    # CONSTANT TIME SERIES{1}
    COM TRHO = %BETA(3)/SQRT(%SEESQ*%XX(3,3))
    }
   }
DIS
DIS @2 't(rho-1)/tao = ' TRHO 'with critical value' CV(2,SIZE)
IF TRHO.LT.CV(2,SIZE)
   {
   DIS @2 'Unit root rejected with t(rho-1)/tao'
   COM CONCLUSION = 7
   BRANCH 100
   }
DIS @2 'Cannot reject a unit root with t(rho-1)/tao'
DIS @4 'Next is joint test of trend=0 and root=1'
EXCLUDE(PRINT=TRACE)
# TIME SERIES{1}
COM PSI3 = %CDSTAT
IF PARAM.EQ.0
   COM PSI3 = (%SEESQ/SIGMA)*PSI3 - $
              ((SIGMA - %SEESQ)/(4*SIGMA)) * $
              (2*%NOBS*%BETA(3) - (%NOBS**3/(DX*24))*%NOBS**3*(SIGMA - %SEESQ))

DIS @4 'psi3 = ' PSI3 'with critical value' CV(3,SIZE)

IF PSI3.GT.CV(3,SIZE)
   {
   DIS @4 'PSI3 significant'
   DIS @6 'Testing trend=0 under the unit root'
   EXCLUDE(PRINT=TRACE)
   # CONSTANT TIME SERIES{1}
   COM PSI2 = %CDSTAT
   IF PARAM.EQ.0
      {
      COM PSI2 = (%SEESQ/SIGMA)*PSI2 - $
                ((SIGMA - %SEESQ)/(6*SIGMA)) * $
                (2*%NOBS*%BETA(3) - (%NOBS**3/(DX*24))*%NOBS**3*(SIGMA-%SEESQ))
      LIN(PRINT=TRACE,ROBUST,LAGS=LAG,DAMP=1) DSERIES TBEG+1 TEND
      # CONSTANT TIME
      CDF(PRINT=TRACE) NORMAL %BETA(2)/SQRT(%XX(2,2))
      }
   ELSE
      {
       if lag>0
       {
       dis 'lag > 0 true, lags = ' lag
       LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
       # CONSTANT TIME DSERIES{1 TO LAG}
       CDF(PRINT=TRACE) NORMAL %BETA(2)/SQRT(%SEESQ*%XX(2,2))
       }
       else
       {
        dis 'lag > 0 false, lags = ' lag
       LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
       # CONSTANT TIME
       CDF(PRINT=TRACE) NORMAL %BETA(2)/SQRT(%SEESQ*%XX(2,2))
       }
      }
   DIS @6 'Trend=0 test = ' %CDSTAT 'using Normal distribution'
   IF %SIGNIF.LT.CV(1,SIZE)
      {
      DIS @6 'Trend significant under the unit root.'
	  DIS @8 'Testing unit root'
      IF PARAM.EQ.0
         {
         LIN(PRINT=TRACE,ROBUST,LAGS=LAG,DAMP=1) DSERIES TBEG+1 TEND
         # CONSTANT TIME SERIES{1}
         COM TRHO = %BETA(3)/SQRT(%XX(3,3))
         }
      DIS @8 'Rho=1 test = ' TRHO 'using Normal distribution'
      CDF(PRINT=TRACE) NORMAL TRHO
      IF (%SIGNIF/2.).LT.CV(1,SIZE)
         {
         DIS @8 'Unit root rejected. Linear trend significant'
         COM CONCLUSION=3
         }
      ELSE {
         DIS @8 'Unit root accepted. Linear trend significant'
         COM CONCLUSION=6
         }
      BRANCH 100
      }
   DIS @6 'Trend insignificant under the unit root. Unit root cannot be rejected'
   DIS @8 'Testing for constant (drift) =0'
   DIS @8 'Psi2 = ' PSI2 'with critical value' CV(4,SIZE)
   IF PSI2.GT.CV(4,SIZE)
      {
      DIS @8 'Drift=0 rejected'
      COM CONCLUSION=5
      }
   ELSE {
      DIS @8 'Drift=0 accepted'
      COM CONCLUSION=4
      }
   BRANCH 100
   }

DIS @4 'PSI3 cannot reject unit root and no linear trend'
DIS

*
*  Regressions with constant and no trend
*

DIS 'REGRESSIONS WITH CONSTANT,NO TREND'
IF PARAM.EQ.0
   {
   CLEAR RESID
   LIN(PRINT=TRACE) DSERIES TBEG+1 TEND RESID
   # CONSTANT SERIES{1}
   MCOV(PRINT=TRACE,LAGS=LAG,DAMP=1) TBEG+1 TEND RESID
   # CONSTANT
   COM SIGMA = %CMOM(1,1)/%NDF
   COM TRHO  = %BETA(2)/SQRT(SIGMA*%XX(2,2)) - $
                0.5*(SIGMA - %SEESQ)*%NOBS*SQRT(%XX(2,2)/SIGMA)
   }
ELSE
   {
    if lag>0
    {
    dis 'lag > 0 true, lags = ' lag
    LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
    # CONSTANT SERIES{1} DSERIES{1 TO LAG}
    COM TRHO = %BETA(2)/SQRT(%SEESQ*%XX(2,2))
    }
    else
    {
    dis 'lag > 0 false, lags = ' lag
    LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
    # CONSTANT SERIES{1}
    COM TRHO = %BETA(2)/SQRT(%SEESQ*%XX(2,2))
    }
   }

DISP @2 't(rho-1)/mu = ' TRHO 'with critical value' CV(5,SIZE)

IF TRHO.LT.CV(5,SIZE)
   {
   DIS @2 'Unit root rejected by t(rho-1)/mu'
   COM CONCLUSION=2
   BRANCH 100
   }

DIS @2 'Cannot reject a unit root with t(rho-1)/mu'
DIS @4 'Next is joint test of constant=0 and root=1'

EXCLUDE(PRINT=TRACE)
# CONSTANT SERIES{1}
COM PSI1 = %CDSTAT
IF PARAM.EQ.0
   COM PSI1 = (%SEESQ/SIGMA)*PSI1 - $
              ((SIGMA - %SEESQ)/(4*SIGMA)) * $
              (2*%NOBS*%BETA(2) - 0.5*(SIGMA - %SEESQ)*%NOBS**2*%XX(2,2))

DISP @4 'psi1 = ' PSI1 'with critical value' CV(6,SIZE)

IF PSI1.GT.CV(6,SIZE)
   {
   DIS @4 'psi1 significant'
   DIS @6 'Testing constant=0 under the unit root'
   IF PARAM.EQ.0
      {
      LIN(PRINT=TRACE,ROBUST,LAGS=LAG,DAMP=1) DSERIES TBEG+1 TEND
      # CONSTANT
      CDF(PRINT=TRACE) NORMAL %BETA(1)/SQRT(%XX(1,1))
      }
   ELSE
      {
       if lag>0
       {
       dis 'lag > 0 true, lags = ' lag
       LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
       # CONSTANT DSERIES{1 TO LAG}
       CDF(PRINT=TRACE) NORMAL %BETA(1)/SQRT(%SEESQ*%XX(1,1))
       }
       else
       {
       dis 'lag > 0 false, lags = ' lag
       LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
       # CONSTANT
       CDF(PRINT=TRACE) NORMAL %BETA(1)/SQRT(%SEESQ*%XX(1,1))
       }
      }
   DISP @6 'Constant=0 test = ' %CDSTAT 'with Normal distribution'
   IF %SIGNIF.LT.CV(1,SIZE)
      {
      DIS @6 'Constant significant under the unit root'
      DIS @8 'Testing unit root'
      IF PARAM.EQ.0
         {
         LIN(PRINT=TRACE,ROBUST,LAGS=LAG,DAMP=1) DSERIES TBEG+1 TEND
         # CONSTANT SERIES{1}
         COM TRHO = %BETA(2)/SQRT(%XX(2,2))
         }
      CDF(PRINT=TRACE) NORMAL TRHO
      DIS @8 'Rho=1 test = ' TRHO 'using Normal distribution'
      IF (%SIGNIF/2.).LT.CV(1,SIZE)
         {
         DIS @8 'Unit root rejected. Constant significant'
         COM CONCLUSION=2
         BRANCH 100
         }
      ELSE
         {
         DIS @8 'Unit root accepted. Constant significant'
         COM CONCLUSION=5
		 BRANCH 100
         }
      }
   ELSE
      {
      DIS @6 'Constant insignificant under unit root. Unit root cannot be rejected'
      COM CONCLUSION=4
      BRANCH 100
      }
   }

DIS @6 'PSI1 cannot reject constant=0 and root=1'
DIS
DIS 'REGRESSIONS WITH NO CONSTANT, NO TREND'
IF PARAM.EQ.0
   {
   CLEAR RESID
   LIN(PRINT=TRACE) DSERIES TBEG+1 TEND RESID
   # SERIES{1}
   MCOV(PRINT=TRACE,LAGS=LAG,DAMP=1) TBEG+1 TEND RESID
   # CONSTANT
   COM SIGMA = %CMOM(1,1)/%NDF
   COM TRHO  = %BETA(1)/SQRT(SIGMA*%XX(1,1)) - $
                0.5*(SIGMA - %SEESQ)*%NOBS*SQRT(%XX(1,1)/SIGMA)
   }
ELSE
   {
   if lag>0
    {
    dis 'lag > 0 true, lags = ' lag
    LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
    # SERIES{1} DSERIES{1 TO LAG}
    COM TRHO = %BETA(1)/SQRT(%SEESQ*%XX(1,1))
    }
    else
    {
    dis 'lag > 0 false, lags = ' lag
    LIN(PRINT=TRACE) DSERIES TBEG+1+LAG TEND
    # SERIES{1}
    COM TRHO = %BETA(1)/SQRT(%SEESQ*%XX(1,1))
    }
   }
DIS @2 't(rho-1) = ' TRHO 'with critical value' CV(7,SIZE)

IF TRHO.LT.CV(7,SIZE)
   {
   DIS @2 'Unit root rejected'
   COM CONCLUSION=1
   }
ELSE
   {
   DIS @2 'Cannot reject a unit root with t(rho-1)'
   COM CONCLUSION=4
   }

:100
   DIS
   DIS 'CONCLUSION:' CONCLUDE(CONCLUSION)

END PROC URAUTO



