## Gregory-Hansen Cointegration Test

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

### Gregory-Hansen Cointegration Test

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) GHtestequation ghcTable GHZGHZ(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 endifendifGHZ(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) 15nextGHZ.setlines(a4:b4) +dGHZ.setlines(a6:b6) +dGHZ.setlines(a10:b10) +dGHZ.setlines(a12:b12) +dfor !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   endifnext     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 GHZdelete res level GHtest break min_t_lagendsubsubroutine 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 @alldelete eq1 eq2 ue uu tempendsub`

fmramos
Posts: 110
Joined: Fri Sep 19, 2008 7:30 am
Location: Sao Paulo / Brazil
Contact:

### Re: Gregory-Hansen Cointegration Test

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.

Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

### Re: Gregory-Hansen Cointegration Test

Thanks for the kind compliment. Indeed it would be my pleasure if I was able to help anyone who is interested in these subjects.

grangel
Posts: 3
Joined: Wed Oct 01, 2008 7:25 am

### Re: Gregory-Hansen Cointegration Test

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.

Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

### Re: Gregory-Hansen Cointegration Test

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.

grangel
Posts: 3
Joined: Wed Oct 01, 2008 7:25 am

### Re: Gregory-Hansen Cointegration Test

My typo. I really wanted to say "Unfortunately, I am not very good at programming" in the second sentence.

Sputnik
Posts: 2
Joined: Fri Aug 21, 2009 7:08 am

### Re: Gregory-Hansen Cointegration Test

Hello,

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

cheers

Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

### Re: Gregory-Hansen Cointegration Test

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.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13286
Joined: Tue Sep 16, 2008 5:38 pm

### Re: Gregory-Hansen Cointegration Test

Wikipedia has a little on it.

wolly77
Posts: 32
Joined: Mon Oct 05, 2009 4:28 am

### Re: Gregory-Hansen Cointegration Test

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

Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

### Re: Gregory-Hansen Cointegration Test

Open a new program window (File/New/Program). Copy and paste the whole code. Just change the part below:

Code: Select all

`group independentsindependents.add ylyt stnw nstnw fedf_ncall 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.

wolly77
Posts: 32
Joined: Mon Oct 05, 2009 4:28 am

### Re: Gregory-Hansen Cointegration Test

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

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13286
Joined: Tue Sep 16, 2008 5:38 pm

### Re: Gregory-Hansen Cointegration Test

Perhaps you (wolly77) could post the copy of the program as you currently have it, plus your workfile.

wolly77
Posts: 32
Joined: Mon Oct 05, 2009 4:28 am

### Re: Gregory-Hansen Cointegration Test

Ok, I send my workfile and the version of code used with the modifications suggested by trubador.
Attachments
prog_gh_prova.prg
my_workfile.WF1

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13286
Joined: Tue Sep 16, 2008 5:38 pm

### Re: Gregory-Hansen Cointegration Test

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)`