How do stationarity and cointegration differ?

Test simulated stock prices for a unit root with the ADF test, test pairs of prices for cointegration with the Engle-Granger test, and see why a high correlation is not cointegration.

After this lesson, you can test whether a price is stationary and whether two prices share a stationary long-run relationship.

How do I test a price for stationarity?

A stationary series has a constant mean and variance, and a random walk does not, because its shocks add up. The augmented Dickey-Fuller (ADF) test has a unit root, as in a random walk, as its null hypothesis, and on three simulated prices its p-values of 0.220, 0.178 and 0.332 cannot reject it at the 5% level.

\[ A_t = A_{t-1} + u_t, \qquad B_t = 10 + 1.5\,A_t + e_t, \qquad C_t = A_t + V_t, \quad V_t = V_{t-1} + v_t \]

Here \(A_t\), \(B_t\) and \(C_t\) are the prices of three made-up stocks on day \(t\), in dollars. AAA is a random walk from 100 with normal shocks \(u_t\) of one dollar, BBB follows AAA with its own stationary noise \(e_t\), and CCC adds a second random walk \(V_t\) with shocks \(v_t\) of 0.5 dollars.

.cumsum() adds up the shocks into a random walk. adfuller(series, regression="c", autolag="AIC") runs the ADF test with a constant and chooses the number of lags, and position 1 of its result is the p-value. adf_p returns that p-value.

The following box simulates 400 days and tests each price.

import numpy as np
from statsmodels.tsa.stattools import adfuller, coint

rng = np.random.default_rng(42)
aaa = 100 + rng.normal(0, 1, 400).cumsum()
bbb = 10 + 1.5 * aaa + rng.normal(0, 1, 400)
ccc = aaa + rng.normal(0, 0.5, 400).cumsum()

def adf_p(series):
    return adfuller(series, regression="c", autolag="AIC")[1]

print(round(adf_p(aaa), 3), round(adf_p(bbb), 3), round(adf_p(ccc), 3))
0.22 0.178 0.332

Failing to reject leaves the unit root possible and does not prove it.

How do correlation and cointegration differ?

Correlation measures how two prices move together, and cointegration means that a combination of two random walks is stationary. Both pairs have high correlations, 0.991 for BBB and AAA and 0.915 for CCC and AAA, but only BBB and AAA are cointegrated, because \(B_t - 10 - 1.5\,A_t = e_t\) is stationary, while CCC’s second random walk lets its gap to AAA wander.

coint(y, x, trend="c", autolag="aic") runs the Engle-Granger test, which regresses y on x and tests the residuals for a unit root. Its null hypothesis is no cointegration, and position 1 of its result is the p-value. np.corrcoef(y, x)[0, 1] gives the correlation.

The following box prints the correlation and the cointegration p-value of both pairs.

for name, y in [("BBB", bbb), ("CCC", ccc)]:
    print(name, round(np.corrcoef(y, aaa)[0, 1], 3), f"{coint(y, aaa, trend='c', autolag='aic')[1]:.3g}")
BBB 0.991 4.12e-26
CCC 0.915 0.235

The p-value of BBB and AAA is close to zero, and that of CCC and AAA is 0.235. A pair trade on cointegrated prices also needs stable coefficients and trading costs low enough to profit from the gap.

Your turn

Test the gap between CCC and AAA, ccc - aaa, for a unit root. You should see a p-value of 0.142, above 0.05.

The gap is CCC’s second random walk, so the test cannot reject a unit root.

print(round(adf_p(ccc - aaa), 3))
0.142