nano-kalman

What is this?

A Kalman filter is an algorithm that combines a predictive model ("things keep moving the way they were") with noisy measurements ("the sensor says it is roughly here") to produce the statistically best estimate of something you cannot observe directly.

Published by Rudolf Kálmán in 1960, it navigated Apollo to the Moon and still runs inside every drone, rocket, GPS receiver, and robot vacuum.

This page is a live one, tracking a ball. The whole implementation is 50 lines of vanilla JavaScript, and the sandbox exists to let you feel what those lines do. The filter never knows where the ball is: it maintains a belief, a best guess plus an honest account of its own uncertainty, updated sixty times a second. Everything you will see on the canvas is that belief, made visible.

What a Kalman filter does, in two moves

Predict. Each frame, push the belief through simple physics: position moves by velocity, and the uncertainty grows a little, because no model of the world is perfect.

Update. When a measurement arrives, blend it with the prediction. The blend weight is the Kalman gain, and it is nothing but a trust ratio between two uncertainties:

K = σ²_prediction / (σ²_prediction + σ²_sensor)

new belief = prediction + K · (measurement − prediction)

A sharp sensor drives K toward 1: believe the dots. A noisy sensor drives K toward 0: trust the physics. The two σ sliders in the sandbox set exactly those two uncertainties, which is why they are the whole control panel: raising a slider admits more noise from that source, which means trusting it less. Everything the filter does is a precision-weighted argument between them:

Two Gaussians blending into a posterior as the Kalman gain slides between trusting the model and trusting the sensor

When the ball enters the occlusion strip, measurements stop and only predict runs. The uncertainty ellipse balloons because the filter is honest about what it no longer knows, and the first measurement on the far side snaps it tight again:

Predict steps inflating the uncertainty ellipse during occlusion, one update snapping it tight

Why it matters

Notice what the filter actually stores: not a position but a compact internal state, advanced by a model, corrected by evidence, decoded to pixels only for display. That loop is the skeleton of every learned world model in modern robotics and physical-world AI, and the Kalman filter is its closed-form ancestor. Its one hard limit is that a single-peaked belief cannot represent a fork like "the ball went left or right of the pillar", and that limit is where the rest of this series picks up.

The full derivation, from one idea to the five equations, is in the README. Now open the sandbox and press the presets.