
Kalman Filter
- Tutorial

On the Internet, including on the hub, you can find a lot of information about the Kalman filter. But it’s hard to find the easily digestible conclusion of the formulas themselves. Without a conclusion, all this science is perceived as a kind of shamanism, formulas look like a faceless set of symbols, and most importantly, many simple statements that lie on the surface of the theory are beyond understanding. The purpose of this article will be to talk about this filter in the most accessible language possible.
The Kalman Filter is a powerful data filtering tool. Its main principle is that when filtering, information about the physics of the phenomenon itself is used. For example, if you filter data from a car’s speedometer, then the inertia of the machine gives you the right to perceive too fast jumps in speed as a measurement error. The Kalman filter is interesting in that, in a sense, it is the best filter. We will discuss in more detail below what exactly the words “best” mean. At the end of the article I will show that in many cases the formulas can be simplified to such an extent that almost nothing remains of them.
Educational program
Before getting acquainted with the Kalman filter, I suggest recalling some simple definitions and facts from probability theory.
Random value
When they say that a random variable is given

In the case of a continuous set of values, the random variable is characterized by the probability density

Quite often in life, random variables are distributed according to Gauss, when the probability density is equal

We see that the function
Once we talked about the Gaussian distribution, it would be a sin not to mention where it came from. Just like numbers
Let there be a random variable
Mean
The average value of a random variable is what we get in the limit if we conduct a lot of experiments and calculate the arithmetic mean of the values that fell out. The mean value is denoted in different ways: mathematicians like to denote by
For example, for a Gaussian distribution
Dispersion
In the case of the Gaussian distribution, we clearly see that the random variable prefers to fall out in a certain neighborhood of its average value
Once again admire the Gaussian distribution

As can be seen from the graph, the characteristic scatter of order values
A simpler way (simple in the sense of calculations) is to find
For example, for the Gaussian distribution,
In fact, a little fraud is hidden here. The fact is that in the definition of the Gaussian distribution, the exponent is an expression
Independent Random Variables
Random variables are dependent and not. Imagine that you throw a needle onto a plane and record the coordinates of both ends. These two coordinates are dependent, they are connected by the condition that the distance between them is always equal to the length of the needle, although they are random variables.
Random variables are independent if the result of the loss of the first of them is completely independent of the result of the loss of the second of them. If the random variables
Evidence
For example, having blue eyes and graduating from school with a gold medal are independent random variables. If the blue-eyed, for example
a gold medal
, the blue-eyed medalists
This example tells us that if the random variables
and
set their probability densities
and
, the independence of the values expressed in the fact that the probability density
(the first value is dropped
and the second
) is given by:

It immediately follows from this that:

As you can see, the proof is carried out for random variables that have a continuous spectrum of values and are given by their probability density. In other cases, the idea of the proof is similar.
It immediately follows from this that:
As you can see, the proof is carried out for random variables that have a continuous spectrum of values and are given by their probability density. In other cases, the idea of the proof is similar.
Kalman Filter
Formulation of the problem
Denote by the
We begin with a simple example, which will lead us to the formulation of the general problem. Imagine that we have a radio-controlled machine that can only go back and forth. Knowing the weight of the car, shape, road surface, etc., we calculated how the controlling joystick affects the speed

Then the coordinate of the machine will change according to the law:
In real life, we can’t take into account in our calculations small disturbances acting on the car (wind, bumps, pebbles on the road), so the real speed of the car will differ from the calculated one. A random variable will be added to the right side of the written equation
We have a GPS-mounted sensor that tries to measure the true coordinate of the
The problem is that, knowing the sensor’s incorrect readings
In the formulation of the general problem,

