What does Liquidation Cascades and Cointegration conclude, in numbers, about “The Kalman Filter and Dynamic Hedge Ratios”?
Answer: The Kalman filter treats the hedge ratio as a hidden random-walk state and corrects it each day by the innovation times the gain; \(Q/R\) sets how quickly it reacts. On synthetic data where its settings are exactly right and observation noise is white, it tracks the hedge ratio far better than rolling regression; but faced with sudden jumps no setting is best everywhere. More importantly, when the spread itself is mean-reverting, the setting that tracks best absorbs the trading signal, and on synthetic pairs its net return falls below that of 60-day rolling regression. Whether the Kalman filter does better on real data must be answered by a pre-registered, out-of-sample test with realistic costs, which is the protocol of Chapter 8. First, the next chapter returns to Sichuan about a thousand years ago, to see how a paper money backed by a "reserve" lost its credit through over-issue.
Scope of the numbers: This book presents mathematical models for teaching and research and is not investment, trading, tax or legal advice; leverage and derivatives can lose all of the money put in and more, and backtests and simulations do not represent future returns. Marked boxes separate published evidence, runnable Python calculations (16 programs, all checked), classical sources and the author's hypotheses.
Data table
The numbers below are printed verbatim by the chapter's Python program and can be reproduced by running it. standard-library Python onlyprinted values that match the answer: 60
| estimator | drift: RMSE of b | step: RMSE of b | reach (days) | |
| Kalman, qb | 0.005^2 | 0.0229 | 0.0471 | 81 |
| Kalman, qb | 0.02^2 | 0.0497 | 0.0479 | 3 |
| rolling OLS, w | 30 | 0.1996 | 0.3188 | 30 |
| rolling OLS, w | 60 | 0.1663 | 0.3738 | 59 |
| rolling OLS, w | 120 | 0.1836 | 0.3288 | 116 |
| rolling OLS, w | 250 | 0.1914 | 0.5079 | 242 |
| step: lowest estimate after day 500 (true value 1.3): | |
| KF 0.005^2 +1.05; KF 0.02^2 +1.14; OLS 30 -0.74 | |
| OLS 60 -0.55; OLS 120 +0.17; OLS 250 +0.24 |
Key formula
Show the full program
# Two-state Kalman filter (a, b random walks) vs rolling OLS on synthetic data:
# b drifts (daily sd 0.005) or jumps 1.0 -> 1.3 on day 500. Scored from day 250.
import math, random
rng = random.Random(1960)
def kalman(xs, ys, qa, qb, R=1.0):
a, b = 0.0, 0.0
P = [[100.0, 0.0], [0.0, 100.0]] # vague start
out = []
for x, y in zip(xs, ys):
P = [[P[0][0] + qa, P[0][1]], [P[1][0], P[1][1] + qb]] # predict
e = y - a - b * x
PH = [P[0][0] + P[0][1] * x, P[1][0] + P[1][1] * x] # P H'
S = PH[0] + PH[1] * x + R
K = [PH[0] / S, PH[1] / S]
a += K[0] * e; b += K[1] * e
P = [[P[i][j] - K[i] * PH[j] for j in range(2)] for i in range(2)]
out.append(b)
return out
def rolling(xs, ys, w):
out = []
for t in range(len(xs)):
lo = max(0, t - w + 1); X, Y = xs[lo:t + 1], ys[lo:t + 1]; n = len(X)
if n < 3: out.append(0.0); continue
mx, my = sum(X) / n, sum(Y) / n
sxx = sum((u - mx) ** 2 for u in X)
out.append(sum((u - mx) * (v - my) for u, v in zip(X, Y)) / sxx if sxx else 0.0)
return out
def data(kind):
xs, bs, x, b = [], [], 50.0, 1.0
for t in range(1000):
x += rng.gauss(0, 1)
if kind == "drift": b += rng.gauss(0, 0.005)
else: b = 1.3 if t >= 500 else 1.0
xs.append(x); bs.append(b)
ys = [b * x + rng.gauss(0, 1) for x, b in zip(xs, bs)]
return xs, ys, bs
def rmse(est, bs):
return math.sqrt(sum((e - b) ** 2 for e, b in zip(est[250:], bs[250:])) / 750)
def first(est): # days after the step until first within 0.05
for t in range(500, 1000):
if abs(est[t] - 1.3) < 0.05: return t - 500
print("estimator | drift: RMSE of b | step: RMSE of b | reach (days)")
xs1, ys1, b1 = data("drift"); xs2, ys2, b2 = data("step")
rows = [(f"Kalman, qb = {q}^2", (lambda q: lambda x, y: kalman(x, y, 1e-6, q * q))(q))
for q in (0.005, 0.02)]
rows += [(f"rolling OLS, w = {w}", (lambda w: lambda x, y: rolling(x, y, w))(w))
for w in (30, 60, 120, 250)]
low = []
for name, f in rows:
e1, e2 = f(xs1, ys1), f(xs2, ys2)
tag = ("KF " if name[0] == "K" else "OLS ") + name.split("= ")[1]
low.append(f"{tag} {min(e2[500:]):+.2f}")
print(f"{name:20s} | {rmse(e1, b1):6.4f} | {rmse(e2, b2):6.4f} |"
f" {first(e2):3d}")
print("step: lowest estimate after day 500 (true value 1.3):")
print(" " + "; ".join(low[:3])); print(" " + "; ".join(low[3:]))Single source and citation
Fan, Y. (2026). Liquidation Cascades, Order-Book Depth Depletion and Cointegrated Statistical Arbitrage (Version 1.0.0), Chapter 6: The Kalman Filter and Dynamic Hedge Ratios. 5D Library. https://doi.org/10.5281/zenodo.23796776
DOI: 10.5281/zenodo.23796776 · Read the monograph · 5DL-B-039 · v1.0.0 · CC-BY-NC-ND-4.0
BibTeX
@book{fan2026039,
author = {Fan, Yafu},
title = {Liquidation Cascades, Order-Book Depth Depletion and Cointegrated Statistical Arbitrage},
year = {2026},
publisher = {5D Library},
edition = {1.0.0},
doi = {10.5281/zenodo.23796776},
url = {https://doi.org/10.5281/zenodo.23796776}
}Other chapter answers in this book
- Chapter 1 · Order-Book Depth and Price Impact
- Chapter 2 · Perpetual Contracts, Funding and Forced Liquidation
- Chapter 3 · Liquidation Cascades, Branching Processes and Hawkes Self-Exciting Point Processes
- Chapter 4 · The Ornstein–Uhlenbeck Process: Mean Reversion and Its Estimation
- Chapter 5 · Cointegration, Pairs Trading and Broken Relationships
- Chapter 8 · Practice and a Pre-Registered Test