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 , find and of the line that minimize the squared vertical distances:
Setting the derivative to zero gives the normal equations and their closed-form solution:
It is cheap and exact, but it measures errors along 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: , and with all equal, is singular.
Total least squares
Write the line as with . Then is the perpendicular distance of a point to the line, and total least squares minimizes
The optimal line passes through the centroid , so . Substituting it leaves only the normal :
Minimizing subject to gives the eigenvector of the smallest eigenvalue of the 2 × 2 matrix , which is the same as the right singular vector of the smallest singular value of (see the SVD). The minimal error is . The other eigenvector, of , points along the line. Nothing here prefers the or axis, so rotating the points rotates the fitted line with them, and vertical lines are no special case.
Recap: two common optimization problems
-
Minimize , the least-squares solution of : , in
MATLAB
x = A \ b. - Minimize subject to , the non-trivial least-squares solution of : the eigenvector of with the smallest eigenvalue.
Both come back when estimating a homography and when calibrating a camera, where they are called Method 1 and Method 2.
Try this
- With the noisy line, compare the dashed residuals: the orange ones are vertical, the teal ones meet the line at a right angle. The two lines are almost the same here.
- Choose Steep line: the vertical residuals get long, and the orange line comes out flatter than the points (about 75° instead of 78°), while the teal one follows them.
- Choose Vertical line: vertical least squares returns a line tilted by only about 19°, far from the points. Set the noise to 0: all are equal, is singular and there is no solution at all, while total least squares finds .
- Go back to the noisy line and drag rotate: the teal line turns with the points and its stays exactly the same. The orange grows, and around 60° to 65° the orange line breaks away from the points.
- Choose One outlier: its bar dwarfs the others and both lines tilt towards it. A point three times as far away counts nine times as much.
- Choose Two clusters: the points would be better described by two short lines, but least squares only returns one, running between the clusters.
- Delete points until only two are left: both fits pass through them exactly and .