Unit 2, lesson 12

Vector Geometry, Rank, and Projection

How the Euclidean norm and orthogonal projection find the closest prediction a linear model can make, and how span, column space, rank, singular matrices and conditioning decide whether its coefficients are unique and stable.

  • Video 12
  • Linear Algebra for Predictions

Two different coefficient vectors can give exactly the same predictions. That happens when a feature column is redundant, and it raises three questions about any linear model. Which prediction vectors can it produce at all, which of them is closest to the targets, and are the coefficients behind that closest prediction determined by the data? This lesson answers the first question with span and column space, the second with orthogonal projection, and the third with linear independence, rank, singular matrices and conditioning.

Key terms

Euclidean norm

Definition On Rn\mathbb{R}^n this page uses the ordinary dot product u⊤v=∑iuivi\mathbf{u}^{\top}\mathbf{v} = \sum_i u_i v_i. The Euclidean norm of a vector v\mathbf{v} is

∥v∥2=v⊤v=v12+⋯+vn2,\begin{aligned} \lVert \mathbf{v} \rVert_2 &= \sqrt{\mathbf{v}^{\top}\mathbf{v}} \\[0.4em] &= \sqrt{v_1^2 + \dots + v_n^2} , \end{aligned}

and the Euclidean distance between u\mathbf{u} and v\mathbf{v} is ∥u−v∥2\lVert \mathbf{u} - \mathbf{v} \rVert_2. The norm is zero only for the zero vector. Two vectors are orthogonal when u⊤v=0\mathbf{u}^{\top}\mathbf{v} = 0. For nonzero vectors this means they meet at a right angle, and the zero vector is orthogonal to every vector. Orthogonal vectors satisfy the Pythagorean identity

∥u+v∥22=∥u∥22+∥v∥22.\lVert \mathbf{u} + \mathbf{v} \rVert_2^2 = \lVert \mathbf{u} \rVert_2^2 + \lVert \mathbf{v} \rVert_2^2 .

In plain words The norm is the length of an arrow, the Pythagorean theorem applied to all coordinates at once. On this page the vectors hold the target values of nn observations, so they live in observation space, with one axis per observation, and every coordinate must carry the same unit for a length to make sense. Orthogonality is a statement about dot products, not a claim that observations are statistically independent.

Example Two observations have the targets y=(4,3)⊤\mathbf{y} = (4, 3)^{\top}, both in the same unit. The norm of y\mathbf{y} is 16+9=5\sqrt{16 + 9} = 5. The vectors (1,2)⊤(1, 2)^{\top} and (2,−1)⊤(2, -1)^{\top} are orthogonal, since 1⋅2+2⋅(−1)=01 \cdot 2 + 2 \cdot (-1) = 0. The distance from y\mathbf{y} to (2,4)⊤(2, 4)^{\top} is the norm of the difference (2,−1)⊤(2, -1)^{\top}, which is 5\sqrt{5}.

Span and column space

Definition The span of vectors a1,…,ak\mathbf{a}_1, \dots, \mathbf{a}_k in Rn\mathbb{R}^n is the set of all their linear combinations c1a1+⋯+ckakc_1\mathbf{a}_1 + \dots + c_k\mathbf{a}_k with real numbers cjc_j. The column space of an n×pn \times p matrix XX is the span of its columns,

Col⁡X={Xw:w∈Rp}.\operatorname{Col} X = \{ X\mathbf{w} : \mathbf{w} \in \mathbb{R}^p \} .

It is a subspace of Rn\mathbb{R}^n, so it contains 0\mathbf{0} and is closed under addition and scalar multiplication. The span of one nonzero vector is a line through the origin, and the span of the zero vector is {0}\{ \mathbf{0} \}. For a linear model without an intercept, Col⁡X\operatorname{Col} X is exactly the set of prediction vectors the model can produce. With an intercept, the same holds for the augmented matrix that includes the ones column.

