More Campus Resources

Useful Tools and Information

Language

Multilingual content from IBKR

# How to Create Kalman Filter in Python – Part III

###### Posted February 2, 2021 at 10:44 am
Rekhit Pachanekar
QuantInsti

See Part I and Part II of this series to get started with the Statistical terms and concepts used in Kalman Filter.

### State extrapolation equation

The state extrapolation system helps us to find the relation between the current state and the next state i.e. anticipate the next state of the system.

Until now, we understood that the Kalman filter is recursive in nature and uses the previous values to anticipate the next value in a system. While we can easily give the formula and be done with it, we want to understand exactly why it is used. In that respect, we will take another example to illustrate the state extrapolation equation.

Now, let’s take the example of a company trying to develop a robotic bike. If you think about it, when someone is riding a bike, they have to balance the bike, control the accelerator, turn etc.

Let’s say that we have a straight road and we have to control the bike’s velocity. For this, we would have to know the bike’s position. As a simple case, we measure the wheels’ rotation to anticipate how much the bike has moved. We remember that the distance travelled by an object is equal to the velocity of the object multiplied by the time travelled.

Now, let’s suppose we measure the rotation at a certain instant of time, ie Δt.

If we say that the bike has a constant velocity v, then we can say the following:

The anticipated position of the bike is equal to the current estimated position of the bike + the distance covered by the bike in time Δt.

Here the distance covered by the bike will be the result of Δt multiplied by the velocity of the bike.

Suppose that the velocity is kept constant at 2 m/s. And the time Δt is 5 seconds. That means the bike moves 10 metres between every successive measurement.

But what if we check the next time and find out the bike moved 12 metres. This gives us an error of 2 metres. This could mean two things,

1. The device used to measure the velocity has error (measurement error)
2. The bike is moving with different velocities, in this instance maybe it is a downhill slope (process error)

We try to find out how to minimise this error by having different gains to apply to the state update equation.

Now, we will introduce a new concept to the Kalman filter tutorial, ie the α – β filter.

Now, if we recall the status update equation, it was given as,

Current state estimated value

= Anticipated value of current state + Kalman Gain * ( measured value – anticipated value of the state)

We will say that α is used to reduce the error in the measurement, and thus it will be used to anticipate the value of the position of the object.

Now if we keep the α in place of the Kalman gain, you can deduce that a high value of α gives more importance to the measured value and a low level of α gives less weightage to the measured value. In this way, we can reduce the error while anticipating the position.

Now, if we assume that the bike is moving with different velocities, we would have to use another equation to compute the velocity and which in turn would lead to a better expectation as to the position of the bike. Here we use β in place of Kalman gain to estimate the velocity of the bike.

We tried to see the relation of how α and β impact the expected value. But how do we know for sure the correct value of α and β in order to get the expected value closer to the actual value.

Stay tuned for the next installment, in which the Rekhit will explain the Kalman Gain equation.