← Allen Wang Code ↗

Comparative study · Python

On PEP/KO, the Kalman hedge lost to a plain rolling hedge out of sample

Test-window Sharpe −0.44 for Kalman vs 0.62 for rolling OLS, after costs. The pair also fails the Engle-Granger cointegration test on the training window (p = 0.182), so neither result shows a tradeable edge.

Real prices: Yahoo Finance daily closes, downloaded 2026-11-23Test 2022-03-14 to 2024-12-30One pair, one 60% / 40% split, no confidence interval1 bp commission + 1 bp slippage per fill

Rolling OLS ends the test window ahead

Cumulative return on the test window, % · PEP/KO · after costs

Return since the last bar of the training window, net of costs. Both hedges use parameters picked on the training window only. One window, so there is no spread across runs.
Test Sharpe, Kalman hedge−0.44vs 0.62 for rolling OLS28 vs 55 trades · after costs
Cointegration, training windowp = 0.182Engle-Granger; fails at 0.05ADF p (spread) 0.066 · half-life 41.0 days
Kalman innovation variance20.17≈ 1.00 if the model fit20 of 20 autocorrelations outside the 95 % band

Which hedge did better out of sample?

Rolling OLS returned 3.4 % a year on the test window; Kalman returned −2.5 %

Both hedges run through the same signal, sizing and cost code. Kalman's max drawdown was −10.1 % against −6.1 % for rolling OLS.

Test window 2022-03-14 to 2024-12-30, after costs
MetricKalmanRolling OLS
Annualized return-2.5%3.4%
Annualized volatility5.4%5.6%
Sharpe (rf = 0)-0.440.62
Sortino (rf = 0)-0.631.03
Max drawdown-10.1%-6.1%
Trades2855
Average holding period (days)21.616.18
Hit rate (closed trades)67.9%64.3%
Annual turnover (x equity)20.2139.74
Annual cost drag0.4%0.8%

Sharpe and Sortino use daily returns, annualized with 252 days and a zero risk-free rate. Hit rate counts closed trades only.

One window. Kalman traded 28 times and rolling OLS 55 times in one test window, with no bootstrap, so the Sharpe gap has no confidence interval.

Train and test metrics, both hedges · 10 rows
Train and test metrics
MetricKalman, trainKalman, testRolling OLS, trainRolling OLS, test
Annualized return3.3%-2.5%6.7%3.4%
Annualized volatility8.0%5.4%7.7%5.6%
Sharpe (rf = 0)0.45-0.440.880.62
Sortino (rf = 0)0.71-0.631.611.03
Max drawdown-11.0%-10.1%-5.1%-6.1%
Trades29289255
Average holding period (days)29.6621.615.016.18
Hit rate (closed trades)71.4%67.9%64.8%64.3%
Annual turnover (x equity)14.6320.2146.2439.74
Annual cost drag0.3%0.4%0.9%0.8%

Parameters chosen on the train window: Kalman: entry_threshold=1.5, exit_threshold=0, delta=1e-05; Rolling OLS: entry_threshold=1.5, exit_threshold=0.5, ols_window=20

How do the two hedges differ?

Rolling OLS's hedge ratio moves 21.9× as much per day as Kalman's

The 20-day OLS window re-fits the ratio from scratch every day; the Kalman filter adjusts it a little at a time and carries an uncertainty band.

Hedge ratio over the full period

Shares of KO per share of PEP · every 5th trading day

The Kalman estimate carries a ± 2 standard deviation band, 0.09 wide on average, so it is barely visible at this scale. Shown from the end of the warm-up (2018-03-29); the test window starts 2022-03-14.

Does the Kalman model fit this pair?

No: its standardized errors have variance 20.17, not ≈ 1, and stay autocorrelated at 20 of 20 lags

If the model fit, each day's prediction error divided by its predicted standard deviation would look like independent draws with variance 1. Errors this large and persistent mean the z-score signal is miscalibrated.

