21 - Concentration and covariance matrix in an autoregressive model and in a dynamic linear model

library(caracas)

Autoregressive model (AR(1))

Consider this model: xi = axi − 1 + ei,  i = 1, …, 3 and x0 = e0. All terms e0, …, e3 are independent and N(0, v2) distributed. Let e = (e0, …, e3) and x = (x0, …x3). Hence e ∼ N(0, v2I). Isolating error terms gives e = tex(e) = tex(L1)tex(x) = L1x

N <- 3
L1 <- diag(4)
L1[cbind(1 + (1:N), 1:N)] <- "-a"
L1 <- as_sym(L1)

Since Var(e) = v2I we have Var(e) = v2I = LVar(x)L so the covariance matrix of x is V1 = Var(x) = v2L(L)′ while the concentration matrix (the inverse covariances matrix) is K = v−2LL.

def_sym(v2)
L1inv <- inv(L1)
V1 <- v2 * L1inv %*% t(L1inv)
K1 <- (t(L1) %*% L1) / v2

Dynamic linear model

Augment the AR(1) process above with yi = bxi + ui for i = 1, 2, 3. Suppose ui ∼ N(0, w2) and all ui are independent and inpendent of e. Then (e, u) can be expressed in terms of (x, y) as (e, u) = tex(eu) = tex(L2)tex(xy) = L2(x, y) where

N <- 3
L2 <- diag("1", 1 + 2*N)
L2[cbind(1 + (1:N), 1:N)] <- "-a"
L2[cbind(1 + N + (1:N), 1 + 1:N)] <- "-b"
L2 <- as_sym(L2)
Veu <- diag(1, 7)
diag(Veu)[1:4] <- "v2"
diag(Veu)[5:7] <- "w2"
Veu
Veu <- as_sym(Veu)
Veu
L2inv <- inv(L2) 
V2 <- L2inv %*% Veu %*% t(L2inv) 
K2 <- t(L2) %*% inv(Veu) %*% L2