Zivot-Andrews Unit Root Test

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

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

Zivot-Andrews Unit Root Test

Postby trubador » Mon Oct 05, 2009 6:59 am

Attached is a subroutine to carry out Zivot-Andrews procedure for testing unit root in the case of a single break in the series (i.e. level, trend or both). Please feel free the optimize the code further.


Code: Select all

'Zivot-Andrews Unit Root Test
'Reference: Zivot, E. and Andrews, D. W. K. (1992), “Further Evidence on the Great Crash, the Oil-Price Shock, and the Unit-Root Hypothesis”, Journal of Business & Economic Statistics, Vol. 10, No. 3, pp. 251-270.


call zivot(y,"C",4)

' ----------------------------------------------------------------------------------------------------
' Arguments
'-----------------------------------------------------------------------------------------------------
'series Y                ' dependent variable
'scalar Maxlag       ' Maximum number of lags for unit root testing
'string %Model      ' Location of the break ("A" = Intercept, "B" = Trend, "C" = Both)
' ----------------------------------------------------------------------------------------------------



subroutine zivot(series y,string %Model,scalar maxlag)
!trim = 0.15 'Trimming parameter
series DY = D(Y)

!nobs = @obs(y)-maxlag-1

smpl @first+maxlag+1 @last
equation temp.ls dy c @trend y(-1)
!aic0 = log(temp.@ssr/!nobs)+2*(temp.@ncoef/!nobs)
!bic0 = log(temp.@ssr/!nobs)+ log(!nobs)*(temp.@ncoef/!nobs)
!min_aic = !aic0

for !lag=maxlag to 1 step -1
  equation temp.ls dy y(-1) c @trend dy(-1 to -!lag)
  !aic = log(temp.@ssr/!nobs)+2*(temp.@ncoef/!nobs)
  !bic = log(temp.@ssr/!nobs)+log(!nobs)*(temp.@ncoef/!nobs)
      if !aic < !min_aic then
       !min_aic = !aic
       !best_lag = !lag
        else if !min_aic = !aic0 then
        !best_lag =0
        endif
     endif
next

smpl @all
!znobs = @obs(y) - !best_lag
!lower = 1+!best_lag+@round(!znobs*!trim)
!upper =  @obs(y)-@round(!znobs*!trim)

vector(!upper-!lower+1) results

smpl @first + !best_lag @last
for !i = !lower to !upper
  if !best_lag=0 and %Model = "A" then
     equation temp.ls DY Y(-1) C @trend (@trend>!i-2)
     else if !best_lag=0 and %Model = "B" then
        equation temp.ls DY Y(-1) C @trend (@trend>!i-2)*(@trend-!i+2)
       else if !best_lag=0 and %Model = "C" then
          equation temp.ls DY Y(-1) C @trend (@trend>!i-2) (@trend>!i-2)*(@trend-!i+2)
         else if !best_lag>0 and %Model = "A" then
            equation temp.ls DY Y(-1) C @trend (@trend>!i-2) DY(-1 to -!best_lag)
           else if !best_lag>0 and %Model = "B" then
              equation temp.ls DY Y(-1) C @trend (@trend>!i-2)*(@trend-!i+2) DY(-1 to -!best_lag)
              else if !best_lag>0 and %Model = "C" then
                 equation temp.ls DY Y(-1) C @trend (@trend>!i-2) (@trend>!i-2)*(@trend-!i+2) DY(-1 to -!best_lag)
              endif
           endif
         endif
       endif
     endif
  endif
  results(!i-!lower+1) = temp.@tstats(1)
next

vector t_min =@cmin(results)
!t_min = t_min(1)
vector break = @cimin(results)+!lower-1
!break = break(1)

            series DT = (@trend>!break-2)*(@trend-!break+2)
              if %Model = "A" or %Model="C" then
                series DU = @trend> !break-2
              endif
         if !best_lag=0 and %Model="A" then
           equation ZA.ls DY Y(-1) C @trend DU 'Selected equation
            else if !best_lag=0 and %Model="B" then
              equation ZA.ls DY Y(-1) C @trend DT 'Selected equation
                else if !best_lag=0 and %Model="C" then
                  equation ZA.ls DY Y(-1) C @trend DU DT 'Selected equation
                    else if !best_lag>0 and %Model = "A" then
                       equation ZA.ls DY Y(-1) C @trend DU DY(-1 to -!best_lag) 'Selected equation
                       else if !best_lag>0 and %Model = "B" then
                          equation ZA.ls DY Y(-1) C @trend DT DY(-1 to -!best_lag) 'Selected equation
                            else if !best_lag>0 and %Model = "C" then
                               equation ZA.ls DY Y(-1) C @trend DU DT DY(-1 to -!best_lag) 'Selected equation
                            endif             
                       endif
                    endif
                 endif
            endif
         endif

