Giter VIP home page Giter VIP logo

rlabbe / filterpy Goto Github PK

View Code? Open in Web Editor NEW
3.3K 3.3K 617.0 1.1 MB

Python Kalman filtering and optimal estimation library. Implements Kalman filter, particle filter, Extended Kalman filter, Unscented Kalman filter, g-h (alpha-beta), least squares, H Infinity, smoothers, and more. Has companion book 'Kalman and Bayesian Filters in Python'.

License: MIT License

Python 99.77% Shell 0.13% Batchfile 0.09%

filterpy's People

Contributors

ateraz avatar ernstklrb avatar frolswe avatar gjacquenot avatar haditab avatar hajapy avatar icaven avatar igorinov avatar jasonmhead avatar jbradt avatar jonathanjfshaw avatar jpcms avatar kinodim avatar lucasb-eyer avatar mahmoudalmasri avatar msabramo avatar nh2 avatar paulolimac avatar pavelmk avatar phlogistique avatar prokhozhijj avatar rlabbe avatar sauravrt avatar slayoo avatar thatch avatar timhutton avatar veeresht avatar voutcn avatar zolabar avatar zougloub avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

filterpy's Issues

Fixed lag smoother not correct

I don't believe the formulation of the fixed lag smoother is correct. I implemented the equations in Dan Simon's book and the performance is much better.

It needs to be tested and then checked in. It doesn't save the smoothed results so that has to be fixed. And then the batch version has to be corrected as well.

To the reader: I don't think the current implementation will break, it is just far from optimal. Still, you may want to steer clear for now.

Wrong weights used in UnscentedKalmanFilter.rts_smoother

Hi Roger,

First of all, thanks for the book and filterpy. Both are awesome!

I believe I found a bug in the rts_smoother method of UnscentedKalmanFilter.
In the code you use mean weights for both mean and covariance which is obviously wrong.

On line 519 of UKF.py
Pb += self.Wm[i] * outer(y, y)
should be
Pb += self.Wc[i] * outer(y, y)

Similarly, on line 527
Pxb += self.Wm[i] * outer(z, y)
should be replaced with
Pxb += self.Wc[i] * outer(z, y)

See this paper for more details https://users.aalto.fi/~ssarkka/pub/uks-preprint.pdf
The bug is not obvious as in many applications the weights are close.

Regards,
Roman

Install behind proxy

I am trying to install filterpy behind a proxy. The security team will not allow it through the proxy. Any help is appreciated.

MerweScaledSigmaPoints weights do not sum to 1, documentation says it should

For example:

import numpy as np
from filterpy.kalman import MerweScaledSigmaPoints as SigmaPoints

sigma_points = SigmaPoints(n=9, alpha=0.001, beta=2, kappa=-6)
Wm, Wc = sigma_points.weights()
print np.sum(Wm)
print np.sum(Wc)

Which returns

1.0000000005238689
3.9999990007490851

While Wm is small enough to be rounding error, Wc seems to go against notes in the documentation. Now, I'm not saying MerweScaledSigmaPoints is set wrong -- as far as I can tell all the equations are right. However, the book and documentation both say that Wc "must sum to 1".

So what's the deal?

Problems with PIP

I put this here so other people can find it.
I, like I suspect many other people uploaded the Anaconda distribution of Python as the easiest option to read the book. I already had a version of Python. I am running Windows 7.
All went well and everything worked until I tried to install filterpy on to my original Python setup.
Of course PIP was now “pointing” at the Anaconda version and despite Googling and asking a question on Stackoverflow I still do not know how this works and how you change it.
I had a rather old version of 2.7 so I decided to upgrade it (to 2.7.10) so I knew for certain that it had PIP installed. That worked but now I could not get at the notebook by typing “ipython notebook” as it now looks at the 2.7.10 version for what it needs.
My next great idea to sort this was to try and use pip2.7 and pip3.4 by reloading Anaconda but installing 3.4 rather than the 2.7 version that is the default. Pip2.7 works but there does not seem to be a version pip3.4 or at least I keep getting a response that the command is not recognized.
You might think that the pip would point to the latest installed version ie the Anaconda but for some unknown reason it still pointed to the 2.7.10 version. I therefore could not install filterpy in the Anaconda version.
The solution to all this is that I found that in the Anaconda folder there is an Anaconda Command Prompt which I got to from the Start Menu. Working in that pip works and finds the Anaconda version of Python and you can do ipython notebook although I now find the simplest way to start is to use the Anaconda graphical start up, choose ipython and navigate to the directory. Typing in that long directory name is a pain.
Why do I keep two versions of Python? Eell partly because I am an old foggy and have not yet got used to programming with iPython but also because my robot has a small Rpi in charge and I want to have a version the same as is on that.

