statsmodels.tsa.statespace.kalman_filter.KalmanFilter¶
- class statsmodels.tsa.statespace.kalman_filter.KalmanFilter(k_endog, k_states, k_posdef=None, loglikelihood_burn=0, tolerance=1e-19, results_class=None, kalman_filter_classes=None, **kwargs)[source]¶
State space representation of a time series process, with Kalman filter
- Parameters:
- k_endog{array_like,
int
} The observed time-series process \(y\) if array like or the number of variables in the process if an integer.
- k_states
int
The dimension of the unobserved state process.
- k_posdef
int
,optional
The dimension of a guaranteed positive definite covariance matrix describing the shocks in the transition equation. Must be less than or equal to k_states. Default is k_states.
- loglikelihood_burn
int
,optional
The number of initial periods during which the loglikelihood is not recorded. Default is 0.
- tolerance
float
,optional
The tolerance at which the Kalman filter determines convergence to steady-state. Default is 1e-19.
- results_class
class
,optional
Default results class to use to save filtering output. Default is FilterResults. If specified, class must extend from FilterResults.
- **kwargs
Keyword arguments may be used to provide values for the filter, inversion, and stability methods. See set_filter_method, set_inversion_method, and set_stability_method. Keyword arguments may be used to provide default values for state space matrices. See Representation for more details.
- k_endog{array_like,
Notes
There are several types of options available for controlling the Kalman filter operation. All options are internally held as bitmasks, but can be manipulated by setting class attributes, which act like boolean flags. For more information, see the set_* class method documentation. The options are:
- filter_method
The filtering method controls aspects of which Kalman filtering approach will be used.
- inversion_method
The Kalman filter may contain one matrix inversion: that of the forecast error covariance matrix. The inversion method controls how and if that inverse is performed.
- stability_method
The Kalman filter is a recursive algorithm that may in some cases suffer issues with numerical stability. The stability method controls what, if any, measures are taken to promote stability.
- conserve_memory
By default, the Kalman filter computes a number of intermediate matrices at each iteration. The memory conservation options control which of those matrices are stored.
- filter_timing
By default, the Kalman filter follows Durbin and Koopman, 2012, in initializing the filter with predicted values. Kim and Nelson, 1999, instead initialize the filter with filtered values, which is essentially just a different timing convention.
The filter_method and inversion_method options intentionally allow the possibility that multiple methods will be indicated. In the case that multiple methods are selected, the underlying Kalman filter will attempt to select the optional method given the input data.
For example, it may be that INVERT_UNIVARIATE and SOLVE_CHOLESKY are indicated (this is in fact the default case). In this case, if the endogenous vector is 1-dimensional (k_endog = 1), then INVERT_UNIVARIATE is used and inversion reduces to simple division, and if it has a larger dimension, the Cholesky decomposition along with linear solving (rather than explicit matrix inversion) is used. If only SOLVE_CHOLESKY had been set, then the Cholesky decomposition method would always be used, even in the case of 1-dimensional data.
- Attributes:
- design
dtype
(dtype) Datatype of currently active representation matrices
- endog
memory_no_filtered
(bool) Flag to prevent storing filtered state and covariance matrices.
memory_no_forecast
(bool) Flag to prevent storing all forecast-related output.
memory_no_predicted
(bool) Flag to prevent storing predicted state and covariance matrices.
obs
(array) Observation vector: \(y~(k\_endog \times nobs)\)
- obs_cov
- obs_intercept
prefix
(str) BLAS prefix of currently active representation matrices
- selection
- state_cov
- state_intercept
time_invariant
(bool) Whether or not currently active representation matrices are
- transition
Methods
bind
(endog)Bind data to the statespace representation
clone
(endog, **kwargs)Clone a state space representation while overriding some elements
diff_endog
(new_endog[, tolerance])extend
(endog[, start, end])Extend the current state space model, or a specific (time) subset
filter
([filter_method, inversion_method, ...])Apply the Kalman filter to the statespace model.
fixed_scale
(scale)Context manager for fixing the scale when FILTER_CONCENTRATED is set
impulse_responses
([steps, impulse, ...])Impulse response function
initialize
(initialization[, ...])Create an Initialization object if necessary
initialize_approximate_diffuse
([variance])Initialize the statespace model with approximate diffuse values.
Initialize the statespace model as stationary.
initialize_known
(constant, stationary_cov)Initialize the statespace model with known distribution for initial state.
Initialize the statespace model as stationary.
loglike
(**kwargs)Calculate the loglikelihood associated with the statespace model.
loglikeobs
(**kwargs)Calculate the loglikelihood for each observation associated with the statespace model.
set_conserve_memory
([conserve_memory])Set the memory conservation method
set_filter_method
([filter_method])Set the filtering method
set_filter_timing
([alternate_timing])Set the filter timing convention
set_inversion_method
([inversion_method])Set the inversion method
set_stability_method
([stability_method])Set the numerical stability method
simulate
(nsimulations[, measurement_shocks, ...])Simulate a new time series following the state space model
Properties
(int) Memory conservation bitmask.
(array) Design matrix: \(Z~(k\_endog \times k\_states \times nobs)\)
(dtype) Datatype of currently active representation matrices
(array) The observation vector, alias for obs.
(bool) Flag for augmented Kalman filtering.
(bool) Flag for filtering with Chandrasekhar recursions.
(bool) Flag for Kalman filtering with collapsed observation vector.
(bool) Flag for Kalman filtering with concentrated log-likelihood.
(bool) Flag for conventional Kalman filtering.
(bool) Flag for exact initial Kalman filtering.
(bool) Flag for extended Kalman filtering.
(int) Filtering method bitmask.
(bool) Flag for square-root Kalman filtering.
(int) Filter timing.
(bool) Flag for univariate filtering of multivariate observation vector.
(bool) Flag for unscented Kalman filtering.
(int) Inversion method bitmask.
(bool) Flag for Cholesky inversion method.
(bool) Flag for LU inversion method.
(bool) Flag for univariate inversion method (recommended).
(bool) Flag to conserve the maximum amount of memory.
(bool) Flag to prevent storing filtered state and covariance matrices.
(bool) Flag to prevent storing filtered state covariance matrices.
(bool) Flag to prevent storing filtered states.
(bool) Flag to prevent storing all forecast-related output.
(bool) Flag to prevent storing forecast error covariance matrices.
(bool) Flag to prevent storing forecasts and forecast errors.
(bool) Flag to prevent storing the Kalman gain matrices.
(bool) Flag to prevent storing likelihood values for each observation.
(bool) Flag to prevent storing predicted state and covariance matrices.
(bool) Flag to prevent storing predicted state covariance matrices.
(bool) Flag to prevent storing predicted states.
(bool) Flag to prevent storing likelihood values for each observation.
(bool) Flag to prevent storing standardized forecast errors.
(bool) Flag for storing all intermediate results in memory (default).
(array) Observation vector: \(y~(k\_endog \times nobs)\)
(array) Observation covariance matrix: \(H~(k\_endog \times k\_endog \times nobs)\)
(array) Observation intercept: \(d~(k\_endog \times nobs)\)
(str) BLAS prefix of currently active representation matrices
(array) Selection matrix: \(R~(k\_states \times k\_posdef \times nobs)\)
(bool) Flag for Cholesky and linear solver inversion method (recommended).
(bool) Flag for LU and linear solver inversion method.
(bool) Flag for enforcing covariance matrix symmetry
(int) Stability method bitmask.
(array) State covariance matrix: \(Q~(k\_posdef \times k\_posdef \times nobs)\)
(array) State intercept: \(c~(k\_states \times nobs)\)
(bool) Whether or not currently active representation matrices are time-invariant
(bool) Flag for the alternate timing convention (Kim and Nelson, 2012).
(bool) Flag for the default timing convention (Durbin and Koopman, 2012).
(array) Transition matrix: \(T~(k\_states \times k\_states \times nobs)\)