[Python In Depth]1D Kalman Filters with in Python

A Ydobon
3 min readOct 7, 2019

--

Photo by Rock’n Roll Monkey on Unsplash

1D Kalman Filters with Gaussians in Python

Further readings about Kalman Filters, such as its definition, and my experience and thoughts over it, are provided below. So, in case you are interested in reading it, scroll down and down. Here, we will start talking about its implementation with Python first.

Let’s start first, building and learning Kalman Filters in Python.

We will import basic dependencies.

1. Import libraries

from math import *
import matplotlib.pyplot as plt
import numpy as np

First of all, we will crawl all the functions in ‘math’ library. And ‘matplotlib.pyplot’ has been aliased with a shortened name ‘plt’, and this library helps to draw pretty graphs. Lastly, ‘numpy’ is for matrix calculations.

2. Gaussian distribution (Normal distribution)

def f(mu, sigma2, x): 
coef = 1/sqrt(2.0*pi*sigma2)
expn = exp(-0.5*(x-mu)**2/sigma2)
return coef * expn

Above is a simple implementation of the Gaussian normal distribution formula, which is far more familiar with the mathematical formula below.

‘pi’ and ‘exp’ are from the ‘math’ library that we imported just before.

3. Information update

def update(mean1, var1, mean2, var2): 
new_mean = (mean1*var2 + mean2*var1)/(var1 + var2)
new_var = 1/(1/var1 + 1/var2)
return [new_mean, new_var]

With the existing Gaussian distribution in hand, we get additional information about the distribution, with its new mean, and new variance. The above code is to update the current normal distribution with the newly measured information of another normal distribution. It is like a multiplication between two Gaussian distributions.

4. Prediction

def predict(mean1, var1, mean2, var2): 
new_mean = mean1 + mean2 new_var = var1 + var2
return [new_mean, new_var]

And now, we predict the next position of our Gaussian distribution. It is very simple, unlike the information update part, just adding the newly coming mean and variance would work. So, It is a simple addition.

Credits to Udacity Computer Vision Nanodegree

The first time that I have ever heard of the words “Kalman Filter” which sounds so fancy and involved, was when I was a member of a data analysis project dealing with a sequential sensor dataset. After that, I also found out that Kalman Filter is widely deployed across different areas, not only sensor processing engineering, robotics, but also in social science such as Economics too.

What is a Kalman Filter?

According to Wikipedia, Kalman filter is

In statistics and control theory, Kalman filtering, also known as linear quadratic estimation (LQE), is an algorithm that uses a series of measurements observed over time, containing statistical noise and other inaccuracies, and produces estimates of unknown variables that tend to be more accurate than those based on a single measurement alone, by estimating a joint probability distribution over the variables for each timeframe. The filter is named after Rudolf E. Kálmán, one of the primary developers of its theory.

Thank you for your time, and hope you enjoyed this posting!

--

--

A Ydobon
A Ydobon

No responses yet