Kalman Filter

To control any dynamical system as accurately as possible, you need two things at once. The first is its mathematical description: the law by which the system evolves in time. The second is monitoring its state: measurements that tell where the system has actually ended up at the given moment. The trouble is that neither the model nor the measurements can be trusted absolutely.

A model always lies — if only a little. It doesn't know all the quirks of the particular object: friction, backlash, wear, random external nudges. It predicts an ideal system, while the one we actually control is real. Measurements lie too. A sensor is subject to noise, it jitters and errs.

It would be great to cross a bulldog with a rhino — take the model's predictive power and the sensors' grip on reality — and get an estimate better than either source alone. That is exactly what the Kalman filter does: at each step it weighs how much to trust the model's prediction against the fresh measurement and returns the state estimate that is optimal in the mean-square sense.

Let's start with a general description of a linear dynamical system:

dxdt\displaystyle \frac{dx}{dt} =Acx\displaystyle = A_c\, x +Bcu\displaystyle + B_c\, u
(1)

where AcA_c is the system matrix, xx is the state vector, BcB_c is the input matrix, and uu is the control input.

Let's write system (1) in discrete form: split time into steps ii =0,1,2,= 0, 1, 2, \dots with interval hh and approximate the derivative by the Euler method — the ratio of increments over one step

dxdt\displaystyle \frac{dx}{dt} xi+1xih\displaystyle \approx \frac{x_{i+1} - x_i}{h} =Acxi\displaystyle = A_c\, x_i +Bcui\displaystyle + B_c\, u_i

solving this for xi+1x_{i+1}, we get a recurrence — the next state through the current one

xi+1\displaystyle x_{i+1} =(I+Ach)xi\displaystyle = (I + A_c h)\, x_i +(Bch)ui\displaystyle + (B_c h)\, u_i

that is, the continuous matrices collapse into a discrete transition matrix AA and a discrete control matrix BB

A\displaystyle A =I\displaystyle = I +Ach,B\displaystyle + A_c h, \qquad B =Bch\displaystyle = B_c h
(2)

and the formula takes its final form

xi+1\displaystyle x_{i+1} =Axi\displaystyle = A\, x_i +Bui\displaystyle + B\, u_i
(3)

here uiu_i is the control input at step ii.

A real system doesn't follow equation (3) exactly. Let's add a random vector wiw_i — everything the model fails to describe

xi+1\displaystyle x_{i+1} =Axi\displaystyle = A\, x_i +Bui\displaystyle + B\, u_i +wi\displaystyle + w_i
(4)

the process noise wiw_i is taken to be random with zero mean; the measure of its spread — the covariance matrix QQ, the model-error matrix — will be defined below, when we derive the covariance prediction.

One filter cycle is two beats: prediction from the model and correction from the measurement. First the prediction. By formula (3) we push the previous estimate x^i1\hat{x}_{i-1} one step forward and obtain the a priori estimate x^i\hat{x}_i^{-} (the "minus" sign means "before the measurement is taken into account")

x^i\displaystyle \hat{x}_i^{-} =Ax^i1\displaystyle = A\, \hat{x}_{i-1} +Bui1\displaystyle + B\, u_{i-1}
(5)

together with the estimate we also predict its uncertainty. Introduce the estimation error — the difference between the true state and the estimate — and let the measure of uncertainty be its covariance matrix PP (on the diagonal — the variances of the errors in each state component)

ei\displaystyle e_i =xi\displaystyle = x_i x^i,Pi\displaystyle - \hat{x}_i, \qquad P_i =E ⁣[eieiT]\displaystyle = \mathbb{E}\!\left[\, e_i\, e_i^{\mathsf{T}} \,\right]

here E[]\mathbb{E}[\,\cdot\,] is the averaging operator (the expected value): it takes a random quantity and returns its mean over all possible realizations. A single error eie_i is random and unpredictable, but averaging the matrix eieiTe_i e_i^{\mathsf{T}} over the ensemble smooths out the randomness and leaves a stable measure of spread — the covariance.

Let's see what this error turns into over one prediction step. The true state follows equation (4) with a shifted index, xix_i =Axi1= A x_{i-1} +Bui1+ B u_{i-1} +wi1+ w_{i-1}, while our estimate follows formula (5); subtract the second from the first. The identical Bui1B u_{i-1} terms cancel, and what remains is

ei\displaystyle e_i^{-} =xi\displaystyle = x_i x^i\displaystyle - \hat{x}_i^{-} =A(xi1x^i1)\displaystyle = A\,(x_{i-1} - \hat{x}_{i-1}) +wi1\displaystyle + w_{i-1} =Aei1\displaystyle = A\, e_{i-1} +wi1\displaystyle + w_{i-1}

