Calculate the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test for stationarity, per asset.

KPSS is the natural complement to the Augmented Dickey-Fuller test: where the ADF null hypothesis is that the series HAS a unit root, the KPSS null hypothesis is that the series IS stationary, with a unit root as the alternative. Running both together is standard practice to triangulate a confident conclusion – ADF rejecting a unit root and KPSS failing to reject stationarity together give a confident stationarity conclusion, while the two tests disagreeing flags an ambiguous case.

For more information about the method, see the following paper:

  • Kwiatkowski, D., Phillips, P.C.B., Schmidt, P., & Shin, Y. (1992). “Testing the Null Hypothesis of Stationarity against the Alternative of a Unit Root.” Journal of Econometrics, 54(1-3), 159-178.

Also known as: KPSS test, stationarity test.

KPSS Test in Python

Install the Finance Toolkit with:

pip install financetoolkit -U

Then call get_kpss_test as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AMZN", "TSLA"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2019-01-01",
    end_date="2023-12-31",
)

toolkit.econometrics.get_kpss_test(period="quarterly")

Which returns:

  AMZN TSLA
KPSS Statistic 0.1739 0.5193
P-Value 0.1 0.0373
Lags Used 2 2
Observations 20 20
Critical Value 1% 0.739 0.739
Critical Value 2.5% 0.574 0.574
Critical Value 5% 0.463 0.463
Critical Value 10% 0.347 0.347
Reject Stationarity (5%) 0 1

Parameters

The function accepts the following parameters:

  • period (str, optional): The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”.
  • column (str, optional): The historical data column to test. Defaults to “Adj Close”.
  • regression (str, optional): Which deterministic term to remove before testing, one of “c” (constant, level-stationarity) or “ct” (constant and trend, trend-stationarity). Defaults to “c”.
  • lags (int, optional): The truncation lag for the long-run variance estimate. Defaults to statsmodels’ automatic (Hobijn, Franses & Ooms, 2004) bandwidth selection.
  • include_benchmark (bool, optional): Whether to include “Benchmark” among the assets tested. Defaults to False.
  • rounding (int | None, optional): The number of decimals to round the results to. Defaults to None.
Share