fks: Fast Kalman Smoother

fksR Documentation

Fast Kalman Smoother

Description

This function can be run after running fkf to produce "smoothed" estimates of the state variable alpha(t). Unlike the output of the filter, these estimates are conditional on the entire set of n data points rather than only the past, see details.

Usage

fks(FKFobj)

Arguments

FKFobj

An S3-object of class "fkf", returned by fkf.

Details

The following notation is taken from the fkf function descriptions and is close to the one of Koopman et al. The smoother estimates

a(t|n)=E[alpha(t)|y(1),...,y(n)]

P(t|n)=Var[alpha(t)|y(1),...,y(n)]

based on the outputs of the forward filtering pass performed by fkf.

The formulation of Koopman and Durbin is used which evolves the two values r(t) in R^m and N(t) in R^{m x m} to avoid inverting the covariance matrix.

Iteration:

If there are no missing values the iteration proceeds as follows:

Initialisation: Set t=n, with r(t)=0 and N(t)=0.

Evolution equations:

L = T(t) - T(t)K(t)Z(t)

r(t-1) = Z(t)' F(t)^{-1} v(t) + L'r(t)

N(t-1) = Z(t)' F(t)^{-1} Z(t) + L' N(t) L

Updating equations:

a(t|n) = a(t|t-1) + P(t|t-1)r(t)

P(t|n) = P(t|t-1) - P(t|t-1)N(t-1)P(t|t-1)

Next iteration: Set t=t-1 and goto “Evolution equations”.

Value

An S3-object of class "fks" which is a list with the following elements:

ahatt A m * n-matrix containing the smoothed state variables, i.e. ahatt[,t] = a(t|n)
Vt A m * m * n-array containing the variances of ahatt, i.e. Vt[,,t] = P(t|n)

References

Koopman, S. J. and Durbin, J. (2000). Fast filtering and smoothing for multivariate state space models Journal of Time Series Analysis Vol. 21, No. 3

Examples

## <--------------------------------------------------------------------------->
## Example: Local level model for the Nile's annual flow.
## <--------------------------------------------------------------------------->
## Transition equation:
## alpha[t+1] = alpha[t] + eta[t], eta[t] ~ N(0, HHt)
## Measurement equation:
## y[t] = alpha[t] + eps[t], eps[t] ~  N(0, GGt)

y <- Nile
y[c(3, 10)] <- NA  # NA values can be handled

## Set constant parameters:
dt <- ct <- matrix(0)
Zt <- Tt <- matrix(1)
a0 <- y[1]            # Estimation of the first year flow
P0 <- matrix(100)     # Variance of 'a0'

## Estimate parameters:
fit.fkf <- optim(c(HHt = var(y, na.rm = TRUE) * .5,
                   GGt = var(y, na.rm = TRUE) * .5),
                 fn = function(par, ...)
                   -fkf(HHt = matrix(par[1]), GGt = matrix(par[2]), ...)$logLik,
                 yt = rbind(y), a0 = a0, P0 = P0, dt = dt, ct = ct,
                 Zt = Zt, Tt = Tt)

## Filter Nile data with estimated parameters:
fkf.obj <- fkf(a0, P0, dt, ct, Tt, Zt, HHt = matrix(fit.fkf$par[1]),
               GGt = matrix(fit.fkf$par[2]), yt = rbind(y))

## Smooth the data based on the filter object
fks.obj <- fks(fkf.obj)

## Plot the flow data together with local levels:
plot(y, main = "Nile flow")
lines(ts(fkf.obj$att[1, ], start = start(y), frequency = frequency(y)), col = "blue")
lines(ts(fks.obj$ahatt[1,], start = start(y), frequency = frequency(y)), col = "red")
legend("top", c("Nile flow data", "Local level (fkf)","Local level (fks)"),
       col = c("black", "green", "blue", "red"), lty = 1)


FKF documentation built on Oct. 11, 2022, 1:06 a.m.