In plain words The column space collects every prediction vector the model can output as its coefficients range over all real values. A target vector outside it cannot be matched exactly, whatever coefficients are chosen.

Example Take one feature column a=(1,2)⊤\mathbf{a} = (1, 2)^{\top} and no intercept. The predictions c a=(c,2c)⊤c\,\mathbf{a} = (c, 2c)^{\top} fill the line through the origin with slope 2 in observation space. The target y=(4,3)⊤\mathbf{y} = (4, 3)^{\top} is not on that line, because its second coordinate is not twice its first. No coefficient fits both observations exactly.

Orthogonal projection

Definition Let SS be a subspace of Rn\mathbb{R}^n and y\mathbf{y} a vector in Rn\mathbb{R}^n. The orthogonal projection of y\mathbf{y} onto SS is the vector p\mathbf{p} in SS for which the residual y−p\mathbf{y} - \mathbf{p} is orthogonal to every vector in SS. It exists and is unique, and it is the point of SS closest to y\mathbf{y}. For any z\mathbf{z} in SS the difference p−z\mathbf{p} - \mathbf{z} also lies in SS, so the Pythagorean identity gives

∥y−z∥22=∥y−p∥22+∥p−z∥22,\begin{aligned} \lVert \mathbf{y} - \mathbf{z} \rVert_2^2 &= \lVert \mathbf{y} - \mathbf{p} \rVert_2^2 \\[0.4em] &\quad + \lVert \mathbf{p} - \mathbf{z} \rVert_2^2 , \end{aligned}

and the second term is zero only when z=p\mathbf{z} = \mathbf{p}. When SS is the span of a nonzero vector a\mathbf{a}, the condition a⊤(y−c a)=0\mathbf{a}^{\top}(\mathbf{y} - c\,\mathbf{a}) = 0 gives

p=c a,c=a⊤ya⊤a.\mathbf{p} = c\,\mathbf{a}, \qquad c = \frac{\mathbf{a}^{\top}\mathbf{y}}{\mathbf{a}^{\top}\mathbf{a}} .

The denominator is positive because a≠0\mathbf{a} \ne \mathbf{0}. On this page the residual is target minus prediction, r=y−p\mathbf{r} = \mathbf{y} - \mathbf{p}.

In plain words Drop a perpendicular from the target onto the set of possible predictions. Its foot is the prediction closest to the target in squared Euclidean distance, and what remains has no component along any direction the model allows. If it had one, moving the prediction along that direction would shorten the residual. A projection is a fitted vector, not a claim that the model generated the data.

Example For a=(1,2)⊤\mathbf{a} = (1, 2)^{\top} and y=(4,3)⊤\mathbf{y} = (4, 3)^{\top}, the dot products are a⊤y=4+6=10\mathbf{a}^{\top}\mathbf{y} = 4 + 6 = 10 and a⊤a=1+4=5\mathbf{a}^{\top}\mathbf{a} = 1 + 4 = 5, so c=2c = 2. The projection is p=(2,4)⊤\mathbf{p} = (2, 4)^{\top} and the residual is r=(2,−1)⊤\mathbf{r} = (2, -1)^{\top}. The check a⊤r=2−2=0\mathbf{a}^{\top}\mathbf{r} = 2 - 2 = 0 confirms the right angle, and the smallest squared distance is ∥r∥22=5\lVert \mathbf{r} \rVert_2^2 = 5. Any other point of the line does worse. The prediction (1,2)⊤(1, 2)^{\top}, from c=1c = 1, is at squared distance 9+1=109 + 1 = 10 from y\mathbf{y}, which is the residual term 5 plus ∥p−(1,2)⊤∥22=5\lVert \mathbf{p} - (1, 2)^{\top} \rVert_2^2 = 5.

Linear independence and rank

