Skip to content
BIASED

Symmetric matrices and quadratic forms

The matrices whose eigenvectors behave perfectly: the spectral theorem, quadratic forms and their ellipses, positive definiteness, why a covariance is always PSD, the Rayleigh quotient and the direction of largest variance.

Why symmetric matrices are special

The topic Determinants and eigenvalues ended on a warning. A general matrix may have too few eigenvectors, or complex ones: a rotation by 90∘90^\circ has eigenvalues ±i\pm i, and the shear (1101)\begin{pmatrix} 1 & 1 \\ 0 & 1 \end{pmatrix} has a single eigen-direction. Even when an eigenbasis exists, it is usually not orthogonal: the running example (4123)\begin{pmatrix} 4 & 1 \\ 2 & 3 \end{pmatrix} of that topic has eigenvectors (1,1)(1, 1) and (1,−2)(1, -2) at an angle of about 108∘108^\circ.

This topic is about matrices where none of that happens. A square matrix is symmetric if it equals its transpose:

A=A⊤,aij=aji.A = A^\top, \qquad a_{ij} = a_{ji}.

The entries mirror each other across the main diagonal. That sounds like a special case, but in ML symmetric matrices are the rule:

  • a covariance matrix, since Cov⁡(xi,xj)=Cov⁡(xj,xi)\operatorname{Cov}(x_i, x_j) = \operatorname{Cov}(x_j, x_i);
  • the Gram matrix X⊤XX^\top X of a data matrix XX, which already appeared in the normal equations (see the topic Inner products, norms and orthogonality);
  • a kernel matrix Kij=k(xi,xj)K_{ij} = k(x_i, x_j) built from a symmetric similarity kk;
  • the Hessian of a loss, its matrix of second derivatives (the topic Gradients, Jacobians and Hessians).

Why X⊤XX^\top X is symmetric takes one line with the rule (PQ)⊤=Q⊤P⊤(PQ)^\top = Q^\top P^\top:

(X⊤X)⊤=X⊤(X⊤)⊤=X⊤X.(X^\top X)^\top = X^\top (X^\top)^\top = X^\top X.

Nothing here needs XX to be square: the data matrix is N×dN \times d, with one example per row.

The running example of the topic is

A=(5222).A = \begin{pmatrix} 5 & 2 \\ 2 & 2 \end{pmatrix}.

Later it will turn out to be the covariance of a tiny dataset.

The spectral theorem

For symmetric matrices the eigenvalue story is as good as it gets.

Spectral theorem. A real symmetric n×nn \times n matrix AA has nn real eigenvalues λ1,…,λn\lambda_1, \dots, \lambda_n and an orthonormal basis of eigenvectors q1,…,qnq_1, \dots, q_n. Put the qiq_i into the columns of an orthogonal matrix QQ and the eigenvalues into Λ=diag⁡(λ1,…,λn)\Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_n). Then

A=QΛQ⊤=∑i=1nλi qiqi⊤.A = Q \Lambda Q^\top = \sum_{i=1}^{n} \lambda_i\, q_i q_i^\top.

This is the diagonalization A=VΛV−1A = V \Lambda V^{-1} from the previous topic with one upgrade: VV is now orthogonal, so its inverse is just Q⊤Q^\top. Each qiqi⊤q_i q_i^\top is the orthogonal projection onto the line through qiq_i. Geometrically, a symmetric matrix stretches space by λi\lambda_i along nn perpendicular axes and does nothing else: no rotation, no shear.

Orthogonality is the easy part to prove. Let Aq1=λ1q1Aq_1 = \lambda_1 q_1 and Aq2=λ2q2Aq_2 = \lambda_2 q_2 with λ1≠λ2\lambda_1 \ne \lambda_2. Move AA from one side of the inner product to the other:

λ1 q1⊤q2=(Aq1)⊤q2=q1⊤A⊤q2=q1⊤Aq2=λ2 q1⊤q2.\lambda_1\, q_1^\top q_2 = (Aq_1)^\top q_2 = q_1^\top A^\top q_2 = q_1^\top A q_2 = \lambda_2\, q_1^\top q_2.

