|
| 1 | +Kalman filters and other optimal and non-optimal estimation filters in Python. |
| 2 | +------------------------------------------------------------------------------ |
| 3 | + |
| 4 | +This library provides Kalman filtering and various related optimal and |
| 5 | +non-optimal filtering software written in Python. It contains Kalman |
| 6 | +filters, Extended Kalman filters, Unscented Kalman filters, Kalman |
| 7 | +smoothers, Least Squares filters, fading memory filters, g-h filters, |
| 8 | +discrete Bayes, and more. |
| 9 | + |
| 10 | +This is code I am developing in conjunction with my book Kalman Filters |
| 11 | +and Random Signals in Python, which you can read/download at |
| 12 | +http://rlabbe.github.io/Kalman-and-Bayesian-Filters-in-Python/ |
| 13 | + |
| 14 | +My aim is largely pedalogical - I opt for clear code that matches the |
| 15 | +equations in the relevant texts on a 1-to-1 basis, even when that has a |
| 16 | +performance cost. There are places where this tradeoff is unclear - for |
| 17 | +example, I find it somewhat clearer to write a small set of equations |
| 18 | +using linear algebra, but numpy's overhead on small matrices makes it |
| 19 | +run slower than writing each equation out by hand, and books such as |
| 20 | +Zarchan present the written out form, not the linear algebra form. It is |
| 21 | +hard for me to choose which presentation is 'clearer' - it depends on |
| 22 | +the audience. In that case I usually opt for the faster implementation. |
| 23 | + |
| 24 | +I use numpy and scipy for all of the computations. I have experimented |
| 25 | +with Numba, Continuum Analytics just in time compiler, and it yields |
| 26 | +impressive speed ups with minimal costs, but I am not convinced that I |
| 27 | +want to add that requirement to my project. It is still on my list of |
| 28 | +things to figure out, however. |
| 29 | + |
| 30 | +As it evolves from alpha status I am adding documentation, tests, and |
| 31 | +examples, but at the moment the my book linked above serves as the best |
| 32 | +documentation. I am developing both in parallel, so one or the other has |
| 33 | +to suffer during the development phase. Reach out to me if you have |
| 34 | +questions or needs and I will either answer directly or shift my |
| 35 | +development to address your problem (assuming your question is a planned |
| 36 | +part of this library. |
| 37 | + |
| 38 | +Basic use: |
| 39 | +---------- |
| 40 | + |
| 41 | +:: |
| 42 | + |
| 43 | + from filterpy.kalman import KalmanFilter |
| 44 | + from filterpy.memory import FadingMemoryFilter |
| 45 | + |
| 46 | + |
| 47 | + my_filter = KalmanFilter(3,4) |
| 48 | + |
| 49 | +Requirements |
| 50 | +------------ |
| 51 | + |
| 52 | +numpy and scipy Python 2 or 3 matplotlib |
| 53 | + |
| 54 | +I haven't extensively tested backwards compatibility - I use the |
| 55 | +Anaconda distribution, and so I am on Python 3.4 and 2.7.5, along with |
| 56 | +whatever version of numpy, scipy, and matplotlib they provide. But I am |
| 57 | +using pretty basic Python - numpy.array, maybe a list comprehension in |
| 58 | +my tests. |
| 59 | + |
| 60 | +I import from **future** to ensure the code works in Python 2 and 3. |
| 61 | + |
| 62 | +The matplotlib library is required because, *for now*, 'tests' are very |
| 63 | +visual. Meaning I generate some data, plot the data against the filtered |
| 64 | +results, and eyeball it. That is great for my personal development, and |
| 65 | +terrible as a foundation for regression testing. If you don't have |
| 66 | +matplotlib installed you won't be able to run the tests, but I'm not |
| 67 | +sure the tests will have a lot of meaning to you anyway. |
| 68 | + |
| 69 | +There is one import from the code from my book to plot ellipses. That |
| 70 | +dependency needs to be removed. This only affects the tests. |
| 71 | + |
| 72 | +Testing |
| 73 | +------- |
| 74 | + |
| 75 | +All tests are written to work with py.test. Just type *py.text* at the |
| 76 | +command line. |
| 77 | + |
| 78 | +As explained above, the tests are not robust. I'm still at the stage |
| 79 | +where visual plots are the best way to see how things are working. |
| 80 | +Apologies, but I think it is a sound choice for development. It is easy |
| 81 | +for a filter to perform within theoretical limits (which we can write a |
| 82 | +non-visual test for) yet be 'off' in some way. The code itself contains |
| 83 | +tests in the form of asserts and properties that ensure that arrays are |
| 84 | +of the proper dimension, etc. |
| 85 | + |
| 86 | +References |
| 87 | +---------- |
| 88 | + |
| 89 | +I use three main texts as my refererence, though I do own the majority |
| 90 | +of the Kalman filtering literature. First is Paul Zarchan's |
| 91 | +'Fundamentals of Kalman Filtering: A Practical Approach'. I think it by |
| 92 | +far the best Kalman filtering book out there if you are interested in |
| 93 | +practical applications more than writing a thesis. The second book I use |
| 94 | +is Eli Brookner's 'Tracking and Kalman Filtering Made Easy'. This is an |
| 95 | +astonishing good bood; its first chapter is actually readable by the |
| 96 | +layperson! Brookner starts from the g-h filter, and shows how all other |
| 97 | +filters - the Kalman filter, least squares, fading memory, etc., all |
| 98 | +derive from the g-h filter. It greatly simplifies many aspects of |
| 99 | +analysis and/or intuitive understanding of your problem. In contrast, |
| 100 | +Zarchan starts from least squares, and then moves on to Kalman |
| 101 | +filtering. I find that he downplays the predict-update aspect of the |
| 102 | +algorithms, but he has a wealth of worked examples and comparisons |
| 103 | +between different methods. I think both viewpoints are needed, and so I |
| 104 | +can't imagine discarding one book. Brookner also focuses on issues that |
| 105 | +are ignored in other books - track initialization, detecting and |
| 106 | +discarding noise, tracking multiple objects, an so on. |
| 107 | + |
| 108 | +I said three books. I also like and use Bar-Shalom's Estimation with |
| 109 | +Applications to Tracking and Navigation. Much more mathmatical than the |
| 110 | +previous two books, I would not recommend it as a first text unless you |
| 111 | +already have a background in control theory or optimal estimation. Once |
| 112 | +you have that experience, this book is a gem. Every sentence is crystal |
| 113 | +clear, his language is precise, but each abstract mathematical statement |
| 114 | +is followed with something like "and this means...". |
| 115 | + |
| 116 | +License |
| 117 | +------- |
| 118 | + |
| 119 | +Copyright (c) 2014 Roger R Labbe Jr |
| 120 | + |
| 121 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 122 | +copy of this software and associated documentation files (the |
| 123 | +"Software"), to deal in the Software without restriction, including |
| 124 | +without limitation the rights to use, copy, modify, merge, publish, |
| 125 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 126 | +permit persons to whom the Software is furnished to do so, subject to |
| 127 | +the following conditions: |
| 128 | + |
| 129 | +The above copyright notice and this permission notice shall be included |
| 130 | +in all copies or substantial portions of the Software. |
| 131 | + |
| 132 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 133 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 134 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 135 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 136 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 137 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 138 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments