Q&A-1Explain what least squares actually fits and how it changes a production decision.
Answer
What it fits. Ordinary least squares picks the coefficients β that minimise Σ(yᵢ − xᵢβ)². The minimiser is the conditional mean: the fitted line estimates E[y | x]. That is the entire content of the choice: it is a mean, not a median, and means move when a single point moves.
Why that matters mechanically. The gradient of the loss with respect to a residual r is 2r, so a point's pull on the fit grows linearly with how far off it is. A residual of 20 has ten times the leverage of a residual of 2. Under Gaussian errors this is the maximum-likelihood fit, and Gauss–Markov says it is the best linear unbiased estimator, but "best" there means minimum variance under homoscedastic, uncorrelated errors, not robustness to a violated error model. The guarantee and the fragility come from the same square.
Worked example. Five points on a clean line, then one bad observation:
import numpy as np
x = np.arange(5) # 0,1,2,3,4
y = 2.0 * x + 1.0 # 1,3,5,7,9
np.polyfit(x, y, 1) # slope 2.0, intercept 1.0
y[4] = 30.0 # one bad point at x=4
np.polyfit(x, y, 1) # slope 6.2, intercept -3.2Trace: with the clean data x̄=2, ȳ=5, Sxx=10, Sxy=20, so slope 2.0. After y[4]=30, ȳ=9.2 and Sxy=62, so slope = 62/10 = 6.2 and intercept = 9.2 − 6.2·2 = −3.2. The line now predicts 21.6 at x=4 where the true value is 9: a 2.4× over-prediction from one point.
Robust alternatives, on the same data. Theil–Sen takes the median of all pairwise slopes. The ten pairwise slopes here are 2, 2, 2, 2, 2, 2, 7.25, 9, 12.5, 23; the median is 2.0, recovering the true slope exactly. Huber loss is quadratic for |r| ≤ δ and linear beyond, so large residuals stop dominating; scikit-learn's HuberRegressor defaults to epsilon=1.35. Quantile regression (statsmodels QuantReg, q=0.5) fits the conditional median instead of the mean. Reach for these when outliers are expected, not exceptional.
Failure modes and detection. High-leverage points (large h_ii) can swing the fit even with small residuals; Cook's distance above roughly 4/n flags influential observations. Heteroscedastic errors break the Gauss–Markov variance claim, so standard errors are wrong even when the point estimate looks fine. Detect all three with residual-vs-fitted plots and leverage/Cook's diagnostics, not with R².
The production decision. Suppose you fit p99 latency against QPS to set an autoscaling threshold. One incident day with a retry storm is a single high-leverage point; OLS bends the line toward it and you provision for a load that never recurs. The decision changes: fit the conditional median (quantile regression) or Theil–Sen when the tail is what you are modelling, keep OLS when errors are genuinely Gaussian and you care about the mean, and check Cook's distance before shipping a threshold derived from a fit.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.