that is, the a priori error is the previous-step error ei1e_{i-1} pushed through the dynamics AA, plus the fresh model error wi1w_{i-1}. Take the covariance of both sides. The past error and the new noise are independent, so the cross terms E[ei1wi1T]\mathbb{E}[e_{i-1} w_{i-1}^{\mathsf{T}}] vanish, and the two that remain are Pi1P_{i-1} and QQ by their definitions

Pi\displaystyle P_i^{-} =E ⁣[ei(ei)T]\displaystyle = \mathbb{E}\!\left[\, e_i^{-} (e_i^{-})^{\mathsf{T}} \,\right] =AE[ei1ei1T]AT\displaystyle = A\, \mathbb{E}[\, e_{i-1} e_{i-1}^{\mathsf{T}} \,]\, A^{\mathsf{T}} +E[wi1wi1T]\displaystyle + \mathbb{E}[\, w_{i-1} w_{i-1}^{\mathsf{T}} \,]

substituting E[ei1ei1T]\mathbb{E}[e_{i-1} e_{i-1}^{\mathsf{T}}] =Pi1= P_{i-1} and E[wi1wi1T]\mathbb{E}[w_{i-1} w_{i-1}^{\mathsf{T}}] =Q= Q, we get the covariance-prediction formula: the old uncertainty is carried through AA, and the model error QQ is added to it

Pi\displaystyle P_i^{-} =APi1AT\displaystyle = A\, P_{i-1}\, A^{\mathsf{T}} +Q\displaystyle + Q
(6)

on the diagonal of QQ are the variances of the error in each state component, off the diagonal — their mutual covariances. A large QQ means "I don't trust the model, it's crude"; a small one — "the model is accurate". Deriving QQ analytically from first principles almost never works, so it is chosen empirically — by tuning against the filter's behavior on real or simulated data. It is the main tuning knob, and QQ is usually taken diagonal, just to avoid guessing the covariances.

A word about PP itself: a small one means "I trust the estimate", a large one — "the estimate is unreliable". The filter drives it on its own — during prediction it grows (formula (6) adds QQ), and at the correction step it will shrink once the measurement brings in information. The starting P0P_0 is taken deliberately large: the initial state is poorly known, so the filter distrusts the first steps — it quickly pulls toward the measurements and "warms up".

Usually we don't observe the whole state vector xx. Only some quantities are observed — the measurement vector yy, whose dimension is smaller than that of the state. The link between the measurement and the state is given by the observation matrix HH

yi\displaystyle y_i =Hxi\displaystyle = H\, x_i
(7)

the role of HH is to reconcile the dimensions: it maps the nn-dimensional state xx to the mm-dimensional measurement yy (the number of rows mm n\le n), selecting and linearly combining the state components that are actually measured. This is the whole point of an observer: to reconstruct what is not measured.

The sensor doesn't output HxiH x_i exactly — the measurement is distorted by noise viv_i

yi\displaystyle y_i =Hxi\displaystyle = H\, x_i +vi\displaystyle + v_i
(8)

for the noise viv_i it is convenient to assume a normal (Gaussian) distribution with zero mean — then the Kalman filter turns out to be the optimal Bayesian estimate as well. But this is not a requirement: for any other distribution with finite variance the filter remains the best linear estimate in the mean-square sense. The spread of the noise is set by the measurement-noise covariance matrix

R\displaystyle R =E ⁣[viviT]\displaystyle = \mathbb{E}\!\left[\, v_i\, v_i^{\mathsf{T}} \,\right]
(9)

it is built from the sensors' variances. If the noises of different sensors are independent, RR is diagonal: on the diagonal — the squared standard deviations (the instrument's rated accuracy), off the diagonal — zeros

R\displaystyle R =(σ12000σ22000σm2)\displaystyle = \begin{pmatrix} \sigma_1^2 & 0 & \cdots & 0 \\ 0 & \sigma_2^2 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & \sigma_m^2 \end{pmatrix}
(10)

the more accurate the sensor, the smaller its σ2\sigma^2 and the more the filter will trust that measurement channel.

Now everything is ready to formulate the filter itself. At step ii we have two independent sources of information about the sought state xx. The first is the model's prediction x^i\hat{x}_i^{-} with error covariance PiP_i^{-}: the true xx lies somewhere around x^i\hat{x}_i^{-} with spread PiP_i^{-}. The second is the measurement yiy_i =Hx= H x +v+ v with noise covariance RR: it points to xx with accuracy RR. Both are inexact — we look for the state that is most likely for both at once.