table(6,2) ZAZ
ZAZ(1,1) = "Variable(s)"
ZAZ(3,1) = "t-stat(s)"
ZAZ(4,1) = "Lag(s)"
ZAZ(5,1) = "Break"
ZAZ(6,1) = "DU1 p-value"
ZAZ(1,2) = y.@name
ZAZ(3,2) = !t_min
ZAZ(4,2) = !best_lag
if @datestr(@now,"F") = "?" then
  ZAZ(5,2) = !break
else
  ZAZ(5,2) = @otod(!break)
endif
ZAZ(6,2) = @tdist(za.@tstat(4),za.@regobs-za.@ncoef)
setline(ZAZ, 2)
show ZAZ
smpl @all

delete temp break results t_min
endsub

hetero
Posts: 18
Joined: Tue Sep 08, 2009 11:40 am

Re: Zivot-Andrews Unit Root Test

Postby hetero » Tue Oct 06, 2009 2:49 am

Hi,

I have compared the results delivered from the above E-views code with those delivered from the corresponding RATS code. They are identical. This is a perfect piece of work. We are grateful Trubador!!

For users convenience the asymptotic critical values are provided below:
model Α model Β model C
1% -5.34 -4.93 -5.57
5% -4.80 -4.42 -5.08
10% -4.58 -4.11 -4.82

Regards

adwy
Posts: 6
Joined: Sun Aug 02, 2009 1:02 am

Re: Zivot-Andrews Unit Root Test

Postby adwy » Wed Oct 07, 2009 12:51 am

Thank you so much Trubador! You are so nice. Cheers.

hetero
Posts: 18
Joined: Tue Sep 08, 2009 11:40 am

Re: Zivot-Andrews Unit Root Test

Postby hetero » Tue Oct 20, 2009 3:13 am

Dear Trubador,

It would be of great pleasure if a Monte Carlo simulation was also applied to calculate the exact critical values according to the sample size used each time. Is it hard to program something like that? Once again thank you for your contribution!

Regards

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

Re: Zivot-Andrews Unit Root Test

Postby trubador » Thu Oct 22, 2009 10:18 am

hetero wrote:It would be of great pleasure if a Monte Carlo simulation was also applied to calculate the exact critical values according to the sample size used each time.

Yes, you are right. This will also complete program and fully replicate the original study.
hetero wrote:Is it hard to program something like that?

It is not that much a difficult task, but definitely requires a different way of programming.

hetero
Posts: 18
Joined: Tue Sep 08, 2009 11:40 am

Re: Zivot-Andrews Unit Root Test

Postby hetero » Fri Oct 23, 2009 3:43 am

Hi Trubador,

Please forgive me for the indiscreet question but is there any intension to complete the program by incorporating Monte Carlo simulations?

Kind Regards

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

Re: Zivot-Andrews Unit Root Test

Postby trubador » Sat Oct 24, 2009 7:12 am

It is in my mind, but I do not think I can spare the time in the foreseeable future.

nat
Posts: 4
Joined: Wed Nov 04, 2009 10:51 am

Re: Zivot-Andrews Unit Root Test

Postby nat » Wed Nov 04, 2009 11:15 am

Hello,
I have a question. When I tried to run this programm in E-views the Error message appeared and it said that @cmin command from

"vector t_min =@cmin(results)
!t_min = t_min(1)
vector break = @cimin(results)+!lower-1
!break = break(1)"

is illigal or allready used. Could you tell me what can be wrong?
Thank you.

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

Re: Zivot-Andrews Unit Root Test

Postby trubador » Wed Nov 04, 2009 12:05 pm

You are probably using an earlier version of EViews. Those functions were introduced as of version 6 and are not supported in earlier versions of EViews. Please try the (slightly) modified code below:

Code: Select all

'Zivot-Andrews Unit Root Test
'Reference: Zivot, E. and Andrews, D. W. K. (1992), “Further Evidence on the Great Crash, the Oil-Price Shock, and the Unit-Root Hypothesis”, Journal of Business & Economic Statistics, Vol. 10, No. 3, pp. 251-270.


call zivot(y,"C",4)

' ----------------------------------------------------------------------------------------------------
' Arguments
'-----------------------------------------------------------------------------------------------------
'series Y                ' dependent variable
'scalar Maxlag       ' Maximum number of lags for unit root testing
'string %Model      ' Location of the break ("A" = Intercept, "B" = Trend, "C" = Both)
' ----------------------------------------------------------------------------------------------------



subroutine zivot(series y,string %Model,scalar maxlag)
!trim = 0.15 'Trimming parameter
series DY = D(Y)

!nobs = @obs(y)-maxlag-1

smpl @first+maxlag+1 @last
equation temp.ls dy c @trend y(-1)
!aic0 = log(temp.@ssr/!nobs)+2*(temp.@ncoef/!nobs)
!bic0 = log(temp.@ssr/!nobs)+ log(!nobs)*(temp.@ncoef/!nobs)
!min_aic = !aic0

for !lag=maxlag to 1 step -1
  equation temp.ls dy y(-1) c @trend dy(-1 to -!lag)
  !aic = log(temp.@ssr/!nobs)+2*(temp.@ncoef/!nobs)
  !bic = log(temp.@ssr/!nobs)+log(!nobs)*(temp.@ncoef/!nobs)
      if !aic < !min_aic then
       !min_aic = !aic
       !best_lag = !lag
        else if !min_aic = !aic0 then
        !best_lag =0
        endif
     endif
next

smpl @all
!znobs = @obs(y) - !best_lag
!lower = 1+!best_lag+@round(!znobs*!trim)
!upper =  @obs(y)-@round(!znobs*!trim)

vector(!upper-!lower+1) results

smpl @first + !best_lag @last
for !i = !lower to !upper
  if !best_lag=0 and %Model = "A" then
     equation temp.ls DY Y(-1) C @trend (@trend>!i-2)
     else if !best_lag=0 and %Model = "B" then
        equation temp.ls DY Y(-1) C @trend (@trend>!i-2)*(@trend-!i+2)
       else if !best_lag=0 and %Model = "C" then
          equation temp.ls DY Y(-1) C @trend (@trend>!i-2) (@trend>!i-2)*(@trend-!i+2)
         else if !best_lag>0 and %Model = "A" then
            equation temp.ls DY Y(-1) C @trend (@trend>!i-2) DY(-1 to -!best_lag)
           else if !best_lag>0 and %Model = "B" then
              equation temp.ls DY Y(-1) C @trend (@trend>!i-2)*(@trend-!i+2) DY(-1 to -!best_lag)
              else if !best_lag>0 and %Model = "C" then
                 equation temp.ls DY Y(-1) C @trend (@trend>!i-2) (@trend>!i-2)*(@trend-!i+2) DY(-1 to -!best_lag)
              endif
           endif
         endif
       endif
     endif
  endif
  results(!i-!lower+1) = temp.@tstats(1)
next

vector t_min =@min(results)
!t_min = t_min(1)

vector break
  !i =1
    while !i<=!upper-!lower+1
     if results(!i) = @min(results) then
       break = !i+!lower-1
     endif
       !i = !i+1
    wend

!break = break(1)

            series DT = (@trend>!break-2)*(@trend-!break+2)
              if %Model = "A" or %Model="C" then
                series DU = @trend> !break-2
              endif
         if !best_lag=0 and %Model="A" then
           equation ZA.ls DY Y(-1) C @trend DU 'Selected equation
            else if !best_lag=0 and %Model="B" then
              equation ZA.ls DY Y(-1) C @trend DT 'Selected equation
                else if !best_lag=0 and %Model="C" then
                  equation ZA.ls DY Y(-1) C @trend DU DT 'Selected equation
                    else if !best_lag>0 and %Model = "A" then
                       equation ZA.ls DY Y(-1) C @trend DU DY(-1 to -!best_lag) 'Selected equation
                       else if !best_lag>0 and %Model = "B" then
                          equation ZA.ls DY Y(-1) C @trend DT DY(-1 to -!best_lag) 'Selected equation
                            else if !best_lag>0 and %Model = "C" then
                               equation ZA.ls DY Y(-1) C @trend DU DT DY(-1 to -!best_lag) 'Selected equation
                            endif             
                       endif
                    endif
                 endif
            endif
         endif

