KS: Kalman Smoother for State Space Models

Description Usage Arguments Details Value References See Also Examples

View source: R/KS.R

Description

These functions run the iterative equations of the Kalman smoother for a state space model upon the output from the Kalman filter.

Usage

1
2
KS(y, ss, kf)
KS.deriv(y, ss, kf)

Arguments

y

a numeric time series or vector.

ss

a list containing the matrices of the state space model.

kf

a list containing the output returned by the Kalman filter KF.

Details

See the details section and the section ‘state space representation’ in KF.

Missing observations are allowed.

The input kf passed to KS.deriv must contain the derivative terms related to the filter that are returned by KF.deriv or KF.deriv.C.

When the Kalman filter was found to convergence at some iteration, i.e., kf$convit is not null, these functions use steady state values for N and varahat in the intermediate iterations of the smoother. For example, if the filter converged at iteration 15 in a series of length n, the equations of the smoother are run for the first iterations from observation n to n-15; then the steady state values are used until there are 15 iterations remaining. In the last iterations, from observation 15 to 1 the equations of the smoother are evaluated again.

In practice, if the disturbance smoother is to be run as well, using the functions described in KFKSDS will be slightly more efficient.

Value

A list containing the following elements:

ahat

smoothed state vector.

varhat

covariance matrix of ahat.

r

weighted sum of innovations used to obtain ahat.

N

intermediate matrix used to obtain varahat

The function KS.deriv returns also the derivatives referred to each of the elements defined above, named respectively dahat, dvarahat, dr and dN.

References

Durbin, J. and Koopman, S. J. (2001). Time Series Analysis by State Space Methods. Oxford University Press.

Harvey, A. C. (1989). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge University Press.

See Also

KF, KSDS; char2numeric in package stsm.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# local level plus seasonal model with arbitrary parameter values
# for the 'JohnsonJohnson' time series
m <- stsm::stsm.model(model = "llm+seas", y = JohnsonJohnson, 
  pars = c("var1" = 2, "var2" = 15, "var3" = 30))
ss <- stsm::char2numeric(m)

kf <- KF(m@y, ss)
ks <- KS(m@y, ss, kf)

plot(ks$ahat[,1:2], main = "smoothed state vector")

kfd <- KF.deriv(m@y, ss)
ksd <- KS.deriv(m@y, ss, kfd)
all.equal(ks$ahat, ksd$ahat)

# extended output is required if 'KF.deriv.C' is used to obtain 
# the necessary elements from the filter, set return.all = TRUE
kfdc <- KF.deriv.C(m@y, ss, return.all = TRUE)
ksd <- KS.deriv(m@y, ss, kfdc)
all.equal(ks$ahat, ksd$ahat)

# compare analytical and numerical derivatives
# yield same results up to a tolerance error
fcn <- function(x, model, type, i)
{
  m <- stsm::set.pars(model, x)
  ss <- stsm::char2numeric(m)
  kf <- KF(m@y, ss)
  ks <- KS(m@y, ss, kf)
  switch(type, "ahat" = sum(ks$ahat[,i]), "r" = sum(ks$r[,i]))
}

dahat <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "ahat", i = 1)
all.equal(dahat, colSums(ksd$dahat[,1,]))
dahat <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "ahat", i = 2)
all.equal(dahat, colSums(ksd$dahat[,2,]))
dahat <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "ahat", i = 3)
all.equal(dahat, colSums(ksd$dahat[,3,]))
dr <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "r", i = 1)
all.equal(dr, colSums(ksd$dr[,1,]), check.attributes = FALSE)
dr <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "r", i = 2)
all.equal(dr, colSums(ksd$dr[,2,]), check.attributes = FALSE)
dr <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "r", i = 3)
all.equal(dr, colSums(ksd$dr[,3,]), check.attributes = FALSE)

KFKSDS documentation built on May 2, 2019, 8:51 a.m.