Logistic Regression
- Developed in the 1950s-1960s as a way to model binary outcomes in epidemiology and social science
- One of the most-used algorithms in industry despite being 70+ years old
- Maximum likelihood estimation connects it directly to information theory
Logistic regression applies a sigmoid function to a linear combination of features, squashing the output to a probability between 0 and 1. Training maximizes the likelihood of the observed labels, equivalent to minimizing cross-entropy loss.
- The sigmoid activation in logistic regression is the same one in neural network nodes
- Multinomial logistic regression with a softmax output is the final layer of most classifiers
- Connects to GLMs, Bayesian inference, and the cross-entropy loss ubiquitous in deep learning
- State the sigmoid model and derive the log-likelihood and its gradient.
- Recognize logistic regression as a generalized linear model with the canonical logit link.
- Derive Newton-Raphson and IRLS as second-order optimization on the log-likelihood.
- Explain why logistic regression is well-calibrated while decision-tree leaf proportions are not.
- Implement logistic regression in NumPy (IRLS), sklearn (l1 and l2), and PyTorch (BCEWithLogitsLoss).
TL;DR
Logistic regression models , where is the logistic sigmoid. It is the GLM with the canonical logit link and Bernoulli likelihood, and its parameters are fit by maximum likelihood. The negative log-likelihood is convex with gradient , so it is solved by gradient descent or, more commonly, by Newton-Raphson which becomes iteratively reweighted least squares (IRLS) [1][3]. The decision boundary is still a hyperplane in feature space, but the output is a calibrated probability rather than a hard label as with the perceptron [4].
Intuition
If you want a probability that an example is class 1, logistic regression is the simplest model that gives you one. It has the same affine score as the perceptron, but instead of thresholding at zero with the sign function it squashes through the logistic sigmoid
which maps and is monotonic. Two facts make this model well behaved:
- The log-odds is linear. The function inverts to , the logit. So logistic regression is exactly the model “the log-odds of class 1 is a linear function of ” - a small, falsifiable assumption.
- The boundary is still a hyperplane. The decision rule is the same as . The sigmoid does not curve the boundary; it curves the probability surface that sits above it.
Logistic regression is trained on the log-likelihood of the observed labels, so its outputs are designed to match the empirical conditional rate when the linear-in-log-odds assumption holds. Decision-tree leaf proportions are noisy because each leaf has a small sample, and they are systematically over-confident on the training set. That is why a logistic head on top of a calibrated representation is often the right last layer for production classifiers [1].
Math
Math: log-likelihood, gradient, and Newton step
Model
For binary labels , the model is
A useful identity: .
Likelihood
Assume the labels are conditionally Bernoulli, . The likelihood of the whole training set is
so the log-likelihood is
The negative log-likelihood is the binary cross-entropy loss; minimizing it equals maximum likelihood.
Gradient
Let and . Differentiating term by term, one shows
By the chain rule,
The negative log-likelihood gradient is therefore - the same shape as the OLS gradient, but with the sigmoid squashed predictions in place of the linear ones. There is no closed form, because depends nonlinearly on .
Hessian
Let . Then
so the negative log-likelihood is convex (and strictly convex if has full column rank), guaranteeing a unique minimum and well-behaved optimization.
Newton’s update and IRLS
Newton-Raphson on the negative log-likelihood gives the update
which can be rewritten as a weighted least-squares problem:
This is exactly the iteratively reweighted least squares (IRLS) algorithm: each step is a weighted OLS regression of the working response on with weights . IRLS is the standard fitting procedure for any GLM, not just logistic regression [3].
GLM view
A generalized linear model has three components:
- Random component: comes from an exponential-family distribution. Here, Bernoulli.
- Systematic component: a linear predictor .
- Link function: relates the mean to the linear predictor via .
For Bernoulli, the canonical link is the logit, . Plugging in, , which inverts to . So logistic regression is the GLM with Bernoulli random component and canonical (logit) link. With the canonical link, the score equation simplifies to , exactly what we derived [3][2].
Multinomial / softmax extension
For classes, replace the sigmoid with the softmax
The negative log-likelihood becomes the categorical cross-entropy, and the gradient w.r.t. is - the multi-class analogue of . It is still a convex problem.
Regularized variants
Add an L2 penalty for ridge-style logistic regression (MAP under a Gaussian prior on ); add an L1 penalty for sparse logistic regression (MAP under a Laplace prior). Both keep convexity. L2 stabilizes IRLS when is ill-conditioned; L1 yields feature selection.
Geometry of the decision boundary
The model predicts class 1 iff , which is iff . So the decision boundary is the hyperplane - identical to the perceptron and the linear SVM. The sigmoid is a monotonic post-processing of the score that produces calibrated probabilities; it does not curve the boundary.
What changes between linear classifiers is the loss and therefore which hyperplane is chosen. Perceptron stops at any separating hyperplane; linear SVM picks the max-margin one; logistic regression picks the one that maximises Bernoulli likelihood, which uses all training points, not just the support vectors.
How it compares to nearby linear classifiers
| Method | Loss | Output | Calibrated? | Multi-class strategy | Closed form? | Cost per epoch (n examples, d features) |
|---|---|---|---|---|---|---|
| Perceptron | max(0, -y (w·x + b)) | class label in {-1, +1} | no | one-vs-rest by hand | no | O(n d) |
| Logistic regression | binary cross-entropy / negative Bernoulli log-likelihood | P(y = 1 | x) in (0, 1) | yes (under linear-in-logit assumption) | softmax (multinomial) | no - convex; use IRLS / GD | O(n d) per IRLS iter, O(n d^2 + d^3) for the WLS step |
| Linear SVM | hinge: max(0, 1 - y (w·x + b)) | class label; Platt-scaled probability | no by default; yes after Platt scaling | one-vs-rest or one-vs-one | no - convex QP | O(n d) per SGD epoch, full QP otherwise |
Algorithm
# Gradient descent on the negative log-likelihood (binary cross-entropy)
w = zeros(d)
b = 0.0
for step in range(max_steps):
z = X @ w + b # affine score, shape (n,)
p = 1.0 / (1.0 + exp(-z)) # sigmoid -> probabilities
grad_w = X.T @ (p - y) # canonical-link gradient
grad_b = (p - y).sum()
w = w - eta * grad_w
b = b - eta * grad_b
# Newton / IRLS: solve a weighted least-squares problem each step
for step in range(max_steps):
z = X @ w + b
p = 1.0 / (1.0 + exp(-z))
W = diag(p * (1 - p)) # working weights
H = X.T @ W @ X # Hessian (positive semidefinite)
grad = X.T @ (p - y)
w = w - solve(H, grad) # Newton stepImplementations
import numpy as np
def sigmoid(z):
# numerically stable sigmoid
return np.where(z >= 0, 1.0 / (1.0 + np.exp(-z)), np.exp(z) / (1.0 + np.exp(z)))
def logreg_irls(X, y, max_iter=25, tol=1e-8):
"""Fit binary logistic regression by IRLS (Newton on the log-likelihood)."""
n = X.shape[0]
X1 = np.hstack([np.ones((n, 1)), X])
d = X1.shape[1]
w = np.zeros(d)
for it in range(max_iter):
z = X1 @ w
p = sigmoid(z)
W = p * (1.0 - p) # diag of W
# Newton step: w <- w + (X^T W X)^{-1} X^T (y - p)
H = (X1.T * W) @ X1
g = X1.T @ (y - p)
delta = np.linalg.solve(H + 1e-8 * np.eye(d), g) # tiny ridge for stability
w_new = w + delta
if np.linalg.norm(delta) < tol:
return w_new[0], w_new[1:], it + 1
w = w_new
return w[0], w[1:], max_iter
# tiny synthetic dataset
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 2))
y = (X[:, 0] - X[:, 1] + 0.3 * rng.normal(size=200) > 0).astype(float)
b, w, iters = logreg_irls(X, y)
print(f"converged in {iters} Newton steps")
print("intercept :", round(float(b), 3))
print("weights :", np.round(w, 3).tolist())Run the from-scratch NumPy IRLS solver live in the browser:
(no output yet)
Real-world uses
| Domain | System | Why logistic regression |
|---|---|---|
| AdTech / search ranking | Click-through-rate (CTR) baselines | Calibrated probabilities feed downstream auction logic; one-feature-per-coefficient interpretation supports auditability. |
| Credit / risk | Credit scoring (FICO-style scorecards) | Per-feature coefficients give clear adverse-action explanations required by regulation; closed-form WLS fits at scale. |
| Healthcare | Clinical prediction models (e.g. Framingham) | Sigmoid output is a probability, not a hand-tuned score; coefficients are interpretable as log-odds-ratios per unit. |
| NLP | Document classification with bag-of-words / TF-IDF features | Trains in seconds on millions of features; L1-regularized variants double as feature selection. |
| Calibration head | Last layer of deep classifiers | When the network output is already a logit, BCEWithLogitsLoss is just logistic regression in disguise. |
Pitfalls
If a feature perfectly separates the two classes, the MLE has - the log-likelihood keeps improving without bound. IRLS will fail to converge (the Hessian goes singular). Fix with L2 regularization, Firth’s bias correction, or by replacing logistic with a max-margin loss.
The intercept absorbs the marginal class prior. With heavy imbalance the learned threshold of 0.5 is wrong; either use class weights, calibrate the threshold on a validation set, or rescale the intercept by the log prior ratio.
L1 and L2 penalties depend on the scale of the features. A feature in cents gets penalized far less than the same feature in dollars. Standardize before fitting any penalized logistic regression, and never penalize the intercept.
Calibration matters for risk-sensitive applications. Train on log-loss (the binary cross-entropy), then evaluate on log-loss, Brier score, and reliability diagrams - these are all proper scoring rules that reward true probabilities and punish over-confidence.
Interview Q&A
Derive the gradient of the logistic-regression log-likelihood.research
Why is logistic regression a generalized linear model?onsite
A GLM has three pieces:
- Random component: the response distribution is in the exponential family. The Bernoulli is, with natural parameter .
- Systematic component: a linear predictor .
- Link function : relates the mean to the linear predictor via .
For logistic regression, the link is the canonical one for Bernoulli, the logit: . So the model says , which inverts to . With the canonical link, the score equation is the simple form , the Hessian is with diagonal, and Newton’s method becomes IRLS - a weighted least-squares problem each step. This is exactly the GLM machinery from McCullagh and Nelder [3].
Why is log-loss a proper scoring rule and what does that mean for training?research
A scoring rule is strictly proper if its expected value over is uniquely minimized by . For log-loss,
we compute the expected score under the true distribution :
which is minimized at because with equality iff . So if the model class is rich enough to represent the true conditional , fitting on log-loss recovers it - log-loss penalizes mis-calibrated probabilities, not just mis-classifications. This is the foundation of why logistic regression outputs are calibrated probabilities. It is also why people use log-loss to compare classifiers when calibration matters: a 0/1-accuracy comparison can rank a worse-calibrated model higher [1].
Derive Newton's update on the negative log-likelihood and show it equals IRLS.research
The negative log-likelihood is . We computed
Newton’s step is
Define the working response (the linearization of the link). Plugging in,
which is the closed-form solution to the weighted least-squares problem . Each Newton step is therefore a weighted OLS regression of the working response on the features, with weights updated each iteration. That is iteratively reweighted least squares, which is the fitting algorithm for GLMs and is what glm() in R and statsmodels.GLM actually run under the hood [3].
- 1. What is the gradient of the negative log-likelihood for binary logistic regression?
- 2. Why does the negative log-likelihood for logistic regression have NO closed form?
- 3. In the GLM framework, logistic regression uses which canonical link?
- 4. What is the geometry of the decision boundary for logistic regression?
- 5. Why are logistic-regression probabilities considered calibrated, while decision-tree leaf proportions are not?
A short evening project:
- Generate a 2-class synthetic dataset with overlap (e.g. two Gaussians with overlapping means).
- Fit logistic regression by IRLS, by gradient descent, and by sklearn; confirm all three give the same coefficients to within 1e-6.
- Plot the decision boundary and the probability heat map.
- Add 50% L2 regularization and watch the boundary tilt back towards the origin.
- Stretch: replicate Platt scaling - take a hard-margin linear SVM, then fit a logistic regression on its scores to recover calibrated probabilities, and compare to native logistic regression on log-loss.
Interactive Demo
References
- Hastie, T., Tibshirani, R., and Friedman, J. (2009). The Elements of Statistical Learning, 2nd ed.. Springer. https://hastie.su.domains/ElemStatLearn/
- Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. ISBN 978-0-387-31073-2
- McCullagh, P. and Nelder, J. A. (1989). Generalized Linear Models, 2nd ed.. Chapman & Hall / CRC. ISBN 978-0-412-31760-6
- Rosenblatt, F. (1958). The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain. Psychological Review 65(6), 386-408. doi:10.1037/h0042519