```
# This function realizes Dickey's high frequency data statinarity test
#
# x - series to test
# ar_filter_order - order of AR(p) filter
# ar_order_fix - if Yes, ar_filter_order is selected by estimating AR(p) models for p=1..ar_filter_order
# and chosing p that minimizes BIC criterion
# deterministic - list of deterministic elements, such seasonal dummies, harmonics, trend
# lag_order - periodicity (lag) at which we look for unit root
# verbose - printing results of all stages or not
#
# returns p-value, H0: series is nonstationary at given periodicity (is SI(1))
#
series det_clean # series cleand by deterministic elements
series sd_det_clean # seasonal (at given lag_order) differences of series det_clean
matrix Phi # vector of coefficients of AR(p) filter
series FY # series det_clean cleaned by AR(p) filter with Phi coefficients
scalar DST_statistic # main test statistic
scalar DST_statistic_p # returned p-value of DST_statistic
scalar shift = 0 # do we have lagged or differenced series?
# We check structure of the sample
DST_statistic_p = DST_check_sample(&x, $nobs, $t1, &shift, verbose)
if (DST_statistic_p != 0)
return DST_statistic_p
endif
# We filter tested series by deterministic components
if (nelem(deterministic) != 0)
det_clean = DST_det_filter(&x, deterministic, verbose)
else
det_clean = x
endif
# We now create seasonal diffrencss of "cleaned" series for further anaysis
sd_det_clean = DST_sdiff(&det_clean, &lag_order)
# STEP 1 of DHF - looking for autoregressive filter
Phi = DST_ar_filter(&sd_det_clean, &ar_filter_order, ar_order_fix, verbose)
# STEP 2 of DHF - cleaning series det_clean by autoregressive filter with Phi coefficients
FY = DST_ar_clean(&det_clean, &Phi, &ar_filter_order, &shift)
# Retrive main test statistic (corrected) DST_statistic ~ N(0,1)
DST_statistic = DST_stat(&FY, &sd_det_clean, &lag_order, ar_filter_order, nelem(deterministic), verbose)
if (DST_statistic < 0)
DST_statistic_p = 2*(1-pvalue(z, DST_statistic))
elif (DST_statistic > 0)
DST_statistic_p = (1-2*pvalue(z, DST_statistic))
else
DST_statistic_p = pvalue(z, DST_statistic)
endif
if (verbose == 1)
printf "#################################################################\n"
printf "H0: series is nonstationary at given periodicity t-%d (is SI(1))\n\n", lag_order
if ar_order_fix == 0
printf "Final AR(p) order selected by BIC:\t%d\n", ar_filter_order
endif
printf "DST statistic (corrected):\t\t%f\n", DST_statistic
printf "p-value of DST:\t\t\t\t%.12f\n", DST_statistic_p
endif
return DST_statistic_p
```

```
# This function regres tested series with deterministic elements
# and returns residuals
#
# *x - pointer to tested series
# deterministic - list of deterministic components
#
# returns $uhat
#
if (verbose == 1)
ols x deterministic --simple-print
else
ols x deterministic --quiet
endif
return $uhat
```

```
# This function estimates AR(p) filter and returs Phi coefficients
#
# *x - pointer to series for which we estimate ARIMA(order,0,0) model
# *order - pointer to given AR(p) order
# fixed - boolean variable: if given order is fixed or maximal, so we
# have to find such ARIMA(p,0,0) specification that minimaze BIC criterion
#
# returns vector with AR(p) coefficients (Phi)
#
# We look for the best ARIMA(p,0,0) specification (in light of BIC criterion)
if (fixed == 0)
scalar BIC = 100000000000
loop for i=1..order --quiet
arima $i 0 0 ; x --nc --quiet
if $bic < BIC
BIC = $bic
order = i
endif
endloop
endif
# Estimate AR(order) model to retrive coefficients Phi
if (verbose == 1)
arima order 0 0 ; x --nc
else
arima order 0 0 ; x --nc --quiet
endif
return $coeff
```

```
# This function cleans given series by autoregressive filter
#
# *x - pointer to series to clean
# *beta - pointer to vector with coefficients (Phi)
# *order - poinetr to scalar with selected order of AR(p) filter
# *shift - pointer to scalar with shift of start of the series
#
# returns cleaned series FY
#
list X_lag = lags(order, x) # list of lagged series x
matrix x_lag = { X_lag } # matrix of lagged series x
matrix x_mat = x # vector of series x
x_mat = x_mat[order+shift+1:$t2] # subvector of x, where we omit first (order) observations
x_mat -= x_lag * beta # we now clean given series
# Now we create series FY to return
series FY = NA
s = $t1+order # the start of the sample range
r = rows(x_mat)
loop for (i=1; i<=r; i++) --quiet
FY[s] = x_mat[i]
s++
endloop
return FY
```

```
# Obvious...
return x - x(-lag)
```

```
# This function estimates main regression and retrive main test statistic
#
# *x - pointer to cleaned series to test (by deterministic and AR(p) filter)
# *sd_det_clean - pointer to seasonal (at lag lag_order) differences of cleaned
# series to test (by deterministic elements)
# *lag_order - periodicity od unit root we look for (lag)
# ar_filter_order - selected order of AR(p) filter
# nelem_deter - number of deterministic elements
#
# returns corrected main test statistic
#
scalar DST_st = 0
# Main test regression
ar_filter_order *= -1
sd_x = DST_sdiff(&x, &lag_order)
if (verbose == 1)
ols sd_x const x(-lag_order) sd_det_clean(-1 to ar_filter_order) --simple-print
else
ols sd_x const x(-lag_order) sd_det_clean(-1 to ar_filter_order) --quiet
endif
# t statistic of x(-lag_order) variable
DST_st = $coeff[2]/$stderr[2]
# Correction of t statistics to N(0,1)
DST_st += (1 + nelem_deter*sqrt(2))/(2*sqrt(lag_order))
return DST_st
```

```
# This function checks structure of sample and
scalar ret = 0
#Now check for any NAs embedded in the sample
smpl --contiguous
shift = nob - $nobs
if (nob != $nobs)
# check if we have lagged or differenced series
if ((shift + startobs) != $t1)
# time-series structure gone: must be missing values
ret = -1
if (verbose == 1)
printf "Missing values encountered!!!\n"
endif
return ret
endif
endif
return ret
```