So (λ1−λ2) q1⊤q2=0(\lambda_1 - \lambda_2)\, q_1^\top q_2 = 0, and q1⊤q2=0q_1^\top q_2 = 0. Symmetry was used exactly once, in A⊤=AA^\top = A. A similar trick shows the eigenvalues are real, and an induction handles repeated eigenvalues; we take both on trust.

Now the running example. Its trace is 77 and its determinant 66, so the characteristic polynomial is λ2−7λ+6=(λ−6)(λ−1)\lambda^2 - 7\lambda + 6 = (\lambda - 6)(\lambda - 1). For λ=6\lambda = 6 the matrix A−6I=(−122−4)A - 6I = \begin{pmatrix} -1 & 2 \\ 2 & -4 \end{pmatrix} sends (2,1)(2, 1) to zero; for λ=1\lambda = 1, A−I=(4221)A - I = \begin{pmatrix} 4 & 2 \\ 2 & 1 \end{pmatrix} sends (1,−2)(1, -2) to zero. These two vectors are orthogonal, as promised. Divided by their length 5\sqrt5, they give

q1=15(2,1),λ1=6,q2=15(1,−2),λ2=1.q_1 = \tfrac{1}{\sqrt5}(2, 1), \quad \lambda_1 = 6, \qquad q_2 = \tfrac{1}{\sqrt5}(1, -2), \quad \lambda_2 = 1.

Rotate, stretch, rotate back

Read Ax=QΛQ⊤xAx = Q \Lambda Q^\top x from right to left. It is three simple moves:

  1. y=Q⊤xy = Q^\top x: the coordinates of xx in the eigenbasis, yi=qi⊤xy_i = q_i^\top x. These are plain inner products, with no linear system to solve as for a general V−1V^{-1}.
  2. Λy\Lambda y: stretch each coordinate yiy_i by λi\lambda_i.
  3. Q(Λy)Q(\Lambda y): return to the standard coordinates.

In one formula,

Ax=∑i=1nλi (qi⊤x) qi.Ax = \sum_{i=1}^{n} \lambda_i\, (q_i^\top x)\, q_i.

Take x=(1,1)x = (1, 1) in the running example. Its eigen-coordinates are y1=3/5y_1 = 3/\sqrt5 and y2=−1/5y_2 = -1/\sqrt5. Stretching gives Ax=6⋅35(2,1)−15(1,−2)=(7,4)Ax = 6 \cdot \tfrac{3}{5}(2, 1) - \tfrac15(1, -2) = (7, 4), the same as multiplying directly.

The payoff is that functions of AA act on the eigenvalues only. In A2=QΛQ⊤QΛQ⊤A^2 = Q\Lambda Q^\top Q \Lambda Q^\top the middle Q⊤Q=IQ^\top Q = I cancels, and the same happens for every power:

Ak=QΛkQ⊤,A−1=QΛ−1Q⊤,A1/2=QΛ1/2Q⊤.A^k = Q \Lambda^k Q^\top, \qquad A^{-1} = Q \Lambda^{-1} Q^\top, \qquad A^{1/2} = Q \Lambda^{1/2} Q^\top.

So A−1A^{-1} exists exactly when no λi\lambda_i is zero; for the running example it has eigenvalues 1/61/6 and 11 along the same q1,q2q_1, q_2. The square root needs λi≥0\lambda_i \ge 0, a condition the next blocks make precise.

An ML use is whitening. Let centered features xx have covariance CC with all λi>0\lambda_i > 0. For any matrix MM, the covariance of MxMx is the average of Mx (Mx)⊤=M xx⊤M⊤Mx\,(Mx)^\top = M\, x x^\top M^\top, that is MCM⊤MCM^\top. Take M=C−1/2M = C^{-1/2}, which is symmetric: the features C−1/2xC^{-1/2} x have covariance C−1/2 C C−1/2=IC^{-1/2}\, C\, C^{-1/2} = I, uncorrelated, each with variance 11.

