A <- matrix(1:6, nrow = 2)
A [,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
lav_mat_vec(A)[1] 1 2 3 4 5 6
This appendix collects several matrix algebra results that are used throughout this book. Proofs are omitted; readers are referred to Magnus and Neudecker (2019) for a comprehensive treatment.
The vec operator transforms a matrix into a vector by stacking its columns one below the other. For an m \times n matrix \mathbf{A} with columns \mathbf{a}_1, \ldots, \mathbf{a}_n:
\text{vec}(\mathbf{A}) = \begin{pmatrix} \mathbf{a}_1 \\ \mathbf{a}_2 \\ \vdots \\ \mathbf{a}_n \end{pmatrix}.
The resulting vector has m \times n elements. In lavaan, lav_mat_vec() implements this operator; it is equivalent to R’s built-in as.vector():1
A <- matrix(1:6, nrow = 2)
A [,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
lav_mat_vec(A)[1] 1 2 3 4 5 6
The vech operator (half-vectorization) applies to a symmetric matrix \mathbf{S} of order n. It extracts the lower-triangular elements, including the diagonal, column by column, producing a vector of length n(n+1)/2:
\text{vech}(\mathbf{S}) = (s_{11},\, s_{21},\, \ldots,\, s_{n1},\, s_{22},\, s_{32},\, \ldots,\, s_{nn})^{\prime}.
S <- matrix(c(4, 2, 1,
2, 5, 3,
1, 3, 6), nrow = 3)
lav_mat_vech(S)[1] 4 2 1 5 3 6
By default, lav_mat_vech() includes the diagonal. Setting diagonal = FALSE drops the diagonal elements, which is sometimes useful when working with correlation matrices.
The n^2 \times n(n+1)/2 duplication matrix \mathbf{D}_n is the unique matrix of zeros and ones satisfying
\mathbf{D}_n\, \text{vech}(\mathbf{S}) = \text{vec}(\mathbf{S})
for every symmetric n \times n matrix \mathbf{S}. In other words, \mathbf{D}_n “duplicates” the lower-triangular elements to reconstruct the full vectorized matrix:
D3 <- lav_mat_dup(3)
D3 [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 0
[2,] 0 1 0 0 0 0
[3,] 0 0 1 0 0 0
[4,] 0 1 0 0 0 0
[5,] 0 0 0 1 0 0
[6,] 0 0 0 0 1 0
[7,] 0 0 1 0 0 0
[8,] 0 0 0 0 1 0
[9,] 0 0 0 0 0 1
as.vector(D3 %*% lav_mat_vech(S))[1] 4 2 1 2 5 3 1 3 6
lav_mat_vec(S)[1] 4 2 1 2 5 3 1 3 6
The Moore-Penrose generalized inverse of \mathbf{D}_n is
\mathbf{D}_n^{+} = (\mathbf{D}_n^{\prime} \mathbf{D}_n)^{-1} \mathbf{D}_n^{\prime}.
This matrix has dimensions n(n+1)/2 \times n^2, and for every symmetric n \times n matrix \mathbf{S} it satisfies \mathbf{D}_n^{+}\, \text{vec}(\mathbf{S}) = \text{vech}(\mathbf{S}):
D3plus <- lav_mat_dup_ginv(3)
D3plus [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 0.0 0.0 0.0 0 0.0 0.0 0.0 0
[2,] 0 0.5 0.0 0.5 0 0.0 0.0 0.0 0
[3,] 0 0.0 0.5 0.0 0 0.0 0.5 0.0 0
[4,] 0 0.0 0.0 0.0 1 0.0 0.0 0.0 0
[5,] 0 0.0 0.0 0.0 0 0.5 0.0 0.5 0
[6,] 0 0.0 0.0 0.0 0 0.0 0.0 0.0 1
as.vector(D3plus %*% lav_mat_vec(S))[1] 4 2 1 5 3 6
lav_mat_vech(S)[1] 4 2 1 5 3 6
One can verify that \mathbf{D}_n^{+} \mathbf{D}_n = \mathbf{I}_{n(n+1)/2}, but \mathbf{D}_n \mathbf{D}_n^{+} \neq \mathbf{I}_{n^2} (since \mathbf{D}_n is not square).
For symmetric matrices, \mathbf{D}_n^{+} has the same effect as the elimination matrix \mathbf{L}_n of Magnus and Neudecker (2019), but the two matrices are not the same. The elimination matrix is a matrix of zeros and ones that satisfies \mathbf{L}_n\, \text{vec}(\mathbf{A}) = \text{vech}(\mathbf{A}) for every square matrix \mathbf{A}: it selects the elements on and below the diagonal. The matrix \mathbf{D}_n^{+} averages each pair of off-diagonal elements instead (its nonzero entries for these elements are equal to one half), and it gives \text{vech}(\mathbf{A}) only if \mathbf{A} is symmetric.
Because \mathbf{D}_n and \mathbf{D}_n^+ are large sparse matrices, lavaan avoids constructing them explicitly when only matrix products are needed. The following convenience functions are provided:
| Function | Computes |
|---|---|
lav_mat_dup_pre(A) |
\mathbf{D}_n^{\prime} \mathbf{A} |
lav_mat_dup_post(A) |
\mathbf{A} \mathbf{D}_n |
lav_mat_dup_pre_post(A) |
\mathbf{D}_n^{\prime} \mathbf{A} \mathbf{D}_n |
lav_mat_dup_ginv_pre(A) |
\mathbf{D}_n^{+} \mathbf{A} |
lav_mat_dup_ginv_pre_post(A) |
\mathbf{D}_n^{+} \mathbf{A} (\mathbf{D}_n^{+})^{\prime} |
These functions are used extensively in lavaan when computing the asymptotic covariance matrix of the sample statistics.
The mn \times mn commutation matrix \mathbf{K}_{mn} is the permutation matrix satisfying
\mathbf{K}_{mn}\, \text{vec}(\mathbf{A}) = \text{vec}(\mathbf{A}^{\prime})
for any m \times n matrix \mathbf{A}. Because \mathbf{K}_{mn} is a permutation matrix, it is orthogonal: \mathbf{K}_{mn}^{\prime} = \mathbf{K}_{mn}^{-1} = \mathbf{K}_{nm}.
K23 <- lav_mat_com(2, 3)
K23 [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 0
[2,] 0 0 1 0 0 0
[3,] 0 0 0 0 1 0
[4,] 0 1 0 0 0 0
[5,] 0 0 0 1 0 0
[6,] 0 0 0 0 0 1
A <- matrix(1:6, nrow = 2) # 2 x 3
as.vector(K23 %*% lav_mat_vec(A))[1] 1 3 5 2 4 6
lav_mat_vec(t(A))[1] 1 3 5 2 4 6
The commutation matrix enables commuting the factors of a Kronecker product. For matrices \mathbf{A} (m \times n) and \mathbf{B} (p \times q):
\mathbf{K}_{pm} (\mathbf{A} \otimes \mathbf{B}) \mathbf{K}_{nq} = \mathbf{B} \otimes \mathbf{A}.
As with the duplication matrix, lavaan provides efficient pre- and post-multiplication functions (lav_mat_com_pre(), lav_mat_com_post(), lav_mat_com_pre_post()) that avoid constructing \mathbf{K}_{mn} explicitly.
The Kronecker product \mathbf{A} \otimes \mathbf{B} of an m \times n matrix \mathbf{A} and a p \times q matrix \mathbf{B} is the mp \times nq block matrix
\mathbf{A} \otimes \mathbf{B} = \begin{pmatrix} a_{11} \mathbf{B} & a_{12} \mathbf{B} & \cdots & a_{1n} \mathbf{B} \\ a_{21} \mathbf{B} & a_{22} \mathbf{B} & \cdots & a_{2n} \mathbf{B} \\ \vdots & & \ddots & \vdots \\ a_{m1} \mathbf{B} & a_{m2} \mathbf{B} & \cdots & a_{mn} \mathbf{B} \end{pmatrix}.
In R, the Kronecker product is computed with the %x% operator (or equivalently kronecker(A, B)):
A <- matrix(c(1, 2, 3, 4), nrow = 2) # 2 x 2
B <- matrix(c(10, 10, 20, 20), nrow = 2) # 2 x 2
A %x% B [,1] [,2] [,3] [,4]
[1,] 10 20 30 60
[2,] 10 20 30 60
[3,] 20 40 40 80
[4,] 20 40 40 80
Key algebraic properties (assuming all dimensions are conformable):
Mixed-product property: (\mathbf{A} \otimes \mathbf{B})(\mathbf{C} \otimes \mathbf{D}) = (\mathbf{A}\mathbf{C}) \otimes (\mathbf{B}\mathbf{D})
Transpose: (\mathbf{A} \otimes \mathbf{B})^{\prime} = \mathbf{A}^{\prime} \otimes \mathbf{B}^{\prime}
Inverse: (\mathbf{A} \otimes \mathbf{B})^{-1} = \mathbf{A}^{-1} \otimes \mathbf{B}^{-1}
Vec lemma: \text{vec}(\mathbf{A}\mathbf{B}\mathbf{C}) = (\mathbf{C}^{\prime} \otimes \mathbf{A})\, \text{vec}(\mathbf{B})
The vec lemma is particularly important in SEM, as it allows expressing derivatives of matrix functions in a compact, closed form.
# Verifying the vec lemma: vec(ABC) = (C' x A) vec(B)
B_mat <- matrix(c(5, 6, 7, 8), nrow = 2)
C_mat <- matrix(c(1, 0, 2, 1), nrow = 2)
lav_mat_vec(A %*% B_mat %*% C_mat)[1] 23 34 77 114
as.vector((t(C_mat) %x% A) %*% lav_mat_vec(B_mat))[1] 23 34 77 114
The trace of a square n \times n matrix \mathbf{A}, denoted \text{tr}(\mathbf{A}), is the sum of its diagonal elements:
\text{tr}(\mathbf{A}) = \sum_{i=1}^{n} a_{ii} = a_{11} + a_{22} + \cdots + a_{nn}.
For example:
\mathbf{A} = \begin{pmatrix} 4 & 2 & 1 \\ 2 & 5 & 3 \\ 1 & 3 & 6 \end{pmatrix}, \qquad \text{tr}(\mathbf{A}) = 4 + 5 + 6 = 15.
In R the trace is computed as sum(diag(A)):
A <- matrix(c(4, 2, 1,
2, 5, 3,
1, 3, 6), nrow = 3)
sum(diag(A))[1] 15
Basic properties (for square conformable matrices \mathbf{A} and \mathbf{B}, and scalar c):
The most useful property of the trace is its invariance under cyclic permutations of a matrix product (cyclic invariance). For matrices \mathbf{A} (m \times n), \mathbf{B} (n \times p), and \mathbf{C} (p \times m):
\text{tr}(\mathbf{A}\mathbf{B}\mathbf{C}) = \text{tr}(\mathbf{B}\mathbf{C}\mathbf{A}) = \text{tr}(\mathbf{C}\mathbf{A}\mathbf{B}).
The two-matrix special case is \text{tr}(\mathbf{A}\mathbf{B}) = \text{tr}(\mathbf{B}\mathbf{A}), where \mathbf{A} is m \times n and \mathbf{B} is n \times m. Note that only cyclic permutations preserve the trace; in general \text{tr}(\mathbf{A}\mathbf{B}\mathbf{C}) \neq \text{tr}(\mathbf{A}\mathbf{C}\mathbf{B}).
set.seed(42)
A_tr <- matrix(rnorm(6), nrow = 2) # 2 x 3
B_tr <- matrix(rnorm(6), nrow = 3) # 3 x 2
sum(diag(A_tr %*% B_tr)) # tr(AB): 2 x 2 matrix[1] 3.472394
sum(diag(B_tr %*% A_tr)) # tr(BA): 3 x 3 matrix[1] 3.472394
Cyclic invariance is frequently used in matrix calculus and in deriving likelihood gradients. A typical example in SEM is the normal-theory fit function:
F(\boldsymbol{\vartheta}) = \text{tr}(\boldsymbol{\Sigma}(\boldsymbol{\vartheta})^{-1}\mathbf{S}) - \log|\boldsymbol{\Sigma}(\boldsymbol{\vartheta})^{-1}\mathbf{S}| - P,
where \boldsymbol{\Sigma}(\boldsymbol{\vartheta}) is the model-implied covariance matrix as a function of the parameter vector \boldsymbol{\vartheta}, \mathbf{S} is the sample covariance matrix, and P is the number of observed variables. When differentiating with respect to \boldsymbol{\vartheta}, cyclic invariance allows one to reorder factors so that the derivative can be applied directly to \boldsymbol{\Sigma}(\boldsymbol{\vartheta}).
Let \mathbf{M} be a square matrix partitioned as
\mathbf{M} = \begin{pmatrix} \mathbf{A} & \mathbf{B} \\ \mathbf{C} & \mathbf{D} \end{pmatrix},
where \mathbf{A} and \mathbf{D} are square and invertible. There are two equivalent identities for the inverse of \mathbf{M}.
The first identity is expressed in terms of the Schur complement of \mathbf{D}, defined as \mathbf{P} = \mathbf{A} - \mathbf{B}\mathbf{D}^{-1}\mathbf{C}:
\begin{pmatrix} \mathbf{A} & \mathbf{B} \\ \mathbf{C} & \mathbf{D} \end{pmatrix}^{-1} = \begin{pmatrix} \mathbf{P}^{-1} & -\mathbf{P}^{-1} \mathbf{B} \mathbf{D}^{-1} \\[4pt] -\mathbf{D}^{-1} \mathbf{C} \mathbf{P}^{-1} & \mathbf{D}^{-1} + \mathbf{D}^{-1} \mathbf{C} \mathbf{P}^{-1} \mathbf{B} \mathbf{D}^{-1} \end{pmatrix}.
The second identity is expressed in terms of the Schur complement of \mathbf{A}, defined as \mathbf{Q} = \mathbf{D} - \mathbf{C}\mathbf{A}^{-1}\mathbf{B}:
\begin{pmatrix} \mathbf{A} & \mathbf{B} \\ \mathbf{C} & \mathbf{D} \end{pmatrix}^{-1} = \begin{pmatrix} \mathbf{A}^{-1} + \mathbf{A}^{-1} \mathbf{B} \mathbf{Q}^{-1} \mathbf{C} \mathbf{A}^{-1} & -\mathbf{A}^{-1} \mathbf{B} \mathbf{Q}^{-1} \\[4pt] -\mathbf{Q}^{-1} \mathbf{C} \mathbf{A}^{-1} & \mathbf{Q}^{-1} \end{pmatrix}.
The Schur complement also arises in conditional distributions: if (\mathbf{x}_1^{\prime}, \mathbf{x}_2^{\prime})^{\prime} follows a multivariate normal distribution with covariance matrix \mathbf{M}, the conditional covariance of \mathbf{x}_1 given \mathbf{x}_2 equals \mathbf{P}, the Schur complement of \mathbf{D}, and the conditional covariance of \mathbf{x}_2 given \mathbf{x}_1 equals \mathbf{Q}, the Schur complement of \mathbf{A}.
The determinant of a partitioned matrix can likewise be computed in two ways:
|\mathbf{M}| = |\mathbf{D}| \cdot |\mathbf{A} - \mathbf{B}\mathbf{D}^{-1}\mathbf{C}| = |\mathbf{A}| \cdot |\mathbf{D} - \mathbf{C}\mathbf{A}^{-1}\mathbf{B}|.
M <- matrix(c(4, 2, 1, 0,
2, 5, 0, 1,
1, 0, 3, 1,
0, 1, 1, 4), nrow = 4, byrow = TRUE)
A_bl <- M[1:2, 1:2]; B_bl <- M[1:2, 3:4]
C_bl <- M[3:4, 1:2]; D_bl <- M[3:4, 3:4]
# Identity 1: Schur complement of D
D_inv <- solve(D_bl)
M_sc_inv <- solve(A_bl - B_bl %*% D_inv %*% C_bl)
M_inv1 <- rbind(
cbind(M_sc_inv,
-M_sc_inv %*% B_bl %*% D_inv),
cbind(-D_inv %*% C_bl %*% M_sc_inv,
D_inv + D_inv %*% C_bl %*% M_sc_inv %*% B_bl %*% D_inv)
)
round(M_inv1, 4) [,1] [,2] [,3] [,4]
[1,] 0.3688 -0.1631 -0.1489 0.0780
[2,] -0.1631 0.2837 0.0851 -0.0922
[3,] -0.1489 0.0851 0.4255 -0.1277
[4,] 0.0780 -0.0922 -0.1277 0.3050
round(solve(M), 4) [,1] [,2] [,3] [,4]
[1,] 0.3688 -0.1631 -0.1489 0.0780
[2,] -0.1631 0.2837 0.0851 -0.0922
[3,] -0.1489 0.0851 0.4255 -0.1277
[4,] 0.0780 -0.0922 -0.1277 0.3050
# Identity 2: Schur complement of A
A_inv <- solve(A_bl)
Q_inv <- solve(D_bl - C_bl %*% A_inv %*% B_bl)
M_inv2 <- rbind(
cbind(A_inv + A_inv %*% B_bl %*% Q_inv %*% C_bl %*% A_inv,
-A_inv %*% B_bl %*% Q_inv),
cbind(-Q_inv %*% C_bl %*% A_inv, Q_inv)
)
round(M_inv2, 4) [,1] [,2] [,3] [,4]
[1,] 0.3688 -0.1631 -0.1489 0.0780
[2,] -0.1631 0.2837 0.0851 -0.0922
[3,] -0.1489 0.0851 0.4255 -0.1277
[4,] 0.0780 -0.0922 -0.1277 0.3050
# Determinant identity
det(A_bl) * det(D_bl - C_bl %*% A_inv %*% B_bl)[1] 141
det(M)[1] 141
Throughout this section, \mathbf{A} (p \times p) is square and nonsingular, \mathbf{U} (p \times r), \mathbf{B} (r \times r), and \mathbf{V} (r \times p) may be rectangular or square. An overview of variants can be found in Henderson and Searle (1981).
The first identity, the classic Woodbury identity, requires \mathbf{B} to be nonsingular:
(\mathbf{A} + \mathbf{U} \mathbf{B} \mathbf{V})^{-1} = \mathbf{A}^{-1} - \mathbf{A}^{-1} \mathbf{U} (\mathbf{B}^{-1} + \mathbf{V} \mathbf{A}^{-1} \mathbf{U})^{-1} \mathbf{V} \mathbf{A}^{-1}.
The second identity, the generalized Woodbury identity, does not require \mathbf{B} to be nonsingular:
(\mathbf{A} + \mathbf{U} \mathbf{B} \mathbf{V})^{-1} = \mathbf{A}^{-1} - \mathbf{A}^{-1} \mathbf{U} (\mathbf{I}_r + \mathbf{B} \mathbf{V} \mathbf{A}^{-1} \mathbf{U})^{-1} \mathbf{B} \mathbf{V} \mathbf{A}^{-1}.
Both identities are computationally attractive when p \gg r: instead of inverting a p \times p matrix, one only needs to invert an r \times r matrix (given \mathbf{A}^{-1}).
The following push-through identities give expressions for the inverse post-multiplied by \mathbf{U} (right) or pre-multiplied by \mathbf{V} (left). Two versions are given in each case; the first requires \mathbf{B} to be nonsingular, the second does not.
Push-through right:
(\mathbf{A} + \mathbf{U} \mathbf{B} \mathbf{V})^{-1} \mathbf{U} = \mathbf{A}^{-1} \mathbf{U} (\mathbf{B}^{-1} + \mathbf{V} \mathbf{A}^{-1} \mathbf{U})^{-1} \mathbf{B}^{-1},
(\mathbf{A} + \mathbf{U} \mathbf{B} \mathbf{V})^{-1} \mathbf{U} = \mathbf{A}^{-1} \mathbf{U} (\mathbf{I}_r + \mathbf{B} \mathbf{V} \mathbf{A}^{-1} \mathbf{U})^{-1}.
Push-through left:
\mathbf{V} (\mathbf{A} + \mathbf{U} \mathbf{B} \mathbf{V})^{-1} = \mathbf{B}^{-1} (\mathbf{B}^{-1} + \mathbf{V} \mathbf{A}^{-1} \mathbf{U})^{-1} \mathbf{V} \mathbf{A}^{-1},
\mathbf{V} (\mathbf{A} + \mathbf{U} \mathbf{B} \mathbf{V})^{-1} = (\mathbf{I}_r + \mathbf{V} \mathbf{A}^{-1} \mathbf{U} \mathbf{B})^{-1} \mathbf{V} \mathbf{A}^{-1}.
set.seed(1)
p <- 5; r <- 2
A_w <- diag(runif(p, 1, 3)) # diagonal p x p (easy to invert)
U_w <- matrix(rnorm(p * r), p, r) # p x r
B_w <- crossprod(matrix(rnorm(r * r), r, r)) # r x r, symmetric pd
V_w <- t(U_w) # r x p (symmetric case)
A_inv_w <- solve(A_w)
# Identity 1 (classic)
lhs1 <- solve(A_w + U_w %*% B_w %*% V_w)
rhs1 <- A_inv_w - A_inv_w %*% U_w %*%
solve(solve(B_w) + V_w %*% A_inv_w %*% U_w) %*%
V_w %*% A_inv_w
round(lhs1, 4) [,1] [,2] [,3] [,4] [,5]
[1,] 0.5733 -0.0310 0.0669 0.0334 0.0250
[2,] -0.0310 0.3555 -0.0266 0.0555 0.1318
[3,] 0.0669 -0.0266 0.3966 -0.0171 0.0102
[4,] 0.0334 0.0555 -0.0171 0.3323 -0.0357
[5,] 0.0250 0.1318 0.0102 -0.0357 0.6323
round(rhs1, 4) [,1] [,2] [,3] [,4] [,5]
[1,] 0.5733 -0.0310 0.0669 0.0334 0.0250
[2,] -0.0310 0.3555 -0.0266 0.0555 0.1318
[3,] 0.0669 -0.0266 0.3966 -0.0171 0.0102
[4,] 0.0334 0.0555 -0.0171 0.3323 -0.0357
[5,] 0.0250 0.1318 0.0102 -0.0357 0.6323
# Identity 2 (generalized, works even if B is singular)
rhs2 <- A_inv_w - A_inv_w %*% U_w %*%
solve(diag(r) + B_w %*% V_w %*% A_inv_w %*% U_w) %*%
B_w %*% V_w %*% A_inv_w
round(rhs2, 4) [,1] [,2] [,3] [,4] [,5]
[1,] 0.5733 -0.0310 0.0669 0.0334 0.0250
[2,] -0.0310 0.3555 -0.0266 0.0555 0.1318
[3,] 0.0669 -0.0266 0.3966 -0.0171 0.0102
[4,] 0.0334 0.0555 -0.0171 0.3323 -0.0357
[5,] 0.0250 0.1318 0.0102 -0.0357 0.6323
# Push-through right (version 2)
lhs_pt <- solve(A_w + U_w %*% B_w %*% V_w) %*% U_w
rhs_pt <- A_inv_w %*% U_w %*%
solve(diag(r) + B_w %*% V_w %*% A_inv_w %*% U_w)
round(lhs_pt, 4) [,1] [,2]
[1,] 0.5753 -0.0823
[2,] 0.0585 0.6391
[3,] -0.5239 0.2404
[4,] -0.2063 -0.1043
[5,] -0.0824 -0.3724
round(rhs_pt, 4) [,1] [,2]
[1,] 0.5753 -0.0823
[2,] 0.0585 0.6391
[3,] -0.5239 0.2404
[4,] -0.2063 -0.1043
[5,] -0.0824 -0.3724
Before version 0.7-2, these functions carried the longer prefix lav_matrix_; the longer names remain exported as aliases. See Section 1.4.↩︎