Definition Vectors a1,…,ak\mathbf{a}_1, \dots, \mathbf{a}_k are linearly independent if c1a1+⋯+ckak=0c_1\mathbf{a}_1 + \dots + c_k\mathbf{a}_k = \mathbf{0} holds only when every cj=0c_j = 0, and linearly dependent otherwise. So the columns of XX are independent exactly when Xw=0X\mathbf{w} = \mathbf{0} forces w=0\mathbf{w} = \mathbf{0}. The rank of XX is the dimension of Col⁡X\operatorname{Col} X. It equals the largest number of linearly independent columns and also the largest number of linearly independent rows, so rank⁡X≤min⁡(n,p)\operatorname{rank} X \le \min(n, p). If Xw0=pX\mathbf{w}_0 = \mathbf{p}, the coefficient vectors that give the same prediction vector p\mathbf{p} are exactly the vectors w0+h\mathbf{w}_0 + \mathbf{h} with Xh=0X\mathbf{h} = \mathbf{0}. Hence every vector in Col⁡X\operatorname{Col} X comes from exactly one coefficient vector when rank⁡X=p\operatorname{rank} X = p, and from infinitely many when rank⁡X<p\operatorname{rank} X < p.

In plain words Columns are independent when none of them can be built from the others. The rank counts the independent directions, which can be fewer than the columns. The projection of the targets is always one definite vector, but the coefficients that produce it are unique only when no column is redundant.

Example Add a second column equal to 2a2\mathbf{a}, so that

X=[1224],Xw=(w1+2w2) a.\begin{gathered} X = \begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}, \\[0.4em] X\mathbf{w} = (w_1 + 2w_2)\,\mathbf{a} . \end{gathered}

The column space is still the line spanned by a\mathbf{a}, so the rank is 1 although there are 2 columns. The projection p=(2,4)⊤\mathbf{p} = (2, 4)^{\top} requires w1+2w2=2w_1 + 2w_2 = 2, which both (2,0)⊤(2, 0)^{\top} and (0,1)⊤(0, 1)^{\top} satisfy, along with infinitely many other pairs. The nonzero vector h=(−2,1)⊤\mathbf{h} = (-2, 1)^{\top} gives Xh=0X\mathbf{h} = \mathbf{0}, which proves that the columns are dependent. The separate coefficients cannot be identified from these data, although the fitted values are the same.

Singular matrix

Definition A square m×mm \times m matrix AA is singular if it has no inverse. Singularity is equivalent to det⁡A=0\det A = 0, to rank⁡A<m\operatorname{rank} A < m, and to Av=0A\mathbf{v} = \mathbf{0} for some nonzero v\mathbf{v}. For an n×pn \times p matrix XX, the p×pp \times p matrix X⊤XX^{\top}X is singular exactly when the columns of XX are dependent, because X⊤Xv=0X^{\top}X\mathbf{v} = \mathbf{0} implies ∥Xv∥22=v⊤X⊤Xv=0\lVert X\mathbf{v} \rVert_2^2 = \mathbf{v}^{\top}X^{\top}X\mathbf{v} = 0. This matrix appears when the projection condition for Col⁡X\operatorname{Col} X is written out. The residual must be orthogonal to every column, X⊤(y−Xw)=0X^{\top}(\mathbf{y} - X\mathbf{w}) = \mathbf{0}, which gives the normal equations

X⊤Xw=X⊤y.X^{\top}X\mathbf{w} = X^{\top}\mathbf{y} .

They always have a solution, and every solution gives the projection, Xw=pX\mathbf{w} = \mathbf{p}. The formula w=(X⊤X)−1X⊤y\mathbf{w} = (X^{\top}X)^{-1}X^{\top}\mathbf{y} is valid only when X⊤XX^{\top}X is invertible, that is, when rank⁡X=p\operatorname{rank} X = p.

In plain words A singular matrix sends some nonzero vector to zero, so it loses information and cannot be undone. With redundant features the normal equations still determine the fitted values, but they do not single out one coefficient vector, so a formula that inverts X⊤XX^{\top}X cannot be used. A least-squares solver that handles rank deficiency returns one of the valid solutions by a stated rule, for example the solution with the smallest norm.

