The Kalman Filter (draft)
Introduction
This post is dedicated to one of the most understated techniques in science and engineering: the Kalman Filter. This filter is used in numerous technologies, such as GPS, autonomous vehicle control, missile guidance, robotic motion planning, and financial signal processing. In addition to all of the technologies not mentioned, this list will keep growing in the future.
So, what does this filter do?
The Kalman Filter makes an educated guess about the state of a dynamical system given incomplete and/or noisy information.
Background  The Bayes Filter
To begin, let’s introduce the concept the Kalman Filter is based upon: Recursive Bayesian estimation, or the Bayes filter. Simply put, the goal is to estimate an unknown probability density function recursively over time using incomplete or noisy measurements [4].
We can represent our dynamical system as a hidden markov model (HMM).
represents the state of the dynamical system at timestep , and represents the measurements at timestep .
We want to estimate the current state, or a distribution over the current state, given a sequence of measurements up to the present time.
In the equation’s current form, things look a bit uncertain on how to proceed. Given the markov assumption, and the HMM structure of our model, we can break things down further.
The joint distribution can thus be computed as follows:
This is all fairly straight forwards so far given our model. How do we make any sense of all this in order to get our state estimate, given we only observe ?
There are two steps to the bayes filter: Pedict, and Update
Prediction
We can formulate the prediction step as follows:
Looking at this formulation more closely, there is a clear pattern. The second term in the integral is simply the previous timestep’s predict step! This is where we can finally implement the recursive structure of the filter, which greatly simplifies things.
Going back one step:
Going back all the way:
Updating
So, now that we have the prediction, we want to update given our current measurement . We’ll simply use Baye’s rule here.
The denominator can be rewritten as follows:
This looks very similar to the numerator. In fact, it is simply the integral of the numerator over the current step! This is just a normalizing term, and is constant relative to .
This term can simply be ignored in application of the filter.
Summary
We now have the two key components of a Bayesian filter:
Predict
Update
The Kalman Filter
The Kalman Filter is one of the most basic implementation of a Bayesian Filters: the distributions of the variables will be continuous and Gaussian distributed. In this case, we simply need to keep track of the mean and covariance of the state we are tracking.
Notation
 , the state transition model
 , the observation model

, the control input model (when needed)
 , the Kalman gain (more on this later)
Since we are assuming the probabilities tracked are Gaussian, we simply need to track means and covariances:
 , the state

, the covariance of the state transition noise
 , the measurement
 , the covariance of the observation noise
Models
Our state transition model is written as follows:
Where represents our state transition noise. Our measurement model is written as follows:
Where represents our state transition noise.
Given these conditions, we want to tract the following quantities:
 , the state estimate
 , the state estimate error covariance
From these models, we can relate things back to the Bayesian filter:
Given this model, we can derive the predict and update steps. The predict step is given. The update step, however, has a crucial component that must be derived: the Kalman gain .
Predict
$$ \mathbf{\hat{x}}_{k \mid k1} = \mathbf{F}_k\mathbf{\hat{x}}_{k1 \mid k1} + \mathbf{B}_k\mathbf{u}_k $$  Predicted state estimate 
$$ \mathbf{P}_{k \mid k1} = \mathbf{F}_k\mathbf{P}_{k1 \mid k1}\mathbf{F}_k^\mathrm{T} + \mathbf{Q}_k $$  Predicted estimate covariance 
Update
$$ \tilde{\mathbf{y}}_k = \mathbf{z}_k  \mathbf{H}_k\hat{\mathbf{x}}_{k\mid k1} $$  prefit residual 
$$ \mathbf{S}_k = \mathbf{R}_k + \mathbf{H}_k \mathbf{P}_{k\mid k1} \mathbf{H}_k^\mathrm{T} $$  prefit residual covariance 
$$ \mathbf{K}_k = \mathbf{P}_{k\mid k1}\mathbf{H}_k^\mathrm{T} \mathbf{S}_k^{1} $$  Optimal Kalman gain 
$$ \hat{\mathbf{x}}_{k\mid k} = \hat{\mathbf{x}}_{k\mid k1} + \mathbf{K}_k\tilde{\mathbf{y}}_k $$  Updated state estimate 
$$ \mathbf{P}_{kk} = (\mathbf{I}  \mathbf{K}_k \mathbf{H}_k )\mathbf{P}_{kk1}(\mathbf{I}  \mathbf{K}_k \mathbf{H}_k )^{\mathrm{T}}+\mathbf{K}_k\mathbf{R}_k\mathbf{K}^{\mathrm{T}}_k $$  Updated estimate covariance 
The full derivation is beyond the scope of this post, although those interested in learning more are recommended references [1], [5], and [6]. In summary, the Kalman filter minimizes the square error between the true state and estimated state,
There are many different formulations of the Kalman filter, and we have covered the most basic formulation.
Experiments
To illustrate the concept of the Kalman Filter, we will construct a toy example: Given a particle’s 1 dimentional position over time, we will try to estimate its velocity.
Notation
 : position
 : velocity
 : timestep
