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

01

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

Direct C++ · 1×1 10 ns

predict + correct

Direct C++ · maximum 235×

versus OpenCV at 1×1

Python batch · 2×1 31 ns

per tick with trajectory

Python batch · 2×1 113×

versus the cv2 loop

02

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 × 110 ns2,459 ns235×
2 × 120 ns2,533 ns125×
3 × 130 ns2,618 ns86×
4 × 268 ns3,071 ns45×
6 × 3226 ns3,456 ns15×
8 × 81,143 ns8,513 ns7×
Table 1. Direct C++ predict-and-correct latency; neither side crosses Python. Lower is better.
cv2 per call3,073 ns
fast-kalman402 ns
Figure 1. Python per-call latency at 2×1: 7.6× faster.
cv2 loop3,495 ns
batch + states31 ns
Figure 2. Stock-trades-shaped batch with the full trajectory retained: 113× faster.
03

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.

01 / INPUT

Measurement columns

zt, Δtt, and Rt arrive as contiguous NumPy arrays.

02 / C++23

Predict and correct

Small-matrix operations execute inside one native loop without per-tick Python allocation.

03 / OUTPUT

State trajectory

One (N, nx) NumPy view is ready for analysis or vectorized decisions.

A

Seven filter families

Linear, adaptive, extended, iterated extended, unscented, ensemble, and shared-dynamics banks.

Filter catalog →
B

RTS smoothing

Backward smoothing with optional covariance history and marginal Gaussian confidence bands.

Smoother guide →
C

Filter banks

Structure-of-arrays storage vectorizes across symbols, pixels, or sensor channels with shared dynamics.

Bank guide →
D

Multi-ISA wheels

Runtime selection across x86, ARM, LoongArch, PowerPC, and a generic fallback.

Dispatch design →
04

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
05

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.

$ python -m pip install fast-kalmanimport as kalman
Pythonbatched trajectory
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"]
C++direct fixed-size API
#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

  1. Reproduce the headline tables with python benchmarks/benchmark_finance_shapes.py and python benchmarks/benchmark_cpp_core.py --nx=2 --nz=1 --opencv=always. See the benchmark documentation for definitions and caveats.
  2. The stock-trades-shaped workload contains 200,000 observations with per-tick delta_t and measurement variance. The 31 ns result includes recording the state trajectory.
  3. OpenCV comparisons use equivalent covariance-form filtering. Direct C++ figures cross no Python boundary; Python figures compare Python-accessible paths.
  4. The source, benchmark harness, implementation, and documentation are available in the public repository.