Autocorrelation of the standardized errors

Lags 1–20 days · n = 1,700 days · full period

The horizontal lines mark the 95 % band for white noise (± 1.96 / √n). Points outside it are autocorrelation the model leaves unexplained.

Appendix · Sanity check

On a constructed cointegrated pair, both hedges profit, as they should

Synthetic data (generated in code, seed 42), not market prices. Test Sharpe 1.15 for Kalman and 1.50 for rolling OLS. This checks the code, not an edge: the spread mean-reverts by construction.

Few trades. Rolling OLS's 100 % hit rate comes from only 5 trades, so it says little about the method.

Synthetic pair metrics · 10 rows
Synthetic pair, train and test metrics
MetricKalman, trainKalman, testRolling OLS, trainRolling OLS, test
Annualized return9.1%5.3%4.9%2.8%
Annualized volatility4.8%4.6%2.4%1.8%
Sharpe (rf = 0)1.851.152.031.50
Sortino (rf = 0)2.951.793.712.62
Max drawdown-4.0%-3.8%-2.3%-1.0%
Trades4530155
Average holding period (days)11.738.739.409.00
Hit rate (closed trades)93.2%70.0%93.3%100.0%
Annual turnover (x equity)26.9225.268.873.75
Annual cost drag0.5%0.5%0.2%0.1%

Train window 2000-03-27 to 2003-06-13, test window 2003-06-16 to 2005-09-30. On the train window: Engle-Granger p = 0.000, ADF p (spread) = 0.000, half-life = 12.8 days.

Parameters chosen on the train window: Kalman: entry_threshold=1, exit_threshold=0, delta=0.0001; Rolling OLS: entry_threshold=2.5, exit_threshold=0.5, ols_window=60

Methodology

How this was tested

The first 60% of the bars are the training window and the rest the test window. Each hedge's entry threshold, exit threshold and speed (Kalman delta or OLS window) are picked by Sharpe on the training window from a grid of 24 (Kalman) or 24 (OLS) settings. Nothing after the training window touches that choice.

Both hedges then trade the whole series with those settings. Positions open when the z-score passes the entry threshold and close when it returns past the exit threshold, sized at account equity. Costs: 1 bp commission + 1 bp slippage per fill.

Hedge ratio
Shares of X held against one share of Y.
Engle-Granger p
Chance of a spread this stationary if the pair were not cointegrated; above 0.05 fails.
Innovation
The filter's one-day prediction error; divided by its predicted standard deviation it should have variance 1.
Sharpe
Annualized mean over standard deviation of daily returns, risk-free rate 0.

What this shows

  • On this pair and window, the Kalman hedge did worse than a plain rolling hedge after costs.
  • The diagnostics explain why: the pair isn't cointegrated in training and the filter's error model doesn't fit.
  • The code behaves as designed on a pair built to mean-revert.

What it doesn't show

  • That either hedge has a tradeable edge.
  • How the gap varies across pairs or windows: one pair, one split.
  • Uncertainty in the Sharpe values: no bootstrap or repeated windows.
  • Borrow fees, margin interest or fills away from the close.

Reproduce

Run it yourself

git clone https://github.com/allenwang0/statarb.git && cd statarb
python3.12 -m venv .venv   # Python 3.11 or 3.12
.venv/bin/pip install -e ".[dev]"
.venv/bin/python scripts/make_report.py --prices data/PEP_KO_2018-01-01_2024-12-31.csv
.venv/bin/streamlit run app.py   # the interactive app
Environment for this build
FieldValue
Pricesdata/PEP_KO_2018-01-01_2024-12-31.csv, downloaded 2026-11-23
Provenanceyfinance 1.8.0, yf.download(auto_adjust=True)
Synthetic pairn 1500, beta 1.5, alpha 0.2, noise 0.01, phi 0.95, level 50.0, seed 42
Python3.12.16
numpy / pandas / statsmodels2.4.6 / 3.1.1 / 0.15.0
Commita122bf2