Orthogonality, projection and least squares

Which point on a line is closest to a given point? The one you reach by dropping straight onto the line at a right angle. The same idea finds the line that fits a set of points best. Drag the points and see.

Short answer. Projection finds the point on a line (or a plane) that is closest to a given point. The segment to that point meets the line at a right angle. Least squares is how you pick an answer when no exact answer exists: you take the one where "how wrong each point is, squared and added up" is as small as possible. Inside, it is the same thing as projection.

a = ( , )

b = ( , )

q = × a

Drag the tips of green a and blue b, or the purple q, or change the numbers. The light green line is the line set by a, and q only moves along it. The orange dashed line goes from b down to the line at a right angle. Where it lands is p.

Definitions

The text below uses arrows in the plane and a few points, but the definitions are the same in ℝⁿ.

orthogonal
Two vectors u, v in ℝⁿ are called orthogonal if their dot product is 0. The length (norm) of a vector v is the square root of its dot product with itself.

𝐮·𝐯=u1v1+u2v2+⋯+unvn=0,‖𝐯‖=𝐯·𝐯

orthogonal projection
Let W be a subspace of ℝⁿ and b a vector. The vector p that lies in W and makes b − p orthogonal to every vector in W is called the orthogonal projection of b onto W. If W is the line spanned by one non-zero vector a, it is the formula below.

proj𝐚𝐛=𝐚·𝐛𝐚·𝐚𝐚

Gram-Schmidt process
Starting from linearly independent vectors v₁, …, vₖ, this process builds u₁, …, uₖ one at a time as below. The vectors u₁, …, uₖ are orthogonal to each other and span the same space as v₁, …, vₖ. Dividing each one by its length gives an orthonormal basis of that space.

𝐮1=𝐯1,𝐮2=𝐯2−proj𝐮1𝐯2

𝐮j=𝐯j−proj𝐮1𝐯j−⋯−proj𝐮j−1𝐯j(j=2,…,k)

column space
For an m×n matrix A, the column space is the subspace of ℝᵐ made of all linear combinations of the columns of A. In other words, it is the set of all vectors of the form Ax.

ColA={A𝐱∣𝐱∈ℝn}

least squares solution, residual
Let A be an m×n matrix and b ∈ ℝᵐ. The vector b − Ax is called the residual (residual vector). A vector x̂ is called a least squares solution of Ax = b if the inequality below holds for every x ∈ ℝⁿ. So it is an x that makes the residual as short as possible.

‖𝐛−A𝐱^‖≤‖𝐛−A𝐱‖

normal equations
The equation below is called the normal equations of Ax = b. x̂ is a least squares solution of Ax = b exactly when x̂ solves the normal equations. If the columns of A are linearly independent, AᵀA has an inverse, and the least squares solution is unique.

A𝖳A𝐱^=A𝖳𝐛

1. Orthogonal: a dot product of 0 means a right angle

When two arrows meet at a right angle, we say they are orthogonal. You can check this with numbers, no picture needed. If the dot product is 0, they are at a right angle. The dot product multiplies across by across and up by up, then adds the two (see the dot product page).

(2,1)·(−1,2)=2×(−1)+1×2=0⟹right angle

Two directions at a right angle do not mix. However far you go along one, you do not move at all along the other. So you can count "how much this way" and "how much that way" separately. The rest of this page uses this again and again.

The length of an arrow v is written ‖v‖. The length squared is the arrow's dot product with itself: ‖v‖² = v·v.

2. Projection: drop onto the line at a right angle

There is a line through the origin. The arrow a shows its direction. A point b sits off the line. Draw a segment from b to the line so that it meets the line at a right angle. The point where it lands, p, is the projection of b. It is like the shadow a stick casts on the ground when the sun is straight overhead.

𝐩=𝐚·𝐛𝐚·𝐚𝐚

The part in brackets is a single number. It says how many times a you need to reach p. The bottom, a·a, is the length of a squared. If a is (0, 0), then a·a is 0 and you cannot divide. With no direction there is no line, so there is no projection.

It works the same way if you drop onto a plane or a bigger space instead of a line. You find the closest point in it, and the difference is at a right angle to it (section 7).

Where the formula comes from: see "How the formula is built" below, one picture at a time.

Work it out by hand

Take a = (2, 1) and b = (3, 4). The lab above starts with these.

  1. a·b = 2×3 + 1×4 = 10
  2. a·a = 2×2 + 1×1 = 5
  3. How many times a: 10 ÷ 5 = 2
  4. p = 2 × (2, 1) = (4, 2)
  5. Check: b − p = (3 − 4, 4 − 2) = (−1, 2). (b − p)·a = (−1)×2 + 2×1 = 0. It is a right angle.
  6. Distance from b to p = length of (−1, 2) = √(1 + 4) = √5 ≈ 2.24

