Line fitting: vertical vs total least squares

Two ways to fit a line to points: minimize the vertical distances, which is the linear system Ap=yAp = y, or the perpendicular distances, which is the homogeneous system Ap=0Ap = 0. Drag the points, turn them and add an outlier to see where the two differ.

Points and fitted lines vertical LS total LS click to add, drag to move, double-click or shift-click to remove
Squared residual per point (yi−(mxi+b))2(y_i - (m x_i + b))^2 (axi+byi+c)2(a x_i + b y_i + c)^2

Least squares

Least squares picks the model that minimizes the sum of squared differences between the data and the model. The objective is clearly specified and the optimum has a closed form, which makes it the standard first tool for fitting. It has two weaknesses: it is sensitive to outliers, because a squared error lets one bad point dominate, and it can only return a single fit even when the data contain several lines.

Vertical least squares

Given points (xi,yi)(x_i, y_i), find mm and bb of the line y=mx+by = mx + b that minimize the squared vertical distances:

E=∑i=1n(yi−(mxi+b))2=∥[x11⋮⋮xn1][mb]−[y1⋮yn]∥2=∥Ap−y∥2E = \sum_{i=1}^{n} \big(y_i - (m x_i + b)\big)^2 = \left\| \begin{bmatrix} x_1 & 1 \\ \vdots & \vdots \\ x_n & 1 \end{bmatrix} \begin{bmatrix} m \\ b \end{bmatrix} - \begin{bmatrix} y_1 \\ \vdots \\ y_n \end{bmatrix} \right\|^2 = \|Ap - y\|^2

Setting the derivative to zero gives the normal equations and their closed-form solution:

A⊤A p=A⊤y  ⇒  p=(A⊤A)−1A⊤yA^\top A\, p = A^\top y \;\Rightarrow\; p = (A^\top A)^{-1} A^\top y

It is cheap and exact, but it measures errors along yy only. The result depends on how the points lie in the coordinate frame: it is not rotation-invariant. And it fails completely for a vertical line: m→∞m \to \infty, and with all xix_i equal, A⊤AA^\top A is singular.

Total least squares

Write the line as ax+by+c=0ax + by + c = 0 with a2+b2=1a^2 + b^2 = 1. Then ∣axi+byi+c∣|a x_i + b y_i + c| is the perpendicular distance of a point to the line, and total least squares minimizes

E=∑i=1n(axi+byi+c)2E = \sum_{i=1}^{n} (a x_i + b y_i + c)^2

The optimal line passes through the centroid (xˉ,yˉ)(\bar x, \bar y), so c=−axˉ−byˉc = -a\bar x - b\bar y. Substituting it leaves only the normal p=(a,b)p = (a, b):

E=∑i=1n(a(xi−xˉ)+b(yi−yˉ))2=∥[x1−xˉy1−yˉ⋮⋮xn−xˉyn−yˉ][ab]∥2=∥Ap∥2=p⊤A⊤A pE = \sum_{i=1}^{n} \big(a (x_i - \bar x) + b (y_i - \bar y)\big)^2 = \left\| \begin{bmatrix} x_1 - \bar x & y_1 - \bar y \\ \vdots & \vdots \\ x_n - \bar x & y_n - \bar y \end{bmatrix} \begin{bmatrix} a \\ b \end{bmatrix} \right\|^2 = \|Ap\|^2 = p^\top A^\top A\, p

Minimizing p⊤A⊤A pp^\top A^\top A\, p subject to p⊤p=1p^\top p = 1 gives the eigenvector of the smallest eigenvalue λ2\lambda_2 of the 2 × 2 matrix A⊤AA^\top A, which is the same as the right singular vector of the smallest singular value of AA (see the SVD). The minimal error is E=λ2E = \lambda_2. The other eigenvector, of λ1\lambda_1, points along the line. Nothing here prefers the xx or yy axis, so rotating the points rotates the fitted line with them, and vertical lines are no special case.

Recap: two common optimization problems

Both come back when estimating a homography and when calibrating a camera, where they are called Method 1 and Method 2.

Try this