Skip to content
nonconvexcard

Linear Algebra for Machine Learning

Inner products, orthogonal projection, the spectral theorem, the SVD and the matrix norms that every later bound is written in.

FoundationsFoundations⏱ 3 hours16 min readlinear algebraspectral theoremsvdnormspsd
5 problems →

Everything later in this laboratory is written in the language of this page. A concentration inequality for random matrices is a statement about ∥A∥\|A\|; a convergence rate for gradient descent is a statement about λmax⁡(∇2f)\lambda_{\max}(\nabla^2 f) and λmin⁡(∇2f)\lambda_{\min}(\nabla^2 f); the kernel trick is a statement about Gram matrices; attention is a statement about the geometry of inner products. This page fixes notation and proves the five or six facts that get used without comment from here on.

Inner products, norms and orthogonality

We work in Rd\R^d with the standard inner product ⟨x,y⟩=∑i=1dxiyi=x⊤y\ip{x}{y} = \sum_{i=1}^d x_i y_i = x^\top y and the Euclidean norm ∥x∥2=⟨x,x⟩\norm{x}_2 = \sqrt{\ip{x}{x}}.

DefinitionNorms on vectors

For p∈[1,∞)p \in [1,\infty) the ℓp\ell^p norm is ∥x∥p=(∑i∣xi∣p)1/p\norm{x}_p = \left(\sum_i |x_i|^p\right)^{1/p}, and ∥x∥∞=max⁡i∣xi∣\norm{x}_\infty = \max_i |x_i|. Each satisfies

  • ∥x∥≥0\norm{x} \ge 0, with ∥x∥=0\norm{x} = 0 only for x=0x = 0 (positivity),
  • ∥cx∥=∣c∣ ∥x∥\norm{cx} = |c|\,\norm{x} (homogeneity),
  • ∥x+y∥≤∥x∥+∥y∥\norm{x+y} \le \norm{x} + \norm{y} (triangle inequality).

Only p=2p=2 comes from an inner product, which is why the Euclidean norm is the one with a geometry — angles, projections, orthogonality — attached to it.

TheoremCauchy–Schwarz inequality

For all x,y∈Rdx, y \in \R^d,

∣⟨x,y⟩∣≤∥x∥2∥y∥2,|\ip{x}{y}| \le \norm{x}_2 \norm{y}_2 ,

with equality if and only if xx and yy are linearly dependent.

The Cauchy–Schwarz inequality is what lets us define the angle θ\theta between non-zero vectors through cos⁡θ=⟨x,y⟩/(∥x∥2∥y∥2)∈[−1,1]\cos\theta = \ip{x}{y}/(\norm{x}_2\norm{y}_2) \in [-1,1]. Two vectors are orthogonal when ⟨x,y⟩=0\ip{x}{y}=0. A set {u1,…,uk}\{u_1,\dots,u_k\} is orthonormal when ⟨ui,uj⟩=δij\ip{u_i}{u_j} = \delta_{ij}.

Orthogonal projection

TheoremOrthogonal projection onto a subspace

Let V⊆RdV \subseteq \R^d be a subspace with orthonormal basis u1,…,uku_1,\dots,u_k, and let P=∑i=1kuiui⊤P = \sum_{i=1}^k u_i u_i^\top. Then for every x∈Rdx \in \R^d:

  1. Px∈VPx \in V and x−Px⊥Vx - Px \perp V;
  2. the decomposition x=Px+(x−Px)x = Px + (x - Px) into a VV-component and a V⊥V^\perp-component is unique;
  3. Px=arg min⁡v∈V∥x−v∥2Px = \argmin_{v \in V} \norm{x - v}_2, and ∥x∥22=∥Px∥22+∥x−Px∥22\norm{x}_2^2 = \norm{Px}_2^2 + \norm{x-Px}_2^2;
  4. P=P⊤=P2P = P^\top = P^2, and ∥P∥=1\norm{P} = 1 unless V={0}V = \{0\}.

Key idea

Least squares is orthogonal projection. The normal equations of linear regression say exactly that the residual is orthogonal to the column space of the design matrix; the geometry of this theorem is the whole derivation.

The spectral theorem

