Reference sheet

Linear algebra refresher

The vector and matrix facts multivariate statistics leans on, from eigenvalues and the spectral decomposition to quadratic forms and expected values.

Bold lowercase letters are column vectors, bold capitals are matrices or random vectors, a prime means transpose and everything is real. The determinant of A\mathbf A is written ∣A∣\lvert \mathbf A \rvert and 1\mathbf 1 is a vector of ones. The covariance matrix of a random vector X\mathbf X is Σ,\boldsymbol\Sigma, and for observations x1,…,xn\mathbf x_1, \dots, \mathbf x_n with mean xˉ\bar{\mathbf x} the sample covariance matrix is

S=1n−1∑j=1n(xj−xˉ)(xj−xˉ)′.\mathbf S = \frac{1}{n-1}\sum_{j=1}^n (\mathbf x_j - \bar{\mathbf x})(\mathbf x_j - \bar{\mathbf x})'.

The multivariate statistics reference has the results built on these facts.

Vectors

For x=(x1,…,xn)′{\mathbf x = (x_1, \dots, x_n)'} and y=(y1,…,yn)′{\mathbf y = (y_1, \dots, y_n)'} in Rn,\mathbb R^n,

IdeaFormula
Inner productx′y=x1y1+⋯+xnyn=y′x\mathbf x'\mathbf y = x_1y_1 + \dots + x_ny_n = \mathbf y'\mathbf x
LengthLx=∥x∥=x′x=x12+⋯+xn2L_{\mathbf x} = \lVert \mathbf x \rVert = \sqrt{\mathbf x'\mathbf x} = \sqrt{x_1^2 + \dots + x_n^2}
Rescaling∥c x∥=∣c∣ ∥x∥,\lVert c\,\mathbf x \rVert = \lvert c \rvert\,\lVert \mathbf x \rVert, so x/∥x∥\mathbf x / \lVert \mathbf x \rVert has length 1 when x≠0{\mathbf x \ne \mathbf 0}
Anglecos⁡θ=x′y∥x∥ ∥y∥\cos\theta = \dfrac{\mathbf x'\mathbf y}{\lVert \mathbf x \rVert\,\lVert \mathbf y \rVert} for nonzero x\mathbf x and y,\mathbf y, with 0≤θ≤π{0 \le \theta \le \pi}
Orthogonalx′y=0,{\mathbf x'\mathbf y = 0}, written x⊥y{\mathbf x \perp \mathbf y}
Outer productxy′\mathbf x\mathbf y' is the n×n{n \times n} matrix with xiyjx_iy_j in row ii and column jj

Projection

The projection of x\mathbf x on a nonzero vector y\mathbf y is

proj⁡yx=x′yy′y y\operatorname{proj}_{\mathbf y}\mathbf x = \frac{\mathbf x'\mathbf y}{\mathbf y'\mathbf y}\,\mathbf y

Its length is ∣x′y∣/∥y∥=∥x∥ ∣cos⁡θ∣,\lvert \mathbf x'\mathbf y \rvert / \lVert \mathbf y \rVert = \lVert \mathbf x \rVert\,\lvert \cos\theta \rvert, and what is left over, x−proj⁡yx,{\mathbf x - \operatorname{proj}_{\mathbf y}\mathbf x}, is orthogonal to y.\mathbf y.

Projecting a column of data y\mathbf y on 1\mathbf 1 gives yˉ 1\bar y\,\mathbf 1 and leaves the deviation vector d=y−yˉ 1.{\mathbf d = \mathbf y - \bar y\,\mathbf 1}. Its squared length d′d=∑j(yj−yˉ)2\mathbf d'\mathbf d = \sum_j (y_j -\nobreak \bar y)^2 is n−1{n - 1} times the sample variance, and the sample correlation of two variables is the cosine of the angle between their deviation vectors.

Cauchy-Schwarz inequality

(x′y)2≤(x′x)(y′y)(\mathbf x'\mathbf y)^2 \le (\mathbf x'\mathbf x)(\mathbf y'\mathbf y)

Equality holds exactly when one vector is a multiple of the other. This is why −1≤cos⁡θ≤1,{-1 \le \cos\theta \le 1}, and so why correlations lie between −1-1 and 1.1. The extended version puts a positive definite matrix in the middle.

Linear independence

Vectors x1,…,xk\mathbf x_1, \dots, \mathbf x_k are linearly independent when c1x1+⋯+ckxk=0c_1\mathbf x_1 + \dots + c_k\mathbf x_k =\nobreak \mathbf 0 only for c1=⋯=ck=0.{c_1 = \dots = c_k = 0}. Otherwise they are dependent, and at least one of them is a linear combination of the others.

  • More than nn vectors in Rn\mathbb R^n are always dependent.
  • Nonzero vectors that are mutually orthogonal are independent.
  • The columns of a matrix A\mathbf A are independent exactly when Ac=0{\mathbf A\mathbf c = \mathbf 0} only for c=0.{\mathbf c = \mathbf 0}.

Matrices

Products and special matrices

If A\mathbf A is m×k{m \times k} and B\mathbf B is k×n,{k \times n}, then AB\mathbf A\mathbf B is m×n,{m \times n}, and its entry in row ii and column jj is

∑ℓ=1kaiℓbℓj.\sum_{\ell=1}^k a_{i\ell}b_{\ell j}.

Order matters, and AB≠BA{\mathbf A\mathbf B \ne \mathbf B\mathbf A} in general, even when both products exist.

  • The identity I\mathbf I has ones on the diagonal and zeros elsewhere, and IA=AI=A.{\mathbf I\mathbf A = \mathbf A\mathbf I = \mathbf A}.
  • A diagonal matrix diag⁡(d1,…,dn)\operatorname{diag}(d_1, \dots, d_n) has zeros everywhere off the diagonal.
  • A symmetric matrix has A′=A,{\mathbf A' = \mathbf A}, so aij=aji.{a_{ij} = a_{ji}}. The matrices Σ,\boldsymbol\Sigma, S\mathbf S and every correlation matrix are symmetric, and so are X′X\mathbf X'\mathbf X and XX′\mathbf X\mathbf X' for any matrix X,\mathbf X, and CAC′\mathbf C\mathbf A\mathbf C' whenever A\mathbf A is symmetric.

Transpose and inverse

A square matrix A\mathbf A is invertible, or nonsingular, when some matrix A−1\mathbf A^{-1} has AA−1=A−1A=I.\mathbf A\mathbf A^{-1} = \mathbf A^{-1}\mathbf A =\nobreak \mathbf I. The inverse rules below are for square matrices of the same size that are invertible, and a number c≠0.{c \ne 0}.

RuleTranspose and inverse
Twice(A′)′=A{(\mathbf A')' = \mathbf A} and (A−1)−1=A{(\mathbf A^{-1})^{-1} = \mathbf A}
Product(AB)′=B′A′(\mathbf A\mathbf B)' = \mathbf B'\mathbf A' and (AB)−1=B−1A−1(\mathbf A\mathbf B)^{-1} = \mathbf B^{-1}\mathbf A^{-1}
Three factors(ABC)′=C′B′A′(\mathbf A\mathbf B\mathbf C)' = \mathbf C'\mathbf B'\mathbf A' and (ABC)−1=C−1B−1A−1(\mathbf A\mathbf B\mathbf C)^{-1} = \mathbf C^{-1}\mathbf B^{-1}\mathbf A^{-1}
Scalar(cA)′=c A′{(c\mathbf A)' = c\,\mathbf A'} and (cA)−1=c−1A−1(c\mathbf A)^{-1} = c^{-1}\mathbf A^{-1}
Sum(A+B)′=A′+B′,(\mathbf A + \mathbf B)' = \mathbf A' + \mathbf B', but (A+B)−1{(\mathbf A + \mathbf B)^{-1}} has no simple formula
Both at once(A′)−1=(A−1)′(\mathbf A')^{-1} = (\mathbf A^{-1})'
Diagonaldiag⁡(d1,…,dn)−1=diag⁡(1/d1,…,1/dn)\operatorname{diag}(d_1, \dots, d_n)^{-1} = \operatorname{diag}(1/d_1, \dots, 1/d_n) when every di≠0{d_i \ne 0}

In products the order reverses. A number is its own transpose, so x′Ay=y′A′x.{\mathbf x'\mathbf A\mathbf y = \mathbf y'\mathbf A'\mathbf x}. The inverse of a symmetric matrix is symmetric, and for invertible A\mathbf A the system Ax=b{\mathbf A\mathbf x = \mathbf b} has exactly one solution, x=A−1b.{\mathbf x = \mathbf A^{-1}\mathbf b}. For a 2×2{2 \times 2} matrix with ad−bc≠0,{ad - bc \ne 0},

[abcd]−1=1ad−bc[d−b−ca]\begin{bmatrix} a & b \\ c & d \end{bmatrix}^{-1} = \frac{1}{ad - bc}\begin{bmatrix} d & -b \\ -c & a \end{bmatrix}

Orthogonal matrices

A square matrix Q\mathbf Q is orthogonal when Q′Q=QQ′=I,{\mathbf Q'\mathbf Q = \mathbf Q\mathbf Q' = \mathbf I}, so Q−1=Q′.{\mathbf Q^{-1} = \mathbf Q'}. Its columns have length 1 and are mutually orthogonal, and so are its rows. It keeps lengths and angles, since (Qx)′(Qy)=x′y,(\mathbf Q\mathbf x)'(\mathbf Q\mathbf y) = \mathbf x'\mathbf y, so geometrically it is a rotation, possibly combined with a reflection, and ∣Q∣=±1.{\lvert \mathbf Q \rvert = \pm 1}.

Principal components use an orthogonal matrix whose columns are the eigenvectors of Σ,\boldsymbol\Sigma, so the components are the original variables seen along rotated axes.

Determinant, trace and rank

Determinant

For a 2×2{2 \times 2} matrix

∣abcd∣=ad−bc\begin{vmatrix} a & b \\ c & d \end{vmatrix} = ad - bc

For larger matrices, expanding along any row ii gives

∣A∣=∑j=1n(−1)i+jaij∣Mij∣,\lvert \mathbf A \rvert = \sum_{j=1}^n (-1)^{i+j}a_{ij}\lvert \mathbf M_{ij} \rvert,

where Mij\mathbf M_{ij} is A\mathbf A with row ii and column jj deleted. Columns work the same way.

For n×n{n \times n} matrices A\mathbf A and B\mathbf B and a number c,c,

RuleFormula
Transpose∣A′∣=∣A∣{\lvert \mathbf A' \rvert = \lvert \mathbf A \rvert}
Product∣AB∣=∣A∣ ∣B∣\lvert \mathbf A\mathbf B \rvert = \lvert \mathbf A \rvert\,\lvert \mathbf B \rvert
Scalar multiple∣cA∣=cn ∣A∣{\lvert c\mathbf A \rvert = c^n\,\lvert \mathbf A \rvert}
Inverse∣A−1∣=1/∣A∣\lvert \mathbf A^{-1} \rvert = 1 / \lvert \mathbf A \rvert when A\mathbf A is invertible
Diagonal or triangularThe product of the diagonal entries
InvertibilityA\mathbf A is invertible exactly when ∣A∣≠0{\lvert \mathbf A \rvert \ne 0}

Watch for two traps. The scalar rule has cn,c^n, not c,c, because each of the nn rows is multiplied by c.c. And ∣A+B∣{\lvert \mathbf A + \mathbf B \rvert} is not ∣A∣+∣B∣{\lvert \mathbf A \rvert + \lvert \mathbf B \rvert} in general.

Geometrically, ∣A∣\lvert \mathbf A \rvert is the factor by which A\mathbf A scales volume, with a sign for orientation. A zero determinant means some direction gets squashed flat.

Trace

The trace of a square matrix is the sum of its diagonal entries, tr⁡(A)=a11+a22+⋯+ann.\operatorname{tr}(\mathbf A) = a_{11} + a_{22} + \dots + a_{nn}.

RuleFormula
Lineartr⁡(A+B)=tr⁡(A)+tr⁡(B)\operatorname{tr}(\mathbf A + \mathbf B) = \operatorname{tr}(\mathbf A) + \operatorname{tr}(\mathbf B) and tr⁡(cA)=ctr⁡(A)\operatorname{tr}(c\mathbf A) = c\operatorname{tr}(\mathbf A)
Transposetr⁡(A′)=tr⁡(A)\operatorname{tr}(\mathbf A') = \operatorname{tr}(\mathbf A)
Swaptr⁡(AB)=tr⁡(BA)\operatorname{tr}(\mathbf A\mathbf B) = \operatorname{tr}(\mathbf B\mathbf A) for A\mathbf A of size m×n{m \times n} and B\mathbf B of size n×m{n \times m}
Cyclictr⁡(ABC)=tr⁡(BCA)=tr⁡(CAB)\operatorname{tr}(\mathbf A\mathbf B\mathbf C) = \operatorname{tr}(\mathbf B\mathbf C\mathbf A) = \operatorname{tr}(\mathbf C\mathbf A\mathbf B)
Sum of squarestr⁡(A′A)=∑i∑jaij2\operatorname{tr}(\mathbf A'\mathbf A) = \sum_i\sum_j a_{ij}^2
Change of basistr⁡(B−1AB)=tr⁡(A),\operatorname{tr}(\mathbf B^{-1}\mathbf A\mathbf B) = \operatorname{tr}(\mathbf A), so tr⁡(P′AP)=tr⁡(A)\operatorname{tr}(\mathbf P'\mathbf A\mathbf P) = \operatorname{tr}(\mathbf A) for orthogonal P\mathbf P

The cyclic rule only lets you move a factor from one end of the product to the other. In general tr⁡(ABC)\operatorname{tr}(\mathbf A\mathbf B\mathbf C) and tr⁡(ACB)\operatorname{tr}(\mathbf A\mathbf C\mathbf B) differ, and tr⁡(AB)\operatorname{tr}(\mathbf A\mathbf B) is not tr⁡(A)tr⁡(B).\operatorname{tr}(\mathbf A)\operatorname{tr}(\mathbf B).

A trick that comes up again and again is writing a quadratic form, which is a single number, as a trace.

x′Ax=tr⁡(x′Ax)=tr⁡(Axx′)\mathbf x'\mathbf A\mathbf x = \operatorname{tr}(\mathbf x'\mathbf A\mathbf x) = \operatorname{tr}(\mathbf A\mathbf x\mathbf x')

The first step holds because a 1×1{1 \times 1} matrix is its own trace, and the second is the cyclic rule. It collapses a sum of quadratic forms into one trace, for example

∑j=1n(xj−xˉ)′A(xj−xˉ)=(n−1)tr⁡(AS).\sum_{j=1}^n (\mathbf x_j - \bar{\mathbf x})'\mathbf A(\mathbf x_j - \bar{\mathbf x}) = (n-1)\operatorname{tr}(\mathbf A\mathbf S).

Rank

The rank of a matrix is the largest number of linearly independent columns it has, which always equals the largest number of linearly independent rows. For an m×n{m \times n} matrix A\mathbf A the rank is at most min⁡(m,n),\min(m, n), and

rank⁡(A′)=rank⁡(A′A)=rank⁡(AA′)=rank⁡(A).\operatorname{rank}(\mathbf A') = \operatorname{rank}(\mathbf A'\mathbf A) = \operatorname{rank}(\mathbf A\mathbf A') = \operatorname{rank}(\mathbf A).

Whenever the product AB\mathbf A\mathbf B is defined,

rank⁡(AB)≤min⁡(rank⁡(A),rank⁡(B)).\operatorname{rank}(\mathbf A\mathbf B) \le \min\left(\operatorname{rank}(\mathbf A), \operatorname{rank}(\mathbf B)\right).

Multiplying by an invertible matrix leaves the rank unchanged, and for nonzero vectors the outer product xy′\mathbf x\mathbf y' has rank 1.

For a square n×n{n \times n} matrix A\mathbf A these all say the same thing. The matrix A\mathbf A is invertible, ∣A∣≠0,{\lvert \mathbf A \rvert \ne 0}, rank⁡(A)=n{\operatorname{rank}(\mathbf A) = n} (full rank), the columns are linearly independent, Ax=0{\mathbf A\mathbf x = \mathbf 0} only for x=0,{\mathbf x = \mathbf 0}, and no eigenvalue is 0.

A sample covariance matrix built from nn observations has rank at most n−1,{n - 1}, because the deviations xj−xˉ{\mathbf x_j - \bar{\mathbf x}} add up to 0.\mathbf 0. So with pp variables, S\mathbf S is singular whenever n≤p.{n \le p}.

Eigenvalues

Definition

A number λ\lambda is an eigenvalue of a square matrix A,\mathbf A, with eigenvector x,\mathbf x, when

Ax=λx,x≠0\mathbf A\mathbf x = \lambda\mathbf x, \qquad \mathbf x \ne \mathbf 0

So A\mathbf A only stretches x\mathbf x by the factor λ\lambda and keeps it on the same line. Any nonzero multiple of an eigenvector is again an eigenvector, so it is common to scale to length 1 and write e=x/∥x∥.{\mathbf e = \mathbf x / \lVert \mathbf x \rVert}. The sign is still a free choice, which is why two programs can report eigenvectors with opposite signs.

Characteristic equation

The eigenvalues are the solutions of

∣A−λI∣=0\lvert \mathbf A - \lambda\mathbf I \rvert = 0

For an n×n{n \times n} matrix this is a polynomial equation of degree n,n, so there are nn eigenvalues counting repeats. Some may be complex numbers when A\mathbf A is not symmetric. For each eigenvalue, the eigenvectors are the nonzero solutions of (A−λI)x=0.{(\mathbf A - \lambda\mathbf I)\mathbf x = \mathbf 0}. For a 2×2{2 \times 2} matrix the equation is

λ2−tr⁡(A) λ+∣A∣=0\lambda^2 - \operatorname{tr}(\mathbf A)\,\lambda + \lvert \mathbf A \rvert = 0

Useful facts

For any n×n{n \times n} matrix A\mathbf A with eigenvalues λ1,…,λn,\lambda_1, \dots, \lambda_n, repeats and complex values included,

FactFormula
Sumλ1+λ2+⋯+λn=tr⁡(A)\lambda_1 + \lambda_2 + \dots + \lambda_n = \operatorname{tr}(\mathbf A)
Productλ1λ2⋯λn=∣A∣\lambda_1\lambda_2\cdots\lambda_n = \lvert \mathbf A \rvert
InvertibleExactly when no λi\lambda_i is 0
InverseFor invertible A,\mathbf A, A−1\mathbf A^{-1} has eigenvalues 1/λi1/\lambda_i with the same eigenvectors
PowersAk\mathbf A^k has eigenvalues λik\lambda_i^k with the same eigenvectors, k=1,2,…{k = 1, 2, \dots}
ShiftA+c I{\mathbf A + c\,\mathbf I} has eigenvalues λi+c{\lambda_i + c} with the same eigenvectors
Diagonal or triangularThe eigenvalues are the diagonal entries
ProductsCD\mathbf C\mathbf D and DC\mathbf D\mathbf C have the same nonzero eigenvalues, for C\mathbf C of size m×n{m \times n} and D\mathbf D of size n×m{n \times m}

The last row is why W−1B\mathbf W^{-1}\mathbf B in MANOVA, with W\mathbf W positive definite and B\mathbf B symmetric, has the same eigenvalues as the symmetric matrix W−1/2BW−1/2\mathbf W^{-1/2}\mathbf B\mathbf W^{-1/2} from functions of a matrix, so they are real.

Symmetric matrices

When A\mathbf A is symmetric, as every covariance matrix is, the picture is much cleaner.

  • Every eigenvalue is real.
  • Eigenvectors for different eigenvalues are orthogonal.
  • There is always a full set of nn orthonormal eigenvectors, meaning length 1 and mutually orthogonal, even when eigenvalues repeat. With a repeated eigenvalue they are not unique, since any orthonormal basis of the space spanned by that eigenvalue’s eigenvectors works.
  • The rank is the number of nonzero eigenvalues, counted with multiplicity.

The orthogonality takes one line. If Aei=λiei{\mathbf A\mathbf e_i = \lambda_i\mathbf e_i} and Aej=λjej,{\mathbf A\mathbf e_j = \lambda_j\mathbf e_j}, then

λi ej′ei=ej′Aei=(Aej)′ei=λj ej′ei\lambda_i\,\mathbf e_j'\mathbf e_i = \mathbf e_j'\mathbf A\mathbf e_i = (\mathbf A\mathbf e_j)'\mathbf e_i = \lambda_j\,\mathbf e_j'\mathbf e_i

using A′=A{\mathbf A' = \mathbf A} in the middle, so ej′ei=0{\mathbf e_j'\mathbf e_i = 0} whenever λi≠λj.{\lambda_i \ne \lambda_j}.

Worked example

Take

A=[5222]\mathbf A = \begin{bmatrix} 5 & 2 \\ 2 & 2 \end{bmatrix}

Here tr⁡(A)=7{\operatorname{tr}(\mathbf A) = 7} and ∣A∣=10−4=6,{\lvert \mathbf A \rvert = 10 - 4 = 6}, so the characteristic equation is

λ2−7λ+6=(λ−6)(λ−1)=0\lambda^2 - 7\lambda + 6 = (\lambda - 6)(\lambda - 1) = 0

and the eigenvalues are λ1=6{\lambda_1 = 6} and λ2=1.{\lambda_2 = 1}.

For λ1=6.{\lambda_1 = 6}. The system (A−6 I)x=0{(\mathbf A - 6\,\mathbf I)\mathbf x = \mathbf 0} is

[−122−4][x1x2]=[00]\begin{bmatrix} -1 & 2 \\ 2 & -4 \end{bmatrix}\begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}

Both rows say x1=2x2,{x_1 = 2x_2}, so x=(2,1)′{\mathbf x = (2, 1)'} works and e1=15(2,1)′.{\mathbf e_1 = \frac{1}{\sqrt 5}(2, 1)'}.

For λ2=1.{\lambda_2 = 1}. Now (A−I)x=0{(\mathbf A - \mathbf I)\mathbf x = \mathbf 0} has rows 4x1+2x2=0{4x_1 + 2x_2 = 0} and 2x1+x2=0.{2x_1 + x_2 = 0}. Both say x2=−2x1,{x_2 = -2x_1}, so x=(1,−2)′{\mathbf x = (1, -2)'} works and e2=15(1,−2)′.{\mathbf e_2 = \frac{1}{\sqrt 5}(1, -2)'}.

Checks.

CheckResult
A (2,1)′\mathbf A\,(2, 1)'(10+2, 4+2)′=(12,6)′=6 (2,1)′(10 + 2,\ 4 + 2)' = (12, 6)' = 6\,(2, 1)'
A (1,−2)′\mathbf A\,(1, -2)'(5−4, 2−4)′=(1,−2)′=1 (1,−2)′(5 - 4,\ 2 - 4)' = (1, -2)' = 1\,(1, -2)'
e1′e2\mathbf e_1'\mathbf e_215(2−2)=0{\frac{1}{5}(2 - 2) = 0}
Sum of eigenvalues6+1=7=tr⁡(A)6 + 1 = 7 = \operatorname{tr}(\mathbf A)
Product of eigenvalues6×1=6=∣A∣6 \times 1 = 6 = \lvert \mathbf A \rvert

The example continues in the next section.

Spectral decomposition

The decomposition

Every symmetric p×p{p \times p} matrix A\mathbf A can be written as

A=PΛP′=∑i=1pλi eiei′\mathbf A = \mathbf P\boldsymbol\Lambda\mathbf P' = \sum_{i=1}^p \lambda_i\,\mathbf e_i\mathbf e_i'

where λ1,…,λp\lambda_1, \dots, \lambda_p are its eigenvalues, e1,…,ep\mathbf e_1, \dots, \mathbf e_p are matching orthonormal eigenvectors, P=[e1 ⋯ ep]{\mathbf P = [\mathbf e_1 \ \cdots \ \mathbf e_p]} has them as columns and Λ=diag⁡(λ1,…,λp).\boldsymbol\Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_p). Since P\mathbf P is orthogonal, the same fact reads P′AP=Λ.{\mathbf P'\mathbf A\mathbf P = \boldsymbol\Lambda}.

The sum builds A\mathbf A from pp rank one pieces, one per eigenvector, each weighted by its eigenvalue.

Functions of a matrix

Apply the function to the eigenvalues and keep the eigenvectors.

MatrixFormulaNeeds
A−1\mathbf A^{-1}PΛ−1P′=∑i1λi eiei′\mathbf P\boldsymbol\Lambda^{-1}\mathbf P' = \sum_i \dfrac{1}{\lambda_i}\,\mathbf e_i\mathbf e_i'Every λi≠0{\lambda_i \ne 0}
Ak\mathbf A^kPΛkP′=∑iλik eiei′\mathbf P\boldsymbol\Lambda^k\mathbf P' = \sum_i \lambda_i^k\,\mathbf e_i\mathbf e_i'k=1,2,…{k = 1, 2, \dots}
A1/2\mathbf A^{1/2}PΛ1/2P′=∑iλi eiei′\mathbf P\boldsymbol\Lambda^{1/2}\mathbf P' = \sum_i \sqrt{\lambda_i}\,\mathbf e_i\mathbf e_i'Every λi≥0{\lambda_i \ge 0}
A−1/2\mathbf A^{-1/2}PΛ−1/2P′=∑i1λi eiei′\mathbf P\boldsymbol\Lambda^{-1/2}\mathbf P' = \sum_i \dfrac{1}{\sqrt{\lambda_i}}\,\mathbf e_i\mathbf e_i'Every λi>0{\lambda_i > 0}

For a positive definite A,\mathbf A, which for a symmetric matrix means every eigenvalue is positive (see quadratic forms), all four exist and the square roots behave the way the notation suggests. The root A1/2\mathbf A^{1/2} is symmetric and positive definite, and

A1/2A1/2=A,A−1/2=(A1/2)−1,A−1/2A−1/2=A−1,A1/2A−1/2=A−1/2A1/2=I.\begin{aligned} \mathbf A^{1/2}\mathbf A^{1/2} &= \mathbf A, \\ \mathbf A^{-1/2} &= (\mathbf A^{1/2})^{-1}, \\ \mathbf A^{-1/2}\mathbf A^{-1/2} &= \mathbf A^{-1}, \\ \mathbf A^{1/2}\mathbf A^{-1/2} = \mathbf A^{-1/2}\mathbf A^{1/2} &= \mathbf I. \end{aligned}

For a positive definite covariance matrix this gives Σ−1/2,\boldsymbol\Sigma^{-1/2}, the matrix that standardizes a random vector.

Back to the example

For the example matrix A\mathbf A with eigenvalues λ1=6{\lambda_1 = 6} and λ2=1,{\lambda_2 = 1}, the rank one pieces are

e1e1′=15[4221],e2e2′=15[1−2−24]\mathbf e_1\mathbf e_1' = \frac{1}{5}\begin{bmatrix} 4 & 2 \\ 2 & 1 \end{bmatrix}, \qquad \mathbf e_2\mathbf e_2' = \frac{1}{5}\begin{bmatrix} 1 & -2 \\ -2 & 4 \end{bmatrix}

and they rebuild A.\mathbf A.

6 e1e1′+1 e2e2′=15[24+112−212−26+4]=[5222]\begin{aligned} 6\,\mathbf e_1\mathbf e_1' + 1\,\mathbf e_2\mathbf e_2' &= \frac{1}{5}\begin{bmatrix} 24 + 1 & 12 - 2 \\ 12 - 2 & 6 + 4 \end{bmatrix} \\ &= \begin{bmatrix} 5 & 2 \\ 2 & 2 \end{bmatrix} \end{aligned}

The inverse comes out the same way and agrees with the 2×2{2 \times 2} formula.

A−1=16 e1e1′+e2e2′=130[4+62−122−121+24]=16[2−2−25]\begin{aligned} \mathbf A^{-1} &= \frac{1}{6}\,\mathbf e_1\mathbf e_1' + \mathbf e_2\mathbf e_2' \\ &= \frac{1}{30}\begin{bmatrix} 4 + 6 & 2 - 12 \\ 2 - 12 & 1 + 24 \end{bmatrix} = \frac{1}{6}\begin{bmatrix} 2 & -2 \\ -2 & 5 \end{bmatrix} \end{aligned}

So does the square root.

A1/2=6 e1e1′+e2e2′=15[46+126−226−26+4]\begin{aligned} \mathbf A^{1/2} &= \sqrt 6\,\mathbf e_1\mathbf e_1' + \mathbf e_2\mathbf e_2' \\ &= \frac{1}{5}\begin{bmatrix} 4\sqrt 6 + 1 & 2\sqrt 6 - 2 \\ 2\sqrt 6 - 2 & \sqrt 6 + 4 \end{bmatrix} \end{aligned}

Squaring it gives back A,\mathbf A, because (eiei′)(ejej′)(\mathbf e_i\mathbf e_i')(\mathbf e_j\mathbf e_j') is eiei′\mathbf e_i\mathbf e_i' when i=j{i = j} and 0\mathbf 0 otherwise.

Projection matrices

A symmetric matrix H\mathbf H with HH=H{\mathbf H\mathbf H = \mathbf H} is a projection matrix. Its eigenvalues are all 0 or 1, so its rank equals its trace. Two come up often.

  • The centering matrix C=I−1n11′{\mathbf C = \mathbf I - \frac{1}{n}\mathbf 1\mathbf 1'} turns a data column into its deviation vector, Cy=y−yˉ 1.{\mathbf C\mathbf y = \mathbf y - \bar y\,\mathbf 1}. It has rank n−1,{n - 1}, and (n−1) S=X′CX(n-1)\,\mathbf S = \mathbf X'\mathbf C\mathbf X for an n×p{n \times p} data matrix X.\mathbf X.
  • For Z\mathbf Z with rr linearly independent columns, H=Z(Z′Z)−1Z′{\mathbf H = \mathbf Z(\mathbf Z'\mathbf Z)^{-1}\mathbf Z'} projects onto the span of those columns and gives the fitted values y^=Hy{\hat{\mathbf y} = \mathbf H\mathbf y} in regression. It has rank r.r.

Quadratic forms

The form

For a symmetric p×p{p \times p} matrix A,\mathbf A, the quadratic form is the number

x′Ax=∑i=1p∑j=1paij xixj\mathbf x'\mathbf A\mathbf x = \sum_{i=1}^p\sum_{j=1}^p a_{ij}\,x_ix_j

For p=2{p = 2} it is a11x12+2a12x1x2+a22x22.a_{11}x_1^2 + 2a_{12}x_1x_2 + a_{22}x_2^2. A nonsymmetric A\mathbf A gives the same form as its symmetric part 12(A+A′),{\frac{1}{2}(\mathbf A + \mathbf A')}, so assuming symmetry costs nothing.

With the spectral decomposition and y=P′x,{\mathbf y = \mathbf P'\mathbf x},

x′Ax=∑i=1pλi (ei′x)2=λ1y12+⋯+λpyp2\mathbf x'\mathbf A\mathbf x = \sum_{i=1}^p \lambda_i\,(\mathbf e_i'\mathbf x)^2 = \lambda_1y_1^2 + \dots + \lambda_py_p^2

In the eigenvector coordinates a quadratic form is just a weighted sum of squares, so the signs of the eigenvalues decide its sign.

Definite matrices

NameDefinitionEigenvalues
Positive definitex′Ax>0{\mathbf x'\mathbf A\mathbf x > 0} for every x≠0{\mathbf x \ne \mathbf 0}All λi>0{\lambda_i > 0}
Positive semidefinitex′Ax≥0{\mathbf x'\mathbf A\mathbf x \ge 0} for every x\mathbf xAll λi≥0{\lambda_i \ge 0}
IndefiniteTakes both positive and negative valuesSome positive and some negative

Positive semidefinite is also called nonnegative definite. For a symmetric A,\mathbf A, each of these is equivalent to being positive definite.

  • every eigenvalue is positive
  • it can be written as A=B′B{\mathbf A = \mathbf B'\mathbf B} for some matrix B\mathbf B with linearly independent columns
  • every leading principal minor is positive, meaning the determinants of the top left blocks of sizes 1×1,2×2,…,p×p{1 \times 1}, {2 \times 2}, \dots, {p \times p}

For a 2×2{2 \times 2} matrix the last test reads a11>0{a_{11} > 0} and a11a22−a122>0.{a_{11}a_{22} - a_{12}^2 > 0}. The example matrix passes with 5>0{5 > 0} and 10−4=6>0,{10 - 4 = 6 > 0}, matching its eigenvalues 6 and 1.

For positive semidefinite, the first two carry over with ≥0{\ge 0} and any matrix B,\mathbf B, but the minor test does not. The matrix [000−1]\begin{bmatrix} 0 & 0 \\ 0 & -1 \end{bmatrix} has leading minors 0 and 0 but is not positive semidefinite.

A few more facts.

  • A positive definite matrix is invertible, has ∣A∣>0{\lvert \mathbf A \rvert > 0} and positive diagonal entries, and its inverse is positive definite.
  • A positive semidefinite matrix is positive definite exactly when it is invertible.
  • If A\mathbf A is positive semidefinite, so is CAC′.\mathbf C\mathbf A\mathbf C'. If A\mathbf A is positive definite and the rows of C\mathbf C are linearly independent, CAC′\mathbf C\mathbf A\mathbf C' is positive definite. This is why CSC′\mathbf C\mathbf S\mathbf C' in a test of contrasts can be inverted when S\mathbf S is positive definite.

Distance and ellipsoids

For a positive definite A,\mathbf A, the number

(x−y)′A(x−y)\sqrt{(\mathbf x - \mathbf y)'\mathbf A(\mathbf x - \mathbf y)}

is a distance between the points x\mathbf x and y.\mathbf y. The points with

(x−μ)′A(x−μ)=c2(\mathbf x - \boldsymbol\mu)'\mathbf A(\mathbf x - \boldsymbol\mu) = c^2

form an ellipsoid centered at μ,\boldsymbol\mu, with axes along the eigenvectors ei\mathbf e_i of A\mathbf A and half lengths c/λi.c/\sqrt{\lambda_i}. When A=Σ−1{\mathbf A = \boldsymbol\Sigma^{-1}} the eigenvectors are those of Σ,\boldsymbol\Sigma, the eigenvalues are the reciprocals of those of Σ,\boldsymbol\Sigma, and the half lengths become cλic\sqrt{\lambda_i} with λi\lambda_i now the eigenvalues of Σ.\boldsymbol\Sigma. That is the picture behind density contours and Mahalanobis distance.

Covariance matrices

For any constant vector a,\mathbf a,

a′Σa=Var⁡(a′X)≥0\mathbf a'\boldsymbol\Sigma\mathbf a = \operatorname{Var}(\mathbf a'\mathbf X) \ge 0

so every covariance matrix is positive semidefinite. It fails to be positive definite exactly when Var⁡(a′X)=0{\operatorname{Var}(\mathbf a'\mathbf X) = 0} for some a≠0.{\mathbf a \ne \mathbf 0}. Then that combination of the variables is constant with probability 1, so any variable with a nonzero coefficient in a\mathbf a is determined exactly by the others through a linear formula. The same argument covers S,\mathbf S, because a′Sa\mathbf a'\mathbf S\mathbf a is the sample variance of the numbers a′x1,…,a′xn.\mathbf a'\mathbf x_1, \dots, \mathbf a'\mathbf x_n. Correlation matrices are positive semidefinite for the same reason.

Maximizing a quadratic form

Let B\mathbf B be symmetric p×p{p \times p} with eigenvalues λ1≥λ2≥⋯≥λp\lambda_1 \ge \lambda_2 \ge \dots \ge\nobreak \lambda_p and orthonormal eigenvectors e1,…,ep.\mathbf e_1, \dots, \mathbf e_p. Then

max⁡x≠0x′Bxx′x=λ1  at x=e1\max_{\mathbf x \ne \mathbf 0}\frac{\mathbf x'\mathbf B\mathbf x}{\mathbf x'\mathbf x} = \lambda_1 \ \text{ at } \mathbf x = \mathbf e_1 min⁡x≠0x′Bxx′x=λp  at x=ep\min_{\mathbf x \ne \mathbf 0}\frac{\mathbf x'\mathbf B\mathbf x}{\mathbf x'\mathbf x} = \lambda_p \ \text{ at } \mathbf x = \mathbf e_p

and among nonzero x\mathbf x perpendicular to the first kk eigenvectors, for k=1,2,…,p−1,k = 1, 2, \dots, p - 1,

max⁡x⊥e1,…,ekx′Bxx′x=λk+1  at x=ek+1\max_{\mathbf x \perp \mathbf e_1, \dots, \mathbf e_k}\frac{\mathbf x'\mathbf B\mathbf x}{\mathbf x'\mathbf x} = \lambda_{k+1} \ \text{ at } \mathbf x = \mathbf e_{k+1}

The ratio does not change when x\mathbf x is rescaled, so these are the same as maximizing x′Bx\mathbf x'\mathbf B\mathbf x over unit vectors x′x=1,{\mathbf x'\mathbf x = 1}, and any nonzero multiple of e1\mathbf e_1 also gives the maximum.

Why it works. Write x=Py.{\mathbf x = \mathbf P\mathbf y}. Then x′x=y′y{\mathbf x'\mathbf x = \mathbf y'\mathbf y} and

x′Bx=∑i=1pλiyi2≤λ1∑i=1pyi2=λ1 x′x\mathbf x'\mathbf B\mathbf x = \sum_{i=1}^p \lambda_i y_i^2 \le \lambda_1\sum_{i=1}^p y_i^2 = \lambda_1\,\mathbf x'\mathbf x

with equality at y=(1,0,…,0)′,{\mathbf y = (1, 0, \dots, 0)'}, which is x=e1.{\mathbf x = \mathbf e_1}. Being perpendicular to e1,…,ek\mathbf e_1, \dots, \mathbf e_k forces y1=⋯=yk=0,{y_1 = \dots = y_k = 0}, and the same argument then gives λk+1.\lambda_{k+1}.

Principal components. Take B=Σ.{\mathbf B = \boldsymbol\Sigma}. Since Var⁡(a′X)=a′Σa,\operatorname{Var}(\mathbf a'\mathbf X) = \mathbf a'\boldsymbol\Sigma\mathbf a, the unit vector a\mathbf a with the largest variance is e1,\mathbf e_1, and the variance is λ1.\lambda_1. That is the first principal component. Because Cov⁡(a′X,e1′X)=a′Σe1=λ1 a′e1,\operatorname{Cov}(\mathbf a'\mathbf X, \mathbf e_1'\mathbf X) = \mathbf a'\boldsymbol\Sigma\mathbf e_1 = \lambda_1\,\mathbf a'\mathbf e_1, being uncorrelated with the first component means a⊥e1{\mathbf a \perp \mathbf e_1} when λ1>0,{\lambda_1 > 0}, so the second component is e2′X\mathbf e_2'\mathbf X with variance λ2,\lambda_2, and so on.

A ratio of two forms. For symmetric B\mathbf B and positive definite W,\mathbf W,

max⁡x≠0x′Bxx′Wx\max_{\mathbf x \ne \mathbf 0}\frac{\mathbf x'\mathbf B\mathbf x}{\mathbf x'\mathbf W\mathbf x}

is the largest eigenvalue of W−1B,\mathbf W^{-1}\mathbf B, reached at a matching eigenvector. Substituting y=W1/2x{\mathbf y = \mathbf W^{1/2}\mathbf x} turns it into the previous problem with the symmetric matrix W−1/2BW−1/2,\mathbf W^{-1/2}\mathbf B\mathbf W^{-1/2}, which has the same eigenvalues as W−1B.\mathbf W^{-1}\mathbf B. This is the problem behind Fisher’s discriminant functions for several groups.

Extended Cauchy-Schwarz inequality

Let B\mathbf B be positive definite. For any vectors b\mathbf b and d,\mathbf d,

(b′d)2≤(b′Bb)(d′B−1d)(\mathbf b'\mathbf d)^2 \le (\mathbf b'\mathbf B\mathbf b)(\mathbf d'\mathbf B^{-1}\mathbf d)

with equality exactly when b=c B−1d{\mathbf b = c\,\mathbf B^{-1}\mathbf d} or d=c Bb{\mathbf d = c\,\mathbf B\mathbf b} for some number c.c. It is the ordinary inequality applied to B1/2b\mathbf B^{1/2}\mathbf b and B−1/2d,\mathbf B^{-1/2}\mathbf d, and with B=I{\mathbf B = \mathbf I} it is the ordinary inequality.

The version used most is a maximization. For positive definite B\mathbf B and a fixed d≠0,{\mathbf d \ne \mathbf 0},

max⁡x≠0(x′d)2x′Bx=d′B−1d\max_{\mathbf x \ne \mathbf 0}\frac{(\mathbf x'\mathbf d)^2}{\mathbf x'\mathbf B\mathbf x} = \mathbf d'\mathbf B^{-1}\mathbf d

reached at x=c B−1d{\mathbf x = c\,\mathbf B^{-1}\mathbf d} for any c≠0.{c \ne 0}.

  • With B=S{\mathbf B = \mathbf S} and d=xˉ−μ0,{\mathbf d = \bar{\mathbf x} - \boldsymbol\mu_0}, it says Hotelling’s T2T^2 is the largest squared tt statistic over all combinations a′xˉ,\mathbf a'\bar{\mathbf x}, which is where the simultaneous intervals come from.
  • With B=Spooled{\mathbf B = \mathbf S_{\text{pooled}}} and d=xˉ1−xˉ2,{\mathbf d = \bar{\mathbf x}_1 - \bar{\mathbf x}_2}, it gives Fisher’s linear discriminant a=Spooled−1(xˉ1−xˉ2).\mathbf a = \mathbf S_{\text{pooled}}^{-1}(\bar{\mathbf x}_1 - \bar{\mathbf x}_2).

Partitioned matrices

Split a p×p{p \times p} matrix after its first qq rows and columns, so A11\mathbf A_{11} is q×q{q \times q} and A22\mathbf A_{22} is (p−q)×(p−q).{(p - q) \times (p - q)}.

A=[A11A12A21A22]\mathbf A = \begin{bmatrix} \mathbf A_{11} & \mathbf A_{12} \\ \mathbf A_{21} & \mathbf A_{22} \end{bmatrix}

Blocks multiply like entries, as long as the sizes fit and you keep the order of each product. The transpose A′\mathbf A' has A11′\mathbf A_{11}' and A22′\mathbf A_{22}' on the diagonal, A21′\mathbf A_{21}' top right and A12′\mathbf A_{12}' bottom left, so a symmetric A\mathbf A has A21=A12′.{\mathbf A_{21} = \mathbf A_{12}'}.

Block determinant

ConditionFormula
A11\mathbf A_{11} invertible∣A∣=∣A11∣ ∣A22−A21A11−1A12∣\lvert \mathbf A \rvert = \lvert \mathbf A_{11} \rvert\,\lvert \mathbf A_{22} - \mathbf A_{21}\mathbf A_{11}^{-1}\mathbf A_{12} \rvert
A22\mathbf A_{22} invertible∣A∣=∣A22∣ ∣A11−A12A22−1A21∣\lvert \mathbf A \rvert = \lvert \mathbf A_{22} \rvert\,\lvert \mathbf A_{11} - \mathbf A_{12}\mathbf A_{22}^{-1}\mathbf A_{21} \rvert
A12=0{\mathbf A_{12} = \mathbf 0} or A21=0{\mathbf A_{21} = \mathbf 0}∣A∣=∣A11∣ ∣A22∣\lvert \mathbf A \rvert = \lvert \mathbf A_{11} \rvert\,\lvert \mathbf A_{22} \rvert

The first row comes from the factorization

A=[I0A21A11−1I][A11A120A22−A21A11−1A12]\mathbf A = \begin{bmatrix} \mathbf I & \mathbf 0 \\ \mathbf A_{21}\mathbf A_{11}^{-1} & \mathbf I \end{bmatrix} \begin{bmatrix} \mathbf A_{11} & \mathbf A_{12} \\ \mathbf 0 & \mathbf A_{22} - \mathbf A_{21}\mathbf A_{11}^{-1}\mathbf A_{12} \end{bmatrix}

in which the first factor has determinant 1 and the second is block triangular.

Block inverse

When A11\mathbf A_{11} is invertible, the matrix

A22⋅1=A22−A21A11−1A12\mathbf A_{22\cdot 1} = \mathbf A_{22} - \mathbf A_{21}\mathbf A_{11}^{-1}\mathbf A_{12}

is the Schur complement of A11.\mathbf A_{11}. By the determinant rule, A\mathbf A is invertible exactly when A22⋅1\mathbf A_{22\cdot 1} is, and then

A−1=[A11−1+A11−1A12A22⋅1−1A21A11−1−A11−1A12A22⋅1−1−A22⋅1−1A21A11−1A22⋅1−1]\mathbf A^{-1} = \begin{bmatrix} \mathbf A_{11}^{-1} + \mathbf A_{11}^{-1}\mathbf A_{12}\mathbf A_{22\cdot 1}^{-1}\mathbf A_{21}\mathbf A_{11}^{-1} & -\mathbf A_{11}^{-1}\mathbf A_{12}\mathbf A_{22\cdot 1}^{-1} \\ -\mathbf A_{22\cdot 1}^{-1}\mathbf A_{21}\mathbf A_{11}^{-1} & \mathbf A_{22\cdot 1}^{-1} \end{bmatrix}

Swapping the roles of the blocks gives the version for invertible A22,\mathbf A_{22}, with

A11⋅2=A11−A12A22−1A21\mathbf A_{11\cdot 2} = \mathbf A_{11} - \mathbf A_{12}\mathbf A_{22}^{-1}\mathbf A_{21}

in place of A22⋅1.\mathbf A_{22\cdot 1}. So when A,\mathbf A, A11\mathbf A_{11} and A22\mathbf A_{22} are all invertible, the bottom right block of A−1\mathbf A^{-1} is A22⋅1−1\mathbf A_{22\cdot 1}^{-1} and the top left block is A11⋅2−1,\mathbf A_{11\cdot 2}^{-1}, inverses of Schur complements rather than of the matching blocks of A.\mathbf A. If A12=0{\mathbf A_{12} = \mathbf 0} and A21=0,{\mathbf A_{21} = \mathbf 0}, the inverse is block diagonal with blocks A11−1\mathbf A_{11}^{-1} and A22−1.\mathbf A_{22}^{-1}.

In statistics. With A=Σ{\mathbf A = \boldsymbol\Sigma} positive definite, the Schur complement

Σ11⋅2=Σ11−Σ12Σ22−1Σ21\boldsymbol\Sigma_{11\cdot 2} = \boldsymbol\Sigma_{11} - \boldsymbol\Sigma_{12}\boldsymbol\Sigma_{22}^{-1}\boldsymbol\Sigma_{21}

is the covariance of the first block X(1)\mathbf X^{(1)} given the second block X(2)\mathbf X^{(2)} for a multivariate normal, as in conditional distributions. The determinant rule gives ∣Σ∣=∣Σ22∣ ∣Σ11⋅2∣,\lvert \boldsymbol\Sigma \rvert = \lvert \boldsymbol\Sigma_{22} \rvert\,\lvert \boldsymbol\Sigma_{11\cdot 2} \rvert, which is how the joint normal density factors into a marginal density times a conditional one.

Matrix calculus

Gradients

For a function ff of x∈Rp,{\mathbf x \in \mathbb R^p}, the gradient ∂f/∂x\partial f/\partial\mathbf x is the column of partial derivatives ∂f/∂x1,…,∂f/∂xp.\partial f/\partial x_1, \dots, \partial f/\partial x_p. Setting it to 0\mathbf 0 finds the stationary points. Every local maximum and minimum of a differentiable ff on Rp\mathbb R^p is one, but a stationary point can also be a saddle, so deciding which it is needs a separate argument, such as the concavity used for the normal likelihood.

FunctionGradientCondition
a′x=x′a{\mathbf a'\mathbf x = \mathbf x'\mathbf a}a\mathbf aa\mathbf a constant
x′x\mathbf x'\mathbf x2x2\mathbf xNone
x′Ax\mathbf x'\mathbf A\mathbf x(A+A′) x{(\mathbf A + \mathbf A')\,\mathbf x}Any constant square A\mathbf A
x′Ax\mathbf x'\mathbf A\mathbf x2Ax2\mathbf A\mathbf xA\mathbf A symmetric
(x−b)′A(x−b)(\mathbf x - \mathbf b)'\mathbf A(\mathbf x - \mathbf b)2A(x−b){2\mathbf A(\mathbf x - \mathbf b)}A\mathbf A symmetric
(y−Zx)′(y−Zx)(\mathbf y - \mathbf Z\mathbf x)'(\mathbf y - \mathbf Z\mathbf x)−2Z′(y−Zx)-2\mathbf Z'(\mathbf y - \mathbf Z\mathbf x)None

The symmetric formula 2Ax2\mathbf A\mathbf x is wrong for a nonsymmetric A.\mathbf A. Take

A=[0100].\mathbf A = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix}.

The form x′Ax=x1x2{\mathbf x'\mathbf A\mathbf x = x_1x_2} has gradient (x2,x1)′=(A+A′)x,(x_2, x_1)' = (\mathbf A + \mathbf A')\mathbf x, while 2Ax=(2x2,0)′.{2\mathbf A\mathbf x = (2x_2, 0)'}. Setting the last gradient in the table to 0\mathbf 0 gives the least squares equations Z′Zx=Z′y.{\mathbf Z'\mathbf Z\mathbf x = \mathbf Z'\mathbf y}.

Matrix derivatives

For a function ff of a matrix X,\mathbf X, ∂f/∂X\partial f/\partial\mathbf X is the matrix of partial derivatives ∂f/∂xij,\partial f/\partial x_{ij}, the same shape as X.\mathbf X. These formulas treat every entry of X\mathbf X as a separate variable.

FunctionDerivativeCondition
tr⁡(AX)=tr⁡(XA)\operatorname{tr}(\mathbf A\mathbf X) = \operatorname{tr}(\mathbf X\mathbf A)A′\mathbf A'A\mathbf A is n×m{n \times m} when X\mathbf X is m×n{m \times n}
a′Xb\mathbf a'\mathbf X\mathbf bab′\mathbf a\mathbf b'None
ln⁡∣X∣\ln\lvert \mathbf X \rvert(X−1)′(\mathbf X^{-1})'X\mathbf X square with ∣X∣>0{\lvert \mathbf X \rvert > 0}
tr⁡(X−1A)\operatorname{tr}(\mathbf X^{-1}\mathbf A)−(X−1AX−1)′-(\mathbf X^{-1}\mathbf A\mathbf X^{-1})'X\mathbf X invertible

When X\mathbf X and A\mathbf A are symmetric the transposes can be dropped. The entries of a symmetric matrix like Σ\boldsymbol\Sigma are not really separate, since σik=σki.{\sigma_{ik} = \sigma_{ki}}. Treating them as separate anyway is fine when the stationary point you find is symmetric, because it is then also a stationary point among symmetric matrices. The normal likelihood works out this way.

Maximum likelihood for the normal

For a random sample x1,…,xn\mathbf x_1, \dots, \mathbf x_n from Np(μ,Σ)N_p(\boldsymbol\mu, \boldsymbol\Sigma) the log likelihood, leaving out a constant that involves neither μ\boldsymbol\mu nor Σ,\boldsymbol\Sigma, is

ℓ(μ,Σ)=−n2ln⁡∣Σ∣−12∑j=1n(xj−μ)′Σ−1(xj−μ)\begin{aligned} \ell(\boldsymbol\mu, \boldsymbol\Sigma) = {} & -\frac{n}{2}\ln\lvert \boldsymbol\Sigma \rvert \\ & - \frac{1}{2}\sum_{j=1}^n (\mathbf x_j - \boldsymbol\mu)'\boldsymbol\Sigma^{-1}(\mathbf x_j - \boldsymbol\mu) \end{aligned}

Mean. The gradient in μ\boldsymbol\mu is

Σ−1∑j=1n(xj−μ)=n Σ−1(xˉ−μ),\boldsymbol\Sigma^{-1}\sum_{j=1}^n(\mathbf x_j - \boldsymbol\mu) = n\,\boldsymbol\Sigma^{-1}(\bar{\mathbf x} - \boldsymbol\mu),

which is zero only at μ^=xˉ,{\hat{\boldsymbol\mu} = \bar{\mathbf x}}, whatever Σ\boldsymbol\Sigma is.

Covariance. Put μ=xˉ,{\boldsymbol\mu = \bar{\mathbf x}}, write K=Σ−1{\mathbf K = \boldsymbol\Sigma^{-1}} and let

A=∑j=1n(xj−xˉ)(xj−xˉ)′=(n−1) S.\mathbf A = \sum_{j=1}^n(\mathbf x_j - \bar{\mathbf x})(\mathbf x_j - \bar{\mathbf x})' = (n-1)\,\mathbf S.

Since ln⁡∣Σ∣=−ln⁡∣K∣,{\ln\lvert \boldsymbol\Sigma \rvert = -\ln\lvert \mathbf K \rvert}, the trace trick gives

ℓ=n2ln⁡∣K∣−12tr⁡(KA)\ell = \frac{n}{2}\ln\lvert \mathbf K \rvert - \frac{1}{2}\operatorname{tr}(\mathbf K\mathbf A)

Its derivative in K\mathbf K is n2K−1−12A,{\frac{n}{2}\mathbf K^{-1} - \frac{1}{2}\mathbf A}, which is zero when K−1=A/n.{\mathbf K^{-1} = \mathbf A/n}. So

Σ^=1n∑j=1n(xj−xˉ)(xj−xˉ)′=n−1n S\hat{\boldsymbol\Sigma} = \frac{1}{n}\sum_{j=1}^n (\mathbf x_j - \bar{\mathbf x})(\mathbf x_j - \bar{\mathbf x})' = \frac{n-1}{n}\,\mathbf S

The answer is symmetric, and since ln⁡∣K∣\ln\lvert \mathbf K \rvert is concave it is the maximum. It needs S\mathbf S to be positive definite, which requires n>p.{n > p}.

Random vectors

Let X\mathbf X be a random vector with mean μ=E(X){\boldsymbol\mu = E(\mathbf X)} and covariance matrix Σ=Cov⁡(X).{\boldsymbol\Sigma = \operatorname{Cov}(\mathbf X)}. Let Y\mathbf Y be a second random vector with mean μY,\boldsymbol\mu_Y, and write μX\boldsymbol\mu_X for μ\boldsymbol\mu when both appear. Let A\mathbf A and B\mathbf B be constant matrices and a,\mathbf a, b\mathbf b and c\mathbf c constant vectors, of sizes that fit. The expected value of a random vector or matrix is taken entry by entry, which is why constants pass through it.

QuantityFormula
Linear function, meanE(AX+b)=Aμ+bE(\mathbf A\mathbf X + \mathbf b) = \mathbf A\boldsymbol\mu + \mathbf b
Random matrix U\mathbf UE(AUB)=A E(U) BE(\mathbf A\mathbf U\mathbf B) = \mathbf A\,E(\mathbf U)\,\mathbf B
Covariance matrixCov⁡(X)=E[(X−μ)(X−μ)′]=E(XX′)−μμ′\operatorname{Cov}(\mathbf X) = E\left[(\mathbf X - \boldsymbol\mu)(\mathbf X - \boldsymbol\mu)'\right] = E(\mathbf X\mathbf X') - \boldsymbol\mu\boldsymbol\mu'
Linear function, covarianceCov⁡(AX+b)=AΣA′\operatorname{Cov}(\mathbf A\mathbf X + \mathbf b) = \mathbf A\boldsymbol\Sigma\mathbf A'
One combinationVar⁡(a′X)=a′Σa\operatorname{Var}(\mathbf a'\mathbf X) = \mathbf a'\boldsymbol\Sigma\mathbf a
Two combinationsCov⁡(a′X,c′X)=a′Σc\operatorname{Cov}(\mathbf a'\mathbf X, \mathbf c'\mathbf X) = \mathbf a'\boldsymbol\Sigma\mathbf c
Cross covarianceCov⁡(X,Y)=E[(X−μX)(Y−μY)′]\operatorname{Cov}(\mathbf X, \mathbf Y) = E\left[(\mathbf X - \boldsymbol\mu_X)(\mathbf Y - \boldsymbol\mu_Y)'\right] and Cov⁡(AX,BY)=ACov⁡(X,Y) B′\operatorname{Cov}(\mathbf A\mathbf X, \mathbf B\mathbf Y) = \mathbf A\operatorname{Cov}(\mathbf X, \mathbf Y)\,\mathbf B'
Uncorrelated sumCov⁡(X+Y)=Cov⁡(X)+Cov⁡(Y)\operatorname{Cov}(\mathbf X + \mathbf Y) = \operatorname{Cov}(\mathbf X) + \operatorname{Cov}(\mathbf Y) when Cov⁡(X,Y)=0{\operatorname{Cov}(\mathbf X, \mathbf Y) = \mathbf 0}
Standardized vectorZ=Σ−1/2(X−μ)\mathbf Z = \boldsymbol\Sigma^{-1/2}(\mathbf X - \boldsymbol\mu) has E(Z)=0{E(\mathbf Z) = \mathbf 0} and Cov⁡(Z)=I,{\operatorname{Cov}(\mathbf Z) = \mathbf I}, for positive definite Σ\boldsymbol\Sigma
Quadratic formE(X′AX)=tr⁡(AΣ)+μ′AμE(\mathbf X'\mathbf A\mathbf X) = \operatorname{tr}(\mathbf A\boldsymbol\Sigma) + \boldsymbol\mu'\mathbf A\boldsymbol\mu

Adding a constant shifts the mean and leaves the covariance alone, which is why b\mathbf b drops out of Cov⁡(AX+b).{\operatorname{Cov}(\mathbf A\mathbf X + \mathbf b)}. For a random sample X1,…,Xn\mathbf X_1, \dots, \mathbf X_n these rules give E(Xˉ)=μ{E(\bar{\mathbf X}) = \boldsymbol\mu} and Cov⁡(Xˉ)=1nΣ.{\operatorname{Cov}(\bar{\mathbf X}) = \frac{1}{n}\boldsymbol\Sigma}.

Expected quadratic form

The last row is the trace trick again.

E(X′AX)=E[tr⁡(AXX′)]=tr⁡(A E(XX′))=tr⁡(A(Σ+μμ′))=tr⁡(AΣ)+μ′Aμ\begin{aligned} E(\mathbf X'\mathbf A\mathbf X) &= E\left[\operatorname{tr}(\mathbf A\mathbf X\mathbf X')\right] \\ &= \operatorname{tr}\left(\mathbf A\,E(\mathbf X\mathbf X')\right) \\ &= \operatorname{tr}\left(\mathbf A(\boldsymbol\Sigma + \boldsymbol\mu\boldsymbol\mu')\right) \\ &= \operatorname{tr}(\mathbf A\boldsymbol\Sigma) + \boldsymbol\mu'\mathbf A\boldsymbol\mu \end{aligned}

It needs no normality and no symmetry of A,\mathbf A, only finite variances. For example

E[(X−μ)′Σ−1(X−μ)]=tr⁡(Σ−1Σ)=tr⁡(Ip)=p\begin{aligned} E\left[(\mathbf X - \boldsymbol\mu)'\boldsymbol\Sigma^{-1}(\mathbf X - \boldsymbol\mu)\right] &= \operatorname{tr}(\boldsymbol\Sigma^{-1}\boldsymbol\Sigma) \\ &= \operatorname{tr}(\mathbf I_p) = p \end{aligned}

which matches the mean of the χp2\chi^2_p distribution this statistic has when X\mathbf X is normal.