(Fast) Stationary Kalman Filter
A simple and fast C++ implementation of the Kalman Filter for stationary data with time-invariant system matrices and missing data.
SKF(X, A, C, Q, R, F_0, P_0, loglik = FALSE)
Arguments
X
: numeric data matrix (Txn).
A
: transition matrix (rpxrp).
C
: observation matrix (nxrp).
Q
: state covariance (rpxrp).
R
: observation covariance (nxn).
F_0
: initial state vector (rpx1).
P_0
: initial state covariance (rpxrp).
loglik
: logical. Compute log-likelihood?
Returns
Predicted and filtered state vectors and covariances. - F
: Txrp filtered state vectors.
-
P
: rpxrpxT filtered state covariances.
-
F_pred
: Txrp predicted state vectors.
-
P_pred
: rpxrpxT predicted state covariances.
-
loglik
: value of the log likelihood.
Details
The underlying state space model is:
xt=CFt+et∼N(0,R)x(t)=CF(t)+e(t) N(0,R)
Ft=A Ft−1+ut∼N(0,Q)F(t)=AF(t−1)+u(t) N(0,Q)
where x(t) is X[t, ]
. The filter then first performs a time update (prediction)
Ft=A Ft−1F(t)=AF(t−1)
Pt=A Pt−1A′+QP(t)=AP(t−1)A′+Q
where P(t)=Cov(F(t)). This is followed by the measurement update (filtering)
Kt=PtC′(C PtC′+R)−1K(t)=P(t)C′inv(CP(t)C′+R)
Ft=Ft+Kt(xt−C Ft)F(t)=F(t)+K(t)(x(t)−CF(t))
Pt=Pt−KtC PtP(t)=P(t)−K(t)CP(t)
If a row of the data is all missing the measurement update is skipped i.e. the prediction becomes the filtered value. The log-likelihood is computed as
1/2t∑log(∣St∣)−et′Stet−nlog(2π)1/2sumt[log(det(S(t)))−e(t)′S(t)e(t)−nlog(2pi)]
where S(t)=inv(CP(t)C′+R) and e(t)=x(t)−CF(t) is the prediction error.
For further details see any textbook on time series such as Shumway & Stoffer (2017), which provide an analogous R implementation in astsa::Kfilter0
. For another fast (C-based) implementation that also allows time-varying system matrices and non-stationary data see FKF::fkf
.
Examples
# See ?SKFS
References
Shumway, R. H., & Stoffer, D. S. (2017). Time Series Analysis and Its Applications: With R Examples. Springer.
Harvey, A. C. (1990). Forecasting, structural time series models and the Kalman filter.
Hamilton, J. D. (1994). Time Series Analysis. Princeton university press.
See Also
FIS
SKFS