Quiz: ridge, lasso & elastic net
Short questions to test your understanding of penalized linear models. Try each on paper first, then open the answer.
Q1. Why does ordinary least squares fail (or become unstable) when you have 20 000 genes and 40 samples?
Count unknowns versus equations.
There are far more unknown coefficients (\(p = 20\,000\), one per gene) than equations (\(n = 40\), one per sample), so the system is underdetermined — infinitely many coefficient sets fit the training data perfectly, including its noise, and nothing tells ordinary least squares which one to pick. Co-regulated genes make it worse: their coefficients can trade off against each other without changing the fit. This is the \(p \gg n\) regime.
Q2. Describe in words what penalized regression makes as small as possible, and give the penalty sum for ridge and for lasso.
Fit term plus \(\lambda\) times a penalty. Squares vs. absolute values.
The total squared miss on the training data (how badly the model fits) plus \(\lambda\) times a penalty for large coefficients; \(\lambda\) sets how much the penalty counts. Ridge (L2): penalty \(= \sum_j \beta_j^2\). Lasso (L1): penalty \(= \sum_j \lvert\beta_j\rvert\).
Q3. What is the single most important behavioural difference between ridge and lasso?
What happens to a coefficient that is not useful?
Lasso sets some coefficients exactly to zero (dropping those predictors — automatic feature selection, a sparse model). Ridge shrinks all coefficients toward zero but keeps them all nonzero. So lasso selects; ridge only shrinks.
Q4. In the constraint-geometry picture, the ridge budget region is a circle and the lasso region is a diamond. Why does that difference produce zero coefficients for lasso but not for ridge?
Where are the corners of the diamond, and what does the RSS ellipse tend to touch first?
The diamond has corners that lie on the axes, where one coordinate is exactly zero. The expanding RSS ellipse tends to first touch a pointy corner, so the lasso solution often lands with a coefficient exactly \(0\). The circle is smooth with no corners, so ridge’s touch point sits at a generic location with all coefficients nonzero.
Q5. Two genes are almost perfectly correlated and both affect the phenotype. Why are the ordinary least squares coefficients unstable, and why is ridge stable?
The data pin down the combined effect of the two genes. What decides the split? What does a sum of squares prefer: one big coefficient or two medium ones?
The two genes carry the same signal, so the data only pin down their combined effect; ordinary least squares is free to split it any way (e.g. \(5\) and \(-3\) instead of \(1\) and \(1\)), and which split it returns is decided by noise — a new dataset gives a very different split. Ridge shares the weight: two medium coefficients cost less in \(\sum\beta_j^2\) than one big and one negative, so it settles on roughly equal values near the truth every time (in the resampling demo, \(\hat\beta_1\) stays between about 0.7 and 1.2 while ordinary least squares ranges from about \(-1.8\) to \(5.4\)).
(Optional, in matrix terms: ridge is \((X^\top X + \lambda I)^{-1}X^\top\mathbf{y}\); adding \(\lambda I\) raises every eigenvalue of \(X^\top X\) by \(\lambda\), so a near-zero eigenvalue becomes at least \(\lambda\) and the matrix is safely invertible.)
Q6. What is the elastic net, and what practical problem with lasso does it fix in genomics?
It mixes two penalties with a parameter \(\alpha\). Think about a group of co-regulated genes.
Elastic net uses a mix of L1 and L2: penalty \(= \alpha\times\) lasso penalty \(+ (1-\alpha)\times\) ridge penalty, with \(\alpha=1\) pure lasso and \(\alpha=0\) pure ridge. It fixes lasso’s tendency to pick one gene arbitrarily from a group of correlated (co-regulated) genes and drop the rest: the L2 part encourages correlated genes to be selected or dropped together (the grouping effect), giving more stable, biologically sensible selection.
Q7. How should you choose \(\lambda\), and why can you not just pick the \(\lambda\) that minimizes the training error?
What happens to training error as \(\lambda \to 0\)? Which model does that favour?
Choose \(\lambda\) by cross-validation — estimate error on held-out folds and pick the \(\lambda\) with the lowest cross-validated error (lambda.min, or the sparser lambda.1se within one SE). You cannot use training error because it falls monotonically as \(\lambda \to 0\), so it would always select the overfit ordinary least squares model. Only out-of-sample error reveals the right amount of shrinkage.
Q8. You run cv.glmnet(x, y, alpha = 1) and extract coefficients at lambda.1se. Most entries are zero; a few genes are nonzero. In biological terms, what is that short list, and what is one important caveat about it?
Selected genes are a signature. Does “selected” mean “causal”?
The nonzero genes are the model’s selected predictive signature — a short, testable panel that predicts the phenotype and could be turned into a targeted assay. Caveat: these genes are predictive, not proven causal. Because of collinearity a dropped gene may be just as relevant as the one kept, and the exact list can shift across CV splits at small \(n\). Interpret it as a hypothesis to validate experimentally, not as established biology.