invalid docstring for rts_smoother

The rts_smoother functions states that that F and Q are optional. They are not. Apparently docstring was taken from the class method,where they are optional.

Improve UKF documentation

If you look at the UKF.py file and read the comments it is impossible to figure out how to use the class. At a minimum, add a reference to my KF book, and add an example or two.

UKF does not support missing data as 'None'

In the doc string to UnscentedKalmanFilter.batch_filter, it states to represent missing data as None. However, this fails in the update step with a type multiply error; there appears to be no code to handle this case. In the standard KalmanFilter.update, there is a simple return statement if z is None.

Comments for filterpy.stats.multivariate_multiply()

In the method signature, comments for parameter c1 and c2 currently say they are the means of the respective Gaussian. Should they not be covariance instead?

Happy to start a PR if that's the case.

Thanks.

Kalman filter G and u problems

  1. the predict() method takes in an optional parameter u instead of using KalmanFilter.u variable. There is no way to get the object's u to be used. This can be worked around if you are calling predict, but batch_filter will always ignore KalmanFilter.u.
  2. I have constrained G to be of a certain size depending on the dim_u parameter. However it is perfectly legal for G to be a scalar such as 1. The constraint should not be that G or u be of a specific size, but only that Gu is of the same size as the state vector. There is no reasonable way to constrain that in the setters, so let it go and accept that there will be an exception thrown in predict() if the user got it wrong.

Refactor unit tests for UKF

I've made a few changes to the UKF code that changed the API; the tests no longer run. Fix the tests and (of course) make sure the UKF is still producing correct results.

kalman filter P update

in file kalman_filter.py, line 174-176, you implemented the following ...
174 # P = (I-KH)P(I-KH)' + KRK'
175 I_KH = self._I - dot(K, H)
176 self._P = dot3(I_KH, P, I_KH.T) + dot3(K, R, K.T)

this is mathematically correct, yet line 176 could be further reduced to ...
self._P = dot(I_KH, P)

