Skip to contents

Kalman Filter with Lag 1 for State Space Models

Usage

KFilterP1(data, Lambda, mu0, Sigma0, beta, chol_psi, chol_theta)

Arguments

data

Numeric matrix. time by k data matrix.

Lambda

Numeric matrix. Measurement or observation matrix.

mu0

Numeric matrix. Initial state mean vector.

Sigma0

Numeric matrix. Initial state covariance matrix.

beta

Numeric matrix. State transition matrix.

chol_psi

Numeric matrix. Cholesky decomposition of the state error covariance matrix Psi.

chol_theta

Numeric matrix. Cholesky decomposition of the observation error covariance matrix Theta.

Value

List of filtered state variables and other Kalman filter results.

Details

The measurement model is given by $$ \mathbf{y}_{t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{t} + \boldsymbol{\varepsilon}_{t} \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right) $$ where \(\mathbf{y}_{t}\), \(\boldsymbol{\eta}_{t}\), and \(\boldsymbol{\varepsilon}_{t}\) are random variables and \(\boldsymbol{\nu}\), \(\boldsymbol{\Lambda}\), and \(\boldsymbol{\Theta}\) are model parameters. \(\mathbf{y}_{t}\) is a vector of observed random variables at time \(t\), \(\boldsymbol{\eta}_{t}\) is a vector of latent random variables at time \(t\),, and \(\boldsymbol{\varepsilon}_{t}\) is a vector of random measurement errors at time \(t\), while \(\boldsymbol{\nu}\) is a vector of intercept, \(\boldsymbol{\Lambda}\) is a matrix of factor loadings, and \(\boldsymbol{\Theta}\) is the covariance matrix of \(\boldsymbol{\varepsilon}\).

The dynamic structure is given by $$ \boldsymbol{\eta}_{t} = \boldsymbol{\alpha} + \boldsymbol{\beta} \boldsymbol{\eta}_{t - 1} + \boldsymbol{\zeta}_{t} \quad \mathrm{with} \quad \boldsymbol{\zeta}_{t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi} \right) $$ where \(\boldsymbol{\eta}_{t}\), \(\boldsymbol{\eta}_{t - 1}\), and \(\boldsymbol{\zeta}_{t}\) are random variables and \(\boldsymbol{\alpha}\), \(\boldsymbol{\beta}\), and \(\boldsymbol{\Psi}\) are model parameters. \(\boldsymbol{\eta}_{t}\) is a vector of latent variables at time \(t\), \(\boldsymbol{\eta}_{t - 1}\) is a vector of latent variables at \(t - 1\), and \(\boldsymbol{\zeta}_{t}\) is a vector of dynamic noise at time \(t\) while \(\boldsymbol{\alpha}\) is a vector of intercepts, \(\boldsymbol{\beta}\) is a matrix of autoregression and cross regression coefficients, and \(\boldsymbol{\Psi}\) is the covariance matrix of \(\boldsymbol{\zeta}_{t}\).

Author

Ivan Jacob Agaloos Pesigan

Examples

data <- dat_univ_p1[, "y", drop = FALSE]
kalman <- KFilterP1(
  data = data,
  Lambda = matrix(1),
  mu0 = matrix(0),
  Sigma0 = matrix(1),
  beta = matrix(0.8),
  chol_psi = matrix(1),
  chol_theta = matrix(1)
)
str(kalman)
#> List of 8
#>  $ eta_predicted    : num [1, 1, 1:100] 0 0.401 1.11 1.053 1.592 ...
#>  $ eta_cov_predicted: num [1, 1, 1:100] 1.64 1.4 1.37 1.37 1.37 ...
#>  $ eta_filtered     : num [1, 1, 1:100] 0.501 1.387 1.316 1.99 3.083 ...
#>  $ eta_cov_filtered : num [1, 1, 1:100] 0.621 0.583 0.579 0.578 0.578 ...
#>  $ neg_log_like     : num 90.4
#>  $ innovations      : num [1, 1, 1:100] 0.806 1.692 0.356 1.622 2.579 ...
#>  $ sigma_innovations: num [1, 1, 1:100] 2.64 2.4 2.37 2.37 2.37 ...
#>  $ gain             : num [1, 1] 0.578

data <- dat_multiv_p1[, c("y1", "y2"), drop = FALSE]
kalman <- KFilterP1(
  data = data,
  Lambda = diag(2),
  mu0 = matrix(data = 0, nrow = 2),
  Sigma0 = diag(2),
  beta = diag(x = 0.8, nrow = 2, ncol = 2),
  chol_psi = chol(diag(2)),
  chol_theta = chol(diag(2))
)
str(kalman)
#> List of 8
#>  $ eta_predicted    : num [1:2, 1, 1:100] 0 0 -0.0339 0.8618 0.293 ...
#>  $ eta_cov_predicted: num [1:2, 1:2, 1:100] 1.64 0 0 1.64 1.4 ...
#>  $ eta_filtered     : num [1:2, 1, 1:100] -0.0423 1.0772 0.3662 2.1155 -0.9051 ...
#>  $ eta_cov_filtered : num [1:2, 1:2, 1:100] 0.621 0 0 0.621 0.583 ...
#>  $ neg_log_like     : num 181
#>  $ innovations      : num [1:2, 1, 1:100] -0.0682 1.7341 0.6864 2.1508 -2.0706 ...
#>  $ sigma_innovations: num [1:2, 1:2, 1:100] 2.64 0 0 2.64 2.4 ...
#>  $ gain             : num [1:2, 1:2] 0.578 0 0 0.578