/*
HEGY series  start  end

Computes Hylleberg et al (1990) seasonal unit root tests for quarterly
time series, with and without intercept, seasonal dummies, and/or trend
from the regression:

     Y4(t) = DET + PI1 Y1(t-1) + PI2 Y2(t-1) + PI3 Y3(t-2) + PI4 Y3(t-1)
            + PIi Y4(t-i) + e(t)

Please note: This procedure is written for use with quarterly time series.
             Modifications would be required for use with monthly data.

The following OPTIONS are available:

DET   =	NONE,CONS,SD,TREND,STREND,[ALL]

         Select the determinstic componant. NONE sets the determinstic
         componant to zero; CONS includes intercept only; SD includes CONS
         and (3) seasonal dummies; TREND includes CONS and trend; STREND
         includes CONS, SD, and Trend; and ALL reports all 5 regressions.

LAGS  = [4]     Controls default lag length. Can be overridden for
                  particular models using the other "lags" options
                  listed below (e.g., "@HEGY(DET=ALL,LAGS=4,STLAGS=8) X").

NLAGS = [LAGS]     Number of lags when DET=NONE.
ILAGS = [LAGS]     Number of lags when DET=CONS.
SLAGS = [LAGS]     Number of lags when DET=SD.
TLAGS = [LAGS]     Number of lags when DET=TREND.
STLAGS= [LAGS]     Number of lags when DET=STREND.
  Note that for these 5 options, the default setting is controlled
   by the LAGS option described above (its default value is 4).

The tests reported by @HEGY are t-statistics on PI1, PI2, PI3, PI4 and
an F-statistic on PI3=PI4=0.  Critical values for these tests are in
Hylleberg et al (1990). Also reported are the extensions of HEGY method
by Ghysels et al (1994).  These extensions are denoted F234 for
PI2=PI3=PI4=0 and F1234 for PI1=PI2=PI3=PI4=0. Critical values for
F234 and F1234 are in Ghysels et al (1994).

The procedure reports a Lagrange Multiplier test for autocorrelation in
the error term of order 1-4 and the significance of the last lag when i>0.

REFERENCES:
E. Ghysels, H. Lee, and J. Noh, "TESTING FOR UNIT ROOTS IN SEASONAL
  TIME SERIES", Journal of Econometrics, 62, pp. 415-442. (1994).

S. Hylleberg, R. Engle, W. Granger, and B. Yoo "SEASONAL INTEGRATION AND
  COINTEGRATION" Journal of Econometrics, 44, pp. 215-238. (1990).


Written by				Suliman Al-Turki
Send comments and suggestions to: 	salturki@imf.org
*/
PROCEDURE HEGY SERIES START END
TYPE SERIES SERIES
TYPE INTEGER START END
*
OPTION INTEGER LAGS     4
OPTION INTEGER NLAGS    LAGS
OPTION INTEGER ILAGS    LAGS
OPTION INTEGER SLAGS    LAGS
OPTION INTEGER TLAGS    LAGS
OPTION INTEGER STLAGS   LAGS
OPTION CHOICE  DET	6  NONE CONS SD TREND STREND ALL
*
LOCAL INTEGER STARTL ENDL
LOCAL SERIES  Y1 Y2 Y3 Y4 TTREND SDUM RS0 RS1 RS2 RS3 RS4
LOCAL REAL    PI1 PI2 PI3 PI4 F23 F234 F1234 F34 LAGN HAR0 HSG0
LOCAL REAL    HSG1 SGLAGI NUMLAGS HAR2 HSG2 SGLAGS LAGT HAR3 HSG3 SGLAGT LAGST HAR4 HSG4 SGLAGST SGLAGN LAGI HAR1
*
INQUIRE(SERIES=SERIES) STARTL>>START ENDL>>END
*
SET Y1   =  SERIES + SERIES{1} + SERIES{2} + SERIES{3}
SET Y2   = -SERIES + SERIES{1} - SERIES{2} + SERIES{3}
SET Y3   = -SERIES + SERIES{2}
SET Y4   =  SERIES - SERIES{4}
SET TTREND STARTL ENDL = T
SEA SDUM
*
DIS
DIS 'Testing for Seasonality in'  %LABEL(SERIES) @-1 '. Sample from' $
     %DATELABEL(startl) 'to' %DATELABEL(endl) @-1 ', for a total of' endl-startl+1 'Observations.'
DIS  @10 'PI1' @+4 'PI2' @+4 'PI3' @+4 'PI4' @+4 'F34' @+3 'F234' $
     @+3 'F1234' @+1 'Lags' @+4 'AR(1-4)' @+5 'Last lag t'
