Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Kalman Filter Math
** author's note:** the ordering of material in this chapter is questionable. I delve into solving ODEs before discussing the basic Kalman equations. If you are reading this while it is being worked on (so long as this notice exists), you may find it easier to skip around a bit until I organize it better.
If you've gotten this far I hope that you are thinking that the Kalman filter's fearsome reputation is somewhat undeserved. Sure, I hand waved some equations away, but I hope implementation has been fairly straightforward for you. The underlying concept is quite straightforward - take two measurements, or a measurement and a prediction, and choose the output to be somewhere between the two. If you believe the measurement more your guess will be closer to the measurement, and if you believe the prediction is more accurate your guess will lie closer it it. That's not rocket science (little joke - it is exactly this math that got Apollo to the moon and back!).
Well, to be honest I have been choosing my problems carefully. For any arbitrary problem finding some of the matrices that we need to feed into the Kalman filter equations can be quite difficult. I haven't been too tricky, though. Equations like Newton's equations of motion can be trivially computed for Kalman filter applications, and they make up the bulk of the kind of problems that we want to solve. If you are a hobbyist, you can safely pass by this chapter for now, and perhaps forever. Some of the later chapters will assume the material in this chapter, but much of the work will still be accessible to you.
But, I urge everyone to at least read the first section, and to skim the rest. It is not much harder than what you have done - the difficulty comes in finding closed form expressions for specific problems, not understanding the math in this chapter.
Bayesian Probability
The title of this book is Kalman and Bayesian Filters in Python but to date I have not touched on the Bayesian aspect much. There was enough going on in the earlier chapters that adding this form of reasoning about filters could be a distraction rather than a help. I now wish to take some time to explain what Bayesian probability is and how a Kalman filter is in fact a Bayesian filter. This is not a diversion. First of all, a lot of the Kalman filter literature uses this formulation when talking about filters, so you will need to understand what they are talking about. Second, this math plays a strong role in filtering design once we move past the Kalman filter.
To do so we will go back to our first tracking problem - tracking a dog in a hallway. Recall the update step - we believed with some degree of precision that the dog was at position 7 (for example), and then receive a measurement that the dog is at position 7.5. We want to incorporate that measurement into our belief. In the Discrete Bayes chapter we used histograms to denote our estimates at each hallway position, and in the One Dimensional Kalman Filters we used Gaussians. Both are method of using Bayesian probability.
Briefly, Bayesian probability is a branch of math that lets us evaluate a hypothesis or new data point given some uncertain information about the past. For example, suppose you are driving down your neighborhood street and see one of your neighbors at their door, using a key to let themselves in. Three doors down you see two people in masks breaking a window of another house. What might you conclude?
It is likely that you would reason that in the first case your neighbors were getting home and unlocking their door to get inside. In the second case you at least strongly suspect a robbery is in progress. In the first case you would proceed on, and in the second case you'd probably call the police.
Of course, things are not always what they appear. Perhaps unbeknownst to you your neighbor sold their house that morning, and they were now breaking in to steal the new owner's belongings. In the second case, perhaps the owners of the house were at a costume event at the next house, they had a medical emergency with their child, realized they lost their keys, and were breaking into their own house to get the much needed medication. Those are both unlikely events, but possible. Adding a few additional pieces of information would allow you to determine the true state of affairs in all but the most complicated situations.
These are instances of Bayesian reasoning. We take knowledge from the past and integrate in new information. You know that your neighbor owned their house yesterday, so it is still highly likely that they still own it today. You know that owners of houses normally have keys to the front door, and that the normal mode of entrance into your own house is not breaking windows, so the second case is likely to be a breaking and entering. The reasoning is not ironclad as shown by the alternative explanations, but it is likely.
Bayes' theorem
Bayes' theorem mathematically formalizes the above reasoning. It is written as
Before we do some computations, let's review what the terms mean. P(A) is called the prior probability of the event A, and is often shortened to the prior. What is the prior? It is the probability of A being true before we incorporate new evidence. In our dog tracking problem above, the prior is the probability we assign to our belief that the dog is positioned at 7 before we make the measurement of 7.5. It is important to master this terminology if you expect to read a lot of the literature.
is the conditional probability that A is true given that B is true. For example, if it is true that your neighbor still owns their house, then it will be very likely that they are not breaking into their house. In Bayesian probability this is called the posterior, and it denotes our new belief after incorporating the measurement/knowledge of B. For our dog tracking problem the posterior is the probability given to the estimated position after incorporating the measurement 7.5. For the neighbor problem the posterior would be the probability of a break in after you find out that your neighbor sold their home last week.
What math did we use for the dog tracking problem? Recall that we used this equation to compute the new mean and probability distribution
Here is the old estimated position, so is an indication of our prior probability. is the mean and variance of our measurement, and so the result can be thought of as the new position and probability distribution after incorporating the new measurement. In other words, our posterior distribution is
This is still a little hard to compare to Bayes' equation because we are dealing with probability distributions rather than probabilities. So let's cast our minds back to the discrete Bayes chapter where we computed the probability that our dog was at any given position in the hallway. It looked like this:
Let's rewrite this using our newly learned terminology.
So what is this doing? It's multiplying the old belief that the dog is at position i (prior probability) with the probability that the measurement is correct for that position, and then dividing by the total probability for that new event.
Now let's look at Bayes' equation again.
It's the same thing being calculated by the code. Multiply the prior () by the probability of the measurement at each position () and divide by the total probability for the event ().
In other words the first half of the Discrete Bayes chapter developed Bayes' equation from a thought experiment. I could have presented Bayes' equation and then given you the Python routine above to implement it, but chances are you would not have understood why Bayes' equation works. Presenting the equation first is the normal approach of Kalman filtering texts, and I always found it extremely nonintuitive.
Computing Means and Covariances
You will not need to do this by hand for the rest of the book, but it is important to understand the computations behind these values. It only takes a few minutes to learn.
Computing the Mean
So let's start with 3 variables: . Let's say we have two values for each: , , and . Another, equivalent way of writing this is that you will sometimes see is
You will not see that often in the Kalman filter literature, so I will adopt the alternative notation of using a subscript to indicate the value for
We compute the mean as for the univariate case - sum the values and divide by the number of values. Formally
Therefore we can write:
Which I can express in our shorthand matrix notation as
Expected Value
I could just give you the formula for the covariance but it will make more sense if you see how it is derived. To do that we first need to talk about expected value of a random variable. The expected value is the value we expect, on average, for the variable.
The expected value of a random variable is the average value it would have if we took an infinite number of samples of it and then averaged those samples together. Let's say we have and each value is equally probable. What would we expect to have, on average?
It would be the average of 1, 3, and 5, of course, which is 3. That should make sense; we would expect equal numbers of 1, 3, and 5 to occur, so is clearly the average of that infinite series of samples.
Now suppose that each value has a different probability of happening. Say 1 has an 80% chance of occurring, 3 has an 15% chance, and 5 has only a 5% chance. In this case we compute the expected value by multiplying each value of by the percent chance of it occurring, and summing the result. So for this case we could compute
Here I have introduced the standard notation for the expected value of .
We can formalize this by letting be the value of , and be the probability of its occurrence. This gives us
The value 1.5 for makes intuitive sense because x is far more like to be 1 than 3 or 5, and 3 is more likely than 5 as well.
It is probably worth me pointing out now that if is continuous we substitute the sum for an integral, like so
where is the probability distribution function of . We won't be using this equation yet, but we will be using it in the next chapter.
Computing the Covariance
Now we are prepared to compute the covariance matrix. The covariance measures how much two random variables move in the same direction, and is defined as
If we compare this to the formula for the variance of a single variable we can see where this definition came from.
And indeed, the covariance of a variable with itself is the variance of the variable, which we can trivially prove with
Let's build some familiarity with this by calculating the covariance for the from the beginning of this section.
We already know the mean is
Recall that we named the three variables as . Let's start by computing the variance of .
Using similar math we can compute that and . This allows us to fill in the covariance matrix with
Now we can compute the covariance of a and b.
We can fill in the row and column in the covariance matrix with this value:
We already talked about the symmetry of the covariance matrix; . We see now why this is true from the equation for the covariance.
This gives the covariance matrix
The arithmetic is a bit tedious, so let's use NumPy's cov()
function to compute the entire covariance matrix. To compute the covariance in accordance with the equations above you will need to set the parameter bias=1
. The meaning of that parameter is not important to this book. If you are interested, wikipedia has a good article on it here http://en.wikipedia.org/wiki/Bias_of_an_estimator.
We said above that "the covariance measures how much two random variables move in the same direction", so let's see what that means in practice. If we start with the value we can create subsequent values that vary in the same direction with , , and .
The values for are perfectly correlated, and the result is a covariance ellipse with zero width. We can see this by making the values not slightly uncorrelated.
Now lets make the second value go away from the first.
As we would hope, the covariance matrix is tilted in the opposite direction, indicating that the variables are inversely correlated. That is, as one gets larger, the other becomes smaller.
Modeling a Dynamic System that Has Noise
We need to start by understanding the underlying equations and assumptions that the Kalman filter uses. We are trying to model real world phenomena, so what do we have to consider?
First, each physical system has a process. For example, a car traveling at a certain velocity goes so far in a fixed amount of time, and its velocity varies as a function of its acceleration. We describe that behavior with the well known Newtonian equations we learned in high school.
And once we learned calculus we saw them in this form:
A typical problem would have you compute the distance traveled given a constant velocity or acceleration. But, of course we know this is not all that is happening. First, we do not have perfect measures of things like the velocity and acceleration - there is always noise in the measurements, and we have to model that. Second, no car travels on a perfect road. There are bumps that cause the car to slow down, there is wind drag, there are hills that raise and lower the speed. If we do not have explicit knowledge of these factors we lump them all together under the term "process noise".
Trying to model all of those factors explicitly and exactly is impossible for anything but the most trivial problem. I could try to include equations for things like bumps in the road, the behavior of the car's suspension system, even the effects of hitting bugs with the windshield, but the job would never be done - there would always be more effects to add and limits to our knowledge (how many bugs do we hit in an hour, for example). What is worse, each of those models would in themselves be a simplification - do I assume the wind is constant, that the drag of the car is the same for all angles of the wind, that the suspension act as perfect springs, that the suspension for each wheel acts identically, and so on.
So control theory makes a mathematically correct simplification. We acknowledge that there are many factors that influence the system that we either do not know or that we don't want to have to model. At any time we say that the actual value (say, the position of our car) is the predicted value plus some unknown process noise:
This is not meant to imply that is a function that we can derive analytically or that it is well behaved. If there is a bump in the road at then the noise factor will incorporate that effect. Again, this is not implying that we model, compute, or even know the value of noise(t), it is merely a statement of fact - we can always describe the actual value as the predicted value from our idealized model plus some other value.
Let's express this with linear algebra. Using the same notation from previous chapters, we can say that our model of the system (without noise) is:
That is, we have a set of linear equations that describe our system. For our car, will be the coefficients for Newton's equations of motion.
Now we need to model the noise. We will call that w, and add it to the equation.
Finally, we need to consider inputs into the system. We are dealing with linear problems here, so we will assume that there is some input into the system, and that we have some linear model that defines how that input changes the system. For example, if you press down on the accelerator in your car the car will accelerate. We will need a matrix to convert into the effect on the system. We add that into our equation:
And that's it. That is one of the equations that Kalman set out to solve, and he found a way to compute an optimal solution if we assume certain properties of .
However, we took advantage of something I left mostly unstated in the last chapter. We were able to provide a definition for because we were able to take advantage of the exact solution that Newtonian equations provide us. However, if you have an engineering background you will realize what a small class of problems that covers. If you don't, I will explain it next, and provide you with several ways to compute for arbitrary systems.
Converting the Multivariate Equations to the Univariate Case
As it turns out the Kalman filter equations are quite easy to deal with in one dimension, so let's do the mathematical proof.
Note: This section will provide you with a strong intuition into what the Kalman filter equations are actually doing. While this section is not strictly required, I recommend reading this section carefully as it should make the rest of the material easier to understand. It is not merely a proof of correctness that you would normally want to skip past! The equations look complicated, but they are actually doing something quite simple.
Let's start with the predict step, which is slightly easier. Here are the multivariate equations.
The state only has one variable, so it is a matrix. Our motion is also be a matrix. Therefore, and must also be matrices. That means that they are all scalars, and we can write
Here the variables are not bold, denoting that they are not matrices or vectors.
Our state transition is simple - the next state is the same as this state, so . The same holds for the motion transition, so, . Thus we have
which is equivalent to the Gaussian equation from the last chapter
Hopefully the general process is clear, so now I will go a bit faster on the rest. Our other equation for the predict step is
Again, since our state only has one variable and must also be matrix, which we can treat as scalars, yielding
We already know . The transpose of a scalar is the scalar, so . This yields
which is equivalent to the Gaussian equation of
This proves that the multivariate equations are performing the same math as the univariate equations for the case of the dimension being 1.
Here our our multivariate Kalman filter equations for the update step.
As above, all of the matrices become scalars. defines how we convert from a position to a measurement. Both are positions, so there is no conversion, and thus . Let's substitute in our known values and convert to scalar in one step. One final thing you need to know - division is scalar's analogous operation for matrix inversion, so we will convert the matrix inversion to division.
Before we continue with the proof, I want you to look at those equations to recognize what a simple concept these equations implement. The residual is nothing more than the measurement minus the previous state. The gain is scaled based on how certain we are about the last prediction vs how certain we are about the measurement. We choose a new state based on the old value of plus the scaled value of the residual. Finally, we update the uncertainty based on how certain we are about the measurement. Algorithmically this should sound exactly like what we did in the last chapter.
So let's finish off the algebra to prove this. It's straightforward, and not at all necessary for you to learn unless you are interested. Feel free to skim ahead to the last paragraph in this section if you prefer skipping the algebra.
Recall that the univariate equations for the update step are:
Here we will say that is the state , and is the measurement . That is entirely arbitrary, we could have chosen the opposite assignment. Thus it follows that that is the state uncertainty , and is the measurement noise . Let's substitute those in.
I will handle first. The corresponding equation in the multivariate case is
Now let's look at . The corresponding equation in the multivariate case is
So we have proven that the multivariate equations are equivalent to the univariate equations when we only have one state variable. I'll close this section by recognizing one quibble - I hand waved my assertion that and . In general we know this is not true. For example, a digital thermometer may provide measurement in volts, and we need to convert that to temperature, and we use to do that conversion. I left that issue out to keep the explanation as simple and streamlined as possible. It is very straightforward to add that generalization to the equations above, redo the algebra, and still have the same results.
Converting Kalman Filter to a g-h Filter
I've stated that the Kalman filter is a form of the g-h filter. It just takes some algebra to prove it. It's more straightforward to do with the one dimensional case, so I will do that. Recall
which I will make more friendly for our eyes as:
We can easily put this into the g-h form with the following algebra
We are almost done, but recall that the variance of estimate is given by
We can incorporate that term into our equation above by observing that
We can tie all of this together with
where
The end result is multiplying the residual of the two measurements by a constant and adding to our previous value, which is the g equation for the g-h filter. g is the variance of the new estimate divided by the variance of the measurement. Of course in this case g is not a constant as it varies with each time step as the variance changes. We can also derive the formula for h in the same way but I don't find this a particularly interesting derivation. The end result is
The takeaway point is that g and h are specified fully by the variance and covariances of the measurement and predictions at time n. In other words, we are picking a point between the measurement and prediction by a scale factor determined by the quality of each of those two inputs. That is all the Kalman filter is.
Compare Performance to Univariate Kalman Filter
Let's implement a simple 1-D kalman filter using the Kalman filter from this chapter, and compare its output to the kalman filter from the previous chapter by plotting it. We will use a simple model of tracking an object that starts at x=0 and moves by 1 at each step. We will assume the arbitrary value 5 for the measurement noise and .02 for the process noise.
First, let's implement the filter from the Univariate Filter chapter.
Now, let's implement the same thing using the Kalman filter. I will implement it as a function that returns a KalmanFilter object so that you can run the analysis code several times with the KalmanFilter initialized to the same starting conditions each time.
Finally, let's compare the two. I will plot the data from the 1-D Kalman filter as a blue line, and the output of the filter from this chapter as red dots. I wrote it as a function so you can easily modify the parameters and regenerate the plots.
As you can see, both filters produce the same results. Feel free to vary the initial guess, the measurement noise, and the process noise; so long as you make the same changes to both filters the output should be the same. This is a solid demonstration, albeit not a rigorous proof, that both filters in fact implement the same math for the 1-D case.
Modeling Dynamic Systems
Modeling dynamic systems is properly the topic of at least one undergraduate course in mathematics. To an extent there is no substitute for a few semesters of ordinary and partial differential equations. If you are a hobbyist, or trying to solve one very specific filtering problem at work you probably do not have the time and/or inclination to devote a year or more to that education.
However, I can present enough of the theory to allow us to create the system equations for many different Kalman filters, and give you enough background to at least follow the mathematics in the literature. My goal is to get you to the stage where you can read a Kalman filtering book or paper and understand it well enough to implement the algorithms. The background math is deep, but we end up using a few simple techniques over and over again in practice.
I struggle a bit with the proper way to present this material. If you have not encountered this math before I fear reading this section will not be very profitable for you. In the Extended Kalman Filter chapter I take a more ad-hoc way of presenting this information where I expose a problem that the KF needs to solve, then provide the math without a lot of supporting theory. This gives you the motivation behind the mathematics at the cost of not knowing why the math I give you is correct. On the other hand, the following section gives you the math, but somewhat divorced from the specifics of the problem we are trying to solve. Only you know what kind of learner your are. If you like the presentation of the book so far (practical first, then the math) you may want to wait until you read the Extended Kalman Filter before In particular, if your intent is to work with Extended Kalman filters (a very prelevant form of nonlinear Kalman filtering) you will need to understand this math at least at the level I present it. If that is not your intent this section may still prove to be beneficial if you need to simulate a nonlinear system in order to test your filter.
Let's lay out the problem and discuss what the solution will be. We model dynamic systems with a set of first order differential equations. This should not be a surprise as calculus is the math of things that vary. For example, we say that velocity is the derivative of distance with respect to time
where is the notation for the derivative of with respect to t.
We need to use these equations for the predict step of the Kalman filter. Given the state of the system at time we want to predict its state at time . The Kalman filter matrices do not accept differential equations, so we need a mathematical technique that will find the solution to those equations at each time step. In general it is extremely difficult to find analytic solutions to systems of differential equations, so we will normally use numerical techniques to find accurate approximations for these equations.
Why This is Hard
We model dynamic systems with a set of first order differential equations. For example, we already presented the Newtonian equation
where is the notation for the derivative of with respect to t, or .
In general terms we can then say that a dynamic system consists of equations of the form
if the behavior of the system depends on time. However, if the system is time invariant the equations are of the form
What does time invariant mean? Consider a home stereo. If you input a signal into it at time , it will output some signal . If you instead make the input at a later time the output signal will still be exactly the same except shifted in time. This is different from, say, an aircraft. If you make a control input to the aircraft at a later time it's behavior will be different because it will have burned additional fuel (and thus lost weight), drag may be different due to being at a different altitude, and so on.
We can solve these equations by integrating each side. The time variant equation is very straightforward. We essentially solved this problem with the Newtonian equations above, but let's be explicit and write it out. Starting with we get the expected
However, integrating the time invariant equation is not so straightforward.
Using the separation of variables techniques, we divide by and move the term to the right so we can integrate each side:
$$\int^x_{x_0} \frac{1}{f(x)} dx = \int^t_{t_0} dt\\$$If we let the solution to the left hand side by named , we get
We then solve for x with
In other words, we need to find the inverse of . This is not at all trivial, and a significant amount of course work in a STEM education is devoted to finding tricky, analytic solutions to this problem, backed by several centuries of research.
In the end, however, they are tricks, and many simple forms of either have no closed form solution, or pose extreme difficulties. Instead, the practicing engineer turns to numerical methods to find a solution to her problems. I would suggest that students would be better served by learning fewer analytic mathematical tricks and instead focusing on learning numerical methods, but that is the topic for another book.
Finding the Fundamental Matrix for Time Invariant Systems
If you already have the mathematical training in solving partial differential equations you may be able to put it to use; I am not assuming that sort of background. So let me skip over quite a bit of mathematics and present the typical numerical techniques used in Kalman filter design.
First, we express the system equations in state-space form (i.e. using linear algebra equations) with
Now we can assert that we want to find the fundamental matrix that propagates the state with the equation
which we can equivalently write as
In other words, we want to compute the value of at time by multiplying its previous value by some matrix . This is not trivial to do because the original equations do not include time.
Taylor Series Expansion
Broadly speaking there are three ways to find that are used in Kalman filters. The technique most often used with Kalman filters is to use a Taylor-series expansion. The Taylor series represents a function as an infinite sum of terms. The terms are linear, even for a nonlinear function, so we can express any arbitrary nonlinear function using linear algebra. The cost of this choice is that unless we use an infinite number of terms (and who has time for that these days) the value we compute will be approximate rather than exact.
For the Kalman filter we will be using a form of the series that uses a matrix. But before we do that, let's work through a couple of examples with real functions since real functions are easier to plot and reason about. The Taylor series for either are nearly identical, so this is a good first step. For a real (or complex) function the Taylor series of a function evaluated at is defined as
This is easy to compute, and thus is the typical approach used in Kalman filter design when the filter is reasonably small. If you are wondering where came from, I point you to the Wikipedia article on the matrix exponential [1]. Here the important point is to recognize the very simple and regular form this equation takes.
Before applying it to a Kalman filter, lets do the Taylor expansion of a math function since this is much easier to visualize than a matrix. I choose sin(x). The Taylor series for a real or complex function f(x) about x=a is
where is the nth derivative of f. To compute the Taylor series for at Let's first work out the terms for f.
Now we can substitute these values into the equation.
And let's test this with some code:
This is not bad for only three terms. If you are curious, go ahead and implement this as a Python function to compute the series for an arbitrary number of terms. But I will forge ahead to the matrix form of the equation.
Let's consider tracking an object moving in a vacuum. In one dimesion the differential equation for motion with zero acceleration is
which we can put in state space matrix form as
This is a first order differential equation, so we can set and solve the following equation.
If you perform the multiplication you will find that , which means that all higher powers of are also . This makes the computation very easy.
We plug this into to get
This should look very familiar to you! This is the equation we used in the Multivariate Kalman Filter chapter to track a moving object.
We derived this equation in that chapter by using techniques that are much easier to understand. The advantage of the Taylor series expansion is that we can use it for any arbitrary set of differential equations which are time invariant.
Time invariant? This means equations that do not depend on time. For example, is not time invariant; the value will be different at different times due to the multiplication by t. On the other hand is time invariant. is dependent on time, but that is allowed. This is because to be time invariant it must be true that if then . This will be true for the second equation, but not the first.
However, we often use a Taylor expansion even when the equations are not time invariant. The answer will still be reasonably accurate so long as the time step is short and the system is nearly constant over that time step.
Linear Time Invariant Theory
Linear Time Invariant Theory, also known as LTI System Theory, gives us a way to find using the inverse Laplace transform. You are either nodding your head now, or completely lost. Don't worry, I will not be using the Laplace transform in this book except in this paragraph, as the computation is quite difficult to perform in practice. LTI system theory tells us that
I have no intention of going into this other than to say that the inverse Laplace transform converts a signal into the frequency (time) domain, but finding a solution to the equation above is non-trivial. If you are interested, the Wikipedia article on LTI system theory provides an introduction [2].
Numerical Solutions
Finally, there are numerical techniques to find . As filters get larger finding analytical solutions becomes very tedious (though packages like SymPy make it easier). C. F. van Loan [3] has developed a technique that finds both and numerically.
I have implemented van Loan's method in FilterPy
. You may use it as follows:
See the docstring documentation for van_loan_discretization for more information, which I have embedded below.
Forming First Order Equations from Higher Order Equations
In the sections above I spoke of first order differential equations; these are equations with only first derivatives. However, physical systems often require second or higher order equations. Any higher order system of equations can be converted to a first order set of equations by defining extra variables for the first order terms and then solving. Let's do an example.
Given the system find the first order equations.
The first step is to isolate the highest order term onto one side of the equation .
We define two new variables:
Now we will substitute these into the original equation and solve, giving us a set of first order equations in terms of these new variables.
First, we know that and that . Therefore
Therefore our first order system of equations is
If you practice this a bit you will become adept at it. Isolate the highest term, define a new variable and its derivatives, and then substitute.
ORPHAN TEXT
I'll address the computation of in the next paragraph. For now you can see that as time passes our uncertainty in velocity is growing slowly. It is a bit harder to see, but if you compare this graph to the previous one the uncertainty in position also increased.
We have not given the math for computing the elements of yet, but if you suspect the math is sometimes difficult you would be correct. One of the problems is that we are usually modeling a continuous system - the behavior of the system is changing at every instant, but the Kalman filter is discrete. That means that we have to somehow convert the continuous noise of the system into a discrete value, which usually involves calculus. There are other difficulties I will not mention now.
However, for the class of problems we are solving in this chapter (discretized continuous-time kinematic filters) we can directly compute the state equations for moving objects by using Newton's equations.
For these kinds of problems we can rely on precomputed forms for . We will learn how to derive these matrices in the next chapter. For now I present them without proof. If we assume that for each time period the acceleration due to process noise is constant and uncorrelated, we get the following.
For constant velocity the form is
and for constant acceleration we have
It general it is not true that acceleration will be constant and uncorrelated, but this is still a useful approximation for moderate time period, and will suffice for this chapter. Fortunately you can get a long way with approximations and simulation. Let's think about what these matrices are implying. We are trying to model the effects of process noise, such as the wind buffeting the flight of a thrown ball. Variations in wind will cause changes in acceleration, and so the effect on the acceleration is large. However, the effects on velocity and position are proportionally smaller. In the matrices, the acceleration term is in the lower right, and this is the largest value. A good rule of thumb is to set somewhere from to , where is the maximum amount that the acceleration will change between sample periods. In practice we pick a number, run simulations on data, and choose a value that works well.
The filtered result will not be optimal, but in my opinion the promise of optimal results from Kalman filters is mostly wishful thinking. Consider, for example, tracking a car. In that problem the process noise would include things like potholes, wind gusts, changing drag due to turning, rolling down windows, and many more factors. We cannot realistically model that analytically, and so in practice we work out a simplified model, compute based on that simplified model, and then add a bit more to in hopes of taking the incalculable factors into account. Then we use a lot of simulations and trial runs to see if the filter behaves well; if it doesn't we adjust until the filter performs well. In this chapter we will focus on forming an intuitive understanding on how adjusting affects the output of the filter. In the Kalman Filter Math chapter we will discuss the analytic computation of , and also provide code that will compute it for you.
For now we will import the code from the FilterPy
module, where it is already implemented. I will import it and call help on it so you can see the documentation for it.
Design of the Process Noise Matrix
Author's note: this section contains some of the more challenging math in this book. Please bear with it, as few books cover this well, and an accurate design is imperative for good filter performance. At the end I present Python functions from FilterPy which will compute the math for you for common scenarios.
In general the design of the matrix is among the most difficult aspects of Kalman filter design. This is due to several factors. First, the math itself is somewhat difficult and requires a good foundation in signal theory. Second, we are trying to model the noise in something for which we have little information. For example, consider trying to model the process noise for a baseball. We can model it as a sphere moving through the air, but that leave many unknown factors - the wind, ball rotation and spin decay, the coefficient of friction of a scuffed ball with stitches, the effects of wind and air density, and so on. I will develop the equations for an exact mathematical solution for a given process model, but since the process model is incomplete the result for will also be incomplete. This has a lot of ramifications for the behavior of the Kalman filter. If is too small than the filter will be overconfident in it's prediction model and will diverge from the actual solution. If is too large than the filter will be unduly influenced by the noise in the measurements and perform sub-optimally. In practice we spend a lot of time running simulations and evaluating collected data to try to select an appropriate value for . But let's start by looking at the math.
Let's assume a kinematic system - some system that can be modeled using Newton's equations of motion. We can make a few different assumptions about this process.
We have been using a process model of
where is the process noise. Kinematic systems are continuous - their inputs and outputs can vary at any arbitrary point in time. However, our Kalman filters are discrete. We sample the system at regular intervals. Therefore we must find the discrete representation for the noise term in the equation above. However, this depends on what assumptions we make about the behavior of the noise. We will consider two different models for the noise.
Continuous White Noise Model
We model kinematic systems using Newton's equations. So far in this book we have either used position and velocity, or position,velocity, and acceleration as the models for our systems. There is nothing stopping us from going further - we can model jerk, jounce, snap, and so on. We don't do that normally because adding terms beyond the dynamics of the real system actually degrades the solution.
Let's say that we need to model the position, velocity, and acceleration. We can then assume that acceleration is constant. Of course, there is process noise in the system and so the acceleration is not actually constant. In this section we will assume that the acceleration changes by a continuous time zero-mean white noise . In other words, we are assuming that velocity is acceleration changing by small amounts that over time average to 0 (zero-mean).
Since the noise is changing continuously we will need to integrate to get the discrete noise for the discretization interval that we have chosen. We will not prove it here, but the equation for the discretization of the noise is
where is the continuous noise. This gives us
for the fundamental matrix, and
for the continuous process noise matrix, where is the spectral density of the white noise.
We could carry out these computations ourselves, but I prefer using SymPy to solve the equation.
For completeness, let us compute the equations for the 0th order and 1st order equations.
Piecewise White Noise Model
Another model for the noise assumes that the that highest order term (say, acceleration) is constant for each time period, but differs for each time period, and each of these is uncorrelated between time periods. This is subtly different than the model above, where we assumed that the last term had a continuously varying noisy signal applied to it.
We will model this as
where is the noise gain of the system, and is the constant piecewise acceleration (or velocity, or jerk, etc).
Lets start with by looking a first order system. In this case we have the state transition function
In one time period, the change in velocity will be , and the change in position will be , giving us
The covariance of the process noise is then
We can compute that with SymPy as follows
The second order system proceeds with the same math.
Here we will assume that the white noise is a discrete time Wiener process. This gives us
There is no 'truth' to this model, it is just convenient and provides good results. For example, we could assume that the noise is applied to the jerk at the cost of a more complicated equation.
The covariance of the process noise is then
We can compute that with SymPy as follows
We cannot say that this model is more or less correct than the continuous model - both are approximations to what is happening to the actual object. Only experience and experiments can guide you to the appropriate model. In practice you will usually find that either model provides reasonable results, but typically one will perform better than the other.
The advantage of the second model is that we can model the noise in terms of which we can describe in terms of the motion and the amount of error we expect. The first model requires us to specify the spectral density, which is not very intuitive, but it handles varying time samples much more easily since the noise is integrated across the time period. However, these are not fixed rules - use whichever model (or a model of your own devising) based on testing how the filter performs and/or your knowledge of the behavior of the physical model.
Using FilterPy to Compute Q
FilterPy offers several routines to compute the matrix. The function Q_continuous_white_noise()
computes for a given value for and the spectral density.
The function Q_discrete_white_noise()
computes assuming a piecewise model for the noise.
Simplification of Q
Through the early parts of this book I used a much simpler form for , often only putting a noise term in the lower rightmost element. Is this justified? Well, consider the value of for a small
We can see that most of the terms are very small. Recall that the only Kalman filter using this matrix is
If the values for are small relative to than it will be contributing almost nothing to the computation of . Setting to
while not correct, is often a useful approximation. If you do this you will have to perform quite a few studies to guarantee that your filter works in a variety of situations. Given the availability of functions to compute the correct values of for you I would strongly recommend not using approximations. Perhaps it is justified for quick-and-dirty filters, or on embedded devices where you need to wring out every last bit of performance, and seek to minimize the number of matrix operations required.
Numeric Integration of Differential Equations
** author's note: this is just notes to a section. If you need to know this in depth, *Computational Physics in Python * by Dr. Eric Ayars is excellent, and available here. http://phys.csuchico.edu/ayars/312/Handouts/comp-phys-python.pdf **
So far in this book we have been working with systems that can be expressed with simple linear differential equations such as
which we can integrate into a closed form solution, in this case . This equation is then put into the system matrix , which allows the Kalman filter equations to predict the system state in the future. For example, our constant velocity filters use
The Kalman filter predict equation is . Hence the prediction is
which multiplies out to
This works for linear ordinary differential equations (ODEs), but does not work (well) for nonlinear equations. For example, consider trying to predict the position of a rapidly turning car. Cars turn by pivoting the front wheels, which cause the car to pivot around the rear axle. Therefore the path will be continuously varying and a linear prediction will necessarily produce an incorrect value. If the change in the system is small enough relative to this can often produce adequate results, but that will rarely be the case with the nonlinear Kalman filters we will be studying in subsequent chapters. Another problem is that even trivial systems produce differential equations for which finding closed form solutions is difficult or impossible.
For these reasons we need to know how to numerically integrate differential equations. This can be a vast topic, and SciPy provides integration routines such as scipy.integrate.ode
. These routines are robust, but
Euler's Method
Let's say we have the initial condition problem of
I happen to know the exact answer is , but of course in general we will not know the exact solution. In general all we know is the derivative of the equation, which is equal to the slope. We also know the initial value: at , . If we know these two pieces of information we can predict that value for y(1): it is the slope of the function at . I've plotted this below
If the curve is relatively straight than the line formed by the slope will not be very far from the curve. Here the step size of 1 is rather large; you can see that the slope is very close to the line at, say, 0.1. But let's continue with a step size of 1 for a moment. We can see that at the estimated value of is 2. Now we can compute the value at by taking the slope of the curve at and adding it to our initial estimate. The slope is computed with , so the slope is 2.
Here we see the next estimate for y is 4. The errors are quickly getting large, and you might be unimpressed. But 1 is a very large step size. Let's put this algorithm in code, and verify that it works by trying to generate the result above.
This looks correct. So now lets plot the result of a much smaller step size.
Here we see that the error is reasonably small, but it took a very large number of iterations to get three digits of precision. In practice Euler's method is too slow for most problems, and we use higher level methods.
Before we go on, let's formally derive Euler's method, as it is the basis for the more advanced Runge Kutta methods used in the next section. In fact, Euler's method is the simplest form of Runge Kutta.
Here are the first 3 terms of the Euler expansion of . An infinite expansion would give an exact answer, so denotes the error due to the finite expansion.
Here we can see that Euler's method is using the first two terms of the Taylor expansion. Each subsequent term is smaller than the previous terms, so we are assured that the estimate will not be too far off from the correct value.
Runge Kutta Methods
Runge Kutta integration is the workhorse of numerical integration. As mentioned earlier there are a vast number of methods in the literature. In practice, using the Runge Kutta algorithm that I present here will solve most any problem you will face. It offers a very good balance of speed, precision, and stability, and it is the 'go to' numerical integration method unless you have a very good reason to choose something different. If you have the knowledge to make that decision you have no need to be reading this section!
Let's dive in. We start with some differential equation
We can substitute the derivative of y with a function f, like so
Deriving these equations is outside the scope of this book, but the Runge Kutta RK4 method is defined with these equations.
Let's use this for a simple example. Let
with the initial values
Iterative Least Squares for Sensor Fusion
A broad category of use for the Kalman filter is sensor fusion. For example, we might have a position sensor and a velocity sensor, and we want to combine the data from both to find an optimal estimate of state. In this section we will discuss a different case, where we have multiple sensors providing the same type of measurement.
The Global Positioning System (GPS) is designed so that at least 6 satellites are in view at any time at any point on the globe. The GPS receiver knows the location of the satellites in the sky relative to the Earth. At each epoch (instant in time) the receiver gets a signal from each satellite from which it can derive the pseudorange to the satellite. In more detail, the GPS receiver gets a signal identifying the satellite along with the time stamp of when the signal was transmitted. The GPS satellite has an atomic clock on board so this time stamp is extremely accurate. The signal travels at the speed of light, which is constant in a vacuum, so in theory the GPS should be able to produce an extremely accurate distance measurement to the measurement by measuring how long the signal took to reach the receiver. There are several problems with that. First, the signal is not traveling through a vacuum, but through the atmosphere. The atmosphere causes the signal to bend, so it is not traveling in a straight line. This causes the signal to take longer to reach the receiver than theory suggests. Second, the on board clock on the GPS receiver is not very accurate, so deriving an exact time duration is nontrivial. Third, in many environments the signal can bounce off of buildings, trees, and other objects, causing either a longer path or multipaths, in which case the receive receives both the original signal from space and the reflected signals.
Let's look at this graphically. I will do this in 2D to make it easier to graph and see, but of course this will generalize to three dimensions. We know the position of each satellite and the range to each (the range is called the pseudorange; we will discuss why later). We cannot measure the range exactly, so there is noise associated with the measurement, which I have depicted with the thickness of the lines. Here is an example of four pseudorange readings from four satellites. I positioned them in a configuration which is unlikely for the actual GPS constellation merely to make the intersections easy to visualize. Also, the amount of error shown is not to scale with the distances, again to make it easier to see.
In 2D two measurements are typically enough to determine a unique solution. There are two intersections of the range circles, but usually the second intersection is not physically realizable (it is in space, or under ground). However, with GPS we also need to solve for time, so we would need a third measurement to get a 2D position.
However, since GPS is a 3D system we need to solve for the 3 dimensions of space, and 1 dimension of time. That is 4 unknowns, so in theory with 4 satellites we have all the information we need. However, we normally have at least 6 satellites in view, and often more than 6. This means the system is overdetermined. Finally, because of the noise in the measurements none of pseudoranges intersect exactly.
If you are well versed in linear algebra you know that this an extremely common problem in scientific computing, and that there are various techniques for solving overdetermined systems. Probably the most common approach used by GPS receivers to find the position is the iterative least squares algorithm, commonly abbreviated ILS. As you know, if the errors are Gaussian then the least squares algorithm finds the optimal solution. In other words, we want to minimize the square of the residuals for an overdetermined system.
Let's start with some definitions which should be familiar to you. First, we define the innovation as
where is the measurement, is the measurement function, and is the innovation, which we abbreviate as in FilterPy. I don't use the symbology often, but it is the prediction for the state variable. In other words, this is the equation in the linear Kalman filter's update step.
Next, the measurement residual is
I don't use the plus superscript much because I find it quickly makes the equations unreadable, but it is the a posteriori state estimate, which is the predicted or unknown future state. In other words, the predict step of the linear Kalman filter computes this value. Here it is stands for the value of x which the ILS algorithm will compute on each iteration.
These equations give us the following linear algebra equation:
is our measurement function, defined as
We find the minimum of an equation by taking the derivative and setting it to zero. In this case we want to minimize the square of the residuals, so our equation is
where
Here I have switched to using the matrix as the measurement function. We want to use linear algebra to peform the ILS, so for each step we will have to compute the matrix which corresponds to during each iteration. is usually nonlinear for these types of problems so you will have to linearize it at each step (more about this soon).
For various reasons you may want to weigh some measurement more than others. For example, the geometry of the problem might favor orthogonal measurements, or some measurements may be more noisy than others. We can do that with the equation
If we solve the first equation for (the derivation is shown in the next section) we get
And the second equation yields
Since the equations are overdetermined we cannot solve these equations exactly so we use an iterative approach. An initial guess for the position is made, and this guess is used to compute for via the equation above. is added to the intial guess, and this new state is fed back into the equation to produce another . We iterate in this manner until the difference in the measurement residuals is suitably small.
Derivation of ILS Equations (Optional)
I will implement the ILS in code, but first let's derive the equation for . You can skip the derivation if you want, but it is somewhat instructive and not too hard if you know basic linear algebra and partial differential equations.
Substituting into the partial differential equation we get
which expands to
We know that
Therefore the third term can be computed as
and the second term as
We also know that
Therefore the first term becomes
Finally, the fourth term is
Replacing the terms in the expanded partial differential equation gives us
Multiplying each side by yields
Taking the transpose of each side gives
For various reasons you may want to weigh some measurement more than others. We can do that with the equation
Replicating the math above with the added term results in
Implementing Iterative Least Squares
Our goal is to implement an iterative solution to
First, we have to compute , where . To keep the example small so the results are easier to interpret we will do this in 2D. Therefore for satellites expands to
We will linearize by computing the partial for as
The equation for just substitutes for .
Then the algorithm is as follows.
So let's think about this. The first iteration is essentially performing the computation that the linear Kalman filter computes during the update step:
where the Kalman gain equals one. You can see that despite the very inaccurate initial guess (900, 90) the computed value for , (805.4, 205.3), was very close to the actual value of (800, 200). However, it was not perfect. But after three iterations the ILS algorithm was able to find the exact answer. So hopefully it is clear why we use ILS instead of doing the sensor fusion with the Kalman filter - it gives a better result. Of course, we started with a very inaccurate guess; what if the guess was better?
The first iteration produced a better estimate, but it still could be improved upon by iterating.
I injected no noise in the measurement to test and display the theoretical performance of the filter. Now let's see how it performs when we inject noise.
Here we can see that the noise means that we no longer find the exact solution but we are still able to quickly converge onto a more accurate solution than the first iteration provides.
This is far from a complete coverage of the iterated least squares algorithm, let alone methods used in GNSS to compute positions from GPS pseudoranges. You will find a number of approaches in the literature, including QR decomposition, SVD, and other techniques to solve the overdetermined system. For a nontrivial task you will have to survey the literature and perhaps design your algorithm depending on your specific sensor configuration, the amounts of noise, your accuracy requirements, and the amount of computation you can afford to do.
References
[1] Matrix Exponential http://en.wikipedia.org/wiki/Matrix_exponential
[2] LTI System Theory http://en.wikipedia.org/wiki/LTI_system_theory
[3] C.F. van Loan, "Computing Integrals Involving the Matrix Exponential," IEEE Transactions Automatic Control, June 1978.
ORPHAN TEXT
I admit this may be a 'magical' equation to you. If you have some experience with linear algebra and statistics, this may help. The covariance due to the prediction can be modeled as the expected value of the error in the prediction step, given by this equation.
Of course, is just , giving us