Computing Library › Machine Learning
Machine Learning

The Kalman Filter

The Kalman filter tracks a continuous hidden state over time by fusing a linear motion model with noisy measurements.

Estimating a moving state

The Kalman filter is the continuous-state analogue of the hidden Markov model: the hidden state is a real-valued vector that evolves linearly with Gaussian noise, and measurements are linear functions of the state plus Gaussian noise. Under these assumptions it computes the exact posterior over the state at each time step, and that posterior stays Gaussian.

Predict and update

Kronos motion — lego machine

Each step has two phases. The predict phase pushes the current state estimate through the motion model, growing the covariance by the process noise. The update phase incorporates a new measurement, shrinking the covariance and pulling the estimate toward the observation by an amount set by the Kalman gain.

The Kalman gain balances trust: when measurement noise is large, the gain is small and the filter leans on its prediction; when the state uncertainty is large, the gain is large and the filter leans on the measurement. This optimal weighting minimizes the mean-squared estimation error for linear-Gaussian systems.

python
# Predict
x = F @ x
P = F @ P @ F.T + Q
# Update with measurement z
y = z - H @ x
S = H @ P @ H.T + R
K = P @ H.T @ inv(S)
x = x + K @ y
P = (I - K @ H) @ P

Nonlinear extensions

Real systems are often nonlinear. The extended Kalman filter linearizes the model around the current estimate via Jacobians; the unscented Kalman filter propagates a set of sigma points through the true nonlinearity for better accuracy. When noise is non-Gaussian or highly multimodal, particle filters replace the Gaussian assumption with sampled hypotheses.

Kalman filters underpin navigation, sensor fusion, target tracking, and time-series smoothing, and the same recursion smooths states offline when a backward pass is added.