Kalman Filter
en.wikipedia.org
en.wikipedia.org
How a Kalman filter works, in pictures https://www.bzarg.com/p/how-a-kalman-filter-works-in-picture...
Derive yourself a Kalman filter https://ngr.yt/blog/kalman/
Kalman and Bayesian Filters in Python https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Pyt...
In this sense, the Kalman Filter in time-dynamic prediction/forecasting problems is like gradient descent in ML prediction problems, or the solution to the Normal equations in linear regression or FFT for discrete Fourier transforms. The algorithms are mathematical pieces of art, but the useful practical skills are how to set up your problems so that they're amendable to the solutions.
In case of Kalman / state space models, the main things that are estimated are positions of things that move in time, but not (directly) observed. These "things" can be concrete (drones, rockets in space, robots on Mars) or abstract (an idealized compuer cursor/pointer, the water level in a river, inflation or unemployment in macro-economics). When concrete "things" are estimated by traditional engineers and the dynamics are governed by physics, the tool of choice is usually (employer-paid) Matlab. For fuzzier economics and forecasting problems, nowadays there's a very good state space / Kalman Filter implementation in Python statsmodels by Chad Fulton[1], which closely follows the academic works from Harvey, Durbin and Koopman. The challenge in these problems is usually setting up a plausible underlying dynamics model, more so than the estimation/prediction part. Just like in linear regression, finding appropriate features and functional forms are the main practical challenge, not calculating the parameters and actual predictions.
[1] http://www.chadfulton.com/fulton_statsmodels_2017/sections/1...
Most striking in deep learning, is that when all is said and done (i.e. unwrapping the so-called "recurrent" part of a deep net) all you're left with is a dumb function from Rn to Rp.
In other words, while the term "recurrent" might fool you into believing it, there isn't any actual feedback loop in deep nets, and they therefore can't properly model something whose output evolves over time.
I think there's something to be explored in adding actual feedback into deep nets.
The price is of course that the output is not a vector anymore: you must compute feed forward passes until the net "settles" to a fixed point or rather until the desired behavior is observed at the outputs.
Backprop also becomes a whole new game in that setting.
[EDIT]: but the prize is: your deep net, instead of being an approximation of a function from Rn to Rp, becomes an approximation of something that can transform a function into another function.
I dunno, WaveNet seems to work pretty good... It's an autoregressive model, and absolutely responds to it's own output.
https://deepmind.com/blog/article/wavenet-generative-model-r...
Deep Equilibrium Models http://implicit-layers-tutorial.org/deep_equilibrium_models/
But, you can build great priors with a well-modeled ODE
1. Figure out the initial state
2. use process model to predict state at next time step
3. adjust belief of the true state by taking account prediction uncertainty
4. get a measurement and belief according to its uncertainty/accuracy
5. compute residual
6. compute scaling factor (what's more important, measurement or prediction)
7. set state with scaling factor
8. update belief in state given uncertainty of measurement
pretty much allows plugging in a black box anywere. from process model to belief updates.
Won't it be more like an approximation of a process instead of an approximation of a function? (I mean better described as that)
The key advantage a kalman filter has is in its long term properties being predictable, things like observability, controllability, and bibo stability.
The key downside of kalman filters is that their nice properties only apply to linear systems and linear observations.
So if you like KF, EKF, UKF, be sure to look into this entire chain of accuracy vs computation tradeoff algorithms.
How a Kalman filter works, in pictures - https://news.ycombinator.com/item?id=23943836 - July 2020 (27 comments)
Corona virus spread prediction using kalman filter - https://news.ycombinator.com/item?id=22578235 - March 2020 (2 comments)
Kalman Filter Simulation - https://news.ycombinator.com/item?id=21709534 - Dec 2019 (1 comment)
Derive Yourself a Kalman Filter - https://news.ycombinator.com/item?id=19894776 - May 2019 (55 comments)
How a Kalman filter works, in pictures (2015) - https://news.ycombinator.com/item?id=17116149 - May 2018 (34 comments)
Reduce GPS data error on Android with Kalman filter and accelerometer - https://news.ycombinator.com/item?id=16627111 - March 2018 (30 comments)
Quick explanation of how a Kalman filter works - https://news.ycombinator.com/item?id=16575679 - March 2018 (76 comments)
Unscented Kalman Filter (UKF) C Library - https://news.ycombinator.com/item?id=15986880 - Dec 2017 (2 comments)
How a Kalman filter works, in pictures (2015) - https://news.ycombinator.com/item?id=13449229 - Jan 2017 (32 comments)
How a Kalman filter works, in pictures - https://news.ycombinator.com/item?id=12648724 - Oct 2016 (1 comment)
Kalman Filter via a Simple and Intuitive Derivation [pdf] - https://news.ycombinator.com/item?id=12648035 - Oct 2016 (24 comments)
The magic of the Kalman filter, in pictures - https://news.ycombinator.com/item?id=10042050 - Aug 2015 (48 comments)
Kalman Filter Simulator - https://news.ycombinator.com/item?id=7878474 - June 2014 (1 comment)
SideCar's Kalman Filter models San Francisco brunch - https://news.ycombinator.com/item?id=7349419 - March 2014 (14 comments)
How to build an Anti Aircraft Missile: Bayes’ Theorem and the Kalman Filter - https://news.ycombinator.com/item?id=6657301 - Nov 2013 (15 comments)
Using A Kalman Filter To Make Sense Of Noisy Data - https://news.ycombinator.com/item?id=3950510 - May 2012 (18 comments)
Poor Man's Explanation of Kalman Filtering [pdf] - https://news.ycombinator.com/item?id=1362597 - May 2010 (11 comments)
Others?
https://web.archive.org/web/20140327203303if_/http://www.cl....
Particle filters do come at a cost of increased memory and processing usage. You need to propagate N particles at each epoch, each of which could have M dimensions of state (position, velocity, orientation, etc). So Kalman filters are likely preferred where memory and compute are at a premium.
As you stated particle filters are computationally expensive. Also I found them more fidgety to configure for my application cases (in activity recognition).
And why the name? Well the EKF stinks in many applications (it diverges). The UKF is more robust, especially when there is more significant uncertainty.
Funny factoid: the paper that named the UKF did so because "it doesn't stink"
https://github.com/Flybrix/flybrix-firmware/blob/master/ukf....
The Unscented Kalman Filter (UKF) is a kind of particle-like filter but instead of propagating a large number of points, it only propagates carefully chosen “sigma points”. It’s much more economical and works relatively well with nonlinear systems.
To me, the gold standard in estimation is the Moving Horizon Estimator (MHE) [1] which solves an explicit optimization problem to arrive at the state estimate, which allows it to accommodate constraints in the system. It is however computationally much heavier than the Kalman family of filters.
I’ve implemented UKFs in the past and think they’re a good trade off between computational speed and accuracy in the presence of nonlinearities. They cannot handle constraints however, which is a major weakness that MHEs don’t succumb to. (Imagine you are estimating a quantity x in the interval [0, 1] - UKFs can return estimates like 1.32 or -0.3 whereas MHEs accommodate bound constraints like 0 <= x <= 1 as well as more complex constraints to prevent weird outputting impossible or weird state estimates)
[1] https://murray.cds.caltech.edu/images/murray.cds/0/0d/L4-2_M...
Also depending on your refresh rate 3s might still be worth it IF the filter gain is so much better.
If you need to simultaneously handle nonlinearities and constraints, it may be a superior choice.
Also there are potential ways to make MHE run faster. For certain forms of the optimization problem (like a linear or quadratic program), it’s possible to convert it to a lookup table (search for “explicit MPC”) which can then be implemented in an embedded system for fast lookups.
That said, I agree with you in practice. An nonlinear optimization problem tends to be a numerical black box and can be hard to diagnose when things go wrong. The Kalman family of algorithms is much more straightforward (only involves function evaluations), faster and have failure modes that are much more easily understood. They just don’t do so well with constraints.
There are Extended Kalman Filter and Unscented Kalman Filters that are designated to relax both of these constraints, but they aren’t perfect.
I feel like I owe it a shoutout because that strategy was the foundation for an algorithm that got me and a couple friends a top finish at UChicago's algo trading competition a few years back.
"These simulations used 36-bit floating point arithmetic, which was adequate for trajectory simulations but marginal for implementing the Riccati equation solution for the measurement updates in the Kalman filter. Performance was not reliable in 36-bit floating point arithmetic, and the Apollo flight computer would have to implement the Kalman filter in 15-bit fixed-point arithmetic. Microprocessors were still a long way off. J.E. Potter was at that time a graduate student in mathematics at MIT, working part time at the Instrumentation Laboratory on the Apollo Project. He took the problem home with him one Friday afternoon and arrived back on Monday with a solution. The trick was to use a Cholesky factor of the covariance matrix as the dependent variable in an equivalent Riccati equation. The solution was brilliant, and the improvement was profound. The approach came to be called square-root filtering, and alternative implementations of square-root filters with better numerical stability were soon discovered."
excerpted from: https://www.researchgate.net/profile/Mohinder-Grewal/publica...
Almost like old sea travelers!
I'm going to use that as my next pickup line
Are there similar equivalences with other HMM algorithms for different questions? A Viterbi-like algorithm for the most likely path, for example, distinct from the series of Kalman-update argmaxes?
so yeah, you can do things like e-m to learn the parameters of the observation and state update models from data (just like fitting an hmm in the fully observed case), or given state and observation models, and past observations you can predict future state given current state. (what it was made for, where state is the state of your rocket given the physics of motion and all the noisy sensors or whatever, which if you think about it is like a viterbi search for the best fitting path through the continuous state space)
The dynamics model in a KF is a linear system with an input and and output (often denoted u and y respectively).
A Hidden Markov Model typically does not have an input. There is an initial state, and then the system transitions "autonomously" (that's a term I'm borrowing from differential equations).
An "Markov model" with input is called a Markov Decision Process (MDP). And if the state of the system is not fully observable, then it's a POMDP.
So Kalman filters are most analogous to "belief updates" in POMDPs. The KF takes into account the known input to the system.
RNNs are still a significant part of the toolbox, and would be found in several chapters in any applied NN book.
https://www.gamasutra.com/view/feature/129919/wheres_the_wii...
But can anyone provide more applications that make learning the Kalman Filter easy?
I recommend the following two articles that helped me get a better working understanding of the Kalman Filter:
https://www.bzarg.com/p/how-a-kalman-filter-works-in-picture...
- you have observations that are normally distributed around a linear function of an unknown state
- you have some state update function where the next state is normally distributed around a linear function of the unknown current state
- your beliefs about your initial state are normally distributed
then a kalman filter will update your beliefs about the state of your system and do as good a job as could be hoped for. There are also simple generalizations that relax some of those assumptions.
This is one of those algorithms that anyone working with data should know. It answers the dynamical question of wtf is going on now, given what I’ve seen over time.
The KF maximizes the likelihood of the observations..
You can easily form the optimization problem, "what state x maximizes the likelihood of my observations z". I had a writeup to this effect in my class notes from Prof Roumeliotis' class. I'll see if I can post it, but you could probably derive it following the KF wiki page.
I'll just go ahead and explain indices and summation in the meantime already because these are so super-common that you basically cannot read math things without knowing them. I will denote things LaTeX-like, that is subscript with _ and superscript with ^, if multiple things go in there, they will be enclosed in {}.
---
First: indices, usually written as sub-script, are super-common. They might be separated by a comma if there are more than one (but they do not have to be. In that case, you need to check which variables make sense as indices.) Usually variables are just single letters and not superWordyDescriptionsOfThingsBecauseCodeCompletionIsDifficultWithPenAndPaper.
Examples:
a_i is like a[i]
m_{ij} is element at position i, j in matrix M, i.e. ~ M[i][j]
---
Summation is also very common, denoted via symbol Σ . That's the upper-case greek letter "sigma". This one will have a few things accompanying it: below it is a variable name and most likely an assignment of a value to it. That's the first value that the variable will take. Above, the last value the variable should have. The expression behind the symbol is evaluated for each of these values and all integers in between and summed up. (It might also appear with just the varaible name below it, in which case it means: sum over all values of the variable).
Example: Summing up the squares of all integers from 3 to 6
Σ_{i=3}^6 i^2
(Properly rendered here: https://wikimedia.org/api/rest_v1/media/math/render/svg/a60f... )
Pseudocode for this is:
for(i = 3; i <= 6; i++)
sum += i^2
---Multiplication: there is also a similiar one for multiplication that I will include because it is so easily explained when having discussed summation already. It's denoted by the greek upper-case letter Π (Pi)
Π works like Σ but the results of the expressions are multiplied instead.
Example: Π_{i=3}^6 i^2 = 3^2 * 4^2 * 5^2 * 6^2
Pseudocode:
for(i = 3; i <= 6; i++)
product *= i^2
This is only meant to help you translate a few formulas into forms you might easier work with. Following a university math course for a single semester or maybe two might get you a long way as well.https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Pyt...
Briefly:
If you have a normal distribution as a prior and a normal distribution as new evidence, then applying Bayes Theorem on them results in a new normal distribution (times a constant we'll ignore). The calculation can actually be pretty simple - the mean is a weighted average, weighted by precision (precision is 1/variance), and the precision is the sum of the two precisions.
If you have two normal distributions modeling unknown variables a and b, and you know x=a+b, then you can model your knowledge of x as another normal distribution. The calculation for this one is also simple - its mean is the sum of the two means (intuitively has to be) and its variance is the sum of the two variances (I've seen several math books say it's surprising. It's very convenient though).
So you have all the pieces you need to do (new state guess = old state knowledge + noise from passage of time) and (new state knowledge = new state guess | new observation), where all the variables are normal distributions, and the new knowledge ends up being a normal distribution too, so you can use it as the old knowledge next time you get information.
[1] https://en.wikipedia.org/wiki/Conjugate_prior#When_likelihoo...
[2] https://en.wikipedia.org/wiki/Sum_of_normally_distributed_ra...
I'd also suggest not only trying to imagine this in your head: implement it in code. That's how I truly learn these types of things.
For extra fun, you can make it into a game. After you've implemented the filter, create a way for a human to input a guess at the filtered output. Score it depending on how close it is. This way you get to practise your intuition for it too.
Let me try: https://en.wikipedia.org/wiki/Sun
Whoa, the sun apparently is a star!
I was thinking about my fitness band at the gym and like yeah it uses... A Kalman Filter? I load HN and there it is.
Yes, this was a Wikipedia link and Kalman filters has been discussed here many times, that said I found the discussion and commentary quite delightful.
https://www.bzarg.com/p/how-a-kalman-filter-works-in-picture...
Frequently this isn't the case. Lots of real world systems have more complex distributions, like for example "which lane of a highway am I in given this GPS position" (it's likely you are in one of the lanes, but possible although unlikely I am between lanes).
Particle filters use more computation than kalman filters, but they can deal with arbitrary (and unknown) distributions of variables. Considering very few applications of this kind of system are limited by compute power, a particle filter is almost always better suited. (especially when you have a GPU available, which is a perfect fit for particle filters).
Citation needed. If I went by my own experience, the vast majority of Kalman filters run in compute/power/time-limited environments.
One quick fix is to add a disturbance model to the KF, for example an integating disturbance. But then you have one more tuning parameter (the disturbance model). Which along the other KF tuning parameters increase complexity and becomes more difficult to implement and understand.
Personally, I have had more success with other types of filters, which have a similar structure as the KF, but include mechanisms for improved predictions, for example this one:
https://folk.ntnu.no/skoge/prost/proceedings/ifac2014/media/...