Description Usage Arguments Details Value References See Also Examples
Kalman filter, smoother and disturbance smoother.
1 2 3 4 | KSDS(y, ss, kf)
KSDS.deriv(y, ss, kf)
KFKSDS.deriv.C(y, ss)
KFKSDS.deriv.steady.C(y, ss, convergence = c(0.001, 10, 1.2))
|
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 |
convergence |
a numeric vector of length three to control and determine the convergence of the filter and smoother. See details below. |
See the details section and the section ‘state space representation’
in KF
.
The iteration at which the the Kalman smoother converges is determined as the
iteration where the Kalman filter converged multiplied by the factor
convergence[3]
. It should be equal or greater than unity.
It can be omitted by setting it equal to one.
These interfaces are fully implemented in R.
In these functions, the Kalman smoother and the disturbance smoother are run in a single loop instead of running two sepate loops over the same observations.
These functions return a relatively extended output than can be used, for example, to implement an expectation-maximization algorithm.
The function KSDS
returns a list containing the following elements:
ahat |
smoothed state disturbance. |
varhat |
covariance matrix of |
r |
weighted sum of innovations used to obtain |
N |
intermediate matrix used to obtain |
epshat |
smoothed estimate of the disturbance term in the observation equation. |
vareps |
error variance of |
etahat |
smoothed estimate of the disturbance term(s) in the state equation. |
vareta |
error variance of |
The function KSDS-deriv
returns also dahat
, dvarahat
,
dr
, dN
which are the derivatives referred to the elements defined above.
The functions KFKSDS.deriv.C
and KFKSDS.deriv.steady.C
return a list containing the elements already defined above:
epshat
, vareps
, etahat
, vareta
,
r
, N
, dr
, dN
, dahat
and dvareps
.
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.
KF
, KS
;
char2numeric
in package stsm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # See tests comparing the analytical derivatives returned by
# these functions and the numerical derivatives evaluated with
# function 'numDeriv::grad' in the folder 'KFKSDS/inst/tests'
# of the source package
# 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)
kfd <- KF.deriv(m@y, ss)
ksds <- KSDS(m@y, ss, kfd)
da <- KSDS.deriv(m@y, ss, kfd)
db <- KFKSDS.deriv.C(m@y, ss)
# the ouput is the same but is returned with a different arrangement
dim(da$dahat)
dim(db$dahat)
for (i in seq_along(m@y))
stopifnot(all.equal(da$dahat[i,,], db$dahat[,,i]))
dim(da$dN)
dim(db$dN)
for (i in seq_along(m@y))
for (j in seq_len(3))
stopifnot(all.equal(diag(da$dN[i,,,j]), db$dN[,j,i], check.attributes = FALSE))
|
[1] 84 4 3
[1] 4 3 84
[1] 84 4 4 3
[1] 4 3 84
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.