Logistic regression

core 11 minlinear-modelsclassification

Builds on:Linear regression

The idea

Logistic regression is a linear model whose output is squashed into a valid probability by the sigmoid:

p=σ(z)=11+e−z,z=β0+β⊤xp = \sigma(z) = \frac{1}{1 + e^{-z}}, \qquad z = \beta_0 + \beta^{\top}x

The squashing hides the linearity, but it is still there — solve for zz and the model is linear in the log-odds:

log⁡p1−p=β0+β⊤x\log\frac{p}{1-p} = \beta_0 + \beta^{\top}x

That single line answers three classic probes:

L(β)=−∑i[yilog⁡pi+(1−yi)log⁡(1−pi)]L(\beta) = -\sum_i \Big[ y_i \log p_i + (1 - y_i)\log(1 - p_i) \Big]

So the loss is not a design choice — it is maximum likelihood.

Why not MSE? Two independent reasons interviewers want to hear. Optimization: MSE through the sigmoid is non-convex in β\beta, and its per-example gradient carries a factor σ′(z)=p(1−p)\sigma'(z) = p(1-p) that vanishes when the model is saturated — a prediction of 0.999 against a true label 0 is confidently wrong yet learns almost nothing. Statistics: cross-entropy is the Bernoulli negative log-likelihood and a proper scoring rule, so minimizing it pushes toward calibrated probabilities. Its gradient is beautifully clean — the sigmoid derivative cancels:

∇βL=∑i(pi−yi) xi\nabla_\beta L = \sum_i (p_i - y_i)\, x_i

Error times input; no vanishing factor.

Failure mode to know: with perfectly separable classes, the unregularized log-likelihood approaches its finite supremum as ∥β∥→∞\lVert\beta\rVert \to \infty but has no finite maximizer, so fitted weights diverge. Regularization (L2 by default in most libraries) restores a finite, stable solution.

Threshold lab

Move one decision threshold. The model scores stay fixed; every metric and cost changes around that choice.

TP
70
FP
270
FN
30
TN
630
Precision
20.6%
Recall
70%
F1
31.8%
Accuracy
70%
Cost / 1k
$17,700

Cost assumes $10 per false alarm and $500 per miss. Lower thresholds catch more positives; prevalence determines how many false alarms surround them.

Worked example

Model for promotion within two years from years of experience:

log⁡p1−p=−3+0.8⋅years\log\frac{p}{1-p} = -3 + 0.8 \cdot \text{years}

For a person with years=5\text{years} = 5: z=−3+4=1z = -3 + 4 = 1, so

p=σ(1)=11+e−1≈0.731p = \sigma(1) = \frac{1}{1 + e^{-1}} \approx 0.731

and the odds are p/(1−p)=0.731/0.269≈2.72=e1p/(1-p) = 0.731/0.269 \approx 2.72 = e^{1} — the odds are literally eze^{z}, which is the whole trick of the log-odds scale.

One more year (years=6\text{years} = 6): odds multiply by e0.8≈2.23e^{0.8} \approx 2.23, giving odds 2.72×2.23≈6.052.72 \times 2.23 \approx 6.05 and p=6.05/7.05≈0.858p = 6.05/7.05 \approx 0.858. So the same +1 year moved probability by about +0.13+0.13 here — but a person at years=1\text{years} = 1 (z=−2.2z = -2.2, p≈0.10p \approx 0.10) moving to z=−1.4z = -1.4 reaches only p≈0.20p \approx 0.20: identical odds ratio, different probability jump. Never report an odds ratio as "the probability increases by."

Cheat sheet

Model: p=σ(β0+β⊤x)p = \sigma(\beta_0 + \beta^{\top}x); log⁡p1−p\log\frac{p}{1-p} linear in xx. Boundary: z=0z = 0 hyperplane.

Loss (= Bernoulli NLL): −∑[ylog⁡p+(1−y)log⁡(1−p)]-\sum \left[ y\log p + (1-y)\log(1-p) \right]; gradient ∑(pi−yi)xi\sum (p_i - y_i)x_i; convex — GD/Newton converge globally.

QuantityValue
σ(0)\sigma(0)0.50.5
Max sigmoid slope0.250.25 at z=0z=0; slope =p(1−p)= p(1-p)
Odds at score zzeze^{z}
Odds ratio per unit of xjx_jeβje^{\beta_j}