Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 40 ยท Stationarity and Transformations

Unit Root Tests: ADF and KPSS

Test stationarity formally with the Augmented Dickey-Fuller and KPSS tests in statsmodels: hypotheses, p-values, the four-way decision table, regression options and common pitfalls.

Plots and rolling statistics give an impression of stationarity; unit root tests give a number. The two most used are the Augmented Dickey-Fuller (ADF) test and the KPSS test. They have opposite null hypotheses, so using both gives a much better answer than either alone. This lesson explains how to run and interpret them, and why they sometimes disagree.

What is a unit root?

A series has a unit root if it behaves like a random walk: shocks never die out. Write the model as y(t) = phi * y(t-1) + e(t). If |phi| < 1, shocks fade and the series is stationary. If phi = 1, it is a random walk, the "root" of the characteristic equation equals 1, and the series needs differencing. See stationarity and random walks for the background.

The ADF test

ADF regresses the change y(t) - y(t-1) on the lagged level y(t-1) plus lagged changes (the "augmented" part, which absorbs autocorrelation):

  • H0 (null): the series has a unit root (non-stationary).
  • H1: the series is stationary (around a constant or a trend).

A small p-value (below 0.05) rejects the null: evidence of stationarity. A large p-value means "cannot reject a unit root".

Python ADF on white noise and on a random walk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Noise and the differenced walk give very small p-values (stationary); the walk gives a large one (cannot reject the unit root). Compare the statistic with the critical values: the more negative it is, the stronger the evidence.

Choosing the regression option

The test has to know what deterministic parts the series might have:

regression Meaning Use when
"n" no constant series centred on zero (rare)
"c" constant only (default) level series, no trend
"ct" constant and linear trend the series visibly trends
"ctt" plus quadratic trend curved trend

Choosing wrongly changes the result. A trending series tested with "c" can look non-stationary even if it is trend-stationary.

Python Trend-stationary series: regression='c' versus 'ct'

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With only a constant the test struggles because the trend dominates; allowing a trend ("ct") correctly identifies the series as stationary around that trend.

The KPSS test

KPSS flips the hypotheses:

  • H0: the series is stationary (around a constant "c" or a trend "ct").
  • H1: the series has a unit root.

Now a small p-value (below 0.05) rejects stationarity. In statsmodels the p-value is capped to the table range (0.01 to 0.1), and a warning appears when the true value lies outside it.

Python KPSS on the same series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For noise the p-value is large (cannot reject stationarity) while the random walk is rejected with p at the lower cap 0.01.

Combining both tests

Run both and read the table:

ADF KPSS Conclusion Action
rejects (small p) does not reject (large p) stationary model as is
does not reject rejects unit root difference
rejects rejects conflicting: trend-stationary or structural break detrend, check for breaks
does not reject does not reject inconclusive, often too little data collect more data, look at the plot
Python A helper that applies both tests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How many differences? ndiffs

Automatic helpers such as pmdarima.arima.ndiffs repeat these tests on successive differences. You can do the same loop yourself: difference, test, and stop when the tests agree on stationarity. Rarely should the answer exceed two. For seasonal data, remove the season first (or take a seasonal difference) because strong seasonality distorts both tests.

Pitfalls

  • Low power: with short series or phi close to 1 (say 0.97), ADF often fails to reject even though the series is technically stationary. Persistent but stationary series look like unit roots in small samples.
  • Structural breaks: a level shift makes a stationary series look like it has a unit root. Plot first. Tests that allow a break (Zivot-Andrews) exist.
  • Seasonal unit roots are not detected by plain ADF.
  • Variance changes (heteroskedasticity) break the assumptions: transform first (log or Box-Cox).
  • A p-value is not a proof; combine the tests with plots and the ACF.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the null hypothesis of the ADF test?

    ADF assumes a unit root under H0; a small p-value is evidence of stationarity.
  2. 2. ADF p-value 0.62 and KPSS p-value 0.01. What do you conclude?

    ADF cannot reject the unit root and KPSS rejects stationarity, so both point to a unit root.
  3. 3. A series with a clear upward trend is tested with regression='c' and looks non-stationary. What should you try?

    The test must be told about the trend; otherwise the trend is mistaken for a unit root.
  4. 4. Why can ADF fail on a stationary series with phi = 0.98 and 80 observations?

    Near-unit-root processes behave like random walks over short samples.

Summary

  • ADF: H0 = unit root; small p means stationary. KPSS: H0 = stationary; small p means a unit root.
  • Pick the regression option ("c", "ct") to match the deterministic trend.
  • Use both tests together; when they agree you can trust the answer, when they conflict look for trends or breaks.
  • Short samples, breaks, seasonality and changing variance can mislead the tests; always look at the plot.
  • Difference once or twice at most, then move on to modelling.

Next: Simple Exponential Smoothing