Page 1 of 8

Gregory-Hansen Cointegration Test

Posted: Sun Jun 14, 2009 9:45 am
by trubador
Attached is a subroutine to carry out Gregory-Hansen procedure for testing cointegration in the case of level, trend and regime shifts. Building the procedure in EViews is a joint work of myself and tcfoon. We urge you to see the original study for details. Comments are welcomed and the usual disclaimer applies.


Code: Select all

'Gregory-Hansen Cointegration Test
'Reference: Gregory, A. W. and Hansen, B. E. (1996). "Residual-Based Tests for Cointegration in Models with Regime Shifts", Journal of Econometrics, Vol. 70, pp. 99-126.

call greghansen(y,x,2,"aic",6)

' ----------------------------------------------------------------------------------------------------
' Arguments
'-----------------------------------------------------------------------------------------------------
'series Y                 ' dependent variable
'group G                 ' group of independent variable(s) (including single series)
'scalar Model         ' 2 = Level Shift, 3 = Level Shift with Trend, 4 = Regime Shift
'scalar Maxlag       ' Maximum number of lags for unit root testing
'string %Criterion  ' Selection criteria for unit root testing (i.e. aic / sic / hqc)
' ----------------------------------------------------------------------------------------------------

subroutine greghansen(series Y, group G, scalar Model, string %Criterion, scalar Maxlag)
smpl @all
!trim = 0.15
!maxlag = Maxlag

!n = @obs(y)
!nindep = G.@count

!lower = @round(@obs(Y)*!trim)
!upper = @round(@obs(Y)*(1-!trim))
matrix(!upper-!lower+1,4) GHtest

equation ghc

Table GHZ
GHZ(1,1) =  "THE GREGORY-HANSEN"
GHZ(2,1) = "COINTEGRATION TEST"
if Model=2 then GHZ(3,1) = "MODEL 2: Level Shift"
 else if Model =3 then GHZ(3,1) = "MODEL 3: Level Shift with Trend"
   else if Model = 4 then GHZ(3,1) = "MODEL 4: Regime Shift"
   endif
 endif
endif
GHZ(5,1) = "ADF Procedure"
GHZ(7,1) = "t-stat"
GHZ(8,1) = "Lag"
GHZ(9,1) = "Break"
GHZ(11,1) = "Phillips Procedure"
GHZ(13,1) = "Za-stat"
GHZ(14,1) = "Za-break"
GHZ(15,1) = "Zt-stat"
GHZ(16,1) = "Zt-break"

for !ref = 2 to 4
  GHZ.setwidth(!ref) 15
next

GHZ.setlines(a4:b4) +d
GHZ.setlines(a6:b6) +d
GHZ.setlines(a10:b10) +d
GHZ.setlines(a12:b12) +d

for !i = !lower to !upper

  if Model=2 then
   'MODEL 2 - C: LEVEL SHIFT MODEL
     ghc.ls Y c G (@trend>!i-2)
     ghc.makeresid res
     uroot(adf, none, info={%criterion}, maxlag=!maxlag, save=level) res
     GHtest(!i-!lower+1,1) = level(3,1)
     GHtest(!i-!lower+1,2) = level(2,1)
     call phillips(res)   
     GHtest(!i-!lower+1,3) = !Za
     GHtest(!i-!lower+1,4) = !Zt
 
   else if Model=3 then
   'MODEL 3 - C/T: LEVEL SHIFT WITH TREND MODEL
     ghc.ls Y c @trend G (@trend>!i-2)
     ghc.makeresid res
     uroot(adf, none, info={%criterion}, maxlag=!maxlag, save=level) res
     GHtest(!i-!lower+1,1) = level(3,1)
     GHtest(!i-!lower+1,2) = level(2,1)
     call phillips(res)   
     GHtest(!i-!lower+1,3) = !Za
     GHtest(!i-!lower+1,4) = !Zt
   
   else if Model = 4 then
   'MODEL 4 - C/S: REGIME SHIFT MODEL
       for !g = 1 to !nindep
         G.add (@trend>!i-2)*G(!g)
       next
     ghc.ls Y c (@trend>!i-2) G
     ghc.makeresid res
     uroot(adf, none, info={%criterion}, maxlag=!maxlag, save=level) res
     GHtest(!i-!lower+1,1) = level(3,1)
     GHtest(!i-!lower+1,2) = level(2,1)
     call phillips(res)   
     GHtest(!i-!lower+1,3) = !Za
     GHtest(!i-!lower+1,4) = !Zt
       for !g = G.@count to !nindep+1 step -1
        %name = G.@seriesname(!g)
         G.drop {%name}
       next
     endif
    endif
   endif
