Systems & scientific computing · Open source
Fast Kalman Filtering for Small State Spaces
A fixed-size C++23 implementation with a thin Python interface for streaming estimation, batched trajectories, and the small matrices general-purpose libraries overlook.
Abstract
fast-kalman specializes Kalman filtering for state and measurement dimensions from 1 through 8. Fixed-size templates, structured kinematic updates, runtime ISA selection, and a batched nanobind interface reduce both matrix work and language-boundary overhead. On the included 2-state, 1-measurement workload, a direct C++ predict-and-correct takes 20 ns; the Python batch path records a complete state trajectory at 31 ns per observation. The same implementation underlies both APIs, with explicit P, Q, and R and no matrix-library dependency.
Keywords: Kalman filter, state estimation, fixed-size matrices, C++23, Python, SIMD, market data, sensor fusion
Principal findings
The performance advantage is largest where the library is most deliberately specialized: tiny filters repeated across long streams. All values below come from the repository's own reproducible benchmark harness.1
predict + correct
versus OpenCV at 1×1
per tick with trajectory
versus the cv2 loop
Benchmark evidence
Direct C++ speedups range from 235× at 1×1 to 7× at 8×8, with the largest gains on the tiny filters that dominate high-frequency streaming workloads.
| State × measurement | fast-kalman | OpenCV C++ | Speedup |
|---|---|---|---|
| 1 × 1 | 10 ns | 2,459 ns | 235× |
| 2 × 1 | 20 ns | 2,533 ns | 125× |
| 3 × 1 | 30 ns | 2,618 ns | 86× |
| 4 × 2 | 68 ns | 3,071 ns | 45× |
| 6 × 3 | 226 ns | 3,456 ns | 15× |
| 8 × 8 | 1,143 ns | 8,513 ns | 7× |
Implementation
The public API stays conventional. The specialization lives beneath it: common shapes use fixed-size templates, other accepted shapes remain in runtime-sized C++, and the wheel selects the best compatible ISA backend when Python imports the module.
Measurement columns
zt, Δtt, and Rt arrive as contiguous NumPy arrays.
Predict and correct
Small-matrix operations execute inside one native loop without per-tick Python allocation.
State trajectory
One (N, nx) NumPy view is ready for analysis or vectorized decisions.
Seven filter families
Linear, adaptive, extended, iterated extended, unscented, ensemble, and shared-dynamics banks.
Filter catalog →RTS smoothing
Backward smoothing with optional covariance history and marginal Gaussian confidence bands.
Smoother guide →Filter banks
Structure-of-arrays storage vectorizes across symbols, pixels, or sensor channels with shared dynamics.
Bank guide →Multi-ISA wheels
Runtime selection across x86, ARM, LoongArch, PowerPC, and a generic fallback.
Dispatch design →Intended scope
fast-kalman is intentionally narrow: small state-space estimation repeated often. Python constructors accept dimensions 1 through 8; the library does not pretend to replace a general linear-algebra stack.
Good fit
- Market-data indicators and historical replay
- Sensor fusion and high-rate telemetry
- Object, pixel, and multi-channel tracking
- Small models on allocation-sensitive paths
Deliberate boundaries
- State vectors in the hundreds belong in Eigen
- Nonlinear callbacks retain their Python cost
- Nonlinear RTS smoothing is not implemented
Usage
The C++ templates and Python binding expose the same underlying implementation. Start with a linear 2×1 filter, then move between per-tick and batched execution without changing the model.
import kalman kf = kalman.LinearKalmanFilter(2, 1) kf.H = [1.0, 0.0] result = kf.update_many( measurements, delta_t=dts, record_states=True, ) trajectory = result["states"]
#include <kalman/kalman.hpp> kalman::LinearKalmanFilter<2, 1> kf; kf.H = {{1.0, 0.0}}; for (double z : measurements) { kf.update({z}); }
Notes and reproducibility
- Reproduce the headline tables with
python benchmarks/benchmark_finance_shapes.pyandpython benchmarks/benchmark_cpp_core.py --nx=2 --nz=1 --opencv=always. See the benchmark documentation for definitions and caveats. - The stock-trades-shaped workload contains 200,000 observations with per-tick
delta_tand measurement variance. The 31 ns result includes recording the state trajectory. - OpenCV comparisons use equivalent covariance-form filtering. Direct C++ figures cross no Python boundary; Python figures compare Python-accessible paths.
- The source, benchmark harness, implementation, and documentation are available in the public repository.