Quadratic forms and their level sets

A quadratic form is the number

x⊤Ax=∑i,jaij xixj,x^\top A x = \sum_{i,j} a_{ij}\, x_i x_j,

a polynomial with only degree-two terms. For the running example, x⊤Ax=5x12+4x1x2+2x22x^\top A x = 5x_1^2 + 4x_1x_2 + 2x_2^2: the cross coefficient 44 is split between a12=2a_{12} = 2 and a21=2a_{21} = 2.

Any square matrix MM defines a quadratic form, but only its symmetric part matters. The number x⊤Mxx^\top M x equals its own transpose x⊤M⊤xx^\top M^\top x, so x⊤Mx=x⊤12(M+M⊤) xx^\top M x = x^\top \tfrac12 (M + M^\top)\, x. That is why quadratic forms are always written with a symmetric matrix.

What shape is q(x)=5x12+4x1x2+2x22q(x) = 5x_1^2 + 4x_1x_2 + 2x_2^2? Positive coefficients do not decide it: x12+4x1x2+x22x_1^2 + 4x_1x_2 + x_2^2 also has them, yet equals −2-2 at (1,−1)(1, -1). The spectral theorem does decide it. Switch to eigen-coordinates y=Q⊤xy = Q^\top x:

x⊤Ax=x⊤QΛQ⊤x=y⊤Λy=∑iλi yi2.x^\top A x = x^\top Q \Lambda Q^\top x = y^\top \Lambda y = \sum_{i} \lambda_i\, y_i^2.

The cross terms are gone. The running example becomes q=6y12+y22q = 6y_1^2 + y_2^2. When all λi>0\lambda_i > 0, the level set q(x)=cq(x) = c is an ellipse (an ellipsoid in higher dimensions) with axes along the qiq_i and semi-axes c/λi\sqrt{c/\lambda_i}. Here the level q=1q = 1 has semi-axis 1/6≈0.411/\sqrt6 \approx 0.41 along q1q_1 and 11 along q2q_2. A large eigenvalue means a steep direction and a short axis.

Mixed signs give a different picture. The matrix (1221)\begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} has eigenvalues 33 and −1-1, so its form is 3y12−y223y_1^2 - y_2^2: hyperbolas and a saddle at the origin.

Why ML cares: a quadratic loss is a quadratic form centered at its minimum. Take f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12 x^\top A x - b^\top x with all λi>0\lambda_i > 0, and let x∗=A−1bx^\ast = A^{-1} b. Put x=x∗+hx = x^\ast + h and multiply out; the two cross terms are equal, since x∗⊤Ahx^{\ast\top} A h is a number and equals its transpose h⊤Ax∗h^\top A x^\ast:

f(x∗+h)=f(x∗)+h⊤(Ax∗−b)+12h⊤Ah=f(x∗)+12h⊤Ah.f(x^\ast + h) = f(x^\ast) + h^\top (A x^\ast - b) + \tfrac12 h^\top A h = f(x^\ast) + \tfrac12 h^\top A h.

Least squares 12∥Xw−y∥2\tfrac12 \lVert Xw - y \rVert^2 is this ff in the variable ww, with A=X⊤XA = X^\top X and b=X⊤yb = X^\top y, plus the constant 12y⊤y\tfrac12 y^\top y, and x∗x^\ast solves the normal equations. So its level sets are these ellipses, centered at the minimum. Near a minimum every smooth loss looks the same, with its Hessian in place of AA. When λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min} is large the ellipses are long and thin, and gradient descent zigzags across the valley (the topic Unconstrained optimization).

Positive definite matrices

The sign of the quadratic form sorts symmetric matrices into classes:

  • positive definite, A≻0A \succ 0: x⊤Ax>0x^\top A x > 0 for every x≠0x \ne 0;
  • positive semidefinite (PSD), A⪰0A \succeq 0: x⊤Ax≥0x^\top A x \ge 0 for every xx;
  • indefinite: the form takes both signs.

