Run both tests, because they start from opposite assumptions: statsmodels' adfuller assumes a unit root and looks for evidence against it, while kpss assumes stationarity and looks for evidence against that. Trust the result only when the two agree. The pairing is weak on highly persistent but stationary series, on trending data tested without a trend term, and on series with a level break. One detail you can check: statsmodels 0.15 reports KPSS p-values only between 0.01 and 0.10 and raises InterpolationWarning outside that range.
This is a comparison, not a tutorial on unit-root theory. I ran both tests on five synthetic series where I know the true answer, then ran 5,400 simulated series to see how often each test gets it right as persistence and sample size change. Everything below comes from running the code with Python 3, statsmodels 0.15.0, NumPy 2.5 and pandas 3.1. All data is synthetic and seeded; there is no market data in this post.
What each test is actually asking
ADF (adfuller) |
KPSS (kpss) |
|
|---|---|---|
| Null hypothesis | series has a unit root | series is stationary (level or trend) |
| Small p-value means | evidence of stationarity | evidence of non-stationarity |
| Trend option | regression="ct" |
regression="ct" |
| p-value range in statsmodels | continuous (MacKinnon) | clipped to 0.01–0.10 |
Because the nulls are flipped, a "fail to reject" from ADF does not mean "unit root confirmed", and a "fail to reject" from KPSS does not mean "stationary confirmed". Each test is good at defending its own null. Combining them gives four outcomes:
- ADF rejects, KPSS does not: stationary, both agree.
- ADF does not, KPSS rejects: unit root, both agree.
- Both reject: conflict. Usually a trend, a break, or a series that is stationary but slow to revert.
- Neither rejects: inconclusive. Usually too little data.
Five series with a known answer
import warnings
import numpy as np
import pandas as pd
from statsmodels.tsa.stattools import adfuller, kpss
from statsmodels.tools.sm_exceptions import InterpolationWarning
rng = np.random.default_rng(42)
n = 250 # about one trading year of daily points
def ar1(phi, n, rng, sigma=1.0):
e = rng.normal(0, sigma, n)
x = np.zeros(n)
for t in range(1, n):
x[t] = phi * x[t - 1] + e[t]
return x
t = np.arange(n)
series = {
"white_noise": rng.normal(0, 1, n),
"ar1_phi_0.90": ar1(0.90, n, rng),
"random_walk": np.cumsum(rng.normal(0, 1, n)),
"trend_plus_ar1": 0.04 * t + ar1(0.5, n, rng),
"level_shift": np.where(t < 125, 0.0, 4.0) + ar1(0.3, n, rng),
}
def run_tests(x):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", InterpolationWarning)
adf_stat, adf_p, *_ = adfuller(x, regression="c", autolag="AIC")
k_stat, k_p, _, _ = kpss(x, regression="c", nlags="auto")
kt_p = kpss(x, regression="ct", nlags="auto")[1]
adf_ct_p = adfuller(x, regression="ct", autolag="AIC")[1]
return dict(adf_stat=adf_stat, adf_p=adf_p, kpss_c_stat=k_stat,
kpss_c_p=k_p, adf_ct_p=adf_ct_p, kpss_ct_p=kt_p,
clipped=len(w) > 0)
def verdict(adf_p, kpss_p, a=0.05):
if adf_p < a and kpss_p >= a: return "stationary (both agree)"
if adf_p >= a and kpss_p < a: return "unit root (both agree)"
if adf_p < a and kpss_p < a: return "conflict: check trend/breaks"
return "inconclusive: too little data"
tab = pd.DataFrame({k: run_tests(v) for k, v in series.items()}).T
tab["verdict"] = [verdict(a, b) for a, b in zip(tab.adf_p, tab.kpss_c_p)]
print(tab.drop(columns="clipped").round(3))
Output (p-values rounded):
| series | ADF p (c) | KPSS p (c) | ADF p (ct) | KPSS p (ct) | combined verdict (c) |
|---|---|---|---|---|---|
| white_noise | 0.000 | 0.10 | 0.000 | 0.100 | stationary (both agree) |
| ar1_phi_0.90 | 0.002 | 0.10 | 0.008 | 0.100 | stationary (both agree) |
| random_walk | 0.918 | 0.01 | 0.028 | 0.021 | unit root (both agree) |
| trend_plus_ar1 | 0.928 | 0.01 | 0.000 | 0.094 | unit root (both agree) |
| level_shift | 0.138 | 0.01 | 0.000 | 0.020 | unit root (both agree) |
Three things in this table matter more than the agreement column.
Every KPSS p-value is a boundary value. The clipped flag was True for all five rows. A KPSS p of 0.10 means "0.10 or larger", and 0.01 means "0.01 or smaller". Do not rank series by KPSS p-value, and do not report 0.10 as if it were exact. Compare the statistic with the critical values (the fourth item kpss returns) instead.
Two of the "unit root" verdicts are wrong. trend_plus_ar1 is stationary around a straight line, and level_shift is stationary on both sides of a jump. With the default constant-only setting, both tests read the drift as a unit root. Switching to regression="ct" fixes the trend case: ADF p drops to 0.000 and KPSS p rises to 0.094, so the two agree on trend-stationary. The level shift is not fixed by a trend term (KPSS ct p = 0.020). Neither test models a break, so a single jump looks like non-stationarity to both. This is common in attention data. When I look at a daily stock heat series, a single news event can lift the baseline for weeks. Plot first, and if you see a step, split the sample or use a break-aware test such as Zivot-Andrews (statsmodels.tsa.stattools.zivot_andrews).
One random walk draw fooled the trend version. With regression="ct", ADF gave p = 0.028 on the random walk. That is one draw out of one, so it proves nothing on its own. It is still a reminder that a 5% test is wrong 5% of the time, and adding a trend term to a series that drifts by chance can produce a false "stationary" reading. KPSS ct still rejected (0.021), so the pair showed a conflict there instead of a false agreement.
Differencing the random walk with np.diff gives ADF p = 0.0000 and KPSS p = 0.100, which is the textbook result.
Head-to-head: 5,400 simulated series
One draw per series is anecdote. To compare the tests properly, I simulated AR(1) series with coefficients from 0.5 to 1.0 (1.0 is a random walk) at three lengths, 300 draws per cell, and counted rejections at the 5% level.
phis, reps, rows = [0.5, 0.8, 0.9, 0.95, 0.98, 1.0], 300, []
for n_mc in (100, 250, 1000):
for phi in phis:
adf_rej = kpss_rej = agree = conflict = 0
for _ in range(reps):
x = ar1(phi, n_mc, rng)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
ap = adfuller(x, regression="c", autolag="AIC")[1]
kp = kpss(x, regression="c", nlags="auto")[1]
adf_rej += ap < 0.05
kpss_rej += kp < 0.05
conflict += (ap < 0.05 and kp < 0.05)
ok = (ap < 0.05 and kp >= 0.05) if phi < 1 else (ap >= 0.05 and kp < 0.05)
agree += ok
rows.append(dict(n=n_mc, phi=phi, adf_reject=adf_rej / reps,
kpss_reject=kpss_rej / reps,
both_correct=agree / reps, conflict=conflict / reps))
mc = pd.DataFrame(rows)
The full run took about 30 seconds in my environment. Selected results:
| phi | n | ADF rejects unit root | KPSS rejects stationarity | both correct | conflict |
|---|---|---|---|---|---|
| 0.50 | 250 | 1.00 | 0.08 | 0.92 | 0.08 |
| 0.90 | 100 | 0.34 | 0.37 | 0.28 | 0.06 |
| 0.90 | 250 | 0.94 | 0.24 | 0.72 | 0.22 |
| 0.95 | 250 | 0.51 | 0.42 | 0.35 | 0.16 |
| 0.98 | 1000 | 0.82 | 0.63 | 0.35 | 0.47 |
| 1.00 | 100 | 0.07 | 0.80 | 0.77 | 0.03 |
| 1.00 | 1000 | 0.05 | 0.98 | 0.93 | 0.04 |
How I read it:
- ADF needs data. At phi = 0.9 it spots stationarity 34% of the time with 100 points and 94% with 250. At phi = 0.98 even 1,000 points only get it to 82%.
-
KPSS over-rejects persistent stationary series. At phi = 0.98 it calls the series non-stationary about 63% of the time at every sample size. More data barely helps, because the default bandwidth (
nlags="auto") cannot absorb that much autocorrelation. - Conflicts are not noise. The "both reject" share peaks at 47% for phi = 0.98, n = 1000. Most conflicts in this simulation come from stationary series that revert slowly. That fits the usual advice: a conflict means "stationary but persistent, or something structural", not "flip a coin".
- Both tests hold size on a true random walk. ADF falsely rejects at 5–9%, close to the nominal 5%. KPSS correctly rejects 80% of the time at n = 100 and 98% at n = 1000.
When to use which
-
One quick check before modelling levels: run both with
regression="c". Agreement on "stationary" is a reasonable green light. -
The series visibly trends: rerun both with
regression="ct"before differencing. Differencing a trend-stationary series adds a moving-average unit root you did not need. - Short samples (under ~150 points): expect "inconclusive" or a false "unit root" from ADF on persistent data. Report that honestly rather than forcing a verdict.
- Daily attention or sentiment indexes: these often look like phi = 0.9–0.98 processes with occasional jumps. If you are testing something like a single-stock sentiment index, expect conflicts, check for breaks, and consider modelling changes or deviations from a rolling baseline instead of raw levels.
FAQ
Which is better, ADF or KPSS? Neither on its own. In the simulation, ADF had low power on persistent series, and KPSS rejected too often on the same series. Together they caught most clear cases and flagged the unclear ones.
Why does statsmodels warn "p-value is greater than the indicated p-value"? That is InterpolationWarning. The KPSS statistic fell outside the 1%–10% critical-value table, so the reported p is a bound, not an estimate.
Should I use AIC or BIC for autolag in ADF? AIC (the default) picks more lags, which protects size but costs some power. I used AIC throughout. If you switch, rerun the comparison on your own series length.
The full script, which also writes both charts, is short enough to paste into a notebook. Change n and phis to match your own data before trusting any rule of thumb from this post.
Lava (熔岩, https://lava.ai-dolphin.com) — A-share stock heat & sentiment monitoring by AI-Dolphin.
中文简介:本文用固定种子的合成数据对比 ADF 与 KPSS 平稳性检验:两者原假设相反,需合用;趋势序列要用 regression="ct",水平突变会让两者都误判为单位根;5400 次模拟显示高持续性平稳序列上 ADF 功效低、KPSS 易误拒,冲突结果往往意味着慢回复或结构变化。


Top comments (0)