Example For the design with the duplicated direction,

X⊤X=[5101020],X⊤y=[1020].\begin{gathered} X^{\top}X = \begin{bmatrix} 5 & 10 \\ 10 & 20 \end{bmatrix}, \\[0.4em] X^{\top}\mathbf{y} = \begin{bmatrix} 10 \\ 20 \end{bmatrix}. \end{gathered}

The determinant is 5⋅20−10⋅10=05 \cdot 20 - 10 \cdot 10 = 0, so X⊤XX^{\top}X is singular and has no inverse. Both normal equations reduce to w1+2w2=2w_1 + 2w_2 = 2, the condition found from the geometry. In NumPy, np.linalg.inv stops with an error on this exact matrix, while np.linalg.lstsq(X, y, rcond=None) returns the smallest-norm solution (0.4,0.8)⊤(0.4, 0.8)^{\top}, which also gives the fitted vector (2,4)⊤(2, 4)^{\top}.

Conditioning

Definition Conditioning describes how much the answer to a problem can change when its inputs change slightly. For an invertible square matrix AA, let σmax⁡\sigma_{\max} and σmin⁡\sigma_{\min} be the largest and smallest values of ∥Av∥2/∥v∥2\lVert A\mathbf{v} \rVert_2 / \lVert \mathbf{v} \rVert_2 over nonzero vectors v\mathbf{v}, the most and the least that AA stretches a vector. The condition number is κ(A)=σmax⁡/σmin⁡≥1\kappa(A) = \sigma_{\max} / \sigma_{\min} \ge 1. If Aw=yA\mathbf{w} = \mathbf{y} with y≠0\mathbf{y} \ne \mathbf{0} and A(w+Δw)=y+ΔyA(\mathbf{w} + \Delta\mathbf{w}) = \mathbf{y} + \Delta\mathbf{y}, then

∥Δw∥2∥w∥2≤κ(A) ∥Δy∥2∥y∥2.\frac{\lVert \Delta\mathbf{w} \rVert_2}{\lVert \mathbf{w} \rVert_2} \le \kappa(A)\, \frac{\lVert \Delta\mathbf{y} \rVert_2}{\lVert \mathbf{y} \rVert_2} .

A matrix with a large condition number is called ill-conditioned. It is invertible, yet its nearly dependent columns make the solution sensitive. A singular matrix is the limiting case, and its condition number is taken to be infinite. Conditioning belongs to the problem, including the units chosen for the features. Floating-point rounding errors are amplified by it, but they do not cause it.

In plain words Exact dependence means many coefficient vectors fit equally well. Near dependence means exactly one does, but small changes in the targets can move it a lot, so the separate coefficients are hard to interpret even when the fit is good. Stable algorithms avoid adding needless rounding error, but no algorithm can recover information the columns do not contain. Rescaling features, collecting more varied data, dropping a redundant feature or regularization can help, depending on the task.

Example The matrix

A=[1111.01]A = \begin{bmatrix} 1 & 1 \\ 1 & 1.01 \end{bmatrix}

has determinant 0.010.01. Its columns differ by (0,0.01)⊤(0, 0.01)^{\top}, so they are independent and AA is invertible. For the targets (2,2)⊤(2, 2)^{\top} the unique solution of Aw=yA\mathbf{w} = \mathbf{y} is (2,0)⊤(2, 0)^{\top}. Changing the second target to 2.0012.001 and subtracting the first equation from the second gives 0.01 w2=0.0010.01\,w_2 = 0.001, so w2=0.1w_2 = 0.1 and w1=1.9w_1 = 1.9. A change of 0.0010.001 in one target moved each coefficient by 0.10.1. The relative change in w\mathbf{w} is 200 times the relative change in y\mathbf{y}, within the bound set by κ(A)≈402\kappa(A) \approx 402. Because AA is square and invertible, both solutions reproduce their targets exactly, so the instability lies only in the coefficients.

