Welcome State space notation Functions > kalman.filter
Last update Jul 10, 2004

DESCRIPTION:

Kalman filter for Gaussian observations assuming linear Gaussian latent process. The specific model is
Observation equation:
Yt
=
FtTqt + nt,
nt
~
N(0,Vt)
System equation:
qt
=
Gtqt-1 + wt,
    wt
~
Np(0,Wt),
Initial prior:
q0
~
Np(m0,C0).

USAGE:

      kalman.filter(ssm)

REQUIRED ARGUMENTS:

ssm:
Object of class ssm (state space model).
The following attributes must be specified in ssm:
Yt:
1 × n vector containing the binomial observations.
Ft:
Function returning a p × 1 design vector.
Gt:
Function returning a p × p evolution transfer matrix.
Vt:
Function returning the observation variance.
Wt:
Function returning a p × 1 evolution variance matrix.
m0:
p × 1 vector (prior mean).
C0:
p × p matrix (prior variance).

OPTIONAL ARGUMENTS:

The following attributes of the ssm object might be needed:
Xt:
Matrix containing the covariates needed in Ft, Gt, Vt and/or Wt. This matrix must have n rows, such that row t contains the covariates needed for generating the system matrices at time t.
psi:
Parameter vector must be supplied if it is needed in the calculation of the system matrices.
The following attributes of the ssm object are optional:
fam:
Tacitly assumed to have value Gaussian.
link:
Tacitly assumed to have value identity.

VALUE:

Returns an object of class ssm with the same attributes as the object in the call, but with attribute filtered updated with:
mt:
Posterior means mt given the observations up to and including time t. These are arranged row-wise in a matrix.
Ct:
Posterior variances Ct given the observations up to and including time t. These are arranged in a 3-dimensional array such that Ct is placed in filtered$Ct[,,t].
llh:
Log-likelihood of of the approximating Gaussian state space model.
Rt:
Prior variances for the states arranged in 3-dimensional array such that Rt is placed in filtered$Rt[,,t]. The reason for returning these is that they are needed in the Kalman smoother.

SIDE EFFECTS:

None

DETAILS:

This is the standard Kalman filter.

REFERENCES:

Durbin & Koopman, (2002), Time Series Analysis by State Space Models, Oxford Statistical Science Series.
Fahrmeir & Tutz, (1994), Multivariate Statistical Modelling Based on Generalized Linear Models, Springer Series in Statistics.
Klein, (2003),State Space Models for Exponential Family Data, Ph.D. Thesis, Department of Statistics, University of Southern Denmark.
West & Harrison, (1997), Bayesian Forecasting and Dynamic Models, Springer Series in Statistics.

EXAMPLES:

# local level state space model
ss <- ssm(
          Ft = function(i, x, phi) {matrix(1,1,1)},
          Gt = function(i, x, phi) {matrix(1,1,1)},
          Vt = function(i, x, phi) {matrix(80,1,1)},
          Wt = function(i, x, phi) {matrix(10,1,1)}, 
          m0 = c(20),
          C0 = matrix(50,1,1))

# simulate latent process and observations
ss <- simulate.ssm(ss)

# set prior
ss$m0 <- c(0)

# apply Kalman filter
ss <- kalman.filter(ss)

# plot like Figure 1.5, upper panel
par(mfrow=c(2,1))
plot(ss$filtered$mt, ylim=c(-30,60), type="l", lty=3)
lines(ss$filtered$mt+2*sqrt(ss$filtered$Ct[1,1,]),lty=2)
lines(ss$filtered$mt-2*sqrt(ss$filtered$Ct[1,1,]),lty=2)
lines(ss$simulated$theta,lty=1)

# Exchange evolution and observation variances
ss$Vt <- function(i, x, phi) matrix(10,1,1)
ss$Wt <- function(i, x, phi) matrix(80,1,1)

# Apply Kalman filter again
ss <- kalman.filter(ss)

# plot like Figure 1.5, lower panel
plot(ss$filtered$mt, ylim=c(-30,60), type="l", lty=3)
lines(ss$filtered$mt+2*sqrt(ss$filtered$Ct[1,1,]),lty=2)
lines(ss$filtered$mt-2*sqrt(ss$filtered$Ct[1,1,]),lty=2)
lines(ss$simulated$theta,lty=1)