The eigen-coordinates give the test at once. As xx runs over all nonzero vectors, so does y=Q⊤xy = Q^\top x, and ∑iλiyi2\sum_i \lambda_i y_i^2 stays positive exactly when every λi\lambda_i is positive:

A≻0  ⟺  all λi>0,A⪰0  ⟺  all λi≥0.A \succ 0 \iff \text{all } \lambda_i > 0, \qquad A \succeq 0 \iff \text{all } \lambda_i \ge 0.

Eigenvalues are not the cheapest check. Sylvester's criterion says A≻0A \succ 0 exactly when all its leading principal minors are positive: the determinants of the top-left 1×11 \times 1, 2×22 \times 2, …, n×nn \times n blocks. For the running example they are 55 and 5⋅2−2⋅2=65 \cdot 2 - 2 \cdot 2 = 6, so A≻0A \succ 0, which agrees with λ=6,1\lambda = 6, 1. For (1221)\begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} they are 11 and −3-3: not positive definite.

A trap: for semidefiniteness, non-negative leading minors are not enough. The matrix (000−1)\begin{pmatrix} 0 & 0 \\ 0 & -1 \end{pmatrix} has leading minors 00 and 00, yet its form is −1-1 at (0,1)(0, 1).

In code the standard test is the Cholesky factorization A=LL⊤A = LL^\top with LL lower triangular and a positive diagonal. It exists exactly when A≻0A \succ 0, costs about n3/3n^3/3 operations, and libraries report failure when the matrix is not positive definite. Near the boundary rounding decides: a matrix that is positive definite on paper but nearly singular can fail in floating point.

One more tool. Adding tItI shifts every eigenvalue by tt and keeps the eigenvectors: (A+tI) qi=(λi+t) qi(A + tI)\, q_i = (\lambda_i + t)\, q_i. So a small multiple of II turns a PSD matrix into a positive definite one. Ridge regression does exactly this with X⊤X+tIX^\top X + tI, and Gaussian processes call it "jitter".

A covariance is always positive semidefinite

Here is a tiny dataset: N=4N = 4 examples with d=2d = 2 features, already centered so that each feature has mean zero. The rows of XX are

(−3,−2),(−1,0),(1,2),(3,0).(-3, -2), \quad (-1, 0), \quad (1, 2), \quad (3, 0).

Its sample covariance is

C=1NX⊤X=14(20888)=(5222),C = \frac1N X^\top X = \frac14 \begin{pmatrix} 20 & 8 \\ 8 & 8 \end{pmatrix} = \begin{pmatrix} 5 & 2 \\ 2 & 2 \end{pmatrix},

our running example. (Many libraries divide by N−1N - 1 instead; that rescales CC and changes no eigenvector.)

Why is a covariance never indefinite? Pick any direction vv and build a new feature, the projection zk=v⊤xkz_k = v^\top x_k of each example. It has mean zero and variance

1N∑k=1N(v⊤xk)2=1N∥Xv∥2=v⊤Cv≥0.\frac1N \sum_{k=1}^{N} (v^\top x_k)^2 = \frac1N \lVert Xv \rVert^2 = v^\top C v \ge 0.

A variance cannot be negative, so every covariance matrix is PSD. The same holds for the covariance of a random vector: v⊤Cov⁡(x) v=Var⁡(v⊤x)v^\top \operatorname{Cov}(x)\, v = \operatorname{Var}(v^\top x).

When is CC only semidefinite? When v⊤Cv=0v^\top C v = 0 for some v≠0v \ne 0, that is, Xv=0Xv = 0: some combination of features is constant on the data. A duplicated feature does it. So does having fewer examples than features, since by rank–nullity (the topic Matrices and linear systems) the null space of XX is then nonzero.