Written as equations, the same solution looks like this.

𝐩=𝐚·𝐛𝐚·𝐚𝐚=2×3+1×42×2+1×1[21]=105[21]=[42]

In equations, (2, 1) is written standing up, as a column. It is the same arrow.

(𝐛−𝐩)·𝐚=[−12]·[21]=(−1)×2+2×1=0

‖𝐛−𝐩‖=(−1)2+22=5≈2.24

Try this. ① Drag q anywhere along the line. The distance from b to q never drops below 2.24. It only reaches 2.24 when q sits on p (q = 2 × a). ② Change a to (4, 2) or (−2, −1). p stays at (4, 2). Same line, same p. ③ Move b to (−1, 2). p becomes (0, 0). b is at a right angle to a, so there is no shadow.

How the formula is built: the projection formula

Let's build p = (a·b ÷ a·a) × a one step at a time. The data is the same as in the hand example: a = (2, 1), b = (3, 4). Press "Next" and a piece is added to the formula on top (the orange background marks the new piece), the picture changes to match, and the reason appears below.

Green a, blue b. The purple point is the point t × a on the line. Orange marks the right angle and the finished p. You can move the t slider in steps ① to ④. From step ⑤ on, t stays at the solved value, 2.

Written only as equations, the steps above look like this.

𝐪=t𝐚,𝐛−𝐪=𝐛−t𝐚

(𝐛−t𝐚)⟂𝐚⟺(𝐛−t𝐚)·𝐚=0

(𝐛−t𝐚)·𝐚=𝐚·𝐛−t(𝐚·𝐚)=0⟹t=𝐚·𝐛𝐚·𝐚

𝐩=t𝐚=𝐚·𝐛𝐚·𝐚𝐚

𝐚·𝐛=10, 𝐚·𝐚=5:t=105=2,𝐩=2[21]=[42]

Why the graph is a parabola. Multiply out the distance squared and you get ‖b − t × a‖² = a·a × t² − 2 × a·b × t + b·b, a quadratic in t. Here it is 5t² − 20t + 25. If a is not (0, 0), the number in front of t², a·a, is above 0, so the parabola opens upward and has one bottom. The vertex formula for a quadratic (t = −(the number in front of t) ÷ (2 × the number in front of t²)) gives t = 2 × a·b ÷ (2 × a·a) = a·b ÷ a·a, the same answer.

The slope of a fitted line has the same shape. The least squares line passes through the mean point (x̄, ȳ) (section 6). So once you subtract the averages from x and y, you only need to fit y − ȳ ≈ m × (x − x̄), with no height k. That is a projection, with the y − ȳ values in the place of b and the x − x̄ values in the place of a (with four points, in 4 dimensions). So m = a·b ÷ a·a = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)². For the hand example in section 6, a = (−1.5, −0.5, 0.5, 1.5), b = (−1.5, 0.5, −0.5, 1.5), a·b = 4, a·a = 5, and m = 4 ÷ 5 = 0.8.

3. Why the right-angle point is the closest

Pick any other point q on the line. b, p and q make a right triangle. The right angle is at p, and the side from b to q is the long side. The Pythagorean theorem says:

‖𝐛−𝐪‖2=‖𝐛−𝐩‖2+‖𝐩−𝐪‖2

The last part, ‖p − q‖², can never be less than 0. So ‖b − q‖ is always at least ‖b − p‖. They are equal only when q is p. In other words, the point you reach at a right angle is the closest point. It also works the other way: if you found the closest point, the difference is at a right angle to the line.

Check it with the lab's starting picture. q = 1.5 × a = (3, 1.5). ‖b − q‖² = 0² + 2.5² = 6.25, ‖b − p‖² = 5, ‖p − q‖² = 1² + 0.5² = 1.25. 5 + 1.25 = 6.25. It matches.

4. Making right-angle base arrows: Gram-Schmidt

Math is easier when the two base arrows of the plane are at a right angle. Then, for any point, "how much along this base arrow, how much along that one" takes just one projection each (see the basis page). Gram-Schmidt turns two arrows a and b that are not at a right angle into a pair that is.

  1. Keep a as it is.
  2. From b, take away the part that goes along a, which is p. u = b − p.
  3. u is at a right angle to a. a and u are the new base arrows.
  4. If you need to, divide each one by its own length so both have length 1.

Example: a = (2, 1), b = (3, 4). We found p = (4, 2) above, so u = (3, 4) − (4, 2) = (−1, 2). Check: a·u = 2×(−1) + 1×2 = 0. Both lengths are √5, so with length 1 they become (2, 1) ÷ √5 and (−1, 2) ÷ √5.

With three or more arrows, repeat the same step. From the third one, take away its parts along every base arrow you already made.

Try this. ① Press "Make a right angle" in the lab. The orange dashed b − p slides over to the origin and becomes the black arrow u = (−1, 2). A right-angle mark appears between a and u. ② Then move b to (4, 2). u becomes (0, 0). When b is on the same line as a, no new direction comes out.

5. Least squares: when there is no exact answer

You want a line through several points. Two points with different x have exactly one line through them. With three or more points that are not on one line, no line goes through all of them. So you pick the line that is "least wrong."

Write the line as y = m·x + k. m is the slope, and k is the height at x = 0. For each point, actual y − the line's y is how wrong the line is there. This is called the residual. The least squares line is the line where the residuals, squared and added up, are as small as possible.

sum of squares=∑i(yi−(mxi+k))2

Σ means "add them all up," and i is the point number (point 1, point 2, and so on). In the lab below, the area of each light orange square is that point's residual squared. You are looking for the line that makes the total area of the squares as small as possible.

( , )

Drag the blue points, or pick a point and change its numbers. "Remove a point" removes the selected point (the black ring). You can have 3 to 12 points. In "Least squares answer," the black dot "mean" is where the average of the x values meets the average of the y values.

Try this. ① For the first 6 points, the least squares line is y = 0.56x + 0.06 and the sum of squares is 2.22. ② Press "Fit it yourself." The starting line is y = 0x + 0, with a sum of squares of 15.12. Move the sliders to bring it down. However you try, it never goes below 2.22. Press "Show the answer" and the sliders move to the answer. ③ In "Least squares answer," drag point 6 from (3.6, 2.6) down to (3.6, −4). The line becomes y = −0.06x − 1.18. One point turns an uphill line into a downhill one. This is what "weak against outliers" means (section 8).

6. The line-fit formula, worked by hand

The least squares line comes straight out of a formula. x̄ ("x bar") is the average of the x values, and ȳ ("y bar") is the average of the y values.

slope m=∑(x−x¯)(y−y¯)∑(x−x¯)2

height k=y¯−mx¯

The second formula says the line passes through the mean point (x̄, ȳ). If the bottom, Σ(x − x̄)², is 0, which means all the x values are the same, the slope cannot be found. This formula is the normal equations from section 7, written out for a line. The end of "How the formula is built" shows that the slope has the same shape as the projection formula a·b ÷ a·a.

Work it out by hand

Four points: (1, 1), (2, 3), (3, 2), (4, 4).

xyx − x̄y − ȳ(x − x̄)(y − ȳ)(x − x̄)²
11−1.5−1.52.252.25
23−0.50.5−0.250.25
320.5−0.5−0.250.25
441.51.52.252.25
Total45
  1. Averages: x̄ = (1 + 2 + 3 + 4) ÷ 4 = 2.5, ȳ = (1 + 3 + 2 + 4) ÷ 4 = 2.5
  2. Table totals: Σ(x − x̄)(y − ȳ) = 4, Σ(x − x̄)² = 5
  3. Slope: m = 4 ÷ 5 = 0.8
  4. Height: k = 2.5 − 0.8 × 2.5 = 0.5
  5. Line: y = 0.8x + 0.5
  6. Residuals: 1 − 1.3 = −0.3, 3 − 2.1 = 0.9, 2 − 2.9 = −0.9, 4 − 3.7 = 0.3. Sum of squares = 0.09 + 0.81 + 0.81 + 0.09 = 1.8

Written as equations, the same solution looks like this.

x¯=1+2+3+44=2.5,y¯=1+3+2+44=2.5

m=∑(x−x¯)(y−y¯)∑(x−x¯)2=45=0.8,k=y¯−mx¯=2.5−0.8×2.5=0.5

y=0.8x+0.5,∑i(yi−(0.8xi+0.5))2=(−0.3)2+0.92+(−0.9)2+0.32=1.8

By eye, y = x looks like a better fit. But its residuals are 0, 1, −1, 0, so its sum of squares is 2. That is more than 1.8.

7. Least squares is a projection: Ax ≈ b

Write the line fit with a matrix, and you can see it is the same thing as a projection. Put the four points above into the line y = m·x + k, and each point gives one equation.

m×1+k=1

m×2+k=3

m×3+k=2

m×4+k=4

There are two unknowns, m and k, but four equations. No m and k satisfy all four. We write this as Ax = b.

Ax is the first column of A times m, plus the second column times k. However you change m and k, Ax can only move around inside "what you can make by mixing the columns of A." This set is called the column space of A (see the rank page). b is not inside it, so there is no exact answer. That is why we write Ax ≈ b (almost equal).

