Skip to content
nonconvexcard

Logistic Regression

The log-likelihood, its convexity, the Hessian as a weighted Gram matrix, and what happens when the data is separable.

Classical MLCore⏱ 1 hour6 min readlogistic regressionmaximum likelihoodconvexityirlsseparability
4 problems →

Model and likelihood

With y∈{0,1}y\in\{0,1\} and σ(t)=(1+e−t)−1\sigma(t) = (1+e^{-t})^{-1}, the model is

P(y=1∣x)=σ(⟨w,x⟩)=:pw(x).\P(y=1\mid x) = \sigma\big(\ip{w}{x}\big) =: p_w(x).
PropositionNegative log-likelihood and its derivatives

The negative log-likelihood of an i.i.d. sample is

L(w)=−∑i=1n[yilog⁡pw(xi)+(1−yi)log⁡(1−pw(xi))]=∑i=1n[log⁡(1+e⟨w,xi⟩)−yi⟨w,xi⟩],\mathcal{L}(w) = -\sum_{i=1}^n\Big[y_i\log p_w(x_i) + (1-y_i)\log\big(1-p_w(x_i)\big)\Big] = \sum_{i=1}^n\Big[\log\big(1+e^{\ip{w}{x_i}}\big) - y_i\ip{w}{x_i}\Big],

with

∇L(w)=X⊤(p−y),∇2L(w)=X⊤SX,S=diag⁡(pi(1−pi)),\nabla\mathcal{L}(w) = X^\top(p - y), \qquad \nabla^2\mathcal{L}(w) = X^\top S X, \quad S = \diag\big(p_i(1-p_i)\big),

where pi=pw(xi)p_i = p_w(x_i).

TheoremConvexity

L\mathcal{L} is convex. It is strictly convex if and only if rank⁡X=p\rank X = p and no pi∈{0,1}p_i\in\{0,1\}; under those conditions the Hessian is positive definite.

Algorithm — IRLS / Newton's method for logistic regression
  1. Initialise w0=0w_0 = 0, so pi=1/2p_i = 1/2 and S=14IS = \tfrac14 I.
  2. Repeat: compute pi=σ(⟨wt,xi⟩)p_i = \sigma(\ip{w_t}{x_i}) and S=diag⁡(pi(1−pi))S = \diag(p_i(1-p_i)).
  3. Solve the weighted least-squares system (X⊤SX) δ=X⊤(y−p)(X^\top SX)\,\delta = X^\top(y-p).
  4. Update wt+1=wt+δw_{t+1} = w_t + \delta (optionally with a line search for robustness).
  5. Until ∥X⊤(y−p)∥∞\norm{X^\top(y-p)}_\infty is below tolerance.

Because ∇2L\nabla^2\mathcal{L} is a weighted Gram matrix, each Newton step is a weighted least-squares solve — hence the name iteratively reweighted least squares. By the local quadratic convergence theorem, this converges in a handful of iterations when a maximiser exists.

Separability

PropositionThe MLE does not exist under separability

Suppose there is w0w_0 with ⟨w0,xi⟩>0\ip{w_0}{x_i}>0 whenever yi=1y_i=1 and ⟨w0,xi⟩<0\ip{w_0}{x_i}<0 whenever yi=0y_i=0. Then L(cw0)→0\mathcal{L}(cw_0)\to0 strictly decreasing as c→∞c\to\infty, so inf⁡wL(w)=0\inf_w\mathcal{L}(w)=0 is not attained and ∥w∥2→∞\norm{w}_2\to\infty along any minimising sequence.

Key idea

Unregularised logistic regression on separable data diverges. The standard fixes are ridge regularisation, which makes the objective coercive and restores a unique minimiser, or early stopping. But the direction of divergence is not arbitrary: Soudry et al. prove that wt/∥wt∥2w_t/\norm{w_t}_2 converges to the maximum-margin direction — the hard-margin SVM solution. So gradient descent on the logistic loss implicitly performs margin maximisation. This is the cleanest rigorous instance of the implicit bias phenomenon discussed on the deep learning theory page, and it is worth knowing that the result is a genuine theorem in this setting, unlike its deep-network analogues.

PropositionThe score equations enforce calibration

At any stationary point, X⊤p=X⊤yX^\top p = X^\top y. In particular, if the design includes an intercept column of ones, ∑ipi=∑iyi\sum_ip_i = \sum_iy_i: the predicted number of positives equals the observed number.

This is a real but limited form of calibration: it holds in-sample and only along the pp feature directions. It says nothing about out-of-sample calibration, and nothing about calibration within subgroups not expressible as feature directions — which is why post-hoc calibration methods (temperature scaling, isotonic regression) exist.

Problems

Convexity of the logistic lossProof · Beginner · 15 min Logistic regression diverges on separable dataProof · Intermediate · 30 min IRLS is a weighted least-squares solveProof · Intermediate · 30 min In-sample calibration of logistic regressionProof · Intermediate · 30 min

References

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page

Problems (4)

All problems

Papers that use this mathematics