Kfilter {astsa} | R Documentation |
Quick Kalman Filter
Description
Returns both the predicted and filtered values for various linear state space models;
it also evaluates the likelihood at the given parameter values.
This script replaces Kfilter0
, Kfilter1
, and Kfilter2
Usage
Kfilter(y, A, mu0, Sigma0, Phi, sQ, sR, Ups = NULL, Gam = NULL,
input = NULL, S = NULL, version = 1)
Arguments
y |
data matrix (n |
A |
can be constant or an array with dimension |
mu0 |
initial state mean vector (p |
Sigma0 |
initial state covariance matrix (p |
Phi |
state transition matrix (p |
sQ |
state error pre-matrix (see details) |
sR |
observation error pre-matrix (see details) |
Ups |
state input matrix (p |
Gam |
observation input matrix (q |
input |
NULL (default) if not needed or a
matrix (n |
S |
covariance matrix between the (not premultiplied) state and observation errors; not necessary to specify if not needed and only used if version=2. See details for more information. |
version |
either 1 (default) or 2; version 2 allows for correlated errors |
Details
This script replaces Kfilter0
, Kfilter1
, and Kfilter2
by combining all
cases. The major difference is how to specify the covariance matrices; in particular,
sQ = t(cQ)
and sR = t(cR)
where cQ
and cR
were used in Kfilter0-1-2
scripts.
The states are p-dimensional, the data
are q-dimensional, and
the inputs
are r-dimensional for
. The initial state is
.
The measurement matrices can be constant or time varying. If time varying, they should be entered as an array of dimension
dim = c(q,p,n)
. Otherwise, just enter the constant value making sure it has the appropriate dimension.
Version 1 (default): The general model is
where . Consequently the state noise covariance matrix is
and the observation noise covariance matrix is
and
do not have to be square as long as everything is
conformable. Notice the specification of the state and observation covariances has changed from the original scripts.
NOTE: If it is easier to model in terms of and
, simply input the square root matrices
sQ = Q %^% .5
and sR = R %^% .5
.
Version 2 (correlated errors): The general model is
where , and NOT
.
NOTE: If it is easier to model in terms of and
, simply input the square root matrices
sQ = Q %^% .5
and sR = R %^% .5
.
Note that in either version, has to be p-dimensional, but
does not, and
has to be q-dimensional, but
does not.
Value
Time varying values are returned as arrays.
Xp |
one-step-ahead prediction of the state |
Pp |
mean square prediction error |
Xf |
filter value of the state |
Pf |
mean square filter error |
like |
the negative of the log likelihood |
innov |
innovation series |
sig |
innovation covariances |
Kn |
last value of the gain, needed for smoothing |
Note
Note that Kfilter
is similar to Kfilter-0-1-2
except that only the essential values need to be entered (and come first in the statement); the optional values such as
input
are set to NULL
by default if they are not needed. This version is faster
than the older versions. The biggest change was to how the covarainces are specified. For example, if you have code that used Kfilter1
, just use sQ = t(cQ)
and sR = t(cR)
here.
NOTE: If it is easier to model in terms of and
, simply input the square root matrices
sQ = Q%^%.5
and sR = R%^%.5
.
Author(s)
D.S. Stoffer
References
You can find demonstrations of astsa capabilities at FUN WITH ASTSA.
The most recent version of the package can be found at https://github.com/nickpoison/astsa/.
In addition, the News and ChangeLog files are at https://github.com/nickpoison/astsa/blob/master/NEWS.md.
The webpages for the texts and some help on using R for time series analysis can be found at https://nickpoison.github.io/.
See Also
Examples
# generate some data
set.seed(1)
sQ = 1; sR = 3; n = 100
mu0 = 0; Sigma0 = 10; x0 = rnorm(1,mu0,Sigma0)
w = rnorm(n); v = rnorm(n)
x = c(x0 + sQ*w[1]); y = c(x[1] + sR*v[1]) # initialize
for (t in 2:n){
x[t] = x[t-1] + sQ*w[t]
y[t] = x[t] + sR*v[t]
}
# run and plot the filter
run = Kfilter(y, A=1, mu0, Sigma0, Phi=1, sQ, sR)
tsplot(cbind(y,run$Xf), spaghetti=TRUE, type='o', col=c(4,6), pch=c(1,NA), margins=1)
# CRAN tests need extra white space :( so margins=1 above is not necessary otherwise
legend('topleft', legend=c("y(t)","Xf(t)"), lty=1, col=c(4,6), bty="n", pch=c(1,NA))