Logistic Regression: Log-Odds Geometry, Cross-Entropy & IRLS
Deep-dive into binary Logistic Regression: how linear logits map to calibrated probabilities via the sigmoid, why Binary Cross-Entropy is convex, and how second-order IRLS (Newton-Raphson) outpaces Gradient Descent.
1. Problem & Discriminative Framing
Logistic Regression is the canonical discriminative binary classifier. Rather than modeling how features x are generated within each class (as Naive Bayes does), it models the conditional posterior P(y = 1 | x) directly:
z = w · x + b
P(y = 1 | x) = σ(z) = 1 / (1 + exp(-z))Notice that both Naive Bayes and Logistic Regression produce a linear decision boundary w · x + b = 0. The difference is how w and b are chosen:
- Naive Bayes sets
w_j = log(P(x_j | 1) / P(x_j | 0))by counting each feature independently. - Logistic Regression optimizes
(w, b)jointly to maximize conditional likelihoodΠ_i P(y_i | x_i), automatically splitting weight across correlated features instead of double-counting them.
2. Scoring & Decision Boundary Geometry
The linear score z = w_1 x_1 + w_2 x_2 + b is the signed distance (scaled by ||w||) from the hyperplane w · x + b = 0:
z = 0corresponds toσ(0) = 0.50(the decision boundary).z = +1.10corresponds toσ(1.10) = 0.75.z = -1.10corresponds toσ(-1.10) = 0.25.
Interactive 2D Decision Boundary & Point Inspector
Adjust w₁, w₂, b, and L2 penalty λ, or click any data point to see its exact logit z, probability σ(z), and Binary Cross-Entropy loss contribution.
3. From Log-Odds to Probabilities
Why use the sigmoid σ(z) instead of a linear threshold? Because inverting p = σ(z) yields the log-odds (logit):
log [ p / (1 - p) ] = w · x + bEvery +1 unit increase in z multiplies the odds p / (1 - p) by e ≈ 2.718. Because σ(z) maps (-∞, +∞) smoothly into (0, 1), predictions are naturally calibrated probabilities (when the model is well-specified and regularized).
4. Inference & Regularization
At inference time, computing z = w · x + b takes O(d) time:
- For a hard label,
z ≥ 0is equivalent toσ(z) ≥ 0.5—you don't even needexp()unless you want calibrated probabilities or a custom precision/recall threshold. - Why L2 regularization (
λ/2 ||w||^2) matters on separable data: In the"Clean Margin"preset, ifλ = 0, any separating hyperplane can scalew → ∞to driveσ(z) → 1and0, making BCE approach0with infinite weights. Addingλ > 0keeps||w||finite and prevents overconfident predictions.
5. Training & Optimization: IRLS (Newton-Raphson) vs. Gradient Descent
Minimizing regularized Binary Cross-Entropy:
L(w, b) = -(1/N) Σ_i [ y_i log p_i + (1 - y_i) log(1 - p_i) ] + (λ / 2) ||w||^2Because L(w, b) is strictly convex (when λ > 0), it has a unique global minimum. Its gradient and Hessian are:
∇L = (1/N) X^T (p - y) + λ w
H = (1/N) X^T S X + λ I, where S = diag( p_i (1 - p_i) )- Gradient Descent (1st-order): Steps
w ← w - η ∇L. When features have different scales ("Scale Skew"), the loss contours form a narrow valley and GD zig-zags slowly. - IRLS / Newton-Raphson (2nd-order): Solves
w ← w - H^{-1} ∇L. Multiplying byH^{-1}rescales every direction by its local curvaturep_i(1 - p_i), jumping to the minimum in ~4–6 iterations! (For very high-dimensionald, L-BFGS approximatesH^{-1}inO(m·d)instead ofO(d^3).)
Interactive GD vs. IRLS Comparison
Step through "Scale Skew" and watch IRLS collapse the gradient norm ||∇L|| in just a handful of steps while Gradient Descent crawls.
6. Comparison & Failure Modes
| Aspect | Logistic Regression | Naive Bayes | Linear-Chain CRF |
|---|---|---|---|
| Output | Single label y ∈ {0, 1} | Single label y ∈ C | Sequence (y_1, ..., y_T) |
| Objective | Binary Cross-Entropy | Joint P(x, y) MLE | Sequence NLL (log Z(x) - score) |
| Correlated features | Handled cleanly | Double-counted | Handled cleanly |
| Non-linear boundaries | Needs feature crosses / kernels | Needs feature engineering | Needs neural emissions (BiLSTM/Transformer) |
Common Failure Modes
- Non-linearly separable data: A single hyperplane cannot solve XOR or circular boundaries without feature engineering (polynomials, splines, tree leaves) or switching to kernels/trees/MLPs.
- Multicollinearity without L2: Highly correlated features can cause large oscillating weights of opposite signs unless
λ > 0stabilizesH. - Unscaled features in GD: Without standardization or second-order curvature (
IRLS/L-BFGS), step sizes are bottlenecked by the largest feature scale.