Trading Dev AcademyFree quant education

Free lesson · Time series

Cointegration: a stationary relationship to test

Open interactive lessonPractice calculationsExplore labs

Start with the idea

Cointegration concerns a stationary combination of nonstationary levels. High return correlation alone does not establish it. A stationary fitted residual can motivate a convergence hypothesis, but the economics of two executable positions still need testing.

Symbols, units & horizon
  • xₜ, yₜ: aligned level series
  • β: estimated hedge coefficient
  • zₜ: residual spread yₜ−βxₜ
  • μ̂_z, σ̂_z: spread mean and positive SD estimated on training data
  • score: centred spread measured in training standard deviations
  • Stationarity: a modelling hypothesis requiring testing, not an entry command

When and why to use this

Use a residual model for related assets or cash-versus-derivative research. Check hedge stability, both-leg costs and whether a structural event can permanently change the relationship.

Two non-stationary series are cointegrated if an appropriate nonzero linear combination is stationary under the specified process model. For two synthetic price series, a fitted residual yt−βxt is a candidate relationship to test on data excluded from estimation. Stationarity does not bound the next excursion, guarantee a return by a deadline, or establish profitable convergence after costs.

zt=yt−βxt,scoret=zt−μ^zσ^z
Algebra and arithmetic

Construct and normalise a residual spread

  1. Fit yt=a+βxt+zt; rearrange to zt=yt−a−βxt. The displayed simplified relation omits a, which can instead be absorbed into the spread mean.
  2. For OLS with an intercept, β=SxySxx. Standardise a fitted spread with zscore=(z−z‾)sz. A levels residual is not automatically a return or a risk-normalised position.
  3. If residual deviations obey AR(1), expected future spread is μ+ϕh(zt−μ). This conditional expectation depends on the model remaining valid.
Work it by hand

y=105, x=50, β=2 gives spread 5. With fitted mean 3 and SD 1, the spread z-score is 2. It says nothing by itself about fill prices or net profitability.

Engle–Granger estimates a level relationship, then tests its residual using cointegration-specific critical values. An AR model can provide a conditional decay forecast if its assumptions hold. That forecast is not a holding-period guarantee or proof that convergence exceeds costs. See the official cointegration test documentation for the null, assumptions and critical-value convention.

Why does a pairs trade on a cointegrated spread not depend on the direction of the market?

Because the position is long one asset and short β of the other, the shared random-walk component (the "common trend", including the market) cancels. What remains is the stationary spread, whose expected path is back toward its mean regardless of where the market goes. The fitted common component cancels only under the assumed model. Factor exposures can remain, and market shocks, changing hedge ratios, borrow and liquidity can all cause losses.

Python implementation

Self-contained teaching example. Python 3.10+; dependencies and input conventions are shown in the code and notation. Run in your own Python environment.

def residual_score(y, x, hedge_beta, training_mean, training_sd):
    """Freeze training parameters before evaluating a new observation."""
    if training_sd <= 0:
        raise ValueError("Positive training spread SD required")
    spread = y-hedge_beta*x
    return spread, (spread-training_mean)/training_sd

print(residual_score(103, 50, 2, 1, 2))

Continue learning

Time Series Analysis — all lessons
  1. Start with time order, lags and differences
  2. Before GARCH: mean, shocks and changing variance
  3. Stationarity: the assumption every test makes and every market breaks
  4. Autocorrelation: momentum, mean reversion, or coin flips
  5. GARCH: volatility clusters, and you can model the cluster
  6. Cointegration: a stationary relationship to test
  7. Forecast horizons, EWMA, and model diagnostics
  8. Markov chains: a two-state model you can calculate by hand
  9. Hidden Markov models: predict, observe, update

Quantitative finance and development glossary · Python resources and libraries · Research sources and limitations