Why penalize?

Week 4 — Why penalize?

Quantitative biology and integrative omics

About this lecture

In the linear-algebra block we learned how to solve a linear system and when a solution is unique. Today we ask a harder question: what do we do when the honest least-squares answer is either impossible to compute or untrustworthy?

  • Why fitting many genes from few samples (p ≫ n) breaks ordinary least squares.
  • The bias–variance tradeoff: why the “best fit to this data” is often a bad predictor.
  • The core idea of penalization — pay a price for large coefficients to buy stability.
  • This is a concept day: no formulas to grind, one idea to internalise.

Biological data typically have many dimensions and few samples

A typical transcriptomics experiment: measure the expression of 20,000 genes across 30 patients, and predict a clinical outcome (say, drug response) from expression.

  • Each patient is one row; each gene is one column (a predictor).
  • You want a linear model: outcome \approx \beta_1 g_1 + \beta_2 g_2 + \dots + \beta_p g_p.
  • You have 30 equations (patients) but 20,000 unknowns (the \beta’s).

There are vastly more predictors than samples. This is the defining shape of omics data, and ordinary regression was never designed for it.

Recall: the linear model

A linear model predicts an outcome y from predictors as a weighted sum:

y \approx \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \dots + \beta_p x_p

Stacked over all n samples, this is a matrix equation:

\mathbf{y} \approx X\boldsymbol{\beta}

  • \mathbf{y}: the outcome, one number per sample (n long).
  • X: the design matrix, n rows (samples) × p columns (predictors).
  • \boldsymbol{\beta}: the coefficients we want to learn (p long).

You have used this as lm() in the statistics block. Today we look underneath it.

With more genes than samples, least squares has no single answer

Ordinary least squares (what lm() does) picks the coefficients with the smallest total squared error: for each sample take the miss, observed y minus predicted y, square it, and add them all up.

Each sample gives one equation; each coefficient is one unknown.

  • 30 patients = 30 equations. 20,000 genes = 20,000 unknowns.
  • With more unknowns than equations, infinitely many sets of coefficients fit the data perfectly, and the data cannot tell them apart. lm() returns NA for every gene beyond the 29th.

Correlated genes give large, unstable coefficients

Genes in the same pathway move together. If gene A activates gene B, then B follows A, and B also predicts the outcome, even though only A does anything.

Say the outcome is really 2 \times A. All of these predict almost the same:

coefficient on A coefficient on B prediction
2 0 2·A
1 1 ≈ 2·A
100 −98 ≈ 2·A
−50 52 ≈ 2·A
  • The data pin down the sum of the two coefficients (about 2), but not the split.
  • A little noise decides which split ordinary least squares returns: large, unstable coefficients that flip sign from one sample to the next.
  • This is multicollinearity, and in genomics it is the rule, not the exception.

Try it: correlated genes, resampled

The real goal: A good prediction of future data, not a perfect fit

It is tempting to want the \boldsymbol{\beta} that fits this dataset best. But we almost never care about this dataset — we care about the next patient.

  • A model with enough free parameters can fit the training data perfectly, noise and all.
  • That model has learned the noise, not the biology — it overfits.
  • On new patients it predicts badly, because the noise it memorised won’t repeat.

The best fit to the data at hand is often the worst predictor of new data.

The bias–variance tradeoff

Prediction error has two competing parts:

  • Bias — error from the model being too rigid to capture the true pattern. (Underfitting.)
  • Variance — error from the model being so flexible it chases noise, changing wildly from sample to sample. (Overfitting.)

\text{prediction error} \;=\; \text{bias}^2 \;+\; \text{variance} \;+\; \text{noise you cannot remove}

Ordinary least squares with many predictors is a low-bias, high-variance machine: flexible enough to fit anything, so unstable it generalises poorly. We can often lower total error by accepting a little bias to cut variance a lot.

Try it: the bias–variance dial

Penalizing large coefficients trades fit for stability

Instead of minimising only the fit error, add a penalty that grows with the size of the coefficients:

\text{what we minimise} \;=\; \underbrace{\text{fit error}}_{\text{fit the data}} \;+\; \lambda \times \underbrace{\text{(size of the coefficients)}}_{\text{stay small}}

  • The first term still rewards fitting the data.
  • The second term punishes large coefficients. \lambda \ge 0 sets how hard.
  • The fit now has to justify every unit of coefficient it uses against the penalty.

