Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Several clues at once
Guessing a house’s price from its size alone is crude. Using size, number of rooms and distance to the station together is better. Multiple regression gives each clue its own weight and adds them up. The weights are chosen so that, across all known houses, the guesses miss by as little as possible (squared misses again). One catch: if two clues say nearly the same thing, such as size in square metres and size in square feet, the method can’t tell how to split the credit between them.
Engineering quantities depend on several factors at once: fuel burn on mass, speed and altitude; motor temperature on current, ambient temperature and duty cycle. Multiple regression separates their effects, as long as the factors were varied independently enough in the data.
- 1. Multiple regression uses…
- 2. If two clues carry the same information, the weights become…
- 3. In the demo, as the correlation approaches 1, predictions…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.A worked example by hand
Data: (x₁, x₂, y) = (1, 1, 3), (2, 1, 4), (3, 2, 6), (4, 3, 8), (5, 5, 11).
Build X with a column of ones, then solve XᵀXβ = Xᵀy:
XᵀX = [[5, 15, 12], [15, 55, 46], [12, 46, 40]] and Xᵀy = [32, 116, 98].
Solving gives β = (1, 1, 1): y = 1 + x₁ + x₂ fits these points exactly. Check the third point: 1 + 3 + 2 = 6 ✓.
The steps in plain English:
- Put the features in a table X, one row per example, with an extra column of 1s for the intercept.
- Compute XᵀX (how features vary together) and Xᵀy (how each feature varies with the target).
- Solve the linear system for the weights. In practice, use a QR or SVD solver rather than forming XᵀX.
Reading a coefficient: βⱼ is the expected change in y when xⱼ increases by 1 with the other features held fixed.
- 1. The column of ones in X gives…
- 2. β₂ = 3 means y rises by 3 when x₂ rises by 1 and…
- 3. Size of XᵀX with 3 features plus an intercept?
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.Matrix form and computation
Model: y = Xβ + ε, with X of shape n × p of full column rank. Least squares minimises ‖y − Xβ‖², with solution:
- β̂ = (XᵀX)⁻¹Xᵀy;
- fitted values ŷ = Hy with hat matrix H = X(XᵀX)⁻¹Xᵀ, the orthogonal projection onto the column space of X.
Leverage hᵢᵢ ∈ (0, 1], with Σhᵢᵢ = p. Large leverage means an unusual x.
Inference:
- s² = RSS/(n − p);
- Cov(β̂) = s²(XᵀX)⁻¹;
- t = β̂ⱼ/SE(β̂ⱼ);
- the F-test for q added columns: F = [(RSS₀ − RSS₁)/q]/[RSS₁/(n − p)].
Computation:
| Method | Cost | Accuracy |
|---|---|---|
| normal equations (Cholesky of XᵀX) | np² + p³/3 | error ∝ κ(X)² — loses digits fast |
| Householder QR | 2np² | error ∝ κ(X) — the standard choice |
| SVD / pseudo-inverse | ~4np² + 8p³ | error ∝ κ(X); handles rank deficiency (minimum-norm solution) |
Multicollinearity: VIFⱼ = 1/(1 − R²ⱼ), where R²ⱼ comes from regressing xⱼ on the other features. Values above about 10 signal unstable coefficients.
import numpy as np
beta, *_ = np.linalg.lstsq(X, y, rcond=None) # SVD-based
from sklearn.linear_model import LinearRegression # uses scipy lstsq
import statsmodels.api as sm; print(sm.OLS(y, sm.add_constant(X)).fit().summary())Use it when effects are roughly additive and linear, and you need interpretability. Watch for p close to n, collinearity, outliers with high leverage, and non-linearity.
- 1. The sum of leverages equals…
- 2. Why prefer QR to the normal equations?
- 3. VIF = 10 corresponds to R²ⱼ =…
Level 3: Advanced
★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.Derivation and geometry
Derivation. f(β) = ‖y − Xβ‖² has gradient −2Xᵀ(y − Xβ). Setting it to zero gives XᵀXβ = Xᵀy. f is convex, and strictly convex when X has full column rank, so the solution is unique.
Geometry. The residual y − Xβ̂ is orthogonal to every column of X, so ŷ is the projection of y onto the column space. Pythagoras gives the decomposition SST = SSR + SSE.
Properties.
- Unbiasedness: E β̂ = β.
- Gauss–Markov: OLS is the best linear unbiased estimator.
- Frisch–Waugh–Lovell theorem: β̂ⱼ equals the slope of y on the part of xⱼ not explained by the other features. This is why collinear features have imprecise coefficients: little unique variation remains.
Complexity. O(np²) to factor and O(p) memory per streaming update (recursive least squares, or accumulating XᵀX in batches). For p > n there are infinitely many solutions: regularise (D1.5–D1.7) or take the minimum-norm one.
Variants. Weighted least squares (known heteroscedasticity), generalised least squares (correlated errors), recursive least squares (online), and total least squares (errors in X).
Interview questions.
- What happens if two columns are identical? XᵀX is singular; use the pseudo-inverse or drop one column.
- Why can adding a feature change another feature’s coefficient sign? Coefficients are partial effects, so they change when correlated features enter (Simpson-like confounding).
- 1. OLS residuals are orthogonal to…
- 2. With p > n, least squares has…
- 3. Recursive least squares is used for…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Numerical accuracy and collinearity, measured
History. Gauss solved least squares through the normal equations by elimination. Householder (1958) and Golub (1965) introduced the orthogonal-factorisation methods behind every modern solver. Golub and Kahan’s SVD algorithm (1965) gave the pseudo-inverse a stable computation.
Exercise 1.
- The hand example gives β = (1, 1, 1) with all three solvers.
- The residuals are orthogonal to the columns, and the leverages sum to p = 3.
- With a duplicated column, the SVD returns the minimum-norm solution with identical fitted values.
- For one extra column, F equals t².
- Two nearly copied features get VIF ≈ 137.
Exercise 2a: solver accuracy on noiseless polynomial designs (powers of x on [0, 1], 100 points; relative error of the computed coefficients against the exact all-ones answer):
| Degree | κ(X) | Normal equations | QR | SVD |
|---|---|---|---|---|
| 2 | 2 × 10¹ | 4 × 10⁻¹⁴ | 1 × 10⁻¹⁵ | 1 × 10⁻¹⁵ |
| 6 | 2 × 10⁴ | 2 × 10⁻⁸ | 7 × 10⁻¹³ | 8 × 10⁻¹³ |
| 10 | 2 × 10⁷ | 3 × 10⁻⁴ | 4 × 10⁻¹⁰ | 2 × 10⁻¹⁰ |
| 12 | 7 × 10⁸ | 4 (garbage) | 1 × 10⁻⁸ | 3 × 10⁻⁸ |
| 14 | 2 × 10¹⁰ | 4 | 8 × 10⁻⁷ | 4 × 10⁻⁷ |
The normal equations lose digits in proportion to κ², QR and SVD in proportion to κ, as theory predicts. At degree 12, forming XᵀX destroys the answer entirely while QR still has 8 correct digits. (Exact digits can vary slightly between machines; the orders of magnitude do not.)
Exercise 2b: collinearity. Degree-8 polynomial fits of a noisy sine (κ ≈ 6 × 10⁵, 200 datasets):
- the monomial coefficients swing with standard deviation up to about 2 000;
- the same fit in an orthonormal basis has coefficient SD 0.055;
- the fitted curve’s error (MSE 0.0004) is well below the noise variance (0.0025).
Collinearity ruins interpretation, not prediction.
Multiple regression in AI, robotics and aerospace
- Aerodynamic coefficient models: lift and drag as linear combinations of angle of attack, Mach and control deflections, fitted from wind-tunnel data.
- Calibration matrices: multi-axis force/torque sensors calibrated by multiple regression.
- Recursive least squares: adaptive estimation of vehicle parameters online.
- Baselines: linear models are the first benchmark for any ML regression task.
References
- G. H. Golub (1965). “Numerical methods for solving linear least squares problems.” Numerische Mathematik 7.
- G. H. Golub, C. F. Van Loan (2013). Matrix Computations, 4th ed., chapter 5. Johns Hopkins.
- N. J. Higham (2002). Accuracy and Stability of Numerical Algorithms, 2nd ed., chapters 19–20. SIAM.
- R. Frisch, F. V. Waugh (1933). “Partial time regressions as compared with individual trends.” Econometrica 1(4).
- T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., chapter 3. Springer.