Regularization Penalties, Regression Basics, and Classification Metrics
Machine LearningModel answerLast updated
Define precision and recall, compare L0/L1/L2/L∞ penalties, state linear regression assumptions, and contrast linear with logistic regression.
Predictive Modeling Fundamentals
You are reviewing a set of candidate models for a binary classification task alongside a continuous prediction task. Before choosing a regularizer or evaluating results, your team wants to align on the core concepts.
Answer each part below. Where relevant, include formulas, trade-offs, and concrete use cases.
Part 1 — Evaluation metrics Define precision and recall. Explain the trade-off between them and describe at least two real scenarios where each metric should take priority.
Part 2 — Regularization Compare L0, L1 (Lasso), L2 (Ridge), and L∞ regularization. For each: write the penalty term, describe the geometric intuition, state whether the resulting optimization is convex, and give a practical use case.
Part 3 — Linear regression assumptions List the key assumptions underlying ordinary least squares (OLS) linear regression. Note which are required for unbiasedness, which for the Gauss-Markov result, and which only for exact inference.
Part 4 — Linear vs. logistic regression Write the model formula and loss function for each. Explain why logistic regression has no closed-form solution while linear regression does, and interpret what a fitted coefficient means in each case.
Hints
Hint 1
For precision vs. recall, anchor your reasoning in the relative cost of false positives versus false negatives rather than just the class imbalance ratio.
Hint 2
The geometric shape of each penalty's constraint region (diamond, sphere, hypercube) explains why L1 produces exact zeros and L2 does not — think about where the loss contours first touch each shape.
Hint 3
When comparing the two regression formulas, focus on the link function and the assumed noise distribution — those two differences drive everything else, including whether a closed-form solution exists.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Part 1 — Precision and Recall
Both metrics are computed from the confusion matrix at a fixed decision threshold.
Precision = TP / (TP + FP): of all instances the model labeled positive, what fraction truly were positive?
Recall (sensitivity) = TP / (TP + FN): of all instances that were actually positive, what fraction did the model catch?
They move in opposite directions as you shift the threshold. Raising the threshold typically increases precision and lowers recall because you predict positive less often, so you make fewer false positive mistakes but miss more true positives.
When precision matters more — false positives are costly:
- Blocking a legitimate payment as fraud loses a real customer transaction and damages trust.
- Spam filtering: a genuine email buried in spam is worse than one junk message slipping through.
When recall matters more — false negatives are costly:
- Medical screening (cancer, sepsis): a missed case can be fatal; a false alarm costs only a follow-up test.
- First-stage retrieval in a search or ranking pipeline: a later re-ranker can filter noise, but it cannot recover items never retrieved.
Summary metrics: F1 = 2PR / (P + R) is the harmonic mean. F-β weights recall β times as heavily as precision. PR-AUC, which summarizes performance across all thresholds, is preferred over ROC-AUC on heavily imbalanced data because ROC's false-positive rate is diluted by the large negative class.
In practice, set the threshold by minimizing a business cost function (cost_FP × FP + cost_FN × FN) or by constraining one metric ("maximize recall subject to precision ≥ 0.95").
Part 2 — Regularization
Every penalty takes the form: minimize L(w) + λ · Ω(w). The penalties differ in geometry, sparsity behavior, and convexity.
| Penalty | Ω(w) | Geometry | Sparsity | Convex | Bayesian prior |
|---|---|---|---|---|---|
| L0 | count of nonzero w_j | — | Exact | No (NP-hard) | Spike-and-slab |
| L1 (Lasso) | Σ|w_j| | Diamond (corners on axes) | Exact | Yes | Laplace |
| L2 (Ridge) | Σw_j² | Sphere | Soft shrinkage | Yes | Gaussian |
| L∞ | max_j |w_j| | Hypercube | Clusters weights | Yes | — |
L0 directly counts the number of active features. This is ideal when you need exactly k features (e.g., a clinical scorecard with 10 inputs) but the optimization is combinatorial. It is approximated in practice via stepwise selection, iterative hard thresholding, or mixed-integer programming.
L1 is the tightest convex relaxation of L0. The diamond shape has corners sitting on the coordinate axes, so the loss ellipse tends to first contact a corner, pushing weights exactly to zero. This makes L1 a built-in feature selector. It is well-suited to high-dimensional sparse inputs like text n-grams or categorical IDs, but it picks one feature arbitrarily among correlated ones and that choice is unstable.
L2 produces a spherical constraint, so the solution is always in the interior — weights shrink smoothly toward zero but rarely reach it exactly. Ridge stabilizes ill-conditioned problems and distributes weight across correlated features rather than dropping them. It has a closed-form solution: w = (XᵀX + λI)⁻¹Xᵀy. It also corresponds to L2 weight decay in neural networks.
Elastic Net (αL1 + (1−α)L2) combines the two: you get sparsity plus grouping stability when features are correlated.
L∞ constrains the single largest weight, preventing any one feature from dominating. It is useful for fairness constraints ("no feature contributes more than X"), bounded sensitivity to a single input, or adversarial robustness. Notably, L∞ and L1 are dual norms: robustness to L∞-bounded input perturbations is equivalent to an L1 weight penalty.
All four penalties are scale-dependent, so features must be standardized. λ is chosen by cross-validation, and the intercept is usually left unpenalized.
Part 3 — OLS Assumptions
- Linearity. E[y | X] = Xβ. The relationship is linear in the parameters (not necessarily in the raw features).
- Strict exogeneity. E[ε | X] = 0. No omitted variable is correlated with X. Violation causes bias in β.
- No perfect multicollinearity. XᵀX must be invertible. High but imperfect collinearity inflates variance; diagnose with VIF and treat with ridge.
- Spherical errors. Var(ε | X) = σ²I — constant variance (homoscedasticity) and uncorrelated errors. Violations (heteroscedasticity, autocorrelation) require robust standard errors or WLS.
- Normality of errors. Needed only for exact finite-sample t and F inference. It is not required for unbiasedness, and in large samples the CLT makes inference valid without it.
Assumptions 1–4 together give the Gauss-Markov result: OLS is BLUE (Best Linear Unbiased Estimator). Normality is an additional, weaker assumption required only for exact inference.
Part 4 — Linear vs. Logistic Regression
Linear regression
Model: y = β₀ + βᵀx + ε, with ε ~ N(0, σ²)
Loss (OLS / MLE under Gaussian noise): minimize Σ(yᵢ − ŷᵢ)²
Closed form: β̂ = (XᵀX)⁻¹Xᵀy. The squared loss is quadratic in β, so the derivative is linear and the system can be solved exactly.
Coefficient interpretation: a one-unit increase in x_j shifts the expected output by β_j, in the original units of y.
Logistic regression
Model: P(y = 1 | x) = σ(β₀ + βᵀx), where σ(z) = 1 / (1 + e^(−z))
Link (logit): log(p / (1 − p)) = β₀ + βᵀx
Loss (cross-entropy / negative log-likelihood): −Σ[yᵢ log pᵢ + (1 − yᵢ) log(1 − pᵢ)]
No closed form: the gradient of the log-likelihood is nonlinear in β because of the sigmoid. The loss is still convex, so it is solved efficiently with gradient descent, Newton's method, or IRLS.
Coefficient interpretation: a one-unit increase in x_j shifts the log-odds by β_j, or equivalently multiplies the odds by e^(β_j).
| Linear | Logistic | |
|---|---|---|
| Output range | (−∞, ∞) | (0, 1) |
| Link function | Identity | Logit |
| Noise model | Gaussian | Bernoulli |
| Loss | Squared error | Log loss |
| Solution | Closed form | Iterative |
Common mistakes to avoid:
- Applying linear regression to a binary target; predicted probabilities can fall outside [0, 1] and the homoscedasticity assumption is violated by construction.
- Forgetting to standardize features before applying any regularized model.
- Using accuracy instead of precision/recall when classes are imbalanced.
- Treating a logistic regression coefficient as a direct probability effect rather than a log-odds effect.