So pick the point in the column space that is closest to b. That is a projection. With four points, b is an arrow with four numbers (4 dimensions), so you cannot draw it. But what happens is the same as the picture in section 2. As section 3 showed, at the closest point the difference b − Ax is at a right angle to the column space. That means its dot product with every column of A is 0. Written in one line, these are the normal equations.

A𝖳(𝐛−A𝐱)=0⟺A𝖳A𝐱=A𝖳𝐛

Aᵀ is A with its rows and columns swapped (the transpose). Multiplying an arrow by Aᵀ gives you the dot product of that arrow with each column of A. Try it on the example.

Same answer as the formula. Check the residuals (−0.3, 0.9, −0.9, 0.3) too. Their dot product with the x column (1, 2, 3, 4) is −0.3 + 1.8 − 2.7 + 1.2 = 0, and with the column of 1s it is −0.3 + 0.9 − 0.9 + 0.3 = 0. The residuals are at a right angle to both columns.

This is not just for lines. A curve y = c₂·x² + c₁·x + c₀, or a formula with several inputs, is solved the same way: just add columns to A. If no column of A can be made by mixing the other columns (the columns are linearly independent), there is exactly one answer. For a line, that means "the x values are not all the same."

8. Why squares and not absolute values?

You could add up the absolute values of the errors instead. Squares are used more for two reasons.

The cost: it is weak against outliers. If one badly measured point sits far away, its square gets huge and pulls the line toward it. That is "Try this ③" in the lab above. If outliers worry you, filter them out first, or use a method that weighs big errors less (the sum of absolute values, or Huber loss).

9. Properties worth knowing

PropertyWhat it meansWhere to see it
Closest point ⇔ difference at a right angleIf p is the point on the line closest to b, then b − p is at a right angle to the line. The reverse is also trueDrag q in the first lab
Only the direction mattersMultiplying a by any number (even a negative one) leaves p the same. Only 0 does not workChange a to (4, 2) or (−2, −1)
Doing it twice changes nothingProjecting p onto the same line again gives p, because it is already on the linePut b on the line and p = b
The least squares line passes through the mean pointThe average point (x̄, ȳ) is always on the lineThe black dot "mean" in the second lab
Residuals add up to 0, and their dot product with x is 0These are exactly the two rows of the normal equations (when the line has a height k)The hand example's (−0.3, 0.9, −0.9, 0.3)

Where is it used?

# Solve the hand example in Python (numpy)
import numpy as np
x = np.array([1, 2, 3, 4]); y = np.array([1, 3, 2, 4])
A = np.column_stack([x, np.ones(4)])            # columns: the x values, 1
(m, k), *_ = np.linalg.lstsq(A, y, rcond=None)  # m ≈ 0.8, k ≈ 0.5

Common questions

What is a projection?

It is the point you reach when you drop a segment from a point onto a line or plane at a right angle. It is also the point on that line (or plane) closest to the original point. For a line through the origin, p = (a·b ÷ a·a) × a.

Is orthogonal the same as perpendicular?

Almost. Orthogonal means the dot product of two arrows is 0, and the word is also used in 4 or more dimensions, where you cannot draw a picture. An arrow of length 0 is orthogonal to every arrow.

Why does least squares use squares and not absolute values?

The math is clean, so one formula gives the answer, and big errors count more. The cost is that one far-off point can pull the line a lot.

Are least squares and linear regression the same thing?

Linear regression is a model that estimates an output by multiplying inputs by numbers and adding them up. Least squares is the most common way to pick those numbers. That is why people often use the two words to mean the same thing.

Does real code solve AᵀA x = Aᵀb directly?

For small problems that is fine. Libraries usually use a QR decomposition or a singular value decomposition (SVD) instead, because forming AᵀA can make rounding errors bigger. In Python, use numpy.linalg.lstsq.

Words

Word used hereWord used in textbooks
at a right angle (dot product 0)orthogonal, perpendicular
the point you reach at a right angle, the shadoworthogonal projection
making right-angle base arrowsGram-Schmidt process
base arrows at right angles, each of length 1orthonormal basis
how wrong a point is (actual y − the line's y)residual
sum of squaresresidual sum of squares (RSS)
best-fit lineleast squares line, regression line
height kintercept, y-intercept
what you can make by mixing the columnscolumn space
swapping rows and columnstranspose. Written Aᵀ
AᵀA x = Aᵀbnormal equations
a far-off pointoutlier
sum of squares ÷ number of pointsmean squared error (MSE)

Sources

← Orthogonal matrices and inner product spacesSingular value decomposition →