next
     vector min_t_lag = @cmin(GHtest)
     vector break = @cimin(GHtest)

     GHZ(7,2) = min_t_lag(1)
     GHZ(8,2) = GHtest(break(1),2)
     GHZ(13,2) = min_t_lag(3)
     GHZ(15,2) = min_t_lag(4)

    if @datestr(@now,"F") = "?" then
     GHZ(9,2) = break(1) + !lower - 2
     GHZ(14,2) = break(3) + !lower - 2
     GHZ(16,2) = break(4) + !lower - 2
    else
     GHZ(9,2) = @otod(break(1) + !lower - 2)
     GHZ(14,2) = @otod(break(3) + !lower - 2)
     GHZ(16,2) = @otod(break(4) + !lower - 2)
    endif

     show GHZ

delete res level GHtest break min_t_lag
endsub

subroutine phillips(series y) 'MATLAB code of this routine is available at Bruce E. Hansen's website: http://www.ssc.wisc.edu/~bhansen/progs/joe_96.html
!n = @obs(y)
equation eq1.ls y y(-1)
!be = eq1.@coefs(1)
series ue = y - !be*y(-1)

'Bandwidth selection
!nu = @obs(ue)
equation eq2.ls ue ue(-1)
!bu = eq2.@coefs(1)
series uu = ue - !bu*ue(-1)
!su = @sumsq(uu)/@obs(uu)
!a2 = (4*!bu^2*!su/(1-!bu)^8)/(!su/(1-!bu)^4)
!bw =1.3221*((!a2*!nu)^0.2)
     
!pi = @acos(-1)
!j=1
!lemda = 0
     while !j <= !bw
        series temp = ue*ue(-!j)
        !gama =  @sum(temp)/!nu
        !w=(75/(6*!pi*!j/!bw)^2)*(@sin(1.2*!pi*!j/!bw)/(1.2*!pi*!j/!bw)-@cos(1.2*!pi*!j/!bw))
        !lemda=!lemda+!w*!gama
        !j=!j+1
     wend
     
series temp = y*y(-1) - !lemda
!p = @sum(temp)/@sumsq(y(-1))
!Za = !n*(!p-1)
!Zt = (!p-1)/@sqrt((2*!lemda + @sumsq(ue)/!nu)/(@sumsq(y(-1))))
smpl @all
delete eq1 eq2 ue uu temp
endsub

Re: Gregory-Hansen Cointegration Test

Posted: Fri Jun 26, 2009 10:55 am
by fmramos
Trubador, Thanks a lot for your contributions. I'm a young MA economist in Brazil and your tips are teaching me a lot (I think, they're teaching most of us). Fabio.

Re: Gregory-Hansen Cointegration Test

Posted: Sat Jun 27, 2009 2:52 pm
by trubador
Thanks for the kind compliment. Indeed it would be my pleasure if I was able to help anyone who is interested in these subjects.

Re: Gregory-Hansen Cointegration Test

Posted: Tue Jun 30, 2009 7:13 pm
by grangel
Hi Trubador,

I am trying to improve on testing for evidence of asset price bubbles and came across your program repository. Unfortunately, I am very good in programming. I must say however the collaborative work you have undertaken is impressive. I was wondering whether you could help me with a methodology for detecting collapsing bubbles via a Markov Switching Unit Root Test since you have done earlier work on regime switching. The work I am referring to is Hall, Psaradakis, and Sola (1999). Detecting periodically collapsing bubbles: A markov-switching unit root test. Journal of Applied Econometrics, 14(2), 143-154. Appreciate if could advise whether a program is available to conduct this unit root test. Thanks in advance.

Re: Gregory-Hansen Cointegration Test

