Gregory-Hansen Test
The Gregory-Hansen Test is the residual-based test of Gregory and Hansen (1996) that allows the long-run relation to shift once at an unknown date. A pair whose level or hedge ratio changed, through a merger, a change in index weights or a new regime, looks not cointegrated to the Engle-Granger Test, while it is cointegrated before and after the shift. The test searches every candidate date for the break that makes the residuals most stationary.
The formula for the Gregory-Hansen Test defines, for a break date \(T_{B}\), a dummy \(\phi_{t}\) that switches on from the break:
\[\phi_{t} = 1 \quad \text{if } t \geq T_{B}\] \[\phi_{t} = 0 \quad \text{if } t < T_{B}\]It fits one of three regressions by OLS, a level shift, a level shift with a trend or a regime shift of both the level and the hedge ratio:
\[A_{t} = \mu_{1} + \mu_{2} \cdot \phi_{t} + \alpha \cdot B_{t} + e_{t}\] \[A_{t} = \mu_{1} + \mu_{2} \cdot \phi_{t} + \beta \cdot t + \alpha \cdot B_{t} + e_{t}\] \[A_{t} = \mu_{1} + \mu_{2} \cdot \phi_{t} + \alpha_{1} \cdot B_{t} + \alpha_{2} \cdot B_{t} \cdot \phi_{t} + e_{t}\]For every break date it computes the ADF statistic and the Phillips-Ouliaris \(Z_{t}\) and \(Z_{\alpha}\) of the residuals \(e_{t}\) and keeps the smallest of each:
\[\text{ADF}_{\text{star}} = \min\left(\text{ADF}(T_{B})\right)\] \[Z_{t,\text{star}} = \min\left(Z_{t}(T_{B})\right)\] \[Z_{\alpha,\text{star}} = \min\left(Z_{\alpha}(T_{B})\right)\]Where \(A_{t}\) and \(B_{t}\) are the two prices, \(\mu_{1}\) the level before the break and \(\mu_{2}\) its shift, alpha the hedge ratio (\(\alpha_{1}\) before and \(\alpha_{1} + \alpha_{2}\) after the break in the regime shift), beta * t the trend and \(e_{t}\) the residual. The minimum runs over every break date outside the first and last 15% of the sample. \(\text{ADF}_{\text{star}}\), \(Z_{t , \text{star}}\) and \(Z_{\alpha , \text{star}}\) (ADF, Zt and Z alpha*) reject no cointegration below the critical values of Gregory and Hansen (1996), and the date of the minimum estimates the break. Only critical values are published, so there are no p-values. A rejection does not by itself show that a break occurred: the test also rejects for a pair that is cointegrated without a break, where the Engle-Granger Test suffices.
Two individually non-stationary series, such as two stock prices that each follow a random walk, are cointegrated if some linear combination of them is stationary: they share a long-run equilibrium even though each wanders on its own in the short run. This is the statistical foundation of pairs trading. If two assets are cointegrated, the spread between them reverts to its equilibrium level, so the spread itself can be traded, with the slope of the regression of one price on the other as the hedge ratio. A residual-based test of cointegration asks whether the residual of that regression, the spread, has a unit root (no cointegration, the null hypothesis) or is stationary (cointegration, the alternative). The result depends on which ticker is the dependent one, so the two orderings of a pair can disagree; the Johansen test treats the pair without that choice. Cointegration found in one window often breaks down in the next, so a relation is usually tested again on a later sample before it is relied on.
Also known as: GH test, cointegration with a structural break, regime shift cointegration.
In the Finance Toolkit this is get_engle_granger_cointegration with method="gregory_hansen", the same function as Engle-Granger Cointegration.
Example · KO, PEP · 2021 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_engle_granger_cointegration:
from financetoolkit import Toolkit
toolkit = Toolkit(
["KO", "PEP"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
cointegration = toolkit.econometrics.get_engle_granger_cointegration(method="gregory_hansen")
cointegration[["Gregory-Hansen ADF Statistic", "Break Date", "Critical Value 5%", "Cointegrated (5%)"]]
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to “daily”. | "weekly" |
column | str | The historical data column to test. Defaults to “Adj Close”. | "Close" |
method | str | The test. Options are: “engle_granger” (Engle-Granger Test), “phillips_ouliaris” (Phillips-Ouliaris Test), “gregory_hansen” (Gregory-Hansen Test, with a structural break). Defaults to “engle_granger”. | "engle_granger" |
regression | str | The break model of “gregory_hansen”; not used by the other methods. Options are: “c” (level shift), “ct” (level shift with trend), “cs” (regime shift of the level and the hedge ratio). Defaults to “c”. | "ct" |
max_lag | int | The maximum number of lagged differences to consider in the underlying ADF test on the residuals (“engle_granger” and “gregory_hansen”). Defaults to the Schwert (1989) rule of thumb, capped at (n - 5) // 3 for short samples. | 12 |
lags | int | The truncation lag of the long-run variances of “phillips_ouliaris” and of Zt* and \(Z \alpha^{*}\) of “gregory_hansen”. Defaults to the Schwert (1989) rule of thumb, capped at (n - 5) // 3 for short samples. | 2 |
trim | float | The fraction of observations left out of the break date search at each end, for “gregory_hansen” only. Defaults to 0.15. | 0.1 |
include_benchmark | bool | Whether to include “Benchmark” among the tickers paired up. Defaults to False. | True |
rounding | int | None | The number of decimals to round the results to. Defaults to None. | 2 |
show_columns | str | list[str] | None | The 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 |
Which returns:
| Item | Gregory-Hansen ADF Statistic | Break Date | Critical Value 5% | Cointegrated (5%) |
|---|---|---|---|---|
| ('KO', 'PEP') | -4.0891 | 2024-10-23 | -4.61 | 0 |
| ('PEP', 'KO') | -3.8331 | 2021-11-30 | -4.61 | 0 |
2 rows
Using Your Own Data
get_engle_granger_cointegration 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_engle_granger_cointegration()
References
- Paper Gregory, A.W. and Hansen, B.E. (1996). “Residual-Based Tests for Cointegration in Models with Regime Shifts.” Journal of Econometrics, 70(1), 99-126.