Unit Root Test

Calculate a unit root test per asset: whether the prices wander like a random walk or are stationary and revert to their mean.

Three tests are available: the Augmented Dickey-Fuller (ADF) test, the Phillips-Perron (PP) test and the Zivot-Andrews test, which allows for a single structural break at an unknown (endogenously estimated) date.

All three test the same null hypothesis: the series has a unit root (is a random walk, not mean-reverting); the alternative is that it is stationary. This is a standard first step before modeling a price series or spread with mean-reverting methods, since those methods assume stationarity. get_kpss_test is the complement with the null and the alternative swapped.

The ADF test regresses the first difference of the series on its own lagged level and p lags of its own first difference. The Phillips-Perron test instead corrects for heteroskedasticity and serial correlation in the errors nonparametrically via a Newey-West long-run variance estimate, rather than by adding lagged-difference terms to the regression, so PP and ADF should broadly agree on the same series.

The ADF and PP tests assume the deterministic component of the series (its constant and/or trend) is stable throughout the sample. If a series instead has a single one-time break – e.g. a permanent level shift or a change in trend slope, such as a stock split, spin-off, or a structural shift in the business – the ordinary ADF test is biased towards not rejecting the unit root even for a genuinely (trend-)stationary series with a break. The Zivot-Andrews test corrects for this by adding a break dummy to the ADF regression and choosing, for each asset, the break date that is most favorable to the stationary alternative – which is why it needs its own (more negative) critical values rather than the ordinary ADF ones.

The formula for the Augmented Dickey-Fuller test is as follows:

\[\Delta Y_{t} = \alpha + \gamma \cdot Y_{t-1} + \sum_{i=1}^{p} \delta_{i} \cdot \Delta Y_{t-i} + \epsilon_{t},\;\; \text{with } Y_{t} \text{ the column level (Adj Close by default) and a constant (the default)};\;\; \text{the trend version adds a trend } \beta \cdot t \text{ and the version without deterministic terms drops } \alpha\] \[\text{ADF} = \frac{\hat{\gamma}}{\operatorname{SE}(\hat{\gamma})},\;\; \text{compared with MacKinnon (1994, 2010) critical values and p-values}\] \[p = \text{the number of lags that minimises the AIC},\;\; \text{searched from 0 up to max\_lag},\;\; \text{which defaults to} \;\min\left(\left\lceil 12 \cdot \left(\frac{n}{100}\right)^{\frac{1}{4}} \right\rceil,\; \left\lfloor \frac{n - 5}{3} \right\rfloor\right)\]

The formula for the Phillips-Perron test is as follows:

\[Y_{t} = \alpha + \rho \cdot Y_{t-1} + u_{t} \;\text{fitted by OLS on the} \;\text{column} \;\text{level} \;(\text{Adj Close by default}),\;\; \text{with a constant (the default)};\;\; \text{the trend version adds a trend } \beta \cdot t\] \[t_{\rho} = \frac{\hat{\rho} - 1}{\operatorname{SE}(\hat{\rho})},\;\; \gamma_{0} = \frac{\sum \hat{u}_{t}^{2}}{n}\;\;\text{and}\;\; s^{2} = \frac{\sum \hat{u}_{t}^{2}}{n - k},\;\; \text{with } k \text{ the number of coefficients}\] \[\lambda^{2} = \gamma_{0} + 2 \cdot \sum_{j=1}^{l}\left(1 - \frac{j}{l + 1}\right) \cdot \gamma_{j},\;\; \text{with}\;\; \gamma_{j} = \frac{1}{n} \cdot \sum_{t=j+1}^{n} \hat{u}_{t} \cdot \hat{u}_{t-j}\;\;\text{and}\;\; l = \text{lags},\;\; \text{which defaults to int}\left(12 \cdot \left(\frac{n}{100}\right)^{\frac{1}{4}}\right)\] \[Z_{t} = \sqrt{\frac{\gamma_{0}}{\lambda^{2}}} \cdot t_{\rho} - \frac{\lambda^{2} - \gamma_{0}}{2 \cdot \lambda \cdot s} \cdot n \cdot \operatorname{SE}(\hat{\rho})\]
  • The p-value is the MacKinnon (1994) approximation of the Dickey-Fuller distribution and the unit root is rejected at 5% when it is below 0.05, as in the ADF test; the critical values are the finite-sample MacKinnon (2010) ones for the number of observations

The formula for the Zivot-Andrews test is as follows:

\[\Delta Y_{t} = \mu + \theta \cdot \text{DU}_{t} + \beta \cdot t + \alpha \cdot Y_{t-1} + \sum_{i=1}^{p} c_{i} \cdot \Delta Y_{t-i} + \epsilon_{t},\;\; \text{with}\;\; Y_{t} \;\text{the} \;\text{column} \;\text{level} \;(\text{Adj Close by default}) \;\text{and}\; \text{DU}_{t} = 1 \;\text{after the break date}\; T_{B} \;\text{and 0 before it} \;(\text{a level break},\; \text{the default})\] \[\text{A trend break replaces } \theta \cdot \text{DU}_{t} \text{ by } \gamma \cdot \text{DT}_{t},\;\; \text{with}\;\; \text{DT}_{t} = t - T_{B} \;\text{after the break and 0 before it},\;\; \text{and a break in both includes both}\] \[\text{ZA} = \min \;\text{over}\; T_{B} \;\text{of} \;\frac{\hat{\alpha}}{\operatorname{SE}(\hat{\alpha})},\;\; \text{searching every break date outside the first and last trim (15\% by default) of the sample}\] \[p = \text{the number of lags that minimises the AIC},\;\; \text{up to max\_lag (same default as the ADF test)}\]

Also known as: ADF test, augmented Dickey-Fuller test, PP test, Z_t test, ZA test, structural break unit root test, stationarity test, mean reversion test, random walk test, trending or mean-reverting.

Example · EURUSD, USDJPY, GBPUSD · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_unit_root_test:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["EURUSD", "USDJPY", "GBPUSD"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_unit_root_test()

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

Did the euro, the yen and the pound against the dollar have a unit root from 2021 to 2025, according to the augmented Dickey-Fuller test?

Which returns:

Using Your Own Data

get_unit_root_test runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_unit_root_test()

Parameters

get_unit_root_test accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”."weekly"
columnstrThe historical data column to test. Defaults to “Adj Close”."Return"
methodstrThe test. Options are: “augmented_dickey_fuller” (Augmented Dickey-Fuller test), “phillips_perron” (Phillips-Perron test), “zivot_andrews” (Zivot-Andrews test with a structural break). Defaults to “augmented_dickey_fuller”."augmented_dickey_fuller"
regressionstrWhich deterministic terms to include. For “augmented_dickey_fuller” one of “n” (none), “c” (constant) or “ct” (constant and trend); for “phillips_perron” one of “c” or “ct” (“n” is not supported, see econometrics.unitroot_model.get_phillips_perron_test for why); for “zivot_andrews” which break to allow for, one of “c” (a break in the level/intercept), “t” (a break in the trend slope) or “ct” (both). Defaults to “c”."ct"
max_lagintThe maximum number of lagged differences to consider in the AIC lag search of “augmented_dickey_fuller” and “zivot_andrews” (which selects a single lag length and reuses it for every break date). Not used with “phillips_perron”. Defaults to the Schwert (1989) rule of thumb, capped at (n - 5) // 3 for short samples.12
lagsintThe truncation lag for the Newey-West long-run variance estimate of “phillips_perron”; not used by the other methods. Defaults to None, meaning the Schwert (1989) rule ceil(12 * (n / 100) ** 0.25), capped for short samples as the ADF lag search is.2
trimfloatThe fraction of observations excluded from the candidate break date search at the start and end of the sample, for “zivot_andrews” only. Must be in [0, 1/3). Defaults to 0.15.0.1
include_benchmarkboolWhether to include “Benchmark” among the assets tested. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None.2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

  • Paper Dickey, D.A. and Fuller, W.A. (1979). “Distribution of the Estimators for Autoregressive Time Series with a Unit Root.” Journal of the American Statistical Association, 74(366a), 427-431.
  • Paper Phillips, P.C.B., & Perron, P. (1988). “Testing for a Unit Root in Time Series Regression.” Biometrika, 75(2), 335-346.
  • Paper Zivot, E., & Andrews, D.W.K. (1992). “Further Evidence on the Great Crash, the Oil-Price Shock, and the Unit-Root Hypothesis.” Journal of Business & Economic Statistics, 10(3), 251-270.
  • Paper Perron, P. (1989). “The Great Crash, the Oil Price Shock, and the Unit Root Hypothesis.” Econometrica, 57(6), 1361-1401.
  • Library Referenced against statsmodels’ adfuller (Augmented Dickey-Fuller test)
  • Library Referenced against arch’s PhillipsPerron (Phillips-Perron test)
Share