The argument works for any matrix of the form B⊤BB^\top B. A kernel matrix is K=ΦΦ⊤K = \Phi \Phi^\top, where row kk of Φ\Phi is the feature map ϕ(xk)\phi(x_k), so c⊤Kc=∥Φ⊤c∥2≥0c^\top K c = \lVert \Phi^\top c \rVert^2 \ge 0. The Hessian of least squares, 2X⊤X2X^\top X, is PSD too; the topic Convex sets and functions will read this as convexity.

The Rayleigh quotient: the extreme directions

How large can a quadratic form be? Its value grows with the length of xx, so divide that out. The Rayleigh quotient is

R(x)=x⊤Axx⊤x,x≠0.R(x) = \frac{x^\top A x}{x^\top x}, \qquad x \ne 0.

Scaling xx does not change RR, so it depends on the direction only. For a unit vector ww, R(w)=w⊤AwR(w) = w^\top A w; when AA is a covariance, that is the variance of the data along ww.

In eigen-coordinates the lengths agree, ∥y∥=∥x∥\lVert y \rVert = \lVert x \rVert, because QQ is orthogonal. Hence

R(x)=∑iλiyi2∑iyi2=∑iwiλi,wi=yi2∑jyj2.R(x) = \frac{\sum_i \lambda_i y_i^2}{\sum_i y_i^2} = \sum_i w_i \lambda_i, \qquad w_i = \frac{y_i^2}{\sum_j y_j^2}.

The weights wiw_i are non-negative and sum to 11, so R(x)R(x) is a weighted average of the eigenvalues. An average lies between the smallest and the largest value:

λmin⁡≤R(x)≤λmax⁡,\lambda_{\min} \le R(x) \le \lambda_{\max},

with the maximum at qmax⁡q_{\max}, the eigenvector of λmax⁡\lambda_{\max}, and the minimum at qmin⁡q_{\min}.

Check it on the running example at x=(1,1)x = (1, 1): R=(5+4+2)/2=5.5R = (5 + 4 + 2)/2 = 5.5. In eigen-coordinates y=(3,−1)/5y = (3, -1)/\sqrt5, so the weights are 0.90.9 and 0.10.1, and indeed 0.9⋅6+0.1⋅1=5.50.9 \cdot 6 + 0.1 \cdot 1 = 5.5.

The Rayleigh quotient is also how power iteration (the topic Determinants and eigenvalues) reads off the eigenvalue. Starting from x0=(1,0)x_0 = (1, 0) and repeating xk+1=Axk/∥Axk∥x_{k+1} = Ax_k / \lVert Ax_k \rVert, the values R(xk)R(x_k) are 55, 5.96555.9655, 5.99905.9990, 5.999975.99997: they reach λmax⁡=6\lambda_{\max} = 6 quickly.

The direction of largest variance

Back to the four data points. Suppose we may keep only one number per example, its projection z=w⊤xz = w^\top x onto a unit direction ww. To keep as much of the spread as possible, we maximize the variance w⊤Cww^\top C w. That is a Rayleigh quotient, so the answer is the top eigenvector:

w∗=q1=15(2,1),Var⁡(z)=λ1=6.w^\ast = q_1 = \tfrac{1}{\sqrt5}(2, 1), \qquad \operatorname{Var}(z) = \lambda_1 = 6.

The projections of the four points are (−8,−2,4,6)/5(-8, -2, 4, 6)/\sqrt5, and the mean of their squares is indeed 120/20=6120/20 = 6. Along q2q_2 the variance is only λ2=1\lambda_2 = 1.

More generally, the variance along each qiq_i is λi\lambda_i, and the total variance of all features is tr⁡C=∑iλi\operatorname{tr} C = \sum_i \lambda_i. Projecting onto the top kk eigenvectors keeps the largest possible share of it. This is principal component analysis (PCA): the qiq_i are the principal components.

Everything in this topic needed a square symmetric matrix, so we formed C=1NX⊤XC = \tfrac1N X^\top X. But the data matrix XX itself is N×dN \times d, and so is a typical weight matrix: rectangular, not symmetric. Is there a decomposition of XX itself, with perpendicular axes on both sides? That is the next topic, The singular value decomposition.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.