We deliberately move away from the perfect-fit solution to buy stability.

A penalty gives a single answer and steadier coefficients

Among all the coefficient sets that fit (almost) equally well, the penalty prefers the one with the smallest coefficients, and there is only one of those.

  • Correlated genes A and B: of 2·A, 1·A + 1·B, 100·A − 98·B, … the penalty picks 1·A + 1·B, the smallest.
  • With p > n: of the infinitely many perfect fits, the penalty picks one. The model can be fitted even with 20,000 genes and 30 patients.

Shrinking coefficients toward zero also makes them less sensitive to noise in any one sample. Estimates stop swinging, as the ridge cloud in the resampling demo showed.

The penalty adds bias, so \lambda must balance bias against variance

Penalization is not free. By pulling coefficients toward zero we introduce bias: the estimates are systematically shrunk, so they no longer match the true values on average.

  • Small \lambda: close to ordinary least squares — low bias, high variance (overfits).
  • Large \lambda: heavy shrinkage — high bias, low variance (underfits, coefficients near 0).
  • Somewhere in between minimises total prediction error.

\text{penalty strength } \lambda \;\longleftrightarrow\; \text{the bias–variance dial}

Choosing \lambda well (e.g. by cross-validation, in a later unit) is the whole game.

Penalized models give stable gene signatures from few patients

Why this matters for the wide, short omics matrix we started with:

  • Solvable at all. With p \gg n, penalization is what makes a gene-expression predictor computable in the first place.
  • Stable signatures. Shrinkage gives coefficients that don’t flip sign when you swap a few patients — reproducible gene signatures.
  • Better prediction on held-out patients, which is what a clinical model is for.
  • Some penalties select genes. One flavour (coming in a later unit) drives most coefficients exactly to zero, yielding a short, interpretable gene list.
  • You have met it already. lfcShrink in Bulk RNA-seq with DESeq2 pulled noisy low-count fold changes towards 0.

A first look in R

You have used lm() for ordinary least squares. Penalized models use glmnet, which fits along a whole range of \lambda at once:

# ordinary least squares -- fails or overfits when p >> n
fit_ols <- lm(response ~ ., data = expr)

# penalized regression (ridge / lasso) with glmnet
library(glmnet)
x <- as.matrix(expr[, genes])   # n samples x p genes
y <- expr$response

fit_pen <- glmnet(x, y, alpha = 0)   # alpha = 0 is ridge; alpha = 1 is lasso

Don’t worry about the details yet — later units unpack alpha, the \lambda path, and choosing \lambda by cross-validation. Today only the idea matters.

Summary

Idea Takeaway
p ≫ n biology omics has many genes, few samples
OLS picks the coefficients with the smallest total squared error; needs enough data to pin each one down
Hard failure more genes than samples, so more unknowns than equations, so no unique solution
Soft failure correlated genes can stand in for each other, giving unstable, huge coefficients
Real goal predict new patients, not fit this data perfectly
Bias–variance trade a little bias for a large cut in variance
Penalize minimise fit error + \lambda × (coefficient size)
The fix the penalty picks the smallest of the equally good answers; coefficients shrink & stabilise
The cost shrinkage adds bias; \lambda is the dial, tuned later

Next in this block: the specific penalties — ridge and lasso — and how to choose \lambda.

Optional: the maths behind penalization

The same story in the linear-algebra language of the first weeks, for those who want it.

In words (this lecture) In matrices
OLS: smallest total squared error \hat{\boldsymbol\beta}=\arg\min_{\boldsymbol\beta}\lVert\mathbf{y}-X\boldsymbol\beta\rVert^2, solved by the normal equations \hat{\boldsymbol\beta}=(X^\top X)^{-1}X^\top\mathbf{y}
more unknowns than equations \text{rank}(X^\top X)\le n<p: the p\times p matrix X^\top X is singular, no inverse
correlated genes, many near-equal answers X^\top X is nearly singular (an eigenvalue near 0), so its inverse blows up
penalty picks the smallest answer ridge: \hat{\boldsymbol\beta}=(X^\top X+\lambda I)^{-1}X^\top\mathbf{y}; adding \lambda I lifts every eigenvalue by \lambda, so it always inverts