Learn R Programming

KFKSDS (version 1.6)

DS: Disturbance Smoother for State Space Models

Description

These functions run the disturbance smoother upon the output from the Kalman filter and smoother.

Usage

DS(y, ss, kf, ks) DS.deriv(ss, ksd)

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 function KF.
ks
a list containing the output returned by the function KS.
ksd
a list containing the output returned by the function KS.deriv.

Value

DS returns a list containing the following elements:
epshat
smoothed estimate of the disturbance term in the observation equation.
vareps
error variance of epshat.
etahat
smoothed estimate of the disturbance term(s) in the state equation.
vareta
error variance of etahat.
DS.deriv returns a list containing the derivatives of the elements above named respectively depshat, dvareps, detahat and dvareta. The derivatives are summed over all the observations.

Details

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

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, KS; char2numeric in package stsm.

Examples

Run this code
# 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)
ds <- DS(m@y, ss, kf, ks)
acf(ds$epshat, main = "ACF of smoothed disturbance")

kfd <- KF.deriv(m@y, ss)
ksd <- KS.deriv(m@y, ss, kfd)
dsd <- DS.deriv(ss, ksd)

# compare analytical and numerical derivatives
fcn <- function(x, model, type, i = 1)
{
  m <- stsm::set.pars(model, x)
  ss <- stsm::char2numeric(m)
  kf <- KF(m@y, ss)
  ks <- KS(m@y, ss, kf)
  ds <- DS(m@y, ss, kf, ks)
  
  switch(type,
    "epshat" = sum(ds$epshat),
    "vareps" = sum(ds$vareps))
}

d <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "epshat")
all.equal(d, dsd$depshat)

d <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "vareps")
all.equal(d, dsd$dvareps)

Run the code above in your browser using DataLab