The standard Kalman filter estimates the state of a discrete-time linear system from noisy measurements. This tutorial derives its prediction, measurement update, and gain.
Model, prior, and posterior
\[\begin{aligned}x_k&=F_{k-1}x_{k-1}+B_{k-1}u_{k-1}+w_{k-1},\\ y_k&=H_kx_k+v_k.\end{aligned}\]The noises \(w\) and \(v\) are zero mean, white, mutually uncorrelated, and uncorrelated with the initial state. Their covariance matrices are \(Q\) and \(R\). With temporally white noise, the cross-time covariance vanishes unless the time indices are equal. Control \(u\) is optional and assumed known.
Write \(\hat x_k^-\) for the prior estimate based on observations through \(k-1\), and \(\hat x_k^+\) for the posterior including observation \(k\). They estimate the same state at different information sets. Under linear Gaussian assumptions these are conditional means; with suitable second-moment conditions alone they are optimal linear estimates. Initialize with a prior mean \(\hat x_0^+\) and error covariance \(P_0^+\). Use a large finite variance for a weak prior rather than numerical infinity.
1. Time update
Take the expectation of the state equation; the process noise has mean zero. In the covariance expansion the cross terms vanish, since the previous estimation error and new process noise are uncorrelated:
\[\begin{aligned}\hat x_k^-&=F_{k-1}\hat x_{k-1}^++B_{k-1}u_{k-1},\\ P_k^-&=F_{k-1}P_{k-1}^+F_{k-1}^\mathsf T+Q_{k-1}.\end{aligned}\]2. Measurement update
The innovation is \(r_k=y_k-H_k\hat x_k^-\), the difference between the actual and predicted measurement. Its covariance is \(S_k=H_kP_k^-H_k^\mathsf T+R_k\). The state correction is:
\[\hat x_k^+=\hat x_k^-+K_kr_k.\]A zero innovation (or zero gain) means that this update does not change the state estimate. The preceding prediction may still have moved it, and the measurement can still reduce uncertainty.
3. Derive the gain
With \(e_k^\pm=x_k-\hat x_k^\pm\), the posterior error is \(e_k^+=(I-K_kH_k)e_k^--K_kv_k\). Its unconditional mean is zero for any deterministic \(K_k\) when the prior is unbiased and the measurement noise has zero mean. Because the error and measurement noise are uncorrelated, the covariance is:
\[P_k^+=(I-K_kH_k)P_k^-(I-K_kH_k)^\mathsf T+K_kR_kK_k^\mathsf T.\]This is the Joseph form. Expanding its trace and setting the derivative with respect to \(K_k\) to zero gives \(K_kS_k=P_k^-H_k^\mathsf T\). For nonsingular \(S_k\):
\[K_k=P_k^-H_k^\mathsf T S_k^{-1}.\]In software, solve the linear system rather than explicitly forming the inverse. The gain minimizes the expected squared norm of the state estimation error under the model, not every individual error. In exact arithmetic with the optimal gain, the shorter covariance update \((I-K_kH_k)P_k^-\) is equivalent. The Joseph form is generally safer against round-off.

Try the equations
Use the measurement-noise estimator to build R from stable readings and the constant-velocity generator to form F and Q. Then compare the resulting behavior in the motion-tracking demo.


