Here is the code (which works, but it is a slow way to do it this way):
Code: Select all
wfcreate FILE u 1 200
genr y = nrnd
y.ppuroot (lag=6,model=C)
'Perron Unit Root Test under Structural Change
'Reference: Perron, P., (1997), “Further Evidence on Breaking Trend Functions in Macroeconomic Variables”, Journal of Econometrics, 80, pp. 355-385.
' ----------------------------------------------------------------------------------------------------
' Perron Subroutine - Arguments
'-----------------------------------------------------------------------------------------------------
'series Y ' Dependent variable
'string %Model ' Location of the break ("A" = Intercept, "B" = Both, "C" = Trend)
'scalar Maxlag ' Maximum number of lags for unit root testing
'table zaz ' Name of table object to put results into.
'series sertstats ' t statistics for each possible break date
' ----------------------------------------------------------------------------------------------------
subroutine local perron(series y,string %Model,scalar maxlag, table zaz, series sertstats)
local smpl
series smplser = 1
series cumsum = @cumsum(smplser)
!trim = 0.15 'Trimming parameter
series DY = D(Y)
!nobs = @obs(y)-maxlag-1
' Calculate Optimum Lag (Method "t-sig")
' smpl @first+maxlag+1 @last
smpl if cumsum>maxlag+1 and smplser=1
equation temp.ls dy c @trend y(-1) dy(-1 to -{maxlag})
!ktemp =maxlag + 3
!cont = 0
While (!cont = 0)
if (temp.@tstats(!ktemp) > 1.96) or (temp.@tstats(!ktemp)< -1.96) then
!best_lag = !ktemp - 3
!cont = 1
else if (!ktemp <> 3) then
!ktemp = !ktemp - 1
else
!best_lag= 0
!cont = 1
endif
endif
wend
'smpl @all
smpl if smplser=1
!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
smpl if cumsum>!best_lag and smplser=1
for !i = !lower to !upper
if !best_lag=0 and %Model = "A" then
equation temp.ls DY Y(-1) C @trend (@trend>!i-2) d((@trend>!i-2))
else if !best_lag=0 and %Model = "B" then
equation temp.ls DY Y(-1) C @trend (@trend>!i-2) d((@trend>!i-2)) (@trend>!i-2)*(@trend + 1)
else if !best_lag=0 and %Model = "C" then
equation detrend.ls y c @trend (@trend>!i-2)*(@trend-!i+2)
detrend.makeresids y_detrend
equation temp.ls D(y_detrend) y_detrend(-1)
else if !best_lag>0 and %Model = "A" then
equation temp.ls DY Y(-1) C @trend (@trend>!i-2) d((@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) d((@trend>!i-2)) (@trend>!i-2)*(@trend + 1) DY(-1 to -{!best_lag})
else if !best_lag>0 and %Model = "C" then
equation detrend.ls y c @trend (@trend>!i-2)*(@trend-!i+2)
detrend.makeresids y_detrend
series DY_detrend = d(y_detrend)
equation temp.ls DY_detrend y_detrend(-1) DY_detrend(-1 to -{!best_lag})
endif
endif
endif
endif
endif
endif
results(!i-!lower+1) = temp.@tstats(1)
next
smpl if cumsum>!lower-1
mtos(results,sertstats)
vector t_min =@cmin(results)
!t_min = t_min(1)
vector break = @cimin(results)+!lower-1
!break = break(1)
ZAZ(1,1) = "Variable(s)"
ZAZ(3,1) = "t-stat(s)"
ZAZ(4,1) = "Lag(s)"
ZAZ(5,1) = "Break"
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
setline(ZAZ, 2)
endsub