# Kalman filter primer: derivations and golang implementation

One word for anyone who doesn’t know about the filter yet: a simple but strong algorithm which combines estimates and measurements to give better predications of some linear systems.

I learned it when studying self-driving during which I revisited really a lot of knowledge of linear algebra. So I wrote my notes here and hope that it would be your most easy-to-understand kalman filter primer.

First of all, let’s assume a linear system which is modelled by the following two equations:

where

**X**t (n*1) is the state vector of a process at time t, e.g., [position, velocity] of a running car**U**t is the control input vector at time t, e.g., brake or acceleration of the car**F**t (n*n) and**B**t are transition matrices at time t**Z**t (n*1) is measurement of**X**t at time t, and**H**t (n*n) is transition matrix at time t**W**t (n*1) is estimation noise vector at time t. It’s normal distribution and covariance is given by**Q**t (n*n)**V**t (n*1) is measurement noise vector at time t. Also normal distribution and covariance is given by**R**t (n*n)

By *linear**, *we mean its dynamics can be described by linear functions or transitions, for instance, we can describe the position (s) and velocity (v) of a car as the following(** a** is acceleration):

Then its state vector can be described as:

State vector at time *t* has linear relations with state vector at time *t-1*. That is just linear.

Now it comes to the predication equations:

**X **with a hat and superscript ‘-’ in (3) is the prior estimation of the state vector, that is to say, we get it only by prediction. **X** with a hat at *t-1* in (3) is the fused state vector at time *t-1* (you will see how we fuse prediction and measurement in update equations). **P** with superscript ‘-’ in (4) is our estimation error covariance with the unknown true value **X**. **P **at *t-1* in (4) is the fused error covariance with the unknown true value **X **at time *t-1***.**

How do we get (4)? Let’s derive it by definition:

Covariance is just the variances between two state vectors and variance is just the estimation of error between two state variables, so it should be easy to understand (8).

Substitution of (1) and (3) into (8) gives:

Please note that **W**t and state predication error are independent from each other, so their covariance is **0** matrix. **F**t can be seen as constant matrix, so its estimation is itself. Also you should know some basic knowledge of probability and linear algebra, mainly:

**C **is constant matrix and superscript ‘*T*’ means transpose matrix. These are what you should know before proceeding to the next stage, so review them first if needed.

Finally we come to the update equations:

From predication we got prior **X **with superscript ‘-’ and from measurement we got **Z**t, how do we fuse them to get a better value which is closer to the true **X**? (5) provides a weighted approach to fuse them together with nice properties that when prediction error is larger, fused **X **is** **closer to measurement, otherwise it’s closer to prediction. **P**t is the error covariance of fused **X** and the unknown true **X**. Our objective is just to minimize **P**t, specifically, the trace of **P**t (the trace of matrix is the sum of elements along its main diagonal), so the fused **X** can be as close to the true **X** as possible. **K**t (n*n) is the optimal weight factor which gives the minimized **P**t. Let’s derive it step by step.

Still by definition:

Substitution of (1), (2) and (5) into (9) gives:

It’s similar to the derivation of prior error covariance, so we won’t explain further.

Now the derivative of tr(**P**t) with respect to **K**t:

Note that tr(**P**t) is scalar while **K**t is matrix and the derivative is matrix too. In fact, tr(**P**t) is the sum function of its main diagonal elements and the derivative matrix is just the derivative of tr(**P**t) with respect to each element of **K**t. The equation above also uses the following *matrix* *trace* properties:

The second item in (10) is the transpose matrix of the third item in (10) (**P**t is symmetric), so their traces are the same, so are their derivatives, as illustrated in Fig 13.

Let (11) be **0**, then we finally get the optimal **K **in (7). The superscript ‘-1’ means inverse matrix.

Then the minimized **P**t is given by:

which is just described in (6).

Well done! We completed the derivation of kalman filter and now let’s put the five equations together:

That’s all about it. Kalman filter is iterative and it’s easy to implement the algorithm following the equations above. Yes you don’t need to know the details simply for applications, but knowing the derivations is certainly better for building reasonable applications😃.

Finally, an implementation in golang for your reference:

Enjoy learning!