kalman

Filename: kalman.py Reference: https://lectures.quantecon.org/py/kalman.html

Implements the Kalman filter for a linear Gaussian state space model.

class quantecon.kalman.Kalman(ss, x_hat=None, Sigma=None)[source]

Bases: object

Implements the Kalman filter for the Gaussian state space model

\[x_{t+1} = A x_t + C w_{t+1} y_t = G x_t + H v_t\]

Here \(x_t\) is the hidden state and \(y_t\) is the measurement. The shocks \(w_t\) and \(v_t\) are iid standard normals. Below we use the notation

\[Q := CC' R := HH'\]
Parameters:

ss : instance of LinearStateSpace

An instance of the quantecon.lss.LinearStateSpace class

x_hat : scalar(float) or array_like(float), optional(default=None)

An n x 1 array representing the mean x_hat and covariance matrix Sigma of the prior/predictive density. Set to zero if not supplied.

Sigma : scalar(float) or array_like(float), optional(default=None)

An n x n array representing the covariance matrix Sigma of the prior/predictive density. Must be positive definite. Set to the identity if not supplied.

References

https://lectures.quantecon.org/py/kalman.html

Attributes

Sigma, x_hat (as above)
Sigma_infinity (array_like or scalar(float)) The infinite limit of Sigma_t
K_infinity (array_like or scalar(float)) The stationary Kalman gain.

Methods

filtered_to_forecast() Updates the moments of the time t filtering distribution to the
prior_to_filtered(y) Updates the moments (x_hat, Sigma) of the time t prior to the time t filtering distribution, using current measurement \(y_t\).
set_state(x_hat, Sigma)
stationary_coefficients(j[, coeff_type]) Wold representation moving average or VAR coefficients for the steady state Kalman filter.
stationary_innovation_covar()
stationary_values() Computes the limit of \(\Sigma_t\) as t goes to infinity by solving the associated Riccati equation.
update(y) Updates x_hat and Sigma given k x 1 ndarray y.
whitener_lss() This function takes the linear state space system
filtered_to_forecast()[source]

Updates the moments of the time t filtering distribution to the moments of the predictive distribution, which becomes the time t+1 prior

prior_to_filtered(y)[source]

Updates the moments (x_hat, Sigma) of the time t prior to the time t filtering distribution, using current measurement \(y_t\).

The updates are according to

\[\hat{x}^F = \hat{x} + \Sigma G' (G \Sigma G' + R)^{-1} (y - G \hat{x}) \Sigma^F = \Sigma - \Sigma G' (G \Sigma G' + R)^{-1} G \Sigma\]
Parameters:

y : scalar or array_like(float)

The current measurement

set_state(x_hat, Sigma)[source]
stationary_coefficients(j, coeff_type='ma')[source]

Wold representation moving average or VAR coefficients for the steady state Kalman filter.

Parameters:

j : int

The lag length

coeff_type : string, either ‘ma’ or ‘var’ (default=’ma’)

The type of coefficent sequence to compute. Either ‘ma’ for moving average or ‘var’ for VAR.

stationary_innovation_covar()[source]
stationary_values()[source]

Computes the limit of \(\Sigma_t\) as t goes to infinity by solving the associated Riccati equation. Computation is via the doubling algorithm (see the documentation in matrix_eqn.solve_discrete_riccati).

Returns:

Sigma_infinity : array_like or scalar(float)

The infinite limit of \(\Sigma_t\)

K_infinity : array_like or scalar(float)

The stationary Kalman gain.

update(y)[source]

Updates x_hat and Sigma given k x 1 ndarray y. The full update, from one period to the next

Parameters:

y : np.ndarray

A k x 1 ndarray y representing the current measurement

whitener_lss()[source]

This function takes the linear state space system that is an input to the Kalman class and it converts that system to the time-invariant whitener represenation given by

\[\tilde{x}_{t+1}^* = \tilde{A} \tilde{x} + \tilde{C} v a = \tilde{G} \tilde{x}\]

where

\[\tilde{x}_t = [x+{t}, \hat{x}_{t}, v_{t}]\]

and

\[\begin{split}\tilde{A} = \begin{bmatrix} A & 0 & 0 \\ KG & A-KG & KH \\ 0 & 0 & 0 \\ \end{bmatrix}\end{split}\]
\[\begin{split}\tilde{C} = \begin{bmatrix} C & 0 \\ 0 & 0 \\ 0 & I \\ \end{bmatrix}\end{split}\]
\[\begin{split}\tilde{G} = \begin{bmatrix} G & -G & H \\ \end{bmatrix}\end{split}\]

with \(A, C, G, H\) coming from the linear state space system that defines the Kalman instance

Returns:

whitened_lss : LinearStateSpace

This is the linear state space system that represents the whitened system