Moving averages smooth prices, but they always lag, and they treat every observation the same way regardless of how noisy the market is. The Kalman filter solves a more ambitious problem: it estimates a hidden state (the "true" underlying value) from noisy observations, updating its estimate recursively as each new data point arrives, and weighting new information by how much it trusts it. That makes it one of the most useful tools a quant can have in MATLAB — for adaptive smoothing, dynamic hedge ratios in pairs trading, and time-varying beta estimation.
The intuition, without the matrix algebra headache
A Kalman filter alternates two steps on every bar:
When observations are noisy relative to the state, the gain is small and the filter smooths hard. When the state itself moves fast, the gain grows and the filter tracks quickly. That adaptivity is exactly what fixed-length moving averages lack.
A minimal price smoother in MATLAB
For a random-walk state model with scalar observations, the whole filter is a few lines:
The ratio Q/R is the only real tuning knob: raise Q (or cut R) and the filter hugs price; do the opposite and it smooths like a long moving average — but with far less lag for the same smoothness, because the gain adapts.
The killer application: dynamic hedge ratios for pairs trading
The textbook approach to a pairs trade estimates the hedge ratio with one OLS regression over a lookback window and assumes it is constant. Real relationships drift, and a stale hedge ratio quietly turns a market-neutral spread into a directional bet.
The Kalman formulation treats the hedge ratio itself (and the intercept) as the hidden state, observed through the regression equation each bar. The state vector is
In MATLAB you can write the 2-state filter by hand exactly as above (with 2x2 matrices), or lean on the Control System Toolbox (
Practical warnings
The bottom line
The Kalman filter gives MATLAB traders an adaptive, recursive, theoretically grounded alternative to fixed-window regressions and moving averages: less lag for the same smoothness, hedge ratios that follow the market instead of a lookback window, and a built-in health check in the innovation series. Master the scalar smoother first, then graduate to the two-state regression filter — it is the workhorse behind many statistical arbitrage desks for a reason.
The intuition, without the matrix algebra headache
A Kalman filter alternates two steps on every bar:
- Predict: project the current state estimate forward one step, and widen its uncertainty (things drift).
- Update: compare the new observation with the prediction. The difference is the innovation. The filter moves its estimate toward the observation by a fraction called the Kalman gain, which is recomputed every step from the relative sizes of process noise (how fast the true state can change) and measurement noise (how noisy the observations are).
When observations are noisy relative to the state, the gain is small and the filter smooths hard. When the state itself moves fast, the gain grows and the filter tracks quickly. That adaptivity is exactly what fixed-length moving averages lack.
A minimal price smoother in MATLAB
For a random-walk state model with scalar observations, the whole filter is a few lines:
% y: column vector of prices (or log prices)
Q = 1e-5; % process noise variance (state can drift)
R = 1e-2; % measurement noise variance (observations are noisy)
n = numel(y);
xhat = zeros(n,1); P = zeros(n,1);
xhat(1) = y(1); P(1) = 1;
for t = 2:n
% predict
xpred = xhat(t-1);
Ppred = P(t-1) + Q;
% update
K = Ppred / (Ppred + R); % Kalman gain
xhat(t) = xpred + K * (y(t) - xpred);
P(t) = (1 - K) * Ppred;
end
The ratio Q/R is the only real tuning knob: raise Q (or cut R) and the filter hugs price; do the opposite and it smooths like a long moving average — but with far less lag for the same smoothness, because the gain adapts.
The killer application: dynamic hedge ratios for pairs trading
The textbook approach to a pairs trade estimates the hedge ratio with one OLS regression over a lookback window and assumes it is constant. Real relationships drift, and a stale hedge ratio quietly turns a market-neutral spread into a directional bet.
The Kalman formulation treats the hedge ratio itself (and the intercept) as the hidden state, observed through the regression equation each bar. The state vector is
[alpha_t; beta_t], the observation matrix each step is H_t = [1, x_t], and the filter re-estimates both coefficients every bar with exponentially decaying memory. The spread you actually trade is the innovation — the difference between the observed y and the filter's prediction — which is naturally mean-zero if the model holds.In MATLAB you can write the 2-state filter by hand exactly as above (with 2x2 matrices), or lean on the Control System Toolbox (
kalman) and the Econometrics Toolbox's state-space machinery (ssm, filter, smooth), which also estimate the noise variances by maximum likelihood instead of hand-tuning.Practical warnings
- Do not use the smoother for backtests. The Kalman smoother (as opposed to the filter) uses the whole sample, including future data — beautiful plots, lookahead bias. For trading signals, only the filtered estimate at time t is legitimate.
- Garbage models, confident garbage. The filter always produces an estimate, with an uncertainty that assumes the model is right. If the pair's relationship genuinely breaks (delisting, corporate action, regime change), the filter will keep tracking — monitor the innovations: if they stop looking like zero-mean noise, the model is broken.
- Tune honestly. Q and R chosen to maximize backtest profit are just another overfit parameter pair. Estimating them by maximum likelihood on a training window, then freezing them out-of-sample, is the defensible route.
The bottom line
The Kalman filter gives MATLAB traders an adaptive, recursive, theoretically grounded alternative to fixed-window regressions and moving averages: less lag for the same smoothness, hedge ratios that follow the market instead of a lookback window, and a built-in health check in the innovation series. Master the scalar smoother first, then graduate to the two-state regression filter — it is the workhorse behind many statistical arbitrage desks for a reason.