Linear and Generalised Linear Models
Deep networks solve problems linear models cannot, but linear models remain the first thing to fit and often the last thing needed; because they are the one model class with closed-form fits, provable optimality, and error bars that need no computation beyond a matrix inverse. This page derives them from the two theories already built: least squares as an orthogonal projection in the Hilbert-space geometry of the fundamental theorems, and generalised linear models as exponential families whose conditional mean is linear; the decision theory and exponential families of page 2 doing the work.
Intuition
Section titled “Intuition”Fitting a line by least squares solves for the point in the column space of the design matrix closest to the observation vector; geometry, not algebra. That geometric picture explains everything at once: why the residuals are orthogonal to the fitted values, why adding a feature can never increase training error, why the fitted coefficients change when features are collinear, and why ridge regression’s penalty is a constraint on which directions of the column space the model may use.
The generalised linear model extends the same picture to responses that are not Gaussian; counts, proportions, survival times; by changing the noise family while keeping the linear structure in a transformed scale. The page 2 theory is what makes “change the family” a well-defined operation.
1. Least squares as projection
Section titled “1. Least squares as projection”Setup. Observations , design matrix with columns the features. The model is with .
Definition. The ordinary least squares (OLS) estimator solves
Theorem (projection solution). If has full column rank, , and is the orthogonal projection of onto the column space .
Proof. The objective is minimised when the residual is orthogonal to every column of ; any other would add a component along a column, increasing the norm. Orthogonality:
the normal equations. Full rank makes invertible, giving the display; and with residual orthogonal to is exactly the orthogonal projection’s characterisation from Hilbert space theory.
Corollary (residual geometry). ; fitted values and residuals are uncorrelated, always. And the training error is non-decreasing in the number of features: each column added enlarges , and projecting onto a larger subspace cannot increase the distance. Overfitting’s training signature is structural, not a bug.
Theorem (Gauss–Markov). Under , ; no normality, no independence, only uncorrelated equal-variance errors; OLS is the best linear unbiased estimator: among all estimators of the form that are unbiased for , OLS has the smallest covariance matrix in the positive-semidefinite order.
Proof. Any linear unbiased estimator has , so . Write for some with . Then
which expands to plus the cross terms and its transpose, plus the positive semi-definite . So
with equality iff , i.e. the estimator is OLS.
The proof is completing the square in matrices; the same move as the bias–variance decomposition of page 3; and the theorem’s honest limits matter: “best linear unbiased” is a restricted class, and page 2’s James–Stein warning already showed that biased estimators beat unbiased ones in three-plus dimensions. Ridge regression below is exactly such a biased estimator, and its success is Gauss–Markov’s boundary in action.
Under normality. Adding makes OLS also the MLE; the two theories’ answers coincide; and gives exact / distributions for inference rather than asymptotics.
2. Ridge and lasso
Section titled “2. Ridge and lasso”Definition. Ridge adds an penalty:
Definition. Lasso adds an penalty: .
The two penalties are not two spellings of regularisation. Ridge shrinks coefficients toward zero smoothly and keeps all of them nonzero; it handles collinearity by spreading weight across correlated features. Lasso sets coefficients exactly to zero; because the ball has corners; and performs feature selection, at the cost of a non-differentiable objective and coefficients whose selection is unstable under resampling.
The spectral view. Diagonalise . OLS’s variance in the -th principal direction scales as ; tiny singular values blow it up. Ridge’s is ; bounded by . The penalty truncates the directions the data barely spans, which is why it repairs collinearity and why trades bias (page 3’s decomposition) against variance.
The bias–variance trade, computed. For fixed , is increasing in and variance decreasing; the MSE is their sum, and the optimal is where their slopes cancel. Page 3’s decomposition is the calculation, and cross-validation is the estimation of it.
3. Generalised linear models
Section titled “3. Generalised linear models”Setup. Responses from an exponential family; Bernoulli, Poisson, Gamma; with means varying with the features. Page 2’s exponential family form is the vehicle.
Definition. A GLM specifies
a link function mapping the mean to the linear predictor. Logistic regression: Bernoulli family, (the logit). Poisson regression: Poisson family, .
Why the canonical link. Setting equal to the natural parameter map makes the natural parameter, at which point the log-likelihood is concave in ; no local maxima; and the sufficient statistics are . Non-canonical links work but lose both.
Theorem (IRLS). The GLM MLE satisfies a self-consistency equation whose iteration; compute weights and working responses , then weighted least squares; converges to it.
Proof sketch. The score is with the diagonal weight matrix and the working response. Setting the score to zero is a weighted normal equation with inside and ; so iterate: fix at the current , solve the weighted normal equations exactly, repeat. It is Newton–Raphson on the concave log-likelihood, hence monotone-convergent for a canonical link.
Logistic regression by IRLS, Newton’s method, and Fisher scoring are the same algorithm named three ways; and the concavity from the canonical link is what makes the “converges” claim safe.
4. The code
Section titled “4. The code”Python; the projection geometry, computed.
import numpy as np
rng = np.random.default_rng(2026)n, p = 200, 3X = np.column_stack([np.ones(n), rng.normal(size=(n, p))])beta = np.array([2.0, -1.0, 0.5, 3.0])y = X @ beta + rng.normal(0, 1.5, n)
beta_hat = np.linalg.solve(X.T @ X, X.T @ y)y_hat = X @ beta_hatresid = y - y_hat
print("residual dot fitted (should be ~0):", np.round(resid @ y_hat, 12))print("residual dot each column (should be ~0):", np.round(X.T @ resid, 12))# The corollary is a theorem about the geometry; the arithmetic confirms it# to machine precision. Adding a fourth column cannot increase the training# error -- the projection is onto a larger subspace.R; Gauss–Markov violated, ridge vindicated.
set.seed(2026)n <- 100x1 <- rnorm(n); x2 <- 0.95 * x1 + rnorm(n, 0, 0.05) # near-collineary <- 2 * x1 + 2 * x2 + rnorm(n, 0, 1)
ols <- coef(lm(y ~ x1 + x2 - 1))ridge <- coef(lm.ridge(y ~ x1 + x2 - 1, lambda = 1))cat("OLS coefficients:", round(ols, 2), " (sum:", round(sum(ols), 2), ")\n")cat("ridge coefficients:", round(ridge, 2), " (sum:", round(sum(ridge), 2), ")\n")# The true sum is 4. OLS's coefficients are wildly wrong individually --# x1 and x2 are nearly interchangeable -- while ridge's are stable and# their SUM is still right. Ridge trades unbiasedness per coefficient for# a variance reduction Gauss-Markov's restricted class cannot see.Python; logistic regression by IRLS, from scratch.
import numpy as np
rng = np.random.default_rng(2026)n, p = 400, 3X = np.column_stack([np.ones(n), rng.normal(size=(n, p))])beta_true = np.array([0.5, -1.0, 2.0])prob = 1 / (1 + np.exp(-X @ beta_true))y = (rng.random(n) < prob).astype(float)
beta = np.zeros(p)for it in range(25): eta = X @ beta mu = 1 / (1 + np.exp(-eta)) w = mu * (1 - mu) # Bernoulli weights z = eta + (y - mu) / w # working response beta_new = np.linalg.solve(X.T @ (X * w[:, None]), X.T @ (w * z)) if np.max(np.abs(beta_new - beta)) < 1e-10: beta = beta_new print(f"IRLS converged in {it+1} iterations") break beta = beta_newprint("beta_hat =", np.round(beta, 3), " truth =", beta_true)Expected: convergence in around 6–8 iterations to coefficients near the truth - and the loop is Newton’s method on the concave log-likelihood, so it converges from any start.
Common Pitfalls
Section titled “Common Pitfalls”- Believing Gauss–Markov licenses OLS everywhere. Best linear unbiased is a restricted class, and biased estimators beat it under collinearity; ridge is the standard example, and §2’s spectral view is why.
- Reading -values on coefficients in the presence of collinearity. With nearly collinear features the coefficient estimates have enormous variance individually while their combination is well determined; the individual -statistics are then noise and the model is still usable for prediction.
- Fitting a GLM without checking the link’s domain. The log link requires positive means; the logit requires means in . A fitted mean outside the support is not a small error, it is a contradiction of the family.
- Expecting lasso’s selected features to be stable. Under correlated features the picks one arbitrarily, and resampling changes the pick. Stability selection or elastic net is the repair, and the instability itself is information about the feature correlation.
- Reporting as goodness. increases with any added feature; the projection corollary again. Adjusted , cross-validated error, or a test set is the honest measure.
Cross-References
Section titled “Cross-References”- The Fundamental Theorems; the Hilbert-space projection, from which §1 is a corollary.
- Statistical Decision Theory; exponential families and conjugacy, the GLM engine.
- Estimation Theory; bias–variance, which §2’s trades.
- Statistical Learning Theory; the capacity control that the penalties instantiate.
- Gaussian Processes; the Bayesian version of the same linear model, with error bars.