Stationarity tests



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?


Posted 2012-04-05T09:37:42.287

Reputation: 73



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

  1. Dickey-Fuller including Augmented Dickey-Fuller
  2. Phillips-Perron
  3. KPSS

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.


Posted 2012-04-05T09:37:42.287

Reputation: 33 191

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


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


Posted 2012-04-05T09:37:42.287

Reputation: 12 994


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 = 
    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, #], #]))) & /@

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 *)

Mike Colacino

Posted 2012-04-05T09:37:42.287

Reputation: 661


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


Posted 2012-04-05T09:37:42.287

Reputation: 4 429

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