This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

# How to Create Kalman Filter in Python – Part V

Contributor:
QuantInsti
Visit: QuantInsti

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

### Estimate uncertainty update

In the Kalman filter tutorial, we saw that the Kalman gain was dependent on the uncertainty in the estimation. Now, as we know that with every successive step, the Kalman filter continuously updates the anticipated value so that we get the estimated value as close to the actual value of a variable, thus, we have to see how this uncertainty in the error can be reduced.

While the derivation of the equation is lengthy, we are only concerned about the equation.

Thus, the estimate uncertainty update equation tells us that the estimate uncertainty of current state varies from the previous estimate uncertainty by the factor of (1 – Kalman gain). We can also call this the covariance update equation.

This brings us to the last equation of the Kalman filter tutorial, which we will see below.

### Estimate uncertainty extrapolation

The reason why the Kalman filter is popular is because it continuously updates its state depending on the anticipated and measured current value. Recall that in the second equation we had extrapolated the state of the estimate. Similarly, the estimate uncertainty of the current error is used to anticipate the uncertainty in the error in the next state.

Ok. That was a simple, no equations way to describe the Kalman filter. If you are confused, let us go through the process and see what we have learned so far.

For input, we have measured value. Initially, we use certain parameters for the Kalman gain as well as the expected value. We will also make a note of the estimate uncertainty.

Now we use the Kalman filter equation to find the next expected value.

In the next iteration, depending on how accurate our expected variable was, we make changes to the uncertainty estimate which in turn would modify our Kalman gain.

Thus, we get a new expected value which will be used as our current estimate in the next phase.

In this way, with each step, we would get closer to anticipating the actual value with a reasonable amount of success.

That is all there is to it. We would reiterate in this Kalman filter tutorial that the reason the Kalman filter is popular is because it only needs the previous value as input and depending on the uncertainty in the measurement, the resulting value is expected. In the real world, the Kalman filter is used by implementing matrix operations as the complexity increases when we take real-world situations. If you are interested in the math part of the Kalman filter, you can go through this resource to find many examples illustrating the individual equations of the Kalman filter.

When it comes to trading, Kalman filter forms an important component in the pairs trading strategy. Let us build a simple pairs trading strategy in Python now.

Stay tuned for the next installment, in which the Rekhit will review pairs trading using Kalman Filter in Python.