The idea
Logistic regression is a linear model whose output is squashed into a valid probability by the sigmoid:
The squashing hides the linearity, but it is still there — solve for and the model is linear in the log-odds:
That single line answers three classic probes:
- Coefficient meaning: is the change in log-odds per unit of ; equivalently, the odds multiply by (the odds ratio). The probability change is not constant — it is largest near where the sigmoid is steep (slope , max ) and tiny near 0 or 1.
- Decision boundary: thresholding at is the same as , a hyperplane. Logistic regression is a linear classifier; the sigmoid changes the scores, not the geometry.
- Loss: with Bernoulli labels, the likelihood of the data is . Taking negative logs gives binary cross-entropy:
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 , and its per-example gradient carries a factor 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:
Error times input; no vanishing factor.
Failure mode to know: with perfectly separable classes, the unregularized log-likelihood approaches its finite supremum as but has no finite maximizer, so fitted weights diverge. Regularization (L2 by default in most libraries) restores a finite, stable solution.
Move one decision threshold. The model scores stay fixed; every metric and cost changes around that choice.
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:
For a person with : , so
and the odds are — the odds are literally , which is the whole trick of the log-odds scale.
One more year (): odds multiply by , giving odds and . So the same +1 year moved probability by about here — but a person at (, ) moving to reaches only : identical odds ratio, different probability jump. Never report an odds ratio as "the probability increases by."
Cheat sheet
Model: ; linear in . Boundary: hyperplane.
Loss (= Bernoulli NLL): ; gradient ; convex — GD/Newton converge globally.
| Quantity | Value |
|---|---|
| Max sigmoid slope | at ; slope |
| Odds at score | |
| Odds ratio per unit of |
- Why not MSE: non-convex through sigmoid + vanishing gradient when confidently wrong; cross-entropy is MLE, convex, gradient error.
- Coefficients are log-odds effects; is an odds ratio, not a probability change or a risk ratio (they coincide only when is small).
- Perfect separation ⇒ divergent weights ⇒ regularize (sklearn penalizes with L2 by default — remember that when comparing to statsmodels output).
- Needs scaled features for gradient-based fitting and for penalized variants; the unpenalized optimum itself is scale-equivariant.
- Multiclass: softmax / multinomial logistic; one-vs-rest is the simpler fallback.
- Outputs are probabilities worth calibration-checking — log loss and reliability curves, not just accuracy.