Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Statistical inference

A one-dimensional Kalman filter

Medium · Target 20 minutes · Python and NumPy

A level xt follows a random walk: xt = xt−1 + wt with wt ~ N(0, q). You observe yt = xt + vt with vt ~ N(0, r), all noises independent. Before the first observation your belief about x1 is N(m0, p0).

Process ys in order. After each observation, record [mean, variance] of the level given everything seen so far. There is no prediction step before the first observation. Return the records.

Examples

Input ys = [10, 12, 11], q = 1, r = 4, m0 = 8, p0 = 9

Output [[9.3846, 2.7692], [10.6535, 1.9406], [10.8003, 1.6947]]

The gain starts at 9/13 and settles as the variance approaches its steady state.

Input ys = [5, 5, 5, 5], q = 0, r = 1, m0 = 0, p0 = 100

Output [[4.9505, 0.9901], [4.9751, 0.4975], [4.9834, 0.3322], [4.9875, 0.2494]]

q = 0: the level never moves, and the filter becomes a running average that forgets the poor prior.

Input ys = [3], q = 2, r = 1, m0 = 0, p0 = 1

Output [[1.5, 0.5]]

One observation: the posterior is halfway between prior and data, since p0 = r.

Constraints

  • 1 ≤ len(ys) ≤ 10⁵
  • q ≥ 0, r > 0, p0 > 0
  • Checked to a relative tolerance of 10⁻⁹

Hints

Hint 1

Keep two numbers, the mean m and the variance P of your belief about the current level.

Hint 2

Before each observation after the first, the level may have moved: add q to P.

Hint 3

The gain is K = P / (P + r). Then m ← m + K(y − m) and P ← (1 − K)P. Record [m, P].

Where candidates go wrong

  • Adding q before the first observation as well: the prior N(m0, p0) already describes x₁.
  • Updating P before computing the new mean with the old gain, or computing K after shrinking P.
  • Writing the update as P ← P − K, which mixes a variance with a dimensionless gain.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

Trading firms use filters like this to estimate fair prices, hedge ratios and spreads that drift. The interview version is one-dimensional so that the reasoning, not the matrix algebra, is on show.

The theory behind it is in the lesson Statistical inference in code: standard errors, permutation tests, the Kalman filter.


Keep going