4

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?

Jim G.
  • 15,141
  • 22
  • 103
  • 166
chutney
  • 157
  • 3
  • 9
  • I suspect there's some special case in the code to detect a single input vs an array of inputs and your code is not formatting the single input quite right. Since no one has answered, the only solution is probably to read the source and see what it's doing. – Ben Jackson Nov 24 '14 at 05:43

2 Answers2

3

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)

Avery Sturzl
  • 82
  • 1
  • 13
1

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

Amar
  • 11
  • 1