Write Sd\mathbb{S}^d for the set of real symmetric d×dd \times d matrices.

TheoremSpectral theorem for real symmetric matrices

Let A∈SdA \in \mathbb{S}^d. Then there exist real numbers λ1≥λ2≥⋯≥λd\lambda_1 \ge \lambda_2 \ge \dots \ge \lambda_d and an orthonormal basis u1,…,udu_1,\dots,u_d of Rd\R^d with Aui=λiuiAu_i = \lambda_i u_i. Equivalently A=UΛU⊤A = U\Lambda U^\top with UU orthogonal and Λ=diag⁡(λ1,…,λd)\Lambda = \diag(\lambda_1,\dots,\lambda_d), equivalently A=∑i=1dλiuiui⊤A = \sum_{i=1}^d \lambda_i u_i u_i^\top.

PropositionRayleigh quotient characterisation of eigenvalues

For A∈SdA \in \mathbb{S}^d and x≠0x \ne 0 define the Rayleigh quotient RA(x)=x⊤Axx⊤xR_A(x) = \dfrac{x^\top A x}{x^\top x}. Then

λmax⁡(A)=max⁡x≠0RA(x),λmin⁡(A)=min⁡x≠0RA(x),\lambda_{\max}(A) = \max_{x \ne 0} R_A(x), \qquad \lambda_{\min}(A) = \min_{x \ne 0} R_A(x),

and more generally λk(A)=max⁡dim⁡V=k min⁡0≠x∈VRA(x)\lambda_k(A) = \max_{\dim V = k}\ \min_{0 \ne x \in V} R_A(x) (the Courant–Fischer min–max characterisation).

Positive semidefinite matrices and quadratic forms

DefinitionPositive semidefinite and positive definite

A∈SdA \in \mathbb{S}^d is positive semidefinite (PSD), written A⪰0A \succeq 0, if x⊤Ax≥0x^\top A x \ge 0 for all xx, and positive definite (PD), written A≻0A \succ 0, if x⊤Ax>0x^\top A x > 0 for all x≠0x \ne 0. We write A⪰BA \succeq B for A−B⪰0A - B \succeq 0; this is the Loewner order, a partial order — not every pair of symmetric matrices is comparable.

PropositionCharacterisations of positive semidefiniteness

For A∈SdA \in \mathbb{S}^d the following are equivalent:

  1. x⊤Ax≥0x^\top A x \ge 0 for all x∈Rdx \in \R^d;
  2. every eigenvalue of AA is non-negative;
  3. A=B⊤BA = B^\top B for some matrix BB;
  4. A=∑i=1rσivivi⊤A = \sum_{i=1}^r \sigma_i v_i v_i^\top for some r≤dr \le d, σi>0\sigma_i > 0 and orthonormal viv_i.

Statement (3) is why PSD matrices are unavoidable in learning theory: every Gram matrix Kij=⟨ϕ(xi),ϕ(xj)⟩K_{ij} = \ip{\phi(x_i)}{\phi(x_j)} is of the form B⊤BB^\top B and therefore PSD, and conversely every PSD matrix is a Gram matrix of some feature map. That equivalence is Mercer's condition in miniature.

DefinitionSquare root and Cholesky factor

For A⪰0A \succeq 0 with spectral decomposition UΛU⊤U\Lambda U^\top, the matrix A1/2:=UΛ1/2U⊤A^{1/2} := U \Lambda^{1/2} U^\top is the unique PSD matrix with (A1/2)2=A(A^{1/2})^2 = A. If A≻0A \succ 0 it also has a unique factorisation A=LL⊤A = LL^\top with LL lower triangular and positive diagonal (the Cholesky factor), computable in 13d3\tfrac13 d^3 flops — which is how every "solve (X⊤X+λI)β=X⊤y(X^\top X + \lambda I)\beta = X^\top y" in this laboratory is actually executed.

The singular value decomposition

TheoremSingular value decomposition

Every A∈Rn×pA \in \R^{n \times p} can be written

A=UΣV⊤=∑i=1rσiuivi⊤,A = U \Sigma V^\top = \sum_{i=1}^{r} \sigma_i u_i v_i^\top ,