We'll assume the errors of both sources are Gaussian (for the measurement noise we've already assumed this). Then each source defines a probability density of the form exp ⁣(12eTΣ1e)\exp\!\left(-\tfrac12\, e^{\mathsf{T}} \Sigma^{-1} e\right), where ee is its error and Σ\Sigma its covariance. The inverse covariance Σ1\Sigma^{-1} plays the role of a weight: the more reliable the source (the smaller its spread), the harder it pulls the estimate toward itself. The likelihood of a state xx is the product of two such densities

p(x)\displaystyle p(x) exp ⁣(12(xx^i)T(Pi)1(xx^i))exp ⁣(12(yiHx)TR1(yiHx))\displaystyle \propto \exp\!\left(-\tfrac12 (x - \hat{x}_i^{-})^{\mathsf{T}} (P_i^{-})^{-1} (x - \hat{x}_i^{-})\right) \cdot \exp\!\left(-\tfrac12 (y_i - H x)^{\mathsf{T}} R^{-1} (y_i - H x)\right)

the most likely xx maximizes this product. The logarithm turns the product into a sum of exponents, and the maximum of p(x)p(x) corresponds to the minimum of 2lnp(x)-2\ln p(x); dropping the constants, we get exactly a sum of two quadratic forms — and that is our functional

J(x)\displaystyle J(x) =(xx^i)T(Pi)1(xx^i)model error\displaystyle = \underbrace{(x - \hat{x}_i^{-})^{\mathsf{T}} (P_i^{-})^{-1} (x - \hat{x}_i^{-})}_{\text{model error}} +(yiHx)TR1(yiHx)measurement error\displaystyle + \underbrace{(y_i - H x)^{\mathsf{T}} R^{-1} (y_i - H x)}_{\text{measurement error}}
(11)

the optimal estimate minimizes JJ. Take the derivative with respect to xx and set it to zero

Jx\displaystyle \frac{\partial J}{\partial x} =2(Pi)1(xx^i)\displaystyle = 2\,(P_i^{-})^{-1} (x - \hat{x}_i^{-}) 2HTR1(yiHx)\displaystyle - 2\, H^{\mathsf{T}} R^{-1} (y_i - H x) =0\displaystyle = 0
(12)

expanding the brackets and collecting xx on the left-hand side, we get the normal equation

[(Pi)1+HTR1H]x^i\displaystyle \left[\, (P_i^{-})^{-1} + H^{\mathsf{T}} R^{-1} H \,\right] \hat{x}_i =(Pi)1x^i\displaystyle = (P_i^{-})^{-1} \hat{x}_i^{-} +HTR1yi\displaystyle + H^{\mathsf{T}} R^{-1} y_i
(13)

the matrix in square brackets is exactly the inverse a posteriori covariance (Pi)1(P_i)^{-1} (information form: the prediction's information plus the measurement's information). From here the rule for updating the uncertainty follows at once

Pi\displaystyle P_i =[(Pi)1+HTR1H]1\displaystyle = \left[\, (P_i^{-})^{-1} + H^{\mathsf{T}} R^{-1} H \,\right]^{-1}
(14)

Inverting matrices the size of the state is expensive. Applying the matrix inversion lemma, we rewrite solution (13)–(14) in a computationally convenient form through the Kalman gain KiK_i

Ki\displaystyle K_i =PiHT(HPiHT+R)1\displaystyle = P_i^{-} H^{\mathsf{T}} \left(\, H P_i^{-} H^{\mathsf{T}} + R \,\right)^{-1}
(15)

then the correction of the estimate is the prediction plus a term proportional to the residual (yiHx^i)(y_i - H \hat{x}_i^{-}) — the discrepancy between what the model predicted and what the sensor showed

x^i\displaystyle \hat{x}_i =x^i\displaystyle = \hat{x}_i^{-} +Ki(yiHx^i)\displaystyle + K_i \left(\, y_i - H \hat{x}_i^{-} \,\right)
(16)

and the error covariance after the correction decreases

Pi\displaystyle P_i =(IKiH)Pi\displaystyle = (I - K_i H)\, P_i^{-}
(17)

the meaning of KK is a trust regulator. If the measurement is accurate (RR is small), KK is large and the estimate leans toward the sensor; if the prediction is reliable (PP^{-} is small), KK is small and the filter sticks to the model. The very same bulldog-and-rhino compromise, only computed optimally.