The Kalman filter
The question
A cart rolls out of sight and its sensor readings jitter. How would you estimate where it is now?
Adjust a filter and check it against the cart’s hidden path.
- 1 SeeTune a filter for a hidden path.
- 2 TryChange the sensor’s noise variance.
- 3 ProveDerive the update from a Gaussian density.
- 4 ApplyUpdate a forecast and handle missing readings.
Before you start · 3 quick checks on variance, persistence and conditioning
Two ideas before you start
2 ideasThe cart moves and the sensor reads
The state disturbance moves the cart. The measurement error offsets the reading from the cart’s position.
A state disturbance changes the cart’s actual position. A measurement error changes what the sensor reports.
Exact definition
At equally spaced steps \(t\), \(X_t\) is the hidden position and \(Y_t\) is the reading. The game uses a random walk with state disturbance \(W_t\) and measurement error \(E_t\).
\[\begin{aligned}X_t &= X_{t-1}+W_t,\\ Y_t &= X_t+E_t.\end{aligned}\]The disturbances are independent across time and of each other and the initial state. They are Gaussian with mean zero and constant variances \(Q\gt 0\) for \(W_t\) and \(R\gt 0\) for \(E_t\). The fixed offsets in this diagram illustrate one step.
An estimate has a center and a spread
The estimate stays at the center while its uncertainty band gets wider or narrower.
A filter carries both a position estimate and a measure of how uncertain that estimate is. The center can stay put while the band changes.
Exact definition
Write \(m_t\) and \(P_t\) for the conditional mean and conditional variance of \(X_t\) after reading \(Y_t\) and all earlier readings. The uncertainty band here runs from \(m_t-\sqrt{P_t}\) to \(m_t+\sqrt{P_t}\). Its half-width is the conditional standard deviation, measured in position units. The variance itself has squared position units.
Under the matching Gaussian model, the band describes the distribution of the state at each step given the readings up to that step. It can miss the true position. The model gives no promise that a whole path will stay inside it.
Refresher: what carries over from other lessons
In the AR(1) model, a coefficient \(a\) multiplies the preceding state before independent noise is added. The model here uses the random-walk case \(a=1\) for the game. The filter works with a distribution for the hidden state, since the sensor sees a noisy reading in place of the state itself.
Conjugate priors explains how a normal prior and a Gaussian reading give a normal posterior. Here the previous posterior first has to move forward in time. The resulting forecast distribution is the prior for the next reading.
Stationarity concerns the distribution of a process over time. A filter’s error variance is a different quantity. A driftless random walk with independent increments of fixed positive variance has increasing state variance, even when the filter’s error variance settles.
Tune the filter while the cart is hidden
- Scan the sensor dots and choose an assumed ratio \(R/Q\). A larger ratio assumes less state variance per step relative to sensor variance.
- Check your setting to reveal the cart and your filtered path. Try to minimize the model score across the available settings.
- Compare the two error scores, then try another setting with the same readings.
The initial position is Gaussian with mean zero and variance \(R\). The filter starts with that same distribution and is given the correct sensor variance. Changing the ratio changes its assumed state variance. Each estimate uses only the sensor readings through its own step.
Why the scores can disagree
The path score is the square root of the average squared distance between the estimates and the true positions on this run. The model score takes the expected squared error at each step under the generating model, averages those expectations, and takes the square root. Both scores use position units.
The game judges the model score. The assumed Gaussian filter acts as if its chosen variance were correct. When that variance matches the generator, its estimate is the true conditional mean, which minimizes expected squared error. Different settings give different weights at the first reading. That makes the matching setting the unique model-score winner among the choices.
The band uses the assumed variance. With a mismatched setting it can be too narrow or too wide for the actual error. A single run can favor a different setting than the model score. Changing the setting keeps the readings and underlying path fixed, so it isolates the effect of tuning.
Change the sensor and keep the forecast
Take a Gaussian forecast with mean \(m\) and positive variance \(V\). Two independent-error sensor models report the same reading \(y\), which differs from \(m\). Each sensor’s error is centered Gaussian with positive variance.
- Adjust the sensor variance. The forecast mean, forecast variance and reading stay fixed.
- Match the goal exactly. The display marks rounded values with ≈.
Follow a forecast into the next update
Suppose the state rule is \(X_t=aX_{t-1}+W_t\), with known \(a\) and fresh independent centered Gaussian noise of variance \(Q\). With previous conditional mean \(m_{t-1}\) and variance \(P_{t-1}\), the new forecast has mean \(m=a m_{t-1}\) and variance \(V=a^2P_{t-1}+Q\). Multiplication by \(a\) scales the variance by \(a^2\). Independence lets the new variance add.
The difference \(y-m\) is the innovation. The gain \(K\) is the fraction of that difference applied as a correction. In this scalar model with direct reading \(Y_t=X_t+E_t\), the update is
\[\begin{aligned}K&=\frac{V}{V+R},\\ m_t&=m+K(y-m),\\ P_t&=(1-K)V.\end{aligned}\]For fixed \(V\) and \(R\), the value of \(y\) enters the mean update and leaves the variance unchanged across different readings. The noise assumptions determine how informative a reading is. If a reading is unavailable for reasons independent of the state and the reading, keep the forecast as the current estimate and variance.
Derive the gain and the updated variance
Given the previous readings, let the forecast for \(X\) be Gaussian with mean \(m\in\mathbb R\) and variance \(V\gt 0\). Observe \(Y=X+E\), where \(E\) is independent of the state and past readings, and Gaussian with mean zero and variance \(R\gt 0\). For every real reading \(y\), the conditional distribution of \(X\) is Gaussian with mean \(m+K(y-m)\) and variance \(P=VR/(V+R)\), where \(K=V/(V+R)\).
Choose the algebra that turns the prior and the likelihood into one Gaussian. Let \(x\) be a possible state value. Up to an additive constant in \(x\), the log posterior has the form \(-Ax^2/2+Bx\). The proof identifies the coefficients and reads the new mean and variance from the square.
Why the band in the game settles
In the random-walk model with constant positive \(Q,R\) and a nonnegative starting variance, one full step maps the previous posterior variance \(p\) to \(f(p)=R(p+Q)/(p+Q+R)\). The nonnegative fixed point is
\[p_*=\frac{\sqrt{Q^2+4RQ}-Q}{2}.\]The other root of \(p^2+Qp-RQ=0\) is negative. On \(p\ge 0\), \(f\) is increasing and \(f(p)-p\) has the sign of \(RQ-p^2-Qp\). Starting above \(p_*\), each iterate is lower and stays above \(p_*\). Starting below it, each iterate rises and stays below it. An iterate at the fixed point stays there. The monotone bounded sequence has a limit, and continuity forces that limit to be \(p_*\).
The game’s assumed starting variance is \(R\), above the fixed point because \(f(R)\lt R\). The half-width converges to \(\sqrt{p_*}\) even if the fitted center keeps moving. With a mismatched setting, all of this describes the filter’s stated variance, not its actual error.
Why expected squared error is smallest at the conditional mean
For fixed readings through a step, let \(b\) be any estimate determined by them and let \(m_t\) be the conditional mean under the generating model. Expand \(X_t-b=(X_t-m_t)+(m_t-b)\). The expected cross term given the readings is zero, so the expected squared error given the readings is \(P_t+(m_t-b)^2\ge P_t\). Averaging over possible readings preserves this inequality. It is strict when \(b\) differs from \(m_t\) with positive probability, as a different first gain does for the game’s nondegenerate first reading.
Which part of the cycle do you need?
Use the forecast, the measurement update, or both. Give a decimal within the absolute error shown. Each problem has hints and a worked solution.
Further reading
- R. E. Kalman, A New Approach to Linear Filtering and Prediction Problems, Journal of Basic Engineering.