Wednesday, March 24, 2010

Kalman Filter in Python

The attached Kalman filter code is based on Python example found in book Machine Learning: An Algorithmic Perspective by Stephen Marsland. We simply turned the code into a class which is able to keep its state across invocations, and therefore work in an online fashion. The example code was assuming all data is available before everything started, and ran everything in one mainline.

7 comments:

  1. Hi.. i was looking at the kalman filter code.. I was looking to use kalman filters to predict the next data point from a dataset, given n data points.. Is it possible to extend your code to do that?

    ReplyDelete
  2. Sure it is possible. Train the filter with n points, than "push" the filter to n+1 using the transition equations, one for hidden state x at n+1, the other for observation y at n+1.

    ReplyDelete
    Replies
    1. Thanks! ill give it a try!

      Delete
    2. I'm getting some errors while doin this.. So i train it by initializing mu to my n-point array right?

      And then how do I push my filter to n+1?

      Delete
  3. The __main__ does it already. k.update trains with each data point. For pushing to n+1, look at the two main eqns of kalman filtering, one is time transition (for n+1) the other is observation (y_{n+1}). Once you have a \mu and a covariance, just apply the transition on x_n getting you a x_{n+1}

    ReplyDelete
  4. merhaba ,
    kalman filtreyi python'dak bir yazılıma adapte etmeye çalışıyorum ama başarılı olamadım.
    accx.plot( pos=(t, words[3]) )
    accy.plot( pos=(t, words[4]) )
    accz.plot( pos=(t, words[5]) )şeklinde 3 ayrı eksen için ivme bilgilerini çizdiriyorum. Altına birde bunların kalman filtresinden geçmiş şeklini çizdirmem gerekiyor fakat uyguladıklarım olmadı.
    Yardımcı olabilir misiniz ? oguzgora@yahoo.com

    ReplyDelete
    Replies
    1. Suradaki pdf belgeye de bir bakin

      https://github.com/burakbayramli/classnotes/tree/master/app-math-tr/filter

      her veri noktasini filtreye teker teker update ile gecmek lazim. kalman.py icinde bir __main__ var, orada ornegi gosteriliyor.

      Delete