Check yourself

  1. Project y=(1,3)⊤\mathbf{y} = (1, 3)^{\top} onto the span of a=(2,1)⊤\mathbf{a} = (2, 1)^{\top}, with no intercept. Find the coefficient, the projection and the residual, and check that the residual is orthogonal to a\mathbf{a}.
Show the answer

The dot products are a⊤y=2+3=5\mathbf{a}^{\top}\mathbf{y} = 2 + 3 = 5 and a⊤a=4+1=5\mathbf{a}^{\top}\mathbf{a} = 4 + 1 = 5, so c=1c = 1 and p=(2,1)⊤\mathbf{p} = (2, 1)^{\top}. The residual is r=y−p=(−1,2)⊤\mathbf{r} = \mathbf{y} - \mathbf{p} = (-1, 2)^{\top}, and a⊤r=−2+2=0\mathbf{a}^{\top}\mathbf{r} = -2 + 2 = 0, so it is orthogonal to a\mathbf{a}.

The smallest squared distance is ∥r∥22=1+4=5\lVert \mathbf{r} \rVert_2^2 = 1 + 4 = 5. By the Pythagorean identity every other point of the line is farther from y\mathbf{y}. For example, the coefficient c=0c = 0 gives the squared distance ∥y∥22=10\lVert \mathbf{y} \rVert_2^2 = 10, which is 5+∥p∥22=5+55 + \lVert \mathbf{p} \rVert_2^2 = 5 + 5.

  1. Now the feature column is duplicated, so XX has the two columns (2,1)⊤(2, 1)^{\top} and (2,1)⊤(2, 1)^{\top}. Which coefficient vectors give the fitted vector (2,1)⊤(2, 1)^{\top}? What is the rank of XX, and can the coefficients be computed as (X⊤X)−1X⊤y(X^{\top}X)^{-1}X^{\top}\mathbf{y}?
Show the answer

Here Xw=(w1+w2) aX\mathbf{w} = (w_1 + w_2)\,\mathbf{a}, so the fitted vector is (2,1)⊤(2, 1)^{\top} exactly when w1+w2=1w_1 + w_2 = 1. Both (1,0)⊤(1, 0)^{\top} and (0.5,0.5)⊤(0.5, 0.5)^{\top} work, and so does (2,−1)⊤(2, -1)^{\top}, along with infinitely many others. The projection itself does not change, because the column space is still the line spanned by a\mathbf{a}.

The rank is 1, not 2. The matrix

X⊤X=[5555]X^{\top}X = \begin{bmatrix} 5 & 5 \\ 5 & 5 \end{bmatrix}

has determinant 25−25=025 - 25 = 0, so it has no inverse and the formula cannot be used. The normal equations, which both read 5w1+5w2=55w_1 + 5w_2 = 5, still describe every valid solution, and the one with the smallest norm is (0.5,0.5)⊤(0.5, 0.5)^{\top}.

  1. The matrix 0.1 I20.1\,I_2 has determinant 0.010.01, the same as the matrix AA in the conditioning example. A classmate concludes that it is just as close to singular. Is that right?
Show the answer

No. The matrix 0.1 I20.1\,I_2 shrinks every vector by the same factor 0.10.1, so its largest and smallest stretches are equal and its condition number is 1, the best possible value. Solving (0.1 I2)w=y(0.1\,I_2)\mathbf{w} = \mathbf{y} gives w=10 y\mathbf{w} = 10\,\mathbf{y}, and a relative change in y\mathbf{y} produces exactly the same relative change in w\mathbf{w}.

The determinant measures how areas scale, and it changes with units. Multiplying a 2×22 \times 2 matrix by a number tt multiplies its determinant by t2t^2 and leaves its condition number unchanged, so 10A10A has determinant 1 but still a condition number of about 402. Near singularity is judged by the condition number, not by the size of the determinant.

How this lesson connects