Regression §
Regression fits a parametric function to observed data by minimizing a loss (typically mean squared error). It is the simplest optimization problem: the objective is a sum of squared residuals, and the parameters are the coefficients of the model. It is relevant whenever a continuous quantity must be predicted from features, or a parametric model must be calibrated to measurements.
Authoritative sources
- The Elements of Statistical Learning (Hastie, Tibshirani, Friedman)
- Convex Optimization (Boyd & Vandenberghe)
- Ridge Regression: Biased Estimation for Nonorthogonal Problems (Hoerl & Kennard, 1970)
- Regression Shrinkage and Selection via the Lasso (Tibshirani, 1996)
- Regularization and Variable Selection via the Elastic Net (Zou & Hastie, 2005)
Linear regression §
Definition: Given design matrix and target vector , the least-squares estimator minimizes and has the closed-form solution:
This is the normal equation. It requires to form and to invert (or via QR decomposition) and is exact: no iteration needed.
Details
Derivation: the objective is . Taking the gradient and setting it to zero:
If is invertible (i.e., has full column rank ), the solution is unique.
When to use the closed form: for small (say ), the normal equation or QR decomposition is faster than any iterative method. For large (deep learning: ), the cost is infeasible, and the loss must be minimized iteratively with gradient descent, which defines the epoch, the learning rate, and the convergence rate.
What the fit looks like: the figure below shows 15 samples of with the least-squares line and the residuals (the vertical segments between each point and the line). The normal equation is the unique line that makes the sum of the squared residual lengths minimal.
Try it yourself: drag the line through the points and watch the MSE change; the dashed line is the least-squares optimum.
Example problem:
Problem
You have samples with features. The design matrix is full rank. Compare the cost of solving linear regression via the normal equation vs. 100 steps of gradient descent with .
Solution
- Normal equation: FLOPs. One shot.
- Gradient descent (100 steps): each step computes at cost FLOPs. Total: FLOPs.
For small and moderate , the closed form wins. For (deep learning), the normal equation is infeasible (), and iterative methods are the only option.
When the normal equation has infinitely many solutions; when is large the closed form is infeasible; in both cases the model can memorize noise, which is the overfitting problem.
Overfitting and the hat matrix §
Definition: the fitted values of least squares are where is the hat matrix ( is the Moore–Penrose pseudoinverse; when has full column rank, ). is symmetric and idempotent (), so its trace is the effective number of parameters of the fit. When the model can interpolate the training data exactly (zero training loss) — it has enough degrees of freedom to fit the noise.
Details
Why tr(H) counts parameters: is the orthogonal projection onto the column space of . An idempotent matrix has only eigenvalues and , so — the sum of the eigenvalues — counts the s, i.e. the dimension of the column space. Each of those dimensions can absorb one independent direction of noise: the more of them, the more noise the fit can memorize.
Bias-variance tradeoff: the expected prediction error decomposes as:
- Underfitting (high bias): the model is too simple; increasing capacity (more parameters, higher degree) reduces bias.
- Overfitting (high variance): the model is too complex; it fits noise in the training data. Reducing the effective number of parameters (regularization, more data) reduces variance.
The learning curve: the standard diagnostic is to plot the training and validation loss against training progress. The figure below shows the typical shape: the training loss keeps falling (the model keeps memorizing), while the validation loss bottoms out and then rises — the point where the two diverge is where the model starts fitting noise.
The train/validation split and these diagnostics are general machine-learning concepts; see Learning Paradigms and Models for their treatment in the context of neural networks.
Example problem:
Problem
A linear model with features is fit to samples whose rows are linearly independent. (a) What are and ? (b) Can the model achieve zero training loss? (c) Give two remedies for the resulting poor generalization.
Solution
(a) (the rows are independent), and : the fit has 100 effective parameters.
(b) Yes. Since , the system has a solution (infinitely many), so the model interpolates the training data exactly — zero training loss is guaranteed, and says nothing about generalization.
(c) 1. Regularization: ridge regression replaces with , which is always invertible and shrinks the coefficients (see Regularization). 2. More data: collect additional samples so that , making the problem well-posed.
Regularization §
Definition: regularization adds a penalty on to the loss, trading bias for variance and reducing the effective number of parameters:
- Ridge (L2): — closed form ; the matrix is always invertible, even when .
- Lasso (L1): — no closed form; solved by coordinate descent or LARS; produces sparse solutions.
- Elastic net: — interpolates between the two.
Details
Ridge: with orthonormal features, each coefficient shrinks smoothly: . The effective number of parameters becomes — regularization literally reduces the degrees of freedom the model can use to fit noise. Ridge is the same L2 penalty used as weight decay in neural network training.
Lasso: with orthonormal features, each coefficient is soft-thresholded: . Coefficients smaller than the threshold become exactly zero — the lasso performs feature selection, keeping at most features. With correlated features it tends to pick one from each group; the elastic net fixes this by adding the ridge term, which keeps correlated groups together.
Coefficient paths: the figure below shows the four coefficients of a fit as grows from 0 (plain least squares) to 2.5. Ridge shrinks everything smoothly toward zero; the lasso drives the small coefficients exactly to zero at .
Choosing λ: the penalty strength is a hyperparameter, chosen by validation loss or cross-validation (next section).
Example problem:
Problem
A model has 500 features. The true signal depends on only 12 of them; the remaining 488 are pure noise. The unregularized least-squares fit shows 12 large coefficients and about 488 small nonzero ones. Which regularizer — ridge or lasso — better matches this structure, and why?
Solution
Lasso. Its soft-thresholding drives the 488 noise coefficients exactly to zero, recovering the sparse 12-feature model. Ridge would shrink them toward zero but never to zero, leaving all 500 features active and retaining capacity to fit noise.
Model selection and goodness of fit §
Definition:
- R² (coefficient of determination): with and : the fraction of the variance of explained by the model.
- Adjusted R²: : penalizes the number of parameters .
- k-fold cross-validation: split the data into folds; train on folds, evaluate on the held-out fold; average over all rotations.
- Split protocol: train (fit the model), validation (choose hyperparameters), test (one final unbiased estimate).
Details
Why R² lies: is computed on the training data. When , interpolation gives and no matter how much of the fit is noise — a training of 1 is a red flag, not an achievement. Adjusted corrects for : it can be negative (the model is worse than predicting the mean) and saturates at 1 under interpolation, so neither metric can judge an over-parameterized model — only a held-out estimate can.
k-fold cross-validation: or in practice. Each fold is used for validation exactly once, so the estimate uses all the data. It is the standard way to choose (regularization strength), the polynomial degree, and other capacity knobs.
The split protocol:
- Train: fit the model.
- Validate: choose hyperparameters (, degree, early-stopping point).
- Test: a single final evaluation on data never used for any decision.
Example problem:
Problem
A model with parameters is fit to samples and achieves training . (a) Compute the adjusted . (b) A colleague fits a model with features to the same data and reports training . What does that tell you about its generalization?
Solution
(a) .
(b) Nothing good. , so the model interpolates the training data (), and is guaranteed regardless of how much of the fit is noise. Adjusted is also 1 (saturated). Only a held-out estimate — cross-validation or a test set — can judge this model.
Polynomial and nonlinear regression §
Definition: replace each feature by a feature map ; the model is still linear in the parameters, so the normal equation, ridge, lasso, and cross-validation all apply unchanged to the expanded design matrix. The degree is a capacity knob.
Details
Feature engineering: the only difference from linear regression is the columns of : instead of , the model sees . Everything else — the closed form, the hat matrix, regularization — carries over.
The degree as a capacity knob: the figure below fits polynomials of degree 1, 3 and 10 to 12 noisy samples of . Degree 1 underfits (the line cannot bend); degree 3 tracks the curve; degree 10 has 11 parameters for 12 points, so it interpolates the samples — and diverges wildly outside the data range.
Ill-conditioning: as grows, the columns become nearly collinear and the normal equation becomes numerically unstable. In practice, scale to or use orthogonal polynomials.
Connection to kernel methods: the ridge solution can be written in dual form as with : the prediction depends on the data only through inner products. Replacing the inner product with a kernel (e.g. the RBF ) gives a nonlinear model without writing the features explicitly; a polynomial kernel of degree is exactly polynomial regression of degree .
When to prefer a nonlinear model: when a linear model leaves systematic structure in the residuals (the fit bends in a consistent direction). But choose the smallest that the validation loss supports (see Model selection) — the degree-10 fit above is the failure mode of turning the knob too far.
Example problem:
Problem
You fit polynomial models of degree to samples. The 5-fold cross-validation MSE is: 0.42, 0.18, 0.17, 0.17, 0.19. Which degree do you choose, and why not ?
Solution
Choose (or 2): the CV error bottoms out at , and has a higher CV error — the extra capacity fits noise rather than signal. The rule is to pick the smallest within the noise floor of the minimum (parsimony): and are statistically indistinguishable here, and the simpler model wins ties.
Quiz §
Problem: A linear model with features is trained on samples (the rows of are linearly independent) by gradient descent. The learning curve shows: the training MSE decreases monotonically toward 0; the validation MSE decreases until iteration 50, then increases.
(a) What are and ? (b) Diagnose what is happening after iteration 50. (c) If only about 10 of the 300 features carry signal, choose between ridge and lasso and justify.
Solution
(a) , and since the rows are independent, . Hence : the model has 100 effective degrees of freedom — exactly enough to interpolate the 100 training points (zero training loss).
(b) Overfitting: after iteration 50 the model starts fitting the noise in the training data; the validation loss rises while the training loss keeps falling. Remedies: regularization (ridge or lasso), more data, or early stopping at iteration 50.
(c) Lasso: it drives the ~290 irrelevant coefficients exactly to zero (sparsity), keeping only the ~10 informative ones. Ridge would shrink them toward zero but never to zero, leaving all 300 features active.