Allin Cottrell and Sven Schreiber
1.2
2017-06-07
Lo-MacKinlay variance ratio test
C22 C12
Variance-Ratio test for the null hypothesis that a time series
follows a random walk. See A. Lo and A. C. MacKinlay,
"Stock Market Prices do not Follow Random Walks: Evidence from
a Simple Specification Test", Review of Financial Studies, 1988.
Parameters:
y: series to examine. It should be given in (probably log) level form.
q: (int) desired order of differencing for the variance comparison.
Typical values are 2, 4, 8 or 16. This parameter has a minimum of 2,
and that is also the default value.
verbosity: (int) Can be set to zero to skip the printout. Default 2
(print everything).
Returns (since version 1.2) a 4-element column vector with the standard
test statistic and (two-sided) p-value pair, and then in 3rd and 4th
position the test stat and p-value of a robustified variant of the test.
Changelog:
June 2017: Version 1.2, now returning a result matrix, and a few other
trivial changes.
August 2009: version 1.1
```
series ydiff = diff(y)
scalar T = sum(ok(ydiff))
rem = T % q
if rem > 0
smpl ; -rem
T -= rem
endif
scalar muhat = mean(ydiff)
series cdiff2 = (ydiff - muhat)^2
scalar Scdiff2 = sum(cdiff2)
scalar s2 = Scdiff2 / (T - 1)
series qdiff2 = (y - y(-q) - q * muhat)^2
# note: using df correction
scalar m = q * (T - q + 1) * (1 - q/T)
scalar s2q = sum(qdiff2) / m
scalar R = s2q / s2
scalar M = R - 1
scalar V = 2 * (2 * q - 1) * (q - 1) / (3 * q)
scalar SE = sqrt(V / T)
scalar Z0 = M / SE
pvalnonrob = 2 * pvalue(z, abs(Z0))
if verbosity
printf "Lo-MacKinlay test, q = %d, T = %d\n\n", q, T
if verbosity >= 2
printf "Variance estimate from first differences = %.5g\n", s2
printf "Variance estimate from q-th differences = %.5g\n", s2q
printf "Variance Ratio: R = %.5g / %.5g = %g\n", s2q, s2, R
printf "M(q) = R - 1 = %.5g\n\n", M
printf "Standard error of M under homoskedasticity = %.5g\n", SE
endif
printf "Test statistic: z = %.5g / %.5g = %.5g\n", M, SE, Z0
printf "Two-tailed p-value = %.5g\n\n", pvalnonrob
endif
# robust variant of the test
V = 0
scalar q1 = q - 1
scalar X = Scdiff2^2
loop i=1..q1 --quiet
scalar deltaj = sum(cdiff2 * cdiff2(-$i)) / X
V += (2 * (q - $i) / q)^2 * deltaj
endloop
SE = sqrt(V)
scalar Z1 = M / SE
pvalrob = 2 * pvalue(z, abs(Z1))
if verbosity
if verbosity >= 2
printf "Robust estimate of s.e. of M = %.5g\n", SE
endif
printf "Robust test statistic: z = %.5g / %.5g = %.5g\n", M, SE, Z1
printf "Two-tailed p-value = %.5g\n\n", pvalrob
endif
return {Z0, pvalnonrob, Z1, pvalrob}'
```

include lomackinlay.gfn
open data9-7
# ignore the return matrix
lomackinlay(QNC, 4)
# retrieve the return matrix
matrix LMK = lomackinlay(QNC, 4)
print LMK
# mute the output
LMK = lomackinlay(QNC, 4, 0)
# print only the end results
verbosity = 1
lomackinlay(QNC, 4, verbosity)