The interview problem
An engineer says ridge regression is Bayesian inference and that changing a feature from meters to centimeters cannot change predictions. Derive the relationship between a Gaussian prior and ridge, test the unit-change claim, and compare ridge with a sparse lasso solution. Explain what uncertainty a fitted coefficient does and does not provide.
Original fundamentals practice, not a reported interview. Numerical examples are invented. Primary technical reference retrieved September 27, 2026.
1. State the likelihood and prior
Use y = Xw + ε with independent zero-mean Gaussian noise ε having variance σ² per observation. For clarity, first omit the intercept and treat σ² as known. Put independent zero-mean Gaussian priors of variance τ² on the coefficients. Assume σ² and τ² are positive and fixed.
The negative log likelihood, after dropping terms constant in w, is ||y − Xw||² / (2σ²). The negative log prior contributes ||w||² / (2τ²). Maximizing posterior density therefore minimizes their sum. Multiplying by σ² gives the objective ½||y − Xw||² + (λ/2)||w||² with λ = σ²/τ².
This is a maximum a posteriori estimate, or MAP, under the stated model and parameterization. It is a single point chosen from a posterior distribution. Calling the optimization equivalent to full Bayesian inference loses that distinction.
2. Solve a small ridge problem
Differentiating the objective gives Xᵀ(Xw − y) + λw = 0, so (XᵀX + λI)w = Xᵀy. For λ > 0, the penalized system has a unique solution even if X has dependent columns. In an implementation, solve the linear system using a suitable factorization rather than explicitly forming a matrix inverse.
Consider one coefficient with observations x = [1, 2] and y = [2, 4]. Here Σx² = 5 and Σxy = 10. Unregularized least squares gives w = 2. With λ = 5, ridge gives w = 10 / (5 + 5) = 1, producing predictions [1, 2]. Shrinkage introduces bias even though these two observations lie perfectly on a line.
This toy example does not show an accuracy improvement; it shows the imposed preference. Whether that preference improves held-out prediction depends on the data-generating situation. Selecting λ on training loss alone favors weaker penalties and does not establish better generalization.
3. Show why feature units matter
Now replace x by x′ = 100x. To represent the same predictions, the new coefficient must be w′ = w/100. If the code retains λ = 5 and penalizes w′², its ridge solution becomes 1,000 / (50,000 + 5) ≈ 0.019998. Predictions are approximately [1.9998, 3.9996], very different from the original ridge predictions [1, 2].
To preserve the original objective, substitute w = 100w′ into the penalty. It becomes (5/2) × 10,000w′², so the transformed coefficient needs λ′ = 50,000. Then w′ = 1,000 / (50,000 + 50,000) = 0.01, restoring predictions [1, 2].
Keeping the same numeric prior variance after changing units changes the prior over the original effect. To preserve it here, the variance of w′ must be τ²/10,000. Standardizing features can make an isotropic penalty easier to interpret, but fit the scaling statistics on training data only and reuse them at inference. Meaningful domain-specific scales or unequal prior variances may be more appropriate than automatic standardization.
4. Track sum versus mean loss
Some libraries minimize a summed loss, while others use an average. Dividing the earlier objective by n gives (1/2n)||y − Xw||² + (λ/2n)||w||². If a mean-loss API writes its penalty as (λ_mean/2)||w||², equivalent settings require λ_mean = λ/n.
For the two-observation example, summed-loss λ = 5 corresponds to mean-loss λ_mean = 2.5. Using mean-loss λ_mean = 5 instead corresponds to summed-loss λ = 10, yielding w = 10/15 = 2/3. Always write the complete objective, including factors of two, before comparing hyperparameters.
With a fixed prior and independent new observations, likelihood evidence grows with sample size while the prior term stays fixed. Holding a mean-loss penalty constant as n grows corresponds to a changing prior strength relative to the summed likelihood. Duplicating existing rows in a file is not new independent evidence merely because the software increases n.
5. Derive sparsity in one dimension
Compare the one-dimensional lasso objective ½Σ(yᵢ − xᵢw)² + λ|w|. Write a = Σxᵢ² and b = Σxᵢyᵢ, with a > 0. For w > 0, the stationary equation is aw − b + λ = 0. For w < 0, it is aw − b − λ = 0. At zero, the absolute-value penalty has subgradient interval [−λ, λ].
Combining the cases gives w = sign(b) max(|b| − λ, 0) / a. For a = 5 and b = 10, λ = 5 gives w = 1, while λ = 12 gives exactly zero. Ridge with λ = 12 instead gives 10/17 ≈ 0.5882. This explains exact zeros through the optimization condition rather than claiming every small coefficient always disappears.
Under Gaussian noise, independent Laplace priors with density proportional to exp(−|w|/s) yield this L1 objective with λ = σ²/s. The prior is continuous: it does not put a probability mass at exactly zero. A sparse MAP solution is not the same as posterior certainty that a coefficient is zero.
6. Treat the intercept deliberately
When predictions are b₀ + Xw, it is common to leave b₀ unpenalized so shrinkage does not force the response baseline toward zero. That is a modeling choice, not a mathematical necessity. A finite prior on the intercept may be justified if its location and scale are meaningful.
With centered training features and an unpenalized intercept in squared-error regression, optimizing b₀ sets it to the training response mean. Applying the same penalty to a constant column changes that result. Document how centering, intercept handling and deployment transformations interact.
For correlated features, ridge spreads coefficients according to the geometry and scale of the design; lasso may select one among similar predictors and can be unstable under small changes. A coefficient selected by lasso is not automatically causal or uniquely important.
7. Distinguish a point estimate from uncertainty
Returning to the no-intercept Gaussian linear model with fixed noise and prior variances, the coefficient posterior covariance is (XᵀX/σ² + I/τ²)⁻¹. Its mean equals its mode, but the fitted ridge coefficients alone do not contain that covariance. Prediction for a new noisy observation also includes observation noise, not only parameter uncertainty.
For the toy data, take σ² = 1 and τ² = 0.2, giving λ = 5. Posterior variance of w is 1/(5 + 5) = 0.1. At x* = 3, latent prediction variance is 3² × 0.1 = 0.9. A new observation has variance 0.9 + 1 = 1.9 under the assumed model.
These uncertainties are conditional on the model and fixed hyperparameters. They do not automatically account for tuning λ on the same data, misspecified noise, distribution shift or uncertain feature construction. Validate predictions on appropriate held-out data and describe which uncertainty sources remain outside the calculation.
8. Follow-ups and self-review
Follow-ups: How should a coefficient prior change after a unit conversion? Why can two libraries report different λ values for equivalent fits? Why does lasso have exact zeros despite a continuous prior? When is penalizing the intercept useful? Does a MAP estimate provide a credible interval?
Score 0–2 each for the likelihood/prior derivation, objective normalization, unit-change calculation, sparse subgradient solution and uncertainty distinction. A strong answer states the exact objective before applying a memorized formula.
Sources and next steps
- Goodfellow, Bengio and Courville: Deep Learning, regularization — parameter penalties and Gaussian/Laplace MAP interpretations.
The numerical ridge/lasso examples, rescaling calculation and posterior predictive example are original derivations.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.