kalman_dk {bvartools}R Documentation

Durbin and Koopman Simulation Smoother

Description

An implementation of the Kalman filter and backward smoothing algorithm proposed by Durbin and Koopman (2002).

Usage

kalman_dk(y, z, sigma_u, sigma_v, B, a_init, P_init)

Arguments

y

a K×TK \times T matrix of endogenous variables.

z

a KT×MKT \times M matrix of explanatory variables.

sigma_u

the constant K×KK \times K error variance-covariance matrix. For time varying variance-covariance matrices a KT×KKT \times K can be specified.

sigma_v

the constant M×MM \times M coefficient variance-covariance matrix. For time varying variance-covariance matrices a MT×MMT \times M can be specified.

B

an M×MM \times M autocorrelation matrix of the transition equation.

a_init

an M-dimensional vector of initial states.

P_init

an M×MM \times M variance-covariance matrix of the initial states.

Details

The function uses algorithm 2 from Durbin and Koopman (2002) to produce a draw of the state vector ata_t for t=1,...,Tt = 1,...,T for a state space model with measurement equation

yt=Ztat+uty_t = Z_t a_t + u_t

and transition equation

at+1=Btat+vt,a_{t + 1} = B_t a_{t} + v_t,

where utN(0,Σu,t)u_t \sim N(0, \Sigma_{u,t}) and vtN(0,Σv,t)v_t \sim N(0, \Sigma_{v,t}). yty_t is a K-dimensional vector of endogenous variables and Zt=ztIKZ_t = z_t^{\prime} \otimes I_K is a K×MK \times M matrix of regressors with ztz_t as a vector of regressors.

The algorithm takes into account Jarociński (2015), where a possible missunderstanding in the implementation of the algorithm of Durbin and Koopman (2002) is pointed out. Following that note the function sets the mean of the initial state to zero in the first step of the algorithm.

Value

A M×T+1M \times T+1 matrix of state vector draws.

References

Durbin, J., & Koopman, S. J. (2002). A simple and efficient simulation smoother for state space time series analysis. Biometrika, 89(3), 603–615.

Jarociński, M. (2015). A note on implementing the Durbin and Koopman simulation smoother. Computational Statistics and Data Analysis, 91, 1–3. doi:10.1016/j.csda.2015.05.001

Examples


# Load data
data("e1")
data <- diff(log(e1))

# Generate model data
temp <- gen_var(data, p = 2, deterministic = "const")
y <- t(temp$data$Y)
z <- temp$data$SUR
k <- nrow(y)
tt <- ncol(y)
m <- ncol(z)

# Priors
a_mu_prior <- matrix(0, m)
a_v_i_prior <- diag(0.1, m)

a_Q <- diag(.0001, m)

# Initial value of Sigma
sigma <- tcrossprod(y) / tt
sigma_i <- solve(sigma)

# Initial values for Kalman filter
y_init <- y * 0
a_filter <- matrix(0, m, tt + 1)

# Initialise the Kalman filter
for (i in 1:tt) {
  y_init[, i] <- y[, i] - z[(i - 1) * k + 1:k,] %*% a_filter[, i]
}
a_init <- post_normal_sur(y = y_init, z = z, sigma_i = sigma_i,
                          a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
y_filter <- matrix(y) - z %*% a_init
y_filter <- matrix(y_filter, k) # Reshape

# Kalman filter and backward smoother
a_filter <- kalman_dk(y = y_filter, z = z, sigma_u = sigma,
                      sigma_v = a_Q, B = diag(1, m),
                      a_init = matrix(0, m), P_init = a_Q)
                      
a <- a_filter + matrix(a_init, m, tt + 1)


[Package bvartools version 0.2.4 Index]