Implements the Kalman Filter, Kalman Smoother, and EM algorithm.
This class implements the Kalman Filter, Kalman Smoother, and EM Algorithm for a Linear Gaussian model specified by,
x_{t+1} &= A_{t} x_{t} + b_{t} + \text{Normal}(0, Q_{t}) \\ z_{t} &= C_{t} x_{t} + d_{t} + \text{Normal}(0, R_{t})
The Kalman Filter is an algorithm designed to estimate P(x_t | z_{0:t}). As all state transitions and observations are linear with Gaussian distributed noise, these distributions can be represented exactly as Gaussian distributions with mean filtered_state_means[t] and covariances filtered_state_covariances[t].
Similarly, the Kalman Smoother is an algorithm designed to estimate P(x_t | z_{0:T-1}).
The EM algorithm aims to find for \theta = (A, b, C, d, Q, R, \mu_0, \Sigma_0)
\max_{\theta} P(z_{0:T-1}; \theta)
If we define L(x_{0:T-1},\theta) = \log P(z_{0:T-1}, x_{0:T-1}; \theta), then the EM algorithm works by iteratively finding,
P(x_{0:T-1} | z_{0:T-1}, \theta_i)
then by maximizing,
\theta_{i+1} = \arg\max_{\theta} \mathbb{E}_{x_{0:T-1}} [ L(x_{0:T-1}, \theta)| z_{0:T-1}, \theta_i ]
Parameters: | transition_matrices : [n_timesteps-1, n_dim_state, n_dim_state] or [n_dim_state,n_dim_state] array-like
observation_matrices : [n_timesteps, n_dim_obs, n_dim_state] or [n_dim_obs, n_dim_state] array-like
transition_covariance : [n_dim_state, n_dim_state] array-like
observation_covariance : [n_dim_obs, n_dim_obs] array-like
transition_offsets : [n_timesteps-1, n_dim_state] or [n_dim_state] array-like
observation_offsets : [n_timesteps, n_dim_obs] or [n_dim_obs] array-like
initial_state_mean : [n_dim_state] array-like
initial_state_covariance : [n_dim_state, n_dim_state] array-like
random_state : optional, numpy random state
em_vars : optional, subset of [‘transition_matrices’, ‘observation_matrices’, ‘transition_offsets’, ‘observation_offsets’, ‘transition_covariance’, ‘observation_covariance’, ‘initial_state_mean’, ‘initial_state_covariance’] or ‘all’
n_dim_state: optional, integer
n_dim_obs: optional, integer
|
---|
Methods
em(X[, y, n_iter, em_vars]) | Apply the EM algorithm |
filter(X) | Apply the Kalman Filter |
filter_update(filtered_state_mean, ...[, ...]) | Update a Kalman Filter state estimate |
loglikelihood(X) | Calculate the log likelihood of all observations |
sample(n_timesteps[, initial_state, ...]) | Sample a state sequence n_{\text{timesteps}} timesteps in length. |
smooth(X) | Apply the Kalman Smoother |
Apply the EM algorithm
Apply the EM algorithm to estimate all parameters specified by em_vars. Note that all variables estimated are assumed to be constant for all time. See _em() for details.
Parameters: | X : [n_timesteps, n_dim_obs] array-like
n_iter : int, optional
em_vars : iterable of strings or ‘all’
|
---|
Apply the Kalman Filter
Apply the Kalman Filter to estimate the hidden state at time t for t = [0...n_{\text{timesteps}}-1] given observations up to and including time t. Observations are assumed to correspond to times [0...n_{\text{timesteps}}-1]. The output of this method corresponding to time n_{\text{timesteps}}-1 can be used in KalmanFilter.filter_update() for online updating.
Parameters: | X : [n_timesteps, n_dim_obs] array-like
|
---|---|
Returns: | filtered_state_means : [n_timesteps, n_dim_state]
filtered_state_covariances : [n_timesteps, n_dim_state, n_dim_state] array
|
Update a Kalman Filter state estimate
Perform a one-step update to estimate the state at time t+1 give an observation at time t+1 and the previous estimate for time t given observations from times [0...t]. This method is useful if one wants to track an object with streaming observations.
Parameters: | filtered_state_mean : [n_dim_state] array
filtered_state_covariance : [n_dim_state, n_dim_state] array
observation : [n_dim_obs] array or None
transition_matrix : optional, [n_dim_state, n_dim_state] array
transition_offset : optional, [n_dim_state] array
transition_covariance : optional, [n_dim_state, n_dim_state] array
observation_matrix : optional, [n_dim_obs, n_dim_state] array
observation_offset : optional, [n_dim_obs] array
observation_covariance : optional, [n_dim_obs, n_dim_obs] array
|
---|---|
Returns: | next_filtered_state_mean : [n_dim_state] array
next_filtered_state_covariance : [n_dim_state, n_dim_state] array
|
Calculate the log likelihood of all observations
Parameters: | X : [n_timesteps, n_dim_obs] array
|
---|---|
Returns: | likelihood : float
|
Sample a state sequence n_{\text{timesteps}} timesteps in length.
Parameters: | n_timesteps : int
|
---|---|
Returns: | states : [n_timesteps, n_dim_state] array
observations : [n_timesteps, n_dim_obs] array
|
Apply the Kalman Smoother
Apply the Kalman Smoother to estimate the hidden state at time t for t = [0...n_{\text{timesteps}}-1] given all observations. See _smooth() for more complex output
Parameters: | X : [n_timesteps, n_dim_obs] array-like
|
---|---|
Returns: | smoothed_state_means : [n_timesteps, n_dim_state]
smoothed_state_covariances : [n_timesteps, n_dim_state]
|
Kalman Filter based on Cholesky decomposition
Parameters: | transition_matrices : [n_timesteps-1, n_dim_state, n_dim_state] or [n_dim_state,n_dim_state] array-like
observation_matrices : [n_timesteps, n_dim_obs, n_dim_obs] or [n_dim_obs, n_dim_obs] array-like
transition_covariance : [n_dim_state, n_dim_state] array-like
observation_covariance : [n_dim_obs, n_dim_obs] array-like
transition_offsets : [n_timesteps-1, n_dim_state] or [n_dim_state] array-like
observation_offsets : [n_timesteps, n_dim_obs] or [n_dim_obs] array-like
initial_state_mean : [n_dim_state] array-like
initial_state_covariance : [n_dim_state, n_dim_state] array-like
random_state : optional, numpy random state
em_vars : optional, subset of [‘transition_matrices’, ‘observation_matrices’, ‘transition_offsets’, ‘observation_offsets’, ‘transition_covariance’, ‘observation_covariance’, ‘initial_state_mean’, ‘initial_state_covariance’] or ‘all’
n_dim_state: optional, integer
n_dim_obs: optional, integer
|
---|
Methods
em | |
filter | |
filter_update | |
loglikelihood | |
sample | |
smooth |
Kalman Filter based on UDU’ decomposition
Parameters: | transition_matrices : [n_timesteps-1, n_dim_state, n_dim_state] or [n_dim_state,n_dim_state] array-like
observation_matrices : [n_timesteps, n_dim_obs, n_dim_obs] or [n_dim_obs, n_dim_obs] array-like
transition_covariance : [n_dim_state, n_dim_state] array-like
observation_covariance : [n_dim_obs, n_dim_obs] array-like
transition_offsets : [n_timesteps-1, n_dim_state] or [n_dim_state] array-like
observation_offsets : [n_timesteps, n_dim_obs] or [n_dim_obs] array-like
initial_state_mean : [n_dim_state] array-like
initial_state_covariance : [n_dim_state, n_dim_state] array-like
random_state : optional, numpy random state
em_vars : optional, subset of [‘transition_matrices’, ‘observation_matrices’, ‘transition_offsets’, ‘observation_offsets’, ‘transition_covariance’, ‘observation_covariance’, ‘initial_state_mean’, ‘initial_state_covariance’] or ‘all’
n_dim_state: optional, integer
n_dim_obs: optional, integer
|
---|
Methods
em | |
filter | |
filter_update | |
loglikelihood | |
sample | |
smooth |
Implements the General (aka Augmented) Unscented Kalman Filter governed by the following equations,
x_0 &\sim \text{Normal}(\mu_0, \Sigma_0) \\ x_{t+1} &= f_t(x_t, \text{Normal}(0, Q)) \\ z_{t} &= g_t(x_t, \text{Normal}(0, R))
Notice that although the input noise to the state transition equation and the observation equation are both normally distributed, any non-linear transformation may be applied afterwards. This allows for greater generality, but at the expense of computational complexity. The complexity of UnscentedKalmanFilter.filter() is O(T(2n+m)^3) where T is the number of time steps, n is the size of the state space, and m is the size of the observation space.
If your noise is simply additive, consider using the AdditiveUnscentedKalmanFilter
Parameters: | transition_functions : function or [n_timesteps-1] array of functions
observation_functions : function or [n_timesteps] array of functions
transition_covariance : [n_dim_state, n_dim_state] array
observation_covariance : [n_dim_obs, n_dim_obs] array
initial_state_mean : [n_dim_state] array
initial_state_covariance : [n_dim_state, n_dim_state] array
n_dim_state: optional, integer
n_dim_obs: optional, integer
random_state : optional, int or RandomState
|
---|
Methods
filter(Z) | Run Unscented Kalman Filter |
filter_update(filtered_state_mean, ...[, ...]) | Update a Kalman Filter state estimate |
sample(n_timesteps[, initial_state, ...]) | Sample from model defined by the Unscented Kalman Filter |
smooth(Z) | Run Unscented Kalman Smoother |
Run Unscented Kalman Filter
Parameters: | Z : [n_timesteps, n_dim_state] array
|
---|---|
Returns: | filtered_state_means : [n_timesteps, n_dim_state] array
filtered_state_covariances : [n_timesteps, n_dim_state, n_dim_state] array
|
Update a Kalman Filter state estimate
Perform a one-step update to estimate the state at time t+1 give an observation at time t+1 and the previous estimate for time t given observations from times [0...t]. This method is useful if one wants to track an object with streaming observations.
Parameters: | filtered_state_mean : [n_dim_state] array
filtered_state_covariance : [n_dim_state, n_dim_state] array
observation : [n_dim_obs] array or None
transition_function : optional, function
transition_covariance : optional, [n_dim_state, n_dim_state] array
observation_function : optional, function
observation_covariance : optional, [n_dim_obs, n_dim_obs] array
|
---|---|
Returns: | next_filtered_state_mean : [n_dim_state] array
next_filtered_state_covariance : [n_dim_state, n_dim_state] array
|
Sample from model defined by the Unscented Kalman Filter
Parameters: | n_timesteps : int
initial_state : optional, [n_dim_state] array
random_state : optional, int or Random
|
---|
Run Unscented Kalman Smoother
Parameters: | Z : [n_timesteps, n_dim_state] array
|
---|---|
Returns: | smoothed_state_means : [n_timesteps, n_dim_state] array
smoothed_state_covariances : [n_timesteps, n_dim_state, n_dim_state] array
|
Implements the Unscented Kalman Filter with additive noise. Observations are assumed to be generated from the following process,
x_0 &\sim \text{Normal}(\mu_0, \Sigma_0) \\ x_{t+1} &= f_t(x_t) + \text{Normal}(0, Q) \\ z_{t} &= g_t(x_t) + \text{Normal}(0, R)
While less general the general-noise Unscented Kalman Filter, the Additive version is more computationally efficient with complexity O(Tn^3) where T is the number of time steps and n is the size of the state space.
Parameters: | transition_functions : function or [n_timesteps-1] array of functions
observation_functions : function or [n_timesteps] array of functions
transition_covariance : [n_dim_state, n_dim_state] array
observation_covariance : [n_dim_obs, n_dim_obs] array
initial_state_mean : [n_dim_state] array
initial_state_covariance : [n_dim_state, n_dim_state] array
n_dim_state: optional, integer
n_dim_obs: optional, integer
random_state : optional, int or RandomState
|
---|
Methods
filter(Z) | Run Unscented Kalman Filter |
filter_update(filtered_state_mean, ...[, ...]) | Update a Kalman Filter state estimate |
sample(n_timesteps[, initial_state, ...]) | Sample from model defined by the Unscented Kalman Filter |
smooth(Z) | Run Unscented Kalman Smoother |
Run Unscented Kalman Filter
Parameters: | Z : [n_timesteps, n_dim_state] array
|
---|---|
Returns: | filtered_state_means : [n_timesteps, n_dim_state] array
filtered_state_covariances : [n_timesteps, n_dim_state, n_dim_state] array
|
Update a Kalman Filter state estimate
Perform a one-step update to estimate the state at time t+1 give an observation at time t+1 and the previous estimate for time t given observations from times [0...t]. This method is useful if one wants to track an object with streaming observations.
Parameters: | filtered_state_mean : [n_dim_state] array
filtered_state_covariance : [n_dim_state, n_dim_state] array
observation : [n_dim_obs] array or None
transition_function : optional, function
transition_covariance : optional, [n_dim_state, n_dim_state] array
observation_function : optional, function
observation_covariance : optional, [n_dim_obs, n_dim_obs] array
|
---|---|
Returns: | next_filtered_state_mean : [n_dim_state] array
next_filtered_state_covariance : [n_dim_state, n_dim_state] array
|
Sample from model defined by the Unscented Kalman Filter
Parameters: | n_timesteps : int
initial_state : optional, [n_dim_state] array
|
---|
Run Unscented Kalman Smoother
Parameters: | Z : [n_timesteps, n_dim_state] array
|
---|---|
Returns: | smoothed_state_means : [n_timesteps, n_dim_state] array
smoothed_state_covariances : [n_timesteps, n_dim_state, n_dim_state] array
|
Implements the Unscented Kalman Filter with additive noise. Observations are assumed to be generated from the following process,
x_0 &\sim \text{Normal}(\mu_0, \Sigma_0) \\ x_{t+1} &= f_t(x_t) + \text{Normal}(0, Q) \\ z_{t} &= g_t(x_t) + \text{Normal}(0, R)
While less general the general-noise Unscented Kalman Filter, the Additive version is more computationally efficient with complexity O(Tn^3) where T is the number of time steps and n is the size of the state space.
Parameters: | transition_functions : function or [n_timesteps-1] array of functions
observation_functions : function or [n_timesteps] array of functions
transition_covariance : [n_dim_state, n_dim_state] array
observation_covariance : [n_dim_obs, n_dim_obs] array
initial_state_mean : [n_dim_state] array
initial_state_covariance : [n_dim_state, n_dim_state] array
n_dim_state: optional, integer
n_dim_obs: optional, integer
random_state : optional, int or RandomState
|
---|
Methods
filter(Z) | Run Unscented Kalman Filter |
filter_update(filtered_state_mean, ...[, ...]) | Update a Kalman Filter state estimate |
sample(n_timesteps[, initial_state, ...]) | Sample from model defined by the Unscented Kalman Filter |
smooth(Z) | Run Unscented Kalman Smoother |