Durbin and Koopman Simulation Smoother

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

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

a \(K \times T\) matrix of endogenous variables.


a \(KT \times M\) matrix of explanatory variables.


the inverse of the constant \(K \times K\) error variance-covariance matrix. For time varying variance-covariance matrices a \(KT \times K\) can be specified.


the inverse of the constant \(M \times M\) coefficient variance-covariance matrix. For time varying variance-covariance matrices a \(MT \times M\) can be specified.


an \(M \times M\) autocorrelation matrix of the transition equation.


an M-dimensional vector of initial states.


an \(M \times M\) variance-covariance matrix of the initial states.


The function uses algorithm 2 from Durbin and Koopman (2002) to produce a draw of the state vector \(a_t\) for \(t = 1,...,T\) for a state space model with measurement equation $$y_t = Z_t a_t + u_t$$ and transition equation $$a_{t + 1} = B_t a_{t} + v_t,$$ where \(u_t \sim N(0, \Sigma_{u,t})\) and \(v_t \sim N(0, \Sigma_{v,t})\). \(y_t\) is a K-dimensional vector of endogenous variables and \(Z_t = z_t^{\prime} \otimes I_K\) is a \(K \times M\) matrix of regressors with \(z_t\) as a vector of regressors.

The algorithm takes into account Jaroci<U+0144>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.


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


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

Jaroci<U+0144>ski, M. (2015). A note on implementing the Durbin and Koopman simulation smoother. Computational Statistics and Data Analysis, 91, 1--3. https://doi.org/10.1016/j.csda.2015.05.001

  • kalman_dk
# Prepare data
data <- diff(log(e1))
temp <- gen_var(data, p = 2, deterministic = "const")
y <- temp$Y
x <- temp$Z
k <- nrow(y)
z <- kronecker(t(x), diag(1, k))
t <- ncol(y)
m <- k * nrow(x)

# 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) / t
sigma_i <- solve(sigma)

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

# Initialise the Kalman filter
for (i in 1:t) {
  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 <- y - matrix(a_init, k) %*% x

# 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, t + 1)

# }
Documentation reproduced from package bvartools, version 0.0.1, License: GPL (>= 2)

Community examples

Looks like there are no examples yet.