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
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.
| Metric | Kalman | Rolling OLS |
|---|---|---|
| Annualized return | -2.5% | 3.4% |
| Annualized volatility | 5.4% | 5.6% |
| Sharpe (rf = 0) | -0.44 | 0.62 |
| Sortino (rf = 0) | -0.63 | 1.03 |
| Max drawdown | -10.1% | -6.1% |
| Trades | 28 | 55 |
| Average holding period (days) | 21.61 | 6.18 |
| Hit rate (closed trades) | 67.9% | 64.3% |
| Annual turnover (x equity) | 20.21 | 39.74 |
| Annual cost drag | 0.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
| Metric | Kalman, train | Kalman, test | Rolling OLS, train | Rolling OLS, test |
|---|---|---|---|---|
| Annualized return | 3.3% | -2.5% | 6.7% | 3.4% |
| Annualized volatility | 8.0% | 5.4% | 7.7% | 5.6% |
| Sharpe (rf = 0) | 0.45 | -0.44 | 0.88 | 0.62 |
| Sortino (rf = 0) | 0.71 | -0.63 | 1.61 | 1.03 |
| Max drawdown | -11.0% | -10.1% | -5.1% | -6.1% |
| Trades | 29 | 28 | 92 | 55 |
| Average holding period (days) | 29.66 | 21.61 | 5.01 | 6.18 |
| Hit rate (closed trades) | 71.4% | 67.9% | 64.8% | 64.3% |
| Annual turnover (x equity) | 14.63 | 20.21 | 46.24 | 39.74 |
| Annual cost drag | 0.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
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
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
| Metric | Kalman, train | Kalman, test | Rolling OLS, train | Rolling OLS, test |
|---|---|---|---|---|
| Annualized return | 9.1% | 5.3% | 4.9% | 2.8% |
| Annualized volatility | 4.8% | 4.6% | 2.4% | 1.8% |
| Sharpe (rf = 0) | 1.85 | 1.15 | 2.03 | 1.50 |
| Sortino (rf = 0) | 2.95 | 1.79 | 3.71 | 2.62 |
| Max drawdown | -4.0% | -3.8% | -2.3% | -1.0% |
| Trades | 45 | 30 | 15 | 5 |
| Average holding period (days) | 11.73 | 8.73 | 9.40 | 9.00 |
| Hit rate (closed trades) | 93.2% | 70.0% | 93.3% | 100.0% |
| Annual turnover (x equity) | 26.92 | 25.26 | 8.87 | 3.75 |
| Annual cost drag | 0.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| Field | Value |
|---|---|
| Prices | data/PEP_KO_2018-01-01_2024-12-31.csv, downloaded 2026-11-23 |
| Provenance | yfinance 1.8.0, yf.download(auto_adjust=True) |
| Synthetic pair | n 1500, beta 1.5, alpha 0.2, noise 0.01, phi 0.95, level 50.0, seed 42 |
| Python | 3.12.16 |
| numpy / pandas / statsmodels | 2.4.6 / 3.1.1 / 0.15.0 |
| Commit | a122bf2 |