Posted: Wed Jul 01, 2009 10:35 am
by trubador
First of all, thank you very much for introducing me to the article.

The concept of change in regime is handled differently in Gregory and Hansen vs. Hall, Psaradakis, and Sola. While the former defines regime shifting as a change in both intercept and trend, the latter requires regime switching to have markov property. Therefore, before writing a procedure for the unit-root test, you should be able to carry out the markov switching analysis. Although EViews currently does not have any built-in procedures to conduct markov switching analysis, it is possible to write a routine in EViews' programming language with available tools, which has been in my mind for a while. I cannot promise anytime soon, but I will give it a try (for the markov switching part at least). In the meantime, you can either write your own procedure or go for other packages that have already implemented the markov switching and/or the unit-root testing procedure.

Re: Gregory-Hansen Cointegration Test

Posted: Thu Jul 30, 2009 1:11 am
by grangel
My typo. I really wanted to say "Unfortunately, I am not very good at programming" in the second sentence.

Re: Gregory-Hansen Cointegration Test

Posted: Tue Sep 15, 2009 11:24 am
by Sputnik
Hello,

what is "bandwidth selection"? How is it choosen and where can i read about it?

cheers

Re: Gregory-Hansen Cointegration Test

Posted: Tue Sep 15, 2009 2:04 pm
by trubador
Bandwidth is a calibration parameter that controls the smoothness of the fitted curve and therefore is an important input in spectral/density estimation. Small bandwidth leads to higher variance, whereas large bandwidth leads to higher bias. I cannot think of any specific reference right now, but you can find detailed explanations on the subject in any of econometrics/statistics textbooks that have chapters devoted on spectral/density estimation.

Re: Gregory-Hansen Cointegration Test

Posted: Tue Sep 15, 2009 2:31 pm
by EViews Gareth
Wikipedia has a little on it.

Re: Gregory-Hansen Cointegration Test

Posted: Mon Oct 05, 2009 10:10 am
by wolly77
Sorry guys, but I'm new in Eviews programe language and I have some problems to run Gregory Hansen code. I have to run this code on this regressione: pce (dependent variable) ylyt stnw nstnw fedf_n. Anyone could be so kind to suggest to me how I have to modify the above code? (that is, the rows of code to modify to my purposes). Thanks in advance.
Davide

Re: Gregory-Hansen Cointegration Test

Posted: Mon Oct 05, 2009 11:04 am
by trubador
Open a new program window (File/New/Program). Copy and paste the whole code. Just change the part below:

Code: Select all

group independents
independents.add ylyt stnw nstnw fedf_n
call greghansen(pce,independents,3,"aic",8)


The code above first groups your independent variables and names them as "independents". After that, it calls the subroutine that will carry out the Gregory-Hansen Cointegration test. In the specification above, the analysis will assume a "level shift with trend". The analysis will be carried out up to maximum of 8 lags and Akaike information criterion will be used to decide the optimum lag length. You can change these arguments to suit your own need.

Re: Gregory-Hansen Cointegration Test

Posted: Mon Oct 05, 2009 2:48 pm
by wolly77
Thanks for you helpful trubador, but I have a problem. I have modified the code as you suggested me, but Eviews 6 gives this message of error: "!nindep is not defined or is an illegal command in "!nindep = G.@count"". What is the problem? Thanks again.
Davide

Re: Gregory-Hansen Cointegration Test

Posted: Mon Oct 05, 2009 4:18 pm
by EViews Gareth
Perhaps you (wolly77) could post the copy of the program as you currently have it, plus your workfile.

Re: Gregory-Hansen Cointegration Test

Posted: Mon Oct 05, 2009 4:53 pm
by wolly77
Ok, I send my workfile and the version of code used with the modifications suggested by trubador.

Re: Gregory-Hansen Cointegration Test

Posted: Mon Oct 05, 2009 4:56 pm
by EViews Gareth
You changed the definition of the subroutine, where you shouldn't. Change the following line:

Code: Select all

   subroutine greghansen(series pce, group independents, scalar Model, string %Criterion, scalar Maxlag)


back to:

Code: Select all

subroutine greghansen(series Y, group G, scalar Model, string %Criterion, scalar Maxlag)