Rasmus Suonio

Kalman filters for pairs trading: better hedge ratios, no better trades

A statistical-arbitrage laboratory built from the mathematics up - Kalman filter, smoother, likelihood, EM, cointegration and structural-break tests written from scratch - and a pre-registered test of whether dynamic hedge ratios beat static and rolling regression after costs.

Kalman vs rolling OLS, pre-registered test (net Sharpe)
-0.28 vs +0.08
Lower hedged-return variance than rolling OLS
8%, in 80% of 480 pair-windows
Process noise chosen by maximum likelihood
The worst setting for trading
Breakdowns flagged within 126 days
18% adaptive vs 58% frozen
  • Python
  • JavaScript
A synthetic pair whose true hedge ratio drifts slowly around 1. The Kalman filter's estimate stays close to it inside its 95% band, a 120-day rolling regression swings far above and below, and a static regression sits at 0.8.

Pairs trading bets that two related stocks drift back together. Everything rests on the hedge ratio: how many dollars of one stock offset a dollar of the other. A Kalman filter lets that ratio change every day instead of fixing it once, and many guides present this as a better pairs strategy. I wanted to know whether it really is, and to learn the mathematics properly on the way, so I wrote the filter, the smoother, maximum likelihood, EM, the cointegration tests, the structural-break tests and an event-driven backtester myself, and checked each piece against statsmodels and pykalman.

Try it below. The Kalman lab filters a synthetic pair with a known hedge ratio live in your browser: move the process and measurement noise and watch the gain, the variances before and after each update, the innovations, the likelihood surface and the trading result change. Pairs shows 24 named pairs, among them LITE/MRVL, NVDA/QQQ and CAT/DE, with their hedge ratios, z-scores, break monitors and a check of whether a deviation is actually tradable after costs. Study holds the full results.

What I found #

I wrote the test down before running it: S&P 500 members in semiconductor, technology and industrial industries, using CRSP data so that companies later acquired or delisted are still in the sample. Each strategy was fitted on two years and traded for the next six months, rolling forward. One configuration per method was chosen on 2005-2012 and run once on 2013-2024. Trades fill at the next day’s open and pay quoted spreads, market impact, commission and short-borrow fees.

  • No method beat rolling regression. The Kalman hedge earned a net Sharpe ratio of -0.28 against +0.08 for a 250-day rolling regression and -0.25 for a static one. A Kalman model that treats the spread itself as a hidden mean-reverting state earned +0.01. None of the three registered comparisons favours a Kalman variant: the best p-value after the Holm multiple-testing correction is 0.55. The configuration that won validation, at a Sharpe of 0.65, scored -0.28 on the test.
  • Kalman filters do hedge better. A moderately adaptive Kalman hedge cut the variance of next-day hedged returns by about 8% against the rolling regression in 80% of 480 test pair-windows. On synthetic pairs with a drifting hedge ratio it tracked the truth with a seventh of the rolling regression’s error. Rolling windows estimate the slope from too little movement in the hedge stock. Better hedges simply did not make better trades.
  • The noise setting decides everything, and maximum likelihood picks the worst one. On the test years the Kalman strategy’s Sharpe ranged from -0.31 to +0.75 across noise settings. Maximum likelihood chose noise that lets the hedge ratio jump around (a median ratio of 56 to the measurement noise). That fits the data best because the filter absorbs every deviation as it happens, which leaves nothing to trade: 2,065 trades and a net Sharpe of -1.05.
  • An adaptive filter follows a broken relationship. On synthetic pairs whose cointegration dies, a CUSUM monitor on the maximum-likelihood filter’s surprises caught the breakdown within six months only 18% of the time, against 58% for the same monitor on a relationship frozen at formation, at the same false-alarm rate. The textbook CUSUM test rejected stability on 96% of perfectly stable pairs, because a persistent spread breaks its assumptions.
  • Costs and timing. Before costs the rolling regression had a test Sharpe of 0.33; costs of 66-78 basis points a year took most of it. Filling at the signal’s own closing price instead of the next open would have added up to 0.2 of Sharpe that a trader could never capture.

Two gold ETFs holding the same metal and two S&P 500 ETFs are cointegrated by construction, yet every method lost money on them: the spread is too small to pay for trading. Marvell’s agreement to buy Lumentum on 1 November 2026 breaks the LITE/MRVL relationship in a way the break monitor cannot see, because the two stocks suddenly move more closely together, not less.

How it works #

  • The mathematics, from scratch. A general linear Gaussian state-space model with time-varying matrices, the Kalman filter in Joseph form with missing data and an exact likelihood, the Rauch-Tung-Striebel smoother, maximum likelihood with parameter transforms and standard errors, EM, steady-state analysis linking the filter to exponential smoothing and rolling windows, and multivariate common-trend models. The filter matches statsmodels to 5e-7 in log-likelihood and pykalman’s EM exactly. Each derivation is written out in the repository.
  • Statistics. Augmented Dickey-Fuller and Engle-Granger tests with MacKinnon p-values (identical to statsmodels), Ornstein-Uhlenbeck half-lives and a closed-form probability that a trade converges before its stop, CUSUM, Page’s CUSUM, Andrews’ sup-F and PELT change-point detection.
  • Backtester. Event-driven, with signal timestamps, execution delays, quote-based spreads, square-root market impact, commissions, daily borrow, exposure limits, stops and delistings. Tests check that every dollar of cash flow is accounted for and that future prices never change a past signal.
  • Terminal. The browser runs a JavaScript port of the filter that reproduces the Python results to nine decimal places. The study views show only derived results, never the licensed or vendor prices.

Limits #

  • Daily data only, and short-borrow fees and availability are assumptions.
  • The test can only detect Sharpe differences larger than about 0.3. The observed difference had the wrong sign anyway.
  • The 24 named pairs are hand-picked stocks that exist today, so they illustrate the methods; the CRSP study is the evidence.
  • No live trading results exist.