Q² · Question 01

Four Points, Three Regressions

What does “best fit” actually mean?
Regression · Linear algebra · Intermediate · 6 min
The question

You observe four points in the plane, (1,0)(1,0), (0,1)(0,1), (−1,0)(-1,0) and (0,−1)(0,-1). Find the single straight line that best fits these points.

Before reading on: which line would you choose, and how are you measuring the distance from a point to that line?

θ = 45°
xy(1, 0)(0, 1)(-1, 0)(0, -1)
Fitted line through the origin
Residuals
Total squared error: 2.000 Perpendicular errors: the total is exactly 2 at every angle.
Figure 1: The four points never move; only the definition of error changes. Rotate the line and switch between vertical, horizontal and perpendicular distances. A horizontal line is not the same as horizontal errors: minimising horizontal errors selects the vertical line x=0x = 0.

A tempting answer is y=0y = 0. But is it the only correct one?

The hidden assumption

A line of best fit is defined by a loss function. Three natural choices give three different answers:

Fitting ruleWhat gets squaredOptimum
OLS, predicting yy from xxVertical distancesy=0y = 0, uniquely
OLS, predicting xx from yyHorizontal distancesx=0x = 0, uniquely
Orthogonal regression (TLS)Perpendicular distancesAny line through (0,0)(0,0)

The points are unchanged. Only the definition of “error” changes.

1. Vertical errors: ordinary least squares

Suppose yy is the dependent variable and we fit

y^=mx+c.\hat y = mx + c.

OLS minimises the sum of squared vertical residuals:

LV(m,c)=∑i=14(yi−mxi−c)2.L_V(m,c) = \sum_{i=1}^{4} (y_i - m x_i - c)^2.

Inserting the four points,

LV(m,c)=(−m−c)2+(1−c)2+(m−c)2+(−1−c)2=2m2+4c2+2.\begin{aligned} L_V(m,c) &= (-m-c)^2 + (1-c)^2 + (m-c)^2 + (-1-c)^2 \\ &= 2m^2 + 4c^2 + 2. \end{aligned}

Both squared terms are non-negative, so the minimum is at m=0m = 0 and c=0c = 0:

y^=0,min⁡LV=2.\boxed{\hat y = 0, \qquad \min L_V = 2.}

The line is horizontal. Two observations lie exactly on it; the other two are one unit above and below it.

2. Horizontal errors: reverse the regression

Now make xx the dependent variable and fit

x^=ay+b,\hat x = a y + b,

which minimises the squared horizontal residuals:

LH(a,b)=∑i=14(xi−ayi−b)2=(1−b)2+(−a−b)2+(−1−b)2+(a−b)2=2a2+4b2+2.\begin{aligned} L_H(a,b) &= \sum_{i=1}^{4} (x_i - a y_i - b)^2 \\ &= (1-b)^2 + (-a-b)^2 + (-1-b)^2 + (a-b)^2 \\ &= 2a^2 + 4b^2 + 2. \end{aligned}

The minimum is at a=b=0a = b = 0:

x^=0,min⁡LH=2.\boxed{\hat x = 0, \qquad \min L_H = 2.}

The best-fitting line is now vertical, with exactly the same data.

An easy distinction to miss
A horizontal line (y=0y = 0) is not the same thing as measuring horizontal errors. Horizontal-error regression leads here to a vertical line (x=0x = 0).

3. Perpendicular errors: total least squares

Instead of treating one variable as the response, allow errors in both coordinates. Write the line in general form,

ax+by+c=0,a2+b2=1.ax + by + c = 0, \qquad a^2 + b^2 = 1.

The condition a2+b2=1a^2 + b^2 = 1 makes (a,b)(a,b) a unit normal vector, so the squared perpendicular distance from (xi,yi)(x_i, y_i) to the line is simply di2=(axi+byi+c)2d_i^2 = (a x_i + b y_i + c)^2. The total orthogonal loss is