table(6,2) ZAZ
ZAZ(1,1) = "Variable(s)"
ZAZ(3,1) = "t-stat(s)"
ZAZ(4,1) = "Lag(s)"
ZAZ(5,1) = "Break"
ZAZ(6,1) = "DU1 p-value"
ZAZ(1,2) = y.@name
ZAZ(3,2) = !t_min
ZAZ(4,2) = !best_lag
ZAZ(5,2) = @otod(!break)
ZAZ(6,2) = @tdist(za.@tstat(4),za.@regobs-za.@ncoef)
setline(ZAZ, 2)
show ZAZ
smpl @all

delete temp break results t_min
endsub
Last edited by trubador on Fri Nov 06, 2009 5:59 am, edited 1 time in total.

nat
Posts: 4
Joined: Wed Nov 04, 2009 10:51 am

Re: Zivot-Andrews Unit Root Test

Postby nat » Wed Nov 04, 2009 12:18 pm

Thank you Trubador, youou are right, I am using 4.0 version. But the lighter version which you sent has some problems as well. It does not recognize
@datestr in
"if @datestr(@now,"F") = "?" then".
So I will try to find upper version.
Thank you Trubador.

nat
Posts: 4
Joined: Wed Nov 04, 2009 10:51 am

Re: Zivot-Andrews Unit Root Test

Postby nat » Wed Nov 04, 2009 12:25 pm

I am sorry version which I am using is 4.1, not 4.0. After showing an error message (which I mentioned above), I noticed that two equations Temp and ZA were created in my workfile, and ZaZ table. Is it what I have to have after this programm? In this case it seems that no problem?

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

Re: Zivot-Andrews Unit Root Test

Postby trubador » Wed Nov 04, 2009 12:41 pm

Since you are using an older version, you may not always be able to run codes written in newer versions. In this case, however, I do not think that you are facing a serious problem. You can remove the following part altogether:

Code: Select all

 if @datestr(@now,"F") = "?" then
  ZAZ(5,2) = !break
else
  ZAZ(5,2) = @otod(!break)
endif

and replace it with the following single line, since @otod provides a more general use:

Code: Select all

  ZAZ(5,2) = @otod(!break)


I have already edited my previous post in the same way...
Last edited by trubador on Fri Nov 06, 2009 6:01 am, edited 1 time in total.

nat
Posts: 4
Joined: Wed Nov 04, 2009 10:51 am

Re: Zivot-Andrews Unit Root Test

Postby nat » Thu Nov 05, 2009 5:02 am

Dear Trubador,
Thank you very much! It worked!
Thanks again!

donihue
Posts: 135
Joined: Wed Oct 07, 2009 8:51 am

Re: Zivot-Andrews Unit Root Test

Postby donihue » Sat Nov 21, 2009 5:27 am

Following up on this very helpful exchange:
Would you have written a code for the Clemente-Montanes-Reyes unit root test, which also alows for two structural breaks (it is implemented in Stata as "clemao" or "clemio", depending on form)?
Many thanks
Regards
Donihue

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

Re: Zivot-Andrews Unit Root Test

Postby trubador » Wed Nov 25, 2009 11:27 am

donihue wrote:Following up on this very helpful exchange:
Would you have written a code for the Clemente-Montanes-Reyes unit root test, which also alows for two structural breaks (it is implemented in Stata as "clemao" or "clemio", depending on form)?
Many thanks
Regards
Donihue

As you might have noticed, we tried to write a code for Lumsdaine-Papell unit root test. However, we did not have enough time to finish it. I cannot promise anytime soon, but I will complete that work as soon as I have spare time.

Actually, I am not an expert on unit root testing and do not follow the recent developments in the literature very closely. I do know that there are lots of variants of unit root tests, each of which serves to a different purpose. I think it would be useful and helpful to implement unit root tests in EViews that are commonly used or proved to be robust in practice.


Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 6 guests