chutney
chutney

Reputation: 167

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

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?

Upvotes: 4

Views: 5646

Answers (2)

Amar
Amar

Reputation: 11

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

Upvotes: 1

Avery Sturzl
Avery Sturzl

Reputation: 92

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)

Upvotes: 3

Related Questions