kalman_dk: Durbin and Koopman Simulation Smoother

View source: R/RcppExports.R

kalman_dkR 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 \times T matrix of endogenous variables.

z

a KT \times M matrix of explanatory variables.

sigma_u

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

sigma_v

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

B

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

a_init

an M-dimensional vector of initial states.

P_init

an M \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 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ń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 \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. \Sexpr[results=rd]{tools:::Rd_expr_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)


bvartools documentation built on May 29, 2024, 5:32 a.m.