where r=rank⁡Ar = \rank A, U∈Rn×nU \in \R^{n\times n} and V∈Rp×pV \in \R^{p \times p} are orthogonal, and Σ∈Rn×p\Sigma \in \R^{n \times p} is zero off the diagonal with σ1≥⋯≥σmin⁡(n,p)≥0\sigma_1 \ge \dots \ge \sigma_{\min(n,p)} \ge 0.

Matrix norms

DefinitionOperator, Frobenius and nuclear norms
∥A∥=∥A∥2→2=max⁡∥x∥2=1∥Ax∥2=σ1,∥A∥F=∑i,jAij2=∑iσi2,∥A∥∗=∑iσi.\norm{A} = \norm{A}_{2\to2} = \max_{\norm{x}_2 = 1}\norm{Ax}_2 = \sigma_1, \qquad \norm{A}_F = \sqrt{\sum_{i,j} A_{ij}^2} = \sqrt{\sum_i \sigma_i^2}, \qquad \norm{A}_* = \sum_i \sigma_i .

The first is the spectral (operator) norm, the second the Frobenius norm, the third the nuclear norm. All three are functions of the singular values alone, hence invariant under A↦QARA \mapsto QAR for orthogonal Q,RQ,R.

The inequalities

∥A∥≤∥A∥F≤r ∥A∥,∥A∥F≤∥A∥∗≤r ∥A∥F\norm{A} \le \norm{A}_F \le \sqrt{r}\,\norm{A}, \qquad \norm{A}_F \le \norm{A}_* \le \sqrt{r}\,\norm{A}_F

follow at once from σ12≤∑iσi2≤rσ12\sigma_1^2 \le \sum_i \sigma_i^2 \le r\sigma_1^2 and Cauchy–Schwarz on the vector of singular values. They matter because the statistically natural norm is often ∥⋅∥F\norm{\cdot}_F while the analytically tractable one is ∥⋅∥\norm{\cdot}, and the conversion costs a r\sqrt{r} — a factor that is harmless for low-rank objects and fatal for full-rank ones in high dimension.

TheoremEckart–Young–Mirsky theorem

Let Ak=∑i=1kσiuivi⊤A_k = \sum_{i=1}^k \sigma_i u_i v_i^\top be the rank-kk truncation of the SVD. Then for every matrix BB of rank at most kk,

∥A−Ak∥=σk+1≤∥A−B∥,∥A−Ak∥F=(∑i>kσi2)1/2≤∥A−B∥F.\norm{A - A_k} = \sigma_{k+1} \le \norm{A-B}, \qquad \norm{A-A_k}_F = \Big(\sum_{i>k}\sigma_i^2\Big)^{1/2} \le \norm{A-B}_F .

This theorem is the reason principal component analysis is what it is: PCA is the Frobenius-optimal low-rank approximation of the centred data matrix, and nothing more.

Matrix calculus in one line each

The derivative conventions used throughout the optimisation pages are collected on the matrix calculus page. The three identities used most often are

∇x(a⊤x)=a,∇x(x⊤Ax)=(A+A⊤)x,∇x12∥Ax−b∥22=A⊤(Ax−b).\nabla_x (a^\top x) = a, \qquad \nabla_x (x^\top A x) = (A + A^\top)x, \qquad \nabla_x \tfrac12\norm{Ax-b}_2^2 = A^\top(Ax-b).

Problems

Equality in Cauchy–SchwarzProof · Beginner · 15 min Characterising orthogonal projectorsProof · Intermediate · 30 min Schur complement and block positive semidefinitenessProof · Advanced · 30 min Bounding the spectral norm by row and column sumsProof · Intermediate · 30 min Implement power iteration and verify the convergence rateCoding · Intermediate · 1 hour

References

  • BookRoger A. Horn and Charles R. Johnson (2012). Matrix AnalysisThe standard reference for everything on this page, in far greater generality.
  • BookLloyd N. Trefethen and David Bau III (1997). Numerical Linear AlgebraExcellent on the SVD and on conditioning.
  • BookStephen Boyd and Lieven Vandenberghe (2004). Convex OptimizationAppendix A collects the linear algebra used throughout optimisation.

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page

Problems (5)

All problems

Papers that use this mathematics