Given these quantities, we can now formulate our problem. Kalman filters are not plug and play like many other signal processing and machine learning techniques; They need to be tailored and fine tuned to the problem at hand.
(measurement)
(state)
(measurement process)
(state transition)
Now that we have formulated our problem, we generate some synthetic measurement data.
We have chosen our true position to be modeled by the sigmoid [7] function ( $ \mathbf{f}() $ ) over time.
)
Given this data, we can compute the velocity as . However, the corrupting noise causes some very serious issues that completely destroy our estimate. The corrupting influence of the noise multiplies when trying to estimate velocity in the graph below.
This is why we use a Kalman filter. The model calculation is severely corrupted by the noise.
In order to implement our Kalman filter, we need to estimate two quantities a priori: the observation covariance $\mathbf{R}$ and the state transition covariance $\mathbf{Q}$.
Given we generated the noise, the observation covariance is already known. While it may seem like there is no state transition covariance in this model, this couldn’t be further from the truth; there is a hidden control input, acceleration.
Where $ \mathbf{G} $ is the effect of acceleration on our state. Written explicitly,
Thus, $ \mathbf{Q} $ can be estimated as
is another free parameter that can be estimated by the expected variance of the acceleration. For the sake of argument, the variance is calculated from the original acceleration data as $ Var[\frac{\Delta^2{x}}{\Delta{t}^2}] $. The graphs below show the application of a Kalman filter to estimate velocity in our problem stated. A moving average on the direct calculation of velocity is shown as a comparison.
It is clear that the Kalman filter performs much better than a moving average at filtering out the noise. The spike at the beginning of the velocity estimation is due to the beginning period where the filter has seen few samples, is more susceptible to the corrupting influence of noise. The delay of the filter is inevitable, since a number of samples need to be read to register a change. A more responsive filter would be more noisy. One way to mitigate this effect is to increase the sampling rate relative to the rate of change.
Note from the Author
I hope you learned just as much reading this post as I did creating it. Questions? Comments? My contact information is available here.
Citation
Please cite this post using the BibTex entry below.
@misc{
author = {Liapis, Yannis},
title = {The Kalman Filter (draft)},
journal = {},
type = {Blog},
number = {Nov 10},
year = {2018},
howpublished = {\url{http://yliapis.github.io}}
This work is licensed under a Creative Commons Attribution 4.0 International License.
References
[1] R. Faragher, “Understanding the Basis of the Kalman Filter Via a Simple and Intuitive Derivation”, IEEE Signal Processing Magazine, September 2012, pp 132
[2] R. E. Kalman, “A New Approach to Linear Filtering and Prediction Problems”, Transactions of the ASME–Journal of Basic Engineering, 1960, 82 (Series D): 3545
[3] T. Babb, “How a Kalman filter works, in pictures”, Bzarg, 2015
[4] “Recursive Bayesian estimation”, Wikipedia
[5] N. Shimkin, “Derivations of the DiscreteTime Kalman Filter”, Israel Institute of Technology, 2009
[6] V. Yadav, “Kalman filter: Intuition and discrete case derivation”, Towards Data Science, 2017
[7] “Sigmoid function”, Wikipedia