## Stationarity tests

7

1

I have the Time Series package and Mathematica 8. In the package, there is the function 'StationaryQ' to test the stationarity of a known model. Is there any function in Mathematica to test the stationarity of a given list of data?

12

The three best-known tests for stationarity (or rather, unit roots) in time series econometrics are:

There are also Bayesian tests of unit roots, as implemented in this conference presentation. If you have access to JSTOR or another way of getting at old journals, this article might be of interest.

As far as I'm aware, nobody has created a pre-digested package for unit root testing in Mathematica. The notebook I wrote - about a decade ago - only includes the KPSS test, and I can give no warranties about whether I did it right. It was written for version 3 and would need considerable updating to work with version 8, as it has dependencies on several packages that have been made obsolete when the functionality was rolled into the main kernel.

Your code works nicely with two modifications. The first is the name of the function! The second is updating to use LinearModelFit to get the residuals when needed. – Mike Colacino – 2019-06-04T02:30:40.683

3

The Dickey-Fuller and the Phillips-Perron tests are now available using UnitRootTest, introduced in Mathematica 9.

1

The very nice KPSS code written by @Verbeia does not require a lot of modification to work properly, so I have done that. First the function name needs to be changed! Then LinearModelFit needs to replace all the regression stuff.

Options[kpssT] = {ResidualData -> True}
SetOptions[kpssT, ResidualData -> True]

kpssT[data : {__?NumericQ}, lag_Integer, opts___?OptionQ] :=
Module[{resids, restrue},
restrue = (ResidualData /. Flatten[{opts}]) /. Options[kpssT];
resids =
If[restrue,
data, (LinearModelFit[data, {1, x}, {x}])["FitResiduals"]];
With[{t = Length[resids]},
Plus @@ (((Plus @@ Drop[resids, -#]) & /@
Range[t])^2)/(t^2 (1/t Plus @@ (resids^2) +
2/t (Plus @@ ((Plus @@ ((1 - #/(lag + 1)) (Drop[
resids, #] Drop[RotateRight[resids, #], #]))) & /@
Range[lag]))))]]


I have also changed a couple of variable names so it would look better pasted here. I haven't tested this extensively, but I took a dataset I have been working with and compared the output (with and without the ResidualData flag set True) with Stata. The MMA output from kppsT[data, #] & /@ Range[0, 10]looks like:

(* {0.191289, 0.106036, 0.0804266, 0.0697203, 0.0652052, 0.0639449, M
0.0645946, 0.0667552, 0.0705423, 0.0761177, 0.0834617} *)


And the Stata output looks like:

(*Lag order    Test statistic
0           .191
1           .106
2          .0804
3          .0697
4          .0652
5          .0639
6          .0646
7          .0668
8          .0705
9          .0761
10          .0835 *)


0

You can check this link. The notebook is sets up several standard tests of stationarity and non-stationarity in time series.

3And to be fair, I haven't looked at that notebook in about a decade, and it only includes the KPSS test. – Verbeia – 2012-04-05T21:18:04.593

1Good link, but next time try to provide some context with your link. A short description, a snippet of documentation or a picture would be highly appreciated. Posting just links is generally fronted upon. – Sjoerd C. de Vries – 2012-04-06T00:15:43.980