How to run pykalman Kalman Filter on a single observation? (python)

5.3k views Asked by At

I can run the simple pykalman Kalman Filter example given in the pykalman documentation:

import pykalman
import numpy as np
kf = pykalman.KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
measurements = np.asarray([[1,0], [0,0], [0,1]])  # 3 observations
(filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
print filtered_state_means

This correctly returns the state estimates (one for each observation):

[[ 0.07285974  0.39708561]
 [ 0.30309693  0.2328318 ]
 [-0.5533711  -0.0415223 ]]

However, if I provide only a single observation, the code fails:

import pykalman
import numpy as np
kf = pykalman.KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
measurements = np.asarray([[1,0]])  # 1 observation
(filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
print filtered_state_means

with the following error:

ValueError: could not broadcast input array from shape (2,2) into shape (2,1)

How can I use pykalman to update an initial state and initial covariance using just a single observation?

2

There are 2 answers

0
Avery Sturzl On BEST ANSWER

From the documentation at: http://pykalman.github.io/#kalmanfilter

filter_update(filtered_state_mean, filtered_state_covariance, observation=None, transition_matrix=None, transition_offset=None, transition_covariance=None, observation_matrix=None, observation_offset=None, observation_covariance=None)

This takes in the filtered_state_mean and filtered_state_covariance at time t, and an observation at t+1, and returns the state mean and state covariance at t+1 (to be used for the next update)

0
Amar On

If I understand Kalman filter algorithm correctly, you can predict the state using just one observation. But, the gain and the covariance would be way off and the prediction would be nowhere close to the actual state. You need to give a Kalman filter a few observations as a training set to reach a steady state