IF DET==1.OR.DET==6 {
IF NLAGS==0
{
LINREG(NOPRINT) Y4 STARTL ENDL RS0
# Y1{1} Y2{1} Y3{2} Y3{1}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT)
	# Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT)
	# Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT)
	# Y3{1 TO 2}
	COM F34 = %CDSTAT
LINREG(NOPRINT) RS0 STARTL+4 ENDL
# RS0{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1}
	EXC(NOPRINT)
	# RS0{1 TO 4}
	COM HAR0 = %CDSTAT
	COM HSG0 = %SIGNIF
	}
ELSE	{
LINREG(NOPRINT) Y4 STARTL+NLAGS+4 ENDL RS0
# Y1{1} Y2{1} Y3{2} Y3{1} Y4{1 TO NLAGS}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT)
	# Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT)
	# Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT)
	# Y3{1 TO 2}
	COM F34 = %CDSTAT
	COM LAGN=(%BETA(4+NLAGS))/SQRT(%SEESQ*%XX(4+NLAGS,4+NLAGS))
	CDF(NOPRINT) TTEST LAGN %NDF
	COM SGLAGN=%SIGNIF
LINREG(NOPRINT) RS0 STARTL+4 ENDL
# RS0{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} Y4{1 TO NLAGS}
	EXC(NOPRINT)
	# RS0{1 TO 4}
	COM HAR0 = %CDSTAT
	COM HSG0 = %SIGNIF
}
IF NLAGS==0
COM LAGN=SGLAGN=0
disp 'None' @+3 ##.### pi1 @+1 ##.### pi2 @+1 ##.### pi3 @+1 ##.### pi4 $
@+1 ##.### f34 @+1 ##.### f234 @+1 ##.### f1234 @+2 nlags @+1 ##.### har0 $
@-1 '[' @-1 #.### hsg0 @-1 ']' @+1 ##.### lagn @-1 '[' @-1 #.### sglagn @-1 ']'
}
IF DET==2.OR.DET==6 {
IF ILAGS==0 {
LINREG(NOPRINT) Y4 STARTL ENDL RS1
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
LINREG(NOPRINT) RS1 STARTL+4 ENDL
# RS1{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT
	EXC(NOPRINT)
	# RS1{1 TO 4}
	COM HAR1 = %CDSTAT
	COM HSG1 = %SIGNIF
	}
ELSE	{
LINREG(NOPRINT) Y4 STARTL+ILAGS ENDL RS1
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT Y4{1 TO ILAGS}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
	COM LAGI=(%BETA(5+ILAGS))/SQRT(%SEESQ*%XX(5+ILAGS,5+ILAGS))
	CDF(NOPRINT) TTEST LAGI %NDF
	COM SGLAGI=%SIGNIF
LINREG(NOPRINT) RS1 STARTL+ILAGS+4 ENDL
# RS1{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT Y4{1 TO ILAGS}
	EXC(NOPRINT); # RS1{1 TO 4}
	COM HAR1 = %CDSTAT
	COM HSG1 = %SIGNIF
	}
IF ILAGS==0
COM LAGI=SGLAGI=0
disp 'I only' @+1 ##.### pi1 @+1 ##.### pi2 @+1 ##.### pi3 @+1 ##.### pi4 $
@+1 ##.### f34 @+1 ##.### f234 @+1 ##.### f1234 @+2 ilags @+1 ##.### har1 $
@-1 '[' @-1 #.### hsg1 @-1 ']' @+1 ##.### lagi @-1 '[' @-1 #.### sglagi @-1 ']'
}
IF DET==3.OR.DET==6 {
IF SLAGS==0 {
LINREG(NOPRINT) Y4 STARTL ENDL RS2
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT SDUM{-2 TO 0}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
LINREG(NOPRINT) RS2 STARTL+4 ENDL
# RS2{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT SDUM{-2 TO 0}
	EXC(NOPRINT); # RS2{1 TO 4}
	COM HAR2 = %CDSTAT
	COM HSG2 = %SIGNIF
	}
ELSE 	{
LINREG(NOPRINT) Y4 STARTL+SLAGS ENDL RS2
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT SDUM{-2 TO 0} Y4{1 TO SLAGS}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
	COM NUMLAGS=(%BETA(8+SLAGS))/SQRT(%SEESQ*%XX(8+SLAGS,8+SLAGS))
	CDF(NOPRINT) TTEST NUMLAGS %NDF
	COM SGLAGS=%SIGNIF
LINREG(NOPRINT) RS2 STARTL+SLAGS+4 ENDL
# RS2{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT SDUM{-2 TO 0} Y4{1 TO SLAGS}
EXC(NOPRINT); # RS2{1 TO 4}
	COM HAR2 = %CDSTAT
	COM HSG2 = %SIGNIF
	}
IF SLAGS==0
COM NUMLAGS=SGLAGS=0
disp 'I,SD' @+3 ##.### pi1 @+1 ##.### pi2 @+1 ##.### pi3 @+1 ##.### pi4 $
@+1 ##.### f34 @+1 ##.### f234 @+1 ##.### f1234 @+2 slags @+1 ##.### har2 $
@-1 '[' @-1 #.### hsg2 @-1 ']' @+1 ##.### numlags @-1 '[' @-1 #.### sglags @-1 ']'
}
IF DET==4.OR.DET==6
{
IF TLAGS==0
	{
LINREG(NOPRINT) Y4 STARTL ENDL RS3
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
LINREG(NOPRINT) RS3 STARTL+4 ENDL
# RS3{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND
	EXC(NOPRINT); # RS3{1 TO 4}
	COM HAR3 = %CDSTAT
	COM HSG3 = %SIGNIF
	}
ELSE 	{
LINREG(NOPRINT) Y4 STARTL+TLAGS ENDL RS3
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND Y4{1 TO TLAGS}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
	COM LAGT=(%BETA(6+TLAGS))/SQRT(%SEESQ*%XX(6+TLAGS,6+TLAGS))
	CDF(NOPRINT) TTEST LAGT %NDF
	COM SGLAGT=%SIGNIF
LINREG(NOPRINT) RS3 STARTL+TLAGS+4 ENDL
# RS3{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND Y4{1 TO TLAGS}
	EXC(NOPRINT); # RS3{1 TO 4}
	COM HAR3 = %CDSTAT
	COM HSG3 = %SIGNIF
	}
IF TLAGS==0
COM LAGT=SGLAGT=0
disp 'I,Tr' @+3 ##.### pi1 @+1 ##.### pi2 @+1 ##.### pi3 @+1 ##.### pi4 $
@+1 ##.### f34 @+1 ##.### f234 @+1 ##.### f1234 @+2 tlags @+1 ##.### har3 $
@-1 '[' @-1 #.### hsg3 @-1 ']' @+1 ##.### lagt @-1 '[' @-1 #.### sglagt @-1 ']'
}
IF DET==5.OR.DET==6
{
IF STLAGS==0 	{
LINREG(NOPRINT) Y4 STARTL ENDL RS4
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND SDUM{-2 TO 0}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
	EXC(NOPRINT); # Y1{1} TTREND
	COM OF = %CDSTAT
LINREG(NOPRINT) RS4 STARTL+4 ENDL
# RS4{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND SDUM{-2 TO 0}
	EXC(NOPRINT); # RS4{1 TO 4}
	COM HAR4 = %CDSTAT
	COM HSG4 = %SIGNIF
	}
ELSE 	{
LINREG(NOPRINT) Y4 STARTL+STLAGS+4 ENDL RS4
# Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND SDUM{-2 TO 0} Y4{1 TO STLAGS}
	COM PI1=(%BETA(1))/SQRT(%SEESQ*%XX(1,1))
	COM PI2=(%BETA(2))/SQRT(%SEESQ*%XX(2,2))
	COM PI3=(%BETA(3))/SQRT(%SEESQ*%XX(3,3))
	COM PI4=(%BETA(4))/SQRT(%SEESQ*%XX(4,4))
	EXC(NOPRINT); # Y1{1} Y2{1} Y3{1 TO 2}
	COM F1234 = %CDSTAT
	EXC(NOPRINT); # Y2{1} Y3{1 TO 2}
	COM F234 = %CDSTAT
	EXC(NOPRINT); # Y3{1 TO 2}
	COM F34 = %CDSTAT
	COM LAGST=(%BETA(9+STLAGS))/SQRT(%SEESQ*%XX(9+STLAGS,9+STLAGS))
	CDF(NOPRINT) TTEST LAGST %NDF
	COM SGLAGST=%SIGNIF
LINREG(NOPRINT) RS4 STARTL+STLAGS+4 ENDL
# RS4{1 TO 4} Y1{1} Y2{1} Y3{2} Y3{1} CONSTANT TTREND SDUM{-2 TO 0} Y4{1 TO STLAGS}
	EXC(NOPRINT); # RS4{1 TO 4}
	COM HAR4 = %CDSTAT
	COM HSG4 = %SIGNIF
}
IF STLAGS==0
COM LAGST=SGLAGST=0
disp 'I,SD,Tr' @+0 ##.### pi1 @+1 ##.### pi2 @+1 ##.### pi3 @+1 ##.### pi4 $
@+1 ##.### f34 @+1 ##.### f234 @+1 ##.### f1234 @+2 stlags @+1 ##.### har4 $
@-1 '[' @-1 #.### hsg4 @-1 ']' @+1 ##.### lagst @-1 '[' @-1 #.### sglagst @-1 ']'
}
end



