# The structure of the concentration and covariance matrix in a naive Bayes model

library(caracas)

Consider this model: $x_i = a x_0 + e_i, \quad i=1, \dots, 4$ and $$x_0=e_0$$. All terms $$e_0, \dots, e_3$$ are independent and $$N(0,1)$$ distributed. Let $$e=(e_0, \dots, e_3)$$ and $$x=(x_0, \dots x_3)$$. Isolating error terms gives that $e = L_1 x$ where $$L_1$$ has the form

L1chr <- diag(4)
L1chr[2:4, 1] <- "-a"
L1 <- as_symbol(L1chr)
L1
#> [caracas]: ⎡1   0  0  0⎤
#>            ⎢           ⎥
#>            ⎢-a  1  0  0⎥
#>            ⎢           ⎥
#>            ⎢-a  0  1  0⎥
#>            ⎢           ⎥
#>            ⎣-a  0  0  1⎦

If error terms have variance $$1$$ then $$\mathbf{Var}(e)=L \mathbf{Var}(x) L'$$ so the covariance matrix is $$V1=\mathbf{Var}(x) = L^- (L^-)'$$ while the concentration matrix (the inverse covariances matrix) is $$K=L' L$$.

L1inv <- inv(L1)
K1 <- t(L1) %*% L1
V1 <- L1inv %*% t(L1inv)
cat(
"\\begin{align}
K_1 &= ", tex(K1), " \\\\
V_1 &= ", tex(V1), "
\\end{align}", sep = "")

\begin{align} K_1 &= \left[\begin{matrix}3 a^{2} + 1 & - a & - a & - a\\- a & 1 & 0 & 0\\- a & 0 & 1 & 0\\- a & 0 & 0 & 1\end{matrix}\right] \\ V_1 &= \left[\begin{matrix}1 & a & a & a\\a & a^{2} + 1 & a^{2} & a^{2}\\a & a^{2} & a^{2} + 1 & a^{2}\\a & a^{2} & a^{2} & a^{2} + 1\end{matrix}\right] \end{align}

Slightly more elaborate:

L2chr <- diag(4)
L2chr[2:4, 1] <- c("-a1", "-a2", "-a3")
L2 <- as_symbol(L2chr)
L2
#> [caracas]: ⎡ 1   0  0  0⎤
#>            ⎢            ⎥
#>            ⎢-a₁  1  0  0⎥
#>            ⎢            ⎥
#>            ⎢-a₂  0  1  0⎥
#>            ⎢            ⎥
#>            ⎣-a₃  0  0  1⎦
Vechr <- diag(4)
Vechr[cbind(1:4, 1:4)] <- c("w1", "w2", "w2", "w2")
Ve <- as_symbol(Vechr)
Ve
#> [caracas]: ⎡w₁  0   0   0 ⎤
#>            ⎢              ⎥
#>            ⎢0   w₂  0   0 ⎥
#>            ⎢              ⎥
#>            ⎢0   0   w₂  0 ⎥
#>            ⎢              ⎥
#>            ⎣0   0   0   w₂⎦
L2inv <- inv(L2)
K2 <- t(L2) %*% inv(Ve) %*% L2
V2 <- L2inv %*% Ve %*% t(L2inv)
cat(
"\\begin{align}
K_2 &= ", tex(K2), " \\\\
V_2 &= ", tex(V2), "
\\end{align}", sep = "")

\begin{align} K_2 &= \left[\begin{matrix}\frac{a_{1}^{2}}{w_{2}} + \frac{a_{2}^{2}}{w_{2}} + \frac{a_{3}^{2}}{w_{2}} + \frac{1}{w_{1}} & - \frac{a_{1}}{w_{2}} & - \frac{a_{2}}{w_{2}} & - \frac{a_{3}}{w_{2}}\\- \frac{a_{1}}{w_{2}} & \frac{1}{w_{2}} & 0 & 0\\- \frac{a_{2}}{w_{2}} & 0 & \frac{1}{w_{2}} & 0\\- \frac{a_{3}}{w_{2}} & 0 & 0 & \frac{1}{w_{2}}\end{matrix}\right] \\ V_2 &= \left[\begin{matrix}w_{1} & a_{1} w_{1} & a_{2} w_{1} & a_{3} w_{1}\\a_{1} w_{1} & a_{1}^{2} w_{1} + w_{2} & a_{1} a_{2} w_{1} & a_{1} a_{3} w_{1}\\a_{2} w_{1} & a_{1} a_{2} w_{1} & a_{2}^{2} w_{1} + w_{2} & a_{2} a_{3} w_{1}\\a_{3} w_{1} & a_{1} a_{3} w_{1} & a_{2} a_{3} w_{1} & a_{3}^{2} w_{1} + w_{2}\end{matrix}\right] \end{align}