because (cf. Simon's book p128, eq 5.16)
P = (I-KH)P(I-KH)' + KRK' = (I-KH)P

Enhancement - check all matrices and sample y input in KalmanFilter.test_matrix_dimensions()

Currently it looks like test_matrix_dimensions only checks x, P, and Q. The docs indicate it checks the size of everything, which is misleading since it doesn't check F, H, R, or a sample input. I would prefer it to check all of those in such a way that the state of the filter is not altered. Running down mismatched sizes is one of my least favorite things to debug and it could be streamlined.

Said slightly differently, in order to fully check the size of inputs, you need to run it, but in order to run it you need to have (mostly) correct inputs, which results in some awkward circular logic.

I think the signature could be test_matrix_dimensions(sample_input=None), where if sample_input is supplied it is additionally checked. That would not change the previous api.

Thank you

Sphinx doesn't use NumPy's docstrings

I use NumPy's documentation standard, but Sphinx does not support it by default. There is a package numpydoc which is supposed to add hooks to support it. I installed it with PIP but saw no change. Haven't had a chance to investigate.

To be clear, things like

Parameters

does not render properly in the documentation.

rts smoother

First of all, thank you for sharing such great work, in which i learnt a lot from it!

so, in kalman_filter.py file, line 374, i saw that you have
P[k] += dot3(K[k], P[k+1]-P_pred, K[k].T)

however, from Simon's book p294, equation (9.138), there's actually a minus sign. Thus i'm suspecting that line 374 should actually be
P[k] -= dot3(K[k], P[k+1]-P_pred, K[k].T)

Can you please verify?

Tests not distributed with pip install

I don't know if this contravenes any standard practice, but I would like it if you do 'pip install filterpy' that all of the unit tests get installed as well. They are extremely useful to use as examples/references.

Kalman .update throwing type error with 'allow_singular' argument with scipy 0.14

Hello,

I am working with python 2.7 on Windows 64 bit. I have scipy 0.14, filterpy 0.14, and numpy 1.11.1 installed.

As expected, in 0.14 Scipy, there is no 'allow_singular' keyword.

https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.multivariate_normal.html

TypeError Traceback (most recent call last)
in ()
88 filt_AngularVelocity.append(kalman['ang'].predict)
89 print image_center[-1][0]
---> 90 kalman['x'].update(float(image_center[-1][0]))
91 kalman['y'].update(float(image_center[-1][1]))
92 kalman['ang'].update(AngularVelocity[-1])

C:\Python27\lib\site-packages\filterpy\kalman\kalman_filter.pyc in update(self, z, R, H)
203 flatz = np.asarray(z).flatten()
204 self.log_likelihood = multivariate_normal.logpdf(
--> 205 flatz, mean, cov=S, allow_singular=True)
206
207

TypeError: logpdf() got an unexpected keyword argument 'allow_singular'

EKF update equation

Hi,

I'm using your library to work on a research project. When I used EKF, I found that the equation for updating P (covariance matrix) is not as same as it should be. I'm wondering if there is a specific reason to do that which updates P with R. Here is the code used in this both update() and predict_update().
I_KH = self._I - dot(K, H)
self._P = dot3(I_KH, P, I_KH.T) + dot3(K, R, K.T)
However, according to the theory of EKF. It should be like this.
self._p = dot(I_KH, P)
Please let me know if I'm wrong. Thx!
picture1

Unit testing needs a lot of work

I mostly use graphs to 'eyeball' how things are working. Tthere are well known performance bounds, such as Cramer-Rao, for these filters. I need to think through what I want re unit testing and turn it into a PR. FilterPy also probably needs supporting functions - if I can turn a Cramer-Rao into a stand alone function it should be part of the general library, for example.

There are better bounds than Cramer-Rao, but they can be a bear to compute. Section 2.7 of Challa covers this to some extent.

It occurs to me that I don't test things like immutability of things like self.F. What if I force an exception - are things in a standard state? Etc. All pameter inputs need to be tested. And so on.

Anyway, it is time to think through a solid testing plan, set some reasonable standards for what tests must be done for any functionality, and then execute on at least some of it.

This is important - this was designed largely as a pedagogical tool, but Ph.D students are using it in their research, some companies are using it for there work - there are real-world consequences to bugs.

Particle Filters

In the particle filter notebook - code block 10 i.e the update function, you're getting rid of the prior every time by filling the weights with 1. Is this on purpose or have I misunderstood something?

Covariance matrix not positive definite

For some reason, the covariance matrix in my system gets entries that are not positive definite.
Do you have any ideas on what may be causing this issue?

Thanks in advance!

EKF predict, parameter u not used

In
https://github.com/rlabbe/filterpy/blob/7c3a683104e07d42f73e9a739a07549389cd67bf/filterpy/kalman/EKF.py

the predict function takes a parameter u
it fails to pass this forward to the predict_x function.

Starting on line 161:

 def predict(self, u=0):
      """ Predict next position.
      **Parameters**
      u : np.array
      Optional control vector. If non-zero, it is multiplied by B
      to create the control input into the system.
      """
      self.predict_x()
      self._P = dot3(self._F, self._P, self._F.T) + self._Q

Should read:

 def predict(self, u=0):
      """ Predict next position.
      **Parameters**
      u : np.array
      Optional control vector. If non-zero, it is multiplied by B
      to create the control input into the system.
      """
      self.predict_x(u)
      self._P = dot3(self._F, self._P, self._F.T) + self._Q

The use of u has not come up in the book, yet (to my knowledge).
(But when it does, this could be a confusing bug).

I could submit a pull request for this, but it seems a bit much for a single character change.

2 D measurements, 3 D state

first, thanks $1M for such an incredible book and set of tools - I had despaired of using Kalman filtering until I discovered them. Second, I am a lapsed C programmer, last active 10 years ago, and have been recently learning Python so as to use this library. Sorry if my inexperience is the cause of my issue.

I am trying to implement the following KF: dim x = 3 (pos, vel, acc), dim z = 2 (pos, acc) - (PS- tried to attach file but it failed):

x dim 3- spot, vel and acc,

z dim = 2, spot and acc

f = KalmanFilter (dim_x=3, dim_z=2)
f.x = np.array([[1.58], # init position
[0.01], # init velocity
[0.0]]) # init acceleration
f.F = np.array([[1.,1., 0.5], # 1, delta T, delta T^2/2 timestep = 1
[0.,1., 1.], # 0, 1, delta T
[0.,0., 1.]]) # 0, 0, 1
f.H = np.array([[1.,0., 0.], # dim_z by dim_x, i.e. 2 x 3
[0.,0., 1.]]) # measure position, and acc, but not vel
f.P = np.array([[1.,0., 0.], # covariance dim_x dim_x, i.e. 3 x 3
[0.,1., 0.],
[0.,0., 1.]])
f.R = np.array([[1.,0.], # measurement noise dim_z dim_z, i.e 2 x 2
[0.,1.]])
f.B = 0 # no control inputs
f.Q = Q_discrete_white_noise(dim=3, dt=1, var=0.5)

my measurements are stored in a .csv file as follows:
pos1, acc1,
pos 2, acc2,
etc

it actually runs for one cycle, then gives a dimensional error. the first estimate is quite incorrect
Thanks for any guidance

Typo in discretization.py

Hello,

There is a typo in function Q_continuous_white_noise, line 82 of file discretization.py. The term (dt**4)/3 should instead read (dt**3)/3.

Problem while implementing Kalman Filter

Hi, I have created a dictionary of Kalman Filters. I'm having an issue in the update function.
AssertionError: shape of z should be (), but it is (1,)
I have a 1D Kalman Filter, here it is the declaration.

K = KalmanFilter (dim_x=1, dim_z=1)
#State space model used
K.F= np.array([1.])
 #Initial State
K.x=np.array([-60.])                          
#Measurement Matrix                         
K.H=np.array([1.])
#Covariance Matrix
K.P=np.array([10.])
#Process Noise          
K.Q=np.array([20.])                              
#Measurement Noise
K.R=np.array([0.002])
#Add KF to the dictionary when a new UUID is detected
KF_dict.update({data:K})

Hope you can help me, thanks.

off-by-one error for Fs/Qs index in kalman_filter.rts_smoother

The key update equations in the current implementation is

        P_pred = dot3(Fs[k], P[k], Fs[k].T) + Qs[k]

        K[k]  = dot3(P[k], Fs[k].T, linalg.inv(P_pred))
        x[k] += dot (K[k], x[k+1] - dot(Fs[k], x[k]))
        P[k] += dot3 (K[k], P[k+1] - P_pred, K[k].T)

I am using this in a situation where the Fs is time varying and I noticed that the smoother results is much worse than filtering. Eyeballing the results of the filter and smoother, it looks like an off-by-one problem in the Fs.

Comparing with the equations in Wikipedia https://en.wikipedia.org/wiki/Kalman_filter#Rauch.E2.80.93Tung.E2.80.93Striebel the correct implementation should use Fs[k+1] and Qs[k+1]

        P_pred = dot3(Fs[k+1], P[k], Fs[k+1].T) + Qs[k+1]

        K[k]  = dot3(P[k], Fs[k+1].T, linalg.inv(P_pred))
        x[k] += dot (K[k], x[k+1] - dot(Fs[k+1], x[k]))
        P[k] += dot3 (K[k], P[k+1] - P_pred, K[k].T)

To compute P_pred based on P[k] we should be using Fs[k+1] and Qs[k+1], similar for X_pred should be computed from x[k] using Fs[k+1].

Using UKF for sensor fusion

Hi,

I'd like to use filterpy's UKF for sensor fusion. However I have two different measurement sources with different sizes, how can I setup the filter to make this work?

Thanks,
Carlos

multivariate_gaussian incorrect if covariance not np.array

stats.multivariate_gaussian(x,mu,cov) silently gives the wrong value if cov is, e.g., a list of lists instead of a numpy array. The failure occurs during the call to _to_cov where the covariance should be checking whether x is a scalar, but is instead checking whether it is not an np.array.

This problem demonstrates itself in the 05-Multivariate-Gaussians chapter of your book, where putting in a non-diagonal covariance for the 3-d plot gives an orthogonal rather than slanted gaussian. Changing the covariance list-of-lists to an np.array fixes the problem.

Example:

import numpy as np
from filterpy import stats
from scipy.stats import multivariate_normal
np.set_printoptions(precision=3)
mean=(0,0)

cov=[[1,.5],[.5,1]]
print("For list and np.array covariances:")
for covariance in (cov,np.asarray(cov)):
    a = [[stats.multivariate_gaussian((i,j),mean,covariance)
          for i in (-1,0,1)]
         for j in (-1,0,1)]
    print(np.asarray(a))
    print()

Gives two different results for the datatypes of the covariance:

For list and np.array covariances:
[[ 0.059  0.097  0.059]
 [ 0.097  0.159  0.097]
 [ 0.059  0.097  0.059]]

[[ 0.094  0.094  0.025]
 [ 0.094  0.184  0.094]
 [ 0.025  0.094  0.094]]

conda install issue

I am on a Mac version 10.11.6 . This is the output of running your conda command:

Phil@MacBook-Pro~/anaconda/envs $ conda install -c rabbi filterpy
Fetching package metadata: ......
Solving package specifications: .
Error:  Package missing in current osx-64 channels: 
  - filterpy

You can search for this package on anaconda.org with

    anaconda search -t condo filterpy

How to use batch_filter, and rts_smoother for multiple sensors with different sampling rates

Thanks for this amazing library coupled with your very informative book. I am really grateful that you spent so much time making this great.

In you book you have an example of creating a filter that could handle measurements from two sensors with different sampling frequencies. The solution there was to create different R and H matrices depending on which sensor we have at a particular time.

My question is how we can do this but using the batch_filter, and rts_smoother functions? Is that already implemented in filterPy and I just missed that, or do you have a recommended way to implement this myself.

Thanks so much.

Best,

Brad

No example for use of EKF

Hi,

I wanted to use your EKF for a project of my own, but I cannot figure out how to set the variables and matrices, because there is no example provided.

Implement CDKF

The central difference KF is a nice, alternative implementation of a sigma point filter that doesn't get enough love. It really needs to be part of the library.

No posterior estimator for IMMEstimator?

Hi,
After carefully reading your source code for IMMEstimator class in filterpy.kalman, my understanding is that after running the update() function,
self.x = \hat{x_{k+1|k}}, that is, the prediction(prior estimate) for time k+1 at time t. This is also the case for each individual filter.
I wonder what if i want to get \hat{x_{k|k}}, i.e., the posterior estimate of x, as well as P, at time k ?

UKF and scaled UKF are not consistent with each other

Different methods are implemented (e.g. scaled does not implement batch_filter), different parameterizations, and so on. They should be made as consistent as possible.

Once that is done, the documentation should make it clear that the scaled version is the preferred implementation as it incorporates all of the most recent research on the algorithm. UnscentedKalmanFilter is mostly for pedagogical purposes.

AttributeError: 'int' object has no attribute 'T'

I'm trying to get started implementing a Kalman Filter using filterpy, using code from the readme:

kf = KalmanFilter(dim_x=2, dim_z=1)
kf.x = np.array([[x],[0.]])       # initial state (location and velocity)
kf.F = np.array([[1.,1.], [0.,1.]])    # state transition matrix
kf.H = np.array([[1.,0.]])    # Measurement function
kf.P *= 1000.                 # covariance matrix
kf.R = 5                      # state uncertainty
kf.Q = Q_discrete_white_noise(2, dt, .1) # process uncertainty

Then later

kf.predict()
kf.update(x)

Then I get this error when invoking predict:

Traceback (most recent call last):
  File "drone/test_predict.py", line 102, in <module>
    main()
  File "drone/test_predict.py", line 73, in main
    kf.predict()
  File "/Users/alex/.virtualenvs/ddc/lib/python2.7/site-packages/filterpy/kalman/kalman_filter.py", line 416, in predict
    self._P = self._alpha_sq * dot3(F, self._P, F.T) + Q
AttributeError: 'int' object has no attribute 'T'

UKF Different Updates

Is there an example of a filter taking in different measurements at different times? Does the library support this feature? It seems that the UKF in particular does not support this. It would improve the usefulness of the library.

UKF.batch_filter doesn't use all parameters

residual and UT are not used. This needs to be either expanded to include all options, or just do the default implementation - there is little value in this function anyway since you can just call predict and update yourself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.