Let's discuss in detail what we know:
Is a known quantity that controls the evolution of a system. We know her from our physical model.
- Model
error and sensor error
are random values. And their distribution laws do not depend on time (iteration number
).
- The mean values of errors are equal to zero
.
- The very law of the distribution of random variables may not be known to us, but their variances
and are known
. Note that variances are independent of
, because the laws of distribution are independent of it.
- It is assumed that all random errors are independent of each other: what error will be at a point in time is
completely independent of the error at another point in time
.
It is worth noting that the filtering task is not a smoothing task. We do not strive to smooth the data from the sensor, we strive to get the closest value to the real coordinate
Kalman Algorithm
We will reason by induction. Imagine that at the
therefore, not yet getting the value from the sensor, we can assume that at the step the
Kalman's idea is that in order to get the best approximation to the true coordinate
The coefficient
We must choose the Kalman coefficient
In the general case, to find the exact value of the Kalman coefficient
We use equations (1) (those with a blue background in the frame) to rewrite the expression for the error:
Evidence
Now is the time to discuss what it means to minimize error? After all, an error, as we see, is itself a random variable and takes on different meanings each time. In fact, there is no unambiguous approach to determining what means that the error is minimal. Just as in the case of dispersion of a random variable, when we tried to estimate the characteristic width of its dispersion, here we choose the simplest criterion for calculations. We will minimize the mean value of the squared error:
We will write the last expression:
key to proof
From the fact that all the random variables in the expression for the
independent and the average values of sensor errors and the model are equal to zero
, it follows that all the "cross" terms are equal to zero
.
Plus, the formulas for variances look much simpler:
and
(since
)
Plus, the formulas for variances look much simpler:
This expression takes its minimum value when (we equate the derivative to zero)
Here we already write the expression for the Kalman coefficient with the step index
We substitute the
Our problem is solved. We got an iterative formula to calculate the Kalman coefficient.
All formulas in one place

Example
In the advertising picture at the beginning of the article, data was filtered from a fictitious GPS sensor installed on a fictitious car that travels equally accelerated with a known fictitious acceleration
Look again at the filtering result.

Matlab code
clear all;
N=100 % number of samples
a=0.1 % acceleration
sigmaPsi=1
sigmaEta=50;
k=1:N
x=k
x(1)=0
z(1)=x(1)+normrnd(0,sigmaEta);
for t=1:(N-1)
x(t+1)=x(t)+a*t+normrnd(0,sigmaPsi);
z(t+1)=x(t+1)+normrnd(0,sigmaEta);
end;
%kalman filter
xOpt(1)=z(1);
eOpt(1)=sigmaEta; % eOpt(t) is a square root of the error dispersion (variance). It's not a random variable.
for t=1:(N-1)
eOpt(t+1)=sqrt((sigmaEta^2)*(eOpt(t)^2+sigmaPsi^2)/(sigmaEta^2+eOpt(t)^2+sigmaPsi^2))
K(t+1)=(eOpt(t+1))^2/sigmaEta^2
xOpt(t+1)=(xOpt(t)+a*t)*(1-K(t+1))+K(t+1)*z(t+1)
end;
plot(k,xOpt,k,z,k,x)
Analysis
If we trace how

In the following example, we will discuss how this will help to greatly simplify our life.
Second example
In practice, it often happens that we don’t know anything about the physical model of what we are filtering. For example, you wanted to filter the readings from your favorite accelerometer. You do not know in advance what law you intend to twist the accelerometer. The maximum information you can grab is the variance of the sensor error
But, frankly, such a system no longer completely satisfies the conditions that we imposed on the random variable
But you can go a different, much simpler way. As we saw above, the Kalman coefficient always stabilizes to a value
The following graph shows data from a fictitious sensor filtered in two different ways. Provided that we do not know anything about the physics of the phenomenon. The first way is honest, with all the formulas from Kalman's theory. And the second is simplified, without formulas.

As we can see, the methods are almost no different. A small difference is observed only at the beginning, when the Kalman coefficient has not yet stabilized.
Discussion
As we saw, the main idea of the Kalman filter is to find a coefficient
on average would be the least different from the real coordinate value
Therefore, the Kalman filter is called a linear filter.
It can be proved that of all the linear filters, the Kalman filter is the best. The best in the sense that the average square of the filter error is minimal.
Multidimensional case
The whole Kalman filter theory can be generalized to the multidimensional case. The formulas there look a little worse, but the very idea of their derivation is the same as in the one-dimensional case. In this wonderful article you can see them: http://habrahabr.ru/post/140274/ .
And in this wonderful video, an example of how to use them is analyzed.
Literature
Kalman's original article can be downloaded here: http://www.cs.unc.edu/~welch/kalman/media/pdf/Kalman1960.pdf .
This post can also be read in English http://david.wf/kalmanfilter