L⊥(a,b,c)=(a+c)2+(b+c)2+(−a+c)2+(−b+c)2=2(a2+b2)+4c2=2+4c2.\begin{aligned} L_\perp(a,b,c) &= (a+c)^2 + (b+c)^2 + (-a+c)^2 + (-b+c)^2 \\ &= 2(a^2+b^2) + 4c^2 = 2 + 4c^2. \end{aligned}

It is minimised by c=0c = 0, whatever the direction (a,b)(a,b):

ax+by=0    (a2+b2=1),min⁡L⊥=2.\boxed{ax + by = 0 \;\; (a^2+b^2=1), \qquad \min L_\perp = 2.}

Every line through the origin is optimal: y=0y = 0, x=0x = 0, y=xy = x, and any other orientation. In Figure 1, the perpendicular total stays at 22 for every angle.

4. No Preferred Direction

The sample is centred at the origin and has the same spread along both axes. Stacking the observations as the rows of a matrix PP,

P⊤P=(2002)=2I.P^\top P = \begin{pmatrix} 2 & 0 \\ 0 & 2 \end{pmatrix} = 2I.

The two eigenvalues are equal. Principal component analysis, which gives the direction of the orthogonal least-squares fit, therefore has no unique leading direction.

This is more precise than saying “the covariance is zero”: zero covariance alone does not produce infinitely many TLS solutions. The degeneracy comes from the variances also being equal.

In practice this is a useful warning: an optimiser may still return a direction even when the data do not identify one. A result can be numerically valid without being uniquely determined by the data.

Takeaway

The first question in regression is not “which solver should I use?” It is:

What is the loss function, and which variable, if any, am I trying to predict?

With these four points:

Follow-up. Move the point (0,1)(0,1) slightly to the right. What happens to the OLS and orthogonal solutions, and which method becomes more sensitive to that small change?

Check it in Python

The script computes both OLS fits and the orthogonal loss at several angles. It requires numpy.

"""Four-point regression: reproduce vertical OLS, horizontal OLS and TLS losses.

Run: python regression_demo.py
Requires: pip install numpy
"""
import numpy as np

P = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]], dtype=float)
x, y = P[:, 0], P[:, 1]

# OLS that predicts y: vertical residuals.
m, c = np.linalg.lstsq(np.column_stack((x, np.ones(len(x)))), y, rcond=None)[0]
vertical_sse = np.sum((y - (m * x + c)) ** 2)

# OLS that predicts x: horizontal residuals.
a, b = np.linalg.lstsq(np.column_stack((y, np.ones(len(y)))), x, rcond=None)[0]
horizontal_sse = np.sum((x - (a * y + b)) ** 2)

print(f"Vertical OLS:   y = {m:.1f}x + {c:.1f}; SSE = {vertical_sse:.1f}")
print(f"Horizontal OLS: x = {a:.1f}y + {b:.1f}; SSE = {horizontal_sse:.1f}")

for degrees in [0, 30, 45, 60, 90]:
    theta = np.deg2rad(degrees)
    normal = np.array([-np.sin(theta), np.cos(theta)])
    tls_sse = np.sum((P @ normal) ** 2)
    print(f"Orthogonal: angle = {degrees:>2}°, SSE = {tls_sse:.1f}")
    assert np.isclose(tls_sse, 2)

print("P^T P =")
print(P.T @ P)
assert np.allclose(P.T @ P, 2 * np.eye(2))

Expected output:

Vertical OLS:   y = 0.0x + 0.0; SSE = 2.0
Horizontal OLS: x = 0.0y + 0.0; SSE = 2.0
Orthogonal: angle =  0°, SSE = 2.0
Orthogonal: angle = 30°, SSE = 2.0
Orthogonal: angle = 45°, SSE = 2.0
Orthogonal: angle = 60°, SSE = 2.0
Orthogonal: angle = 90°, SSE = 2.0
P^T P =
[[2. 0.]
 [0. 2.]]

Download regression_demo.py