Course contents 10 / 27

10 / 27 · Linear Classification

Logistic regression

A linear classifier that outputs probabilities: the sigmoid from a linear model of the log-odds, maximum likelihood and the log-loss, the gradient as residuals times inputs, convexity and Newton's method, why separable data make the weights run away and how a penalty stops them, calibration with reliability diagrams, Platt scaling and isotonic regression, and the softmax for several classes.

On this page 13

The linear classifiers note ended with a table of what each surrogate loss estimates. The hinge recovers only the side of the boundary; the logistic loss recovers the log-odds log⁡η1−η\log\frac{\eta}{1-\eta}, from which the probability η\eta itself follows. Many decisions need that probability, not just the side: an advertiser compares the chance of a click with its price, a clinic weighs a patient’s risk against the cost of a test, and the next note chooses thresholds from costs. Logistic regression is the linear model built for this. This note derives it from a probability model of the label, fits it by maximum likelihood, works out its gradient and its failure on separable data, and asks when its probabilities can be taken at face value.

Notation. In this note the labels are yi∈{0,1}y_i \in \{0, 1\}, which suits the Bernoulli distribution; the margin form of the previous note uses ±1\pm 1 labels, 2yi−12y_i - 1. The features xi∈Rdx_i \in \mathbb R^d include a constant 1, so the bias is one of the weights w∈Rdw \in \mathbb R^d. The score (or logit) is zi=⟨w,xi⟩z_i = \langle w, x_i\rangle, the sigmoid is σ(z)=1/(1+e−z)\sigma(z) = 1/(1 + e^{-z}), and the model’s probability is pi=σ(zi)p_i = \sigma(z_i). XX is the n×dn \times d matrix with rows xix_i, yy and pp are the vectors of labels and probabilities, and η(x)=P(y=1∣x)\eta(x) = P(y = 1 \mid x) is the true probability.

Why the log-odds?#

A linear function ⟨w,x⟩\langle w, x\rangle takes every real value; a probability lives in [0,1][0, 1]. Modelling pp itself as linear produces “probabilities” of 1.31.3 and −0.2-0.2. The odds p/(1−p)p/(1 - p) live in (0,∞)(0, \infty) and the log-odds in (−∞,∞)(-\infty, \infty), so the log-odds is a quantity that a linear model can describe without leaving its range:

log⁡P(y=1∣x)1−P(y=1∣x)=⟨w,x⟩⟺P(y=1∣x)=11+e−⟨w,x⟩=σ(⟨w,x⟩).\log\frac{P(y = 1 \mid x)}{1 - P(y = 1 \mid x)} = \langle w, x\rangle \quad\Longleftrightarrow\quad P(y = 1 \mid x) = \frac{1}{1 + e^{-\langle w, x\rangle}} = \sigma(\langle w, x\rangle) .

The sigmoid is just the inverse of the log-odds. Two of its properties are used below: σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z), and σ′(z)=σ(z) (1−σ(z))\sigma'(z) = \sigma(z)\,(1 - \sigma(z)).

Why linear log-odds are natural. By Bayes’ formula in odds form, the posterior log-odds are the prior log-odds plus the log likelihood ratio log⁡p(x∣y=1)p(x∣y=0)\log\frac{p(x \mid y = 1)}{p(x \mid y = 0)}. When the two classes are Gaussian with a common covariance Σ\Sigma and means μ1,μ0\mu_1, \mu_0, the quadratic terms of the two log-densities cancel and

log⁡p(x∣y=1)p(x∣y=0)=(μ1−μ0)⊤Σ−1x+const,\log\frac{p(x \mid y = 1)}{p(x \mid y = 0)} = (\mu_1 - \mu_0)^\top \Sigma^{-1} x + \text{const},

which is linear in xx (Bishop, 2006, Section 4.2.1; the Gaussian itself is in the multivariate Gaussian note). The same happens for many other class-conditional families. Logistic regression keeps the conclusion, linear log-odds, and drops the assumption: it models P(y∣x)P(y \mid x) directly and never describes how xx is distributed.

Worked example. Classes N(1,1)N(1, 1) and N(−1,1)N(-1, 1) on a line, equally likely. The log likelihood ratio is [(x+1)2−(x−1)2]/2=2x\big[(x + 1)^2 - (x - 1)^2\big]/2 = 2x, so the log-odds are exactly 2x2x and at x=0.5x = 0.5 the probability of class 1 is σ(1)=0.731\sigma(1) = 0.731. If class 1 is rarer, with prior 0.20.2, the prior log-odds log⁡(0.2/0.8)=−1.386\log(0.2/0.8) = -1.386 are added: σ(1−1.386)=0.405\sigma(1 - 1.386) = 0.405. A change of base rate shifts every log-odds by the same constant, the rule the probability course derived for deployment under a different prevalence.

Reading the weights. Increasing feature jj by one unit adds wjw_j to the log-odds, which multiplies the odds by ewje^{w_j}, whatever the other features are: a weight of 0.70.7 doubles the odds (e0.7=2.01e^{0.7} = 2.01). This is the odds ratio that medical and social-science papers report. It describes the model, and it is causal only if the data allow a causal reading.

Maximum likelihood gives the log-loss#

Each label is a Bernoulli variable with success probability pi=σ(⟨w,xi⟩)p_i = \sigma(\langle w, x_i\rangle), and the labels are independent given the features. The likelihood of the training labels is ∏ipiyi(1−pi)1−yi\prod_i p_i^{y_i} (1 - p_i)^{1 - y_i}, and its negative logarithm is

L(w)=−∑i=1n[yilog⁡pi+(1−yi)log⁡(1−pi)],\mathcal L(w) = -\sum_{i=1}^n \Big[ y_i \log p_i + (1 - y_i) \log(1 - p_i) \Big] ,

the log-loss, or binary cross-entropy. That cross-entropy losses are negative log-likelihoods of a categorical model is derived in the maximum likelihood note; the information-theoretic reading, the surprise of the observed labels under the model’s beliefs, is in the entropy note of the generative models course.

It is the logistic loss. For yi=1y_i = 1 the term is −log⁡σ(zi)=log⁡(1+e−zi)-\log\sigma(z_i) = \log(1 + e^{-z_i}). For yi=0y_i = 0 it is −log⁡(1−σ(zi))=−log⁡σ(−zi)=log⁡(1+ezi)-\log(1 - \sigma(z_i)) = -\log\sigma(-z_i) = \log(1 + e^{z_i}). With the margin Mi=(2yi−1) ziM_i = (2y_i - 1)\, z_i both cases read

L(w)=∑i=1nlog⁡(1+e−Mi),\mathcal L(w) = \sum_{i=1}^n \log\big(1 + e^{-M_i}\big) ,

the logistic surrogate of the previous note, with the natural logarithm. So the surrogate chosen there for its shape is exactly maximum likelihood under the model of linear log-odds.

Worked example. One feature, w=1w = 1, no bias, three points: x=(2,−1,0.5)x = (2, -1, 0.5) with labels y=(1,0,0)y = (1, 0, 0). The probabilities are p=σ(x)=(0.881,0.269,0.622)p = \sigma(x) = (0.881, 0.269, 0.622). The losses are −log⁡0.881=0.127-\log 0.881 = 0.127 for the confident correct point, −log⁡(1−0.269)=0.313-\log(1 - 0.269) = 0.313 for the moderately correct one, and −log⁡(1−0.622)=0.974-\log(1 - 0.622) = 0.974 for the third point, which the model places on the wrong side; the total is 1.4141.414. A model that said p=0.99p = 0.99 for the third point would pay −log⁡0.01=4.6-\log 0.01 = 4.6 for it: the log-loss punishes confident mistakes without limit.

The gradient: residuals times inputs#

Differentiate one term with respect to its score. For yi=1y_i = 1, ddz[−log⁡σ(z)]=−(1−σ(z))\frac{d}{dz}\big[-\log\sigma(z)\big] = -(1 - \sigma(z)); for yi=0y_i = 0, ddz[−log⁡(1−σ(z))]=σ(z)\frac{d}{dz}\big[-\log(1 - \sigma(z))\big] = \sigma(z); both cases equal σ(zi)−yi\sigma(z_i) - y_i. By the chain rule, with ∂zi/∂w=xi\partial z_i / \partial w = x_i,

∇L(w)=∑i=1n(σ(zi)−yi) xi=X⊤(p−y).\nabla \mathcal L(w) = \sum_{i=1}^n \big(\sigma(z_i) - y_i\big)\, x_i = X^\top (p - y) .

The form is the one of least squares, X⊤(Xw−y)X^\top(Xw - y): prediction minus target, times the input. A point pulls the weights in proportion to how wrong its probability is, and a correctly and confidently classified point, with pi≈yip_i \approx y_i, hardly pulls at all. In the worked example p−y=(−0.119,0.269,0.622)p - y = (-0.119, 0.269, 0.622), the gradient with respect to ww is −0.119⋅2+0.269⋅(−1)+0.622⋅0.5=−0.196-0.119 \cdot 2 + 0.269 \cdot (-1) + 0.622 \cdot 0.5 = -0.196, and with respect to a bias it would be ∑i(pi−yi)=0.772\sum_i (p_i - y_i) = 0.772: the predictions are too high on average (their mean is 0.5910.591, the share of positives 1/31/3), and a gradient step lowers the bias.

Convexity. Differentiating once more, with σ′=σ(1−σ)\sigma' = \sigma(1 - \sigma), gives the Hessian

∇2L(w)=∑i=1npi(1−pi) xixi⊤=X⊤SX,S=diag⁡(pi(1−pi)).\nabla^2 \mathcal L(w) = \sum_{i=1}^n p_i (1 - p_i)\, x_i x_i^\top = X^\top S X, \qquad S = \operatorname{diag}\big(p_i (1 - p_i)\big) .

For any vector vv, v⊤X⊤SXv=∑ipi(1−pi)⟨xi,v⟩2≥0v^\top X^\top S X v = \sum_i p_i(1 - p_i) \langle x_i, v\rangle^2 \ge 0, so the log-loss is convex, and strictly convex when XX has full column rank (every pip_i is strictly between 0 and 1 at finite ww). Every local minimum is global.

No closed form, but Newton’s method. Setting the gradient to zero gives X⊤(σ(Xw)−y)=0X^\top(\sigma(Xw) - y) = 0, equations that are nonlinear in ww, unlike the normal equations of least squares. They are solved iteratively. Gradient descent works; with a few hundred features, Newton’s method converges in a handful of steps:

w←w−(X⊤SX)−1X⊤(p−y).w \leftarrow w - (X^\top S X)^{-1} X^\top (p - y) .

Each step is a weighted least-squares problem with weights SS and a working response Xw+S−1(y−p)Xw + S^{-1}(y - p), which is why it is called iteratively reweighted least squares (Bishop, 2006, Section 4.3.3). On the breast cancer data with two standardized features, 25 Newton steps reach a gradient of 10−1510^{-15} and the same weights as scikit-learn (Code section).

What the optimum guarantees. The bias is the weight of the constant feature, so the gradient equation for it reads ∑i(pi−yi)=0\sum_i (p_i - y_i) = 0: at the maximum likelihood fit, the average predicted probability equals the share of positives in the training set. The same holds within any group marked by a binary feature of the model. On the breast cancer fit, the predicted probabilities of malignancy add up to 212.0212.0, the number of malignant tumours.

Figure

Logistic regression: the boundary and the contours of probability

Probability contours of logistic regression and the sigmoid along wLeft: clouds of blue and red points and five parallel slanted lines labelled 0.1, 0.25, 0.5, 0.75, 0.9. Right: an S-shaped curve of probability against z, with ticks for class 1 at the top and class 0 at the bottom, overlapping near z = 0.data and contours of p0.10.250.50.750.9along w: p = σ(z)−6−303600.51z = w·x + bProbability contours of logistic regression and the sigmoid along wLeft: clouds of blue and red points and five parallel slanted lines labelled 0.1, 0.25, 0.5, 0.75, 0.9. Right: an S-shaped curve of probability against z, with ticks for class 1 at the top and class 0 at the bottom, overlapping near z = 0.data and contours of p0.10.250.50.750.9along w: p = σ(z)−6−303600.51z = w·x + b
Left: 60 points of class 1 (blue circles) and 60 of class 0 (red squares) from two Gaussian clouds with equal covariance, for which the true log-odds are linear, 2x₁ + 1.2x₂. The maximum likelihood fit is w = (1.61, 1.70), b = 0.22. The boundary p = 0.5 (black) and the contours p = 0.1, 0.25, 0.75, 0.9 are parallel lines w·x + b = logit(p); the band between 0.1 and 0.9 is 2 ln 9 / ‖w‖ = 1.88 wide. Right: the same model along the direction of w. The probability depends on x only through z = w·x + b, along a sigmoid; the ticks at the top and bottom are the labels of the points. Training accuracy 0.875 (105 of 120), mean log-loss 0.30.

The figure shows the geometry. The probability depends on xx only through the score z=⟨w,x⟩z = \langle w, x\rangle, so its contours are the hyperplanes z=logit⁡(p)z = \operatorname{logit}(p), all parallel to the boundary. Moving along ww by a distance 1/∥w∥1/\lVert w\rVert changes the log-odds by one; the larger ∥w∥\lVert w\rVert, the steeper the transition from “surely 0” to “surely 1”. The width of the band between p=0.1p = 0.1 and p=0.9p = 0.9, 2log⁡9/∥w∥2\log 9/\lVert w\rVert, is a direct picture of how confident the model is.

Separable data: the weights run away#

If some w0w_0 classifies the training set perfectly, every margin Mi=(2yi−1)⟨w0,xi⟩M_i = (2y_i - 1)\langle w_0, x_i\rangle is positive, and scaling w0w_0 by c>1c > 1 multiplies them all by cc. Each term log⁡(1+e−cMi)\log(1 + e^{-cM_i}) decreases towards zero as cc grows, while L(w)>0\mathcal L(w) > 0 for every finite ww. The infimum, zero, is not attained: the maximum likelihood estimate does not exist. It exists exactly when the classes overlap, neither completely nor quasi-completely separated (Albert and Anderson, 1984).

Gradient descent on separable data keeps going. Its direction settles down, towards the maximum-margin separator of the support vector machines note (Soudry et al., 2018), while its norm grows without bound, only logarithmically in the number of steps, and all predicted probabilities drift to 0 and 1. On four points x=−2,−1,1,2x = -2, -1, 1, 2 with the two right ones positive, gradient descent with step 0.50.5 from zero has slope 5.555.55 after 10310^3 steps, 7.837.83 after 10410^4 and 10.1310.13 after 10510^5: about log⁡(t/4)\log(t/4), so every tenfold increase in steps adds log⁡10=2.30\log 10 = 2.30 to the weight and divides the loss, about 2/t2/t, by ten.

Separation is not exotic. As the previous note showed, random labels on nn points in dd dimensions are separable with probability 1/21/2 when n=2(d+1)n = 2(d + 1), and real data are far from random. The breast cancer data with all 30 standardized features are linearly separable: as the penalty is relaxed, the fitted weight norm goes from 3.83.8 to 1,8911{,}891 and the training log-loss from 0.0530.053 to 0.0040.004 (Code section).

The penalty fixes it. Minimizing

L(w)+λ2∥w∥2\mathcal L(w) + \frac{\lambda}{2}\lVert w\rVert^2

adds λI\lambda I to the Hessian, so the objective is strictly convex and grows without bound in every direction: a unique, finite minimizer exists for any data. The penalty is a Gaussian prior w∼N(0,λ−1I)w \sim N(0, \lambda^{-1} I), and the penalized fit is its MAP estimate; the bias is usually left unpenalized. Larger λ\lambda keeps ∥w∥\lVert w\rVert small and the probability contours wide apart. scikit-learn’s LogisticRegression applies this penalty by default, with C = 1 the inverse of its strength; C = np.inf gives plain maximum likelihood.

Calibration: do the probabilities mean what they say?#

A model is calibrated if, among the examples to which it assigns probability pp, a share pp is positive:

P(y=1∣p^(x)=p)=pfor every p.P\big(y = 1 \mid \hat p(x) = p\big) = p \qquad \text{for every } p .

A weather forecaster who says “70% chance of rain” is calibrated if it rains on 70% of such days. Calibration is checked with a reliability diagram: split the test predictions into bins (ten equal-width bins are common), and plot the mean prediction in each bin against the share of positives in it. A calibrated model’s points lie on the diagonal. The expected calibration error summarizes the distance,

ECE=∑bnbn ∣yˉb−pˉb∣,\mathrm{ECE} = \sum_{b} \frac{n_b}{n} \,\big\lvert \bar y_b - \bar p_b \big\rvert ,

with nbn_b examples, mean prediction pˉb\bar p_b and share of positives yˉb\bar y_b in bin bb. The number depends on the bins and on the sample size. Each bin’s share of positives is noisy, so even perfectly calibrated predictions, with labels drawn as y∼Bernoulli(p)y \sim \mathrm{Bernoulli}(p), have a positive ECE: on 10,000 examples it averages 0.0100.010 with ten bins and 0.0220.022 with fifty. A reliability diagram should therefore show the count of each bin, and the ECE should be read next to the log-loss and the Brier score below, which need no bins. Calibration alone is not quality: predicting the base rate for everyone is perfectly calibrated and useless. A good probabilistic classifier is calibrated and separates the classes, which the next note measures.

Why the log-loss aims at calibrated probabilities. At a single point with true probability η\eta, a prediction pp has expected log-loss −ηlog⁡p−(1−η)log⁡(1−p)-\eta\log p - (1 - \eta)\log(1 - p). Its derivative −η/p+(1−η)/(1−p)-\eta/p + (1 - \eta)/(1 - p) vanishes at p=ηp = \eta: the expected loss is minimized by telling the truth. Losses with this property are called proper scoring rules (Gneiting and Raftery, 2007). The squared error (y−p)2(y - p)^2, the Brier score (Brier, 1950), is proper too; the absolute error ∣y−p∣\lvert y - p\rvert is not, since its expected value η(1−p)+(1−η)p\eta(1 - p) + (1 - \eta)p is linear in pp and minimized at 00 or 11. Propriety is a statement about the best possible predictor. A fitted logistic regression is calibrated in the large on its training set, by the bias equation above, and calibrated everywhere only to the extent that the true log-odds are linear and the sample is large. Overfitting makes it overconfident; a strong penalty makes it underconfident.

Other models are not even aiming. A hinge-trained score is not a probability at all. Naive Bayes multiplies the evidence of correlated features as if it were independent, counts the same signal several times, and tends to push its probabilities towards 0 and 1. Max-margin methods and boosted trees tend to push them away from 0 and 1, giving S-shaped reliability curves (Niculescu-Mizil and Caruana, 2005). Modern deep networks trained to zero training error tend to be overconfident (Guo et al., 2017). These are observed tendencies of model families, not guarantees. In the Code section, naive Bayes on Gaussian classes with correlated features has ECE 0.2360.236 and log-loss 1.1331.133, while logistic regression, the correct model there, has 0.0120.012 and 0.6330.633. On these 10,000 test examples, ECE values near 0.010.01 are at the level of perfectly calibrated predictions, so they cannot rank well-calibrated models against each other.

Recalibration. A score that ranks well but is miscalibrated can be repaired by a monotone map fitted on data not used for training (a held-out set or cross-validation, as in the validation note):

  • Platt scaling (Platt, 1999) fits a one-feature logistic regression on the score: p^=σ(as+b)\hat p = \sigma(a s + b), where ss is the model’s score or the logit of its probability, by maximum likelihood. To limit overfitting on small sets, Platt replaced the targets 11 and 00 by (N++1)/(N++2)(N_+ + 1)/(N_+ + 2) and 1/(N−+2)1/(N_- + 2), with N±N_\pm the class counts. Temperature scaling of neural networks is the special case b=0b = 0, a single factor on the logits (Guo et al., 2017).
  • Isotonic regression (Zadrozny and Elkan, 2002) fits the best non-decreasing step function of the score in squared error, by the pool-adjacent-violators algorithm (Ayer et al., 1955): walk through the examples sorted by score and, whenever a block’s mean exceeds the next block’s, merge them. For six labels in score order, 0,1,0,0,1,10, 1, 0, 0, 1, 1, the second label exceeds the third, the merged block (1,0)(1, 0) with mean 0.50.5 exceeds the fourth label, and the result is 0,13,13,13,1,10, \tfrac13, \tfrac13, \tfrac13, 1, 1. It can fix any monotone distortion, but it needs more data: with fewer than a few hundred to a thousand calibration examples Platt scaling usually does better (Niculescu-Mizil and Caruana, 2005).

Platt’s map is strictly increasing (for a>0a > 0) and leaves the ranking, and therefore the ROC curve of the next note, unchanged. The isotonic map is only non-decreasing: it can merge neighbouring scores into ties, which changes the AUC little. Both statements are about one fixed score. CalibratedClassifierCV with cv=5, as in the Code section, fits the model and its map on each of five folds and averages the five calibrated outputs; an average of different models can rank the examples differently from any one of them. In the Code section the AUC of naive Bayes is 0.6950.695 before and after either recalibration.

Figure

Reliability diagram: an overconfident model before and after calibration

Reliability diagram of three versions of one modelA square with its diagonal. The red S-shaped line of the raw scores lies above the diagonal on the left and below it on the right. The blue line after Platt scaling and the green line after isotonic regression follow the diagonal. Under the square, a histogram of the raw scores with tall bars at 0 and at 1.00.5100.51mean predicted probability in the binshare of positiveshistogram of the raw scoresperfect calibrationraw scores, ECE 0.143Platt, a = 0.41, b = 0.09, ECE 0.022isotonic, ECE 0.03956% of scores below 0.1 or above 0.9(15% of the true probabilities)Reliability diagram of three versions of one modelA square with its diagonal. The red S-shaped line of the raw scores lies above the diagonal on the left and below it on the right. The blue line after Platt scaling and the green line after isotonic regression follow the diagonal. Under the square, a histogram of the raw scores with tall bars at 0 and at 1.00.5100.51mean predicted probability in the binshare of positiveshistogram of the raw scoresperfect calibrationraw scores, ECE 0.143Platt, a = 0.41, b = 0.09, ECE 0.022isotonic, ECE 0.03956% of scores below 0.1 or above 0.9(15% of the true probabilities)
Simulation: true log-odds z ~ N(0, 1.5²), labels y ~ Bernoulli(σ(z)), model scores σ(2.5 z), the right order but log-odds 2.5 times too large. The 4,000 test examples are split into 10 bins by prediction; each point is the mean predicted probability in a bin against the share of positives in it. The raw scores give an S-shaped curve (ECE 0.143): a prediction of 0.97 comes true 83% of the time. The calibrators are fitted on 2,000 other examples: Platt scaling σ(a·logit s + b) with a = 0.41 (the true value is 1/2.5 = 0.4) brings the points back to the diagonal (ECE 0.022), isotonic regression too (0.039), more noisily. Bottom: the histogram of the raw scores; 56% lie below 0.1 or above 0.9, against 15% of the true probabilities.

Several classes: the softmax#

With KK classes, each class gets its own weight vector wkw_k and score zk=⟨wk,x⟩z_k = \langle w_k, x\rangle, and the scores are turned into probabilities by the softmax:

P(y=k∣x)=ezk∑j=1Kezj.P(y = k \mid x) = \frac{e^{z_k}}{\sum_{j=1}^K e^{z_j}} .

Maximum likelihood gives the cross-entropy loss −∑ilog⁡P(yi∣xi)-\sum_i \log P(y_i \mid x_i), and its gradient has the same residual form, X⊤(P−Y)X^\top(P - Y), with PP the n×Kn \times K matrix of predicted probabilities and YY the one-hot labels. Only differences of scores matter: adding the same vector to every wkw_k changes nothing, so one class can be fixed at wK=0w_K = 0. For K=2K = 2 the softmax probability of class 1 is ez1/(ez1+ez2)=σ(z1−z2)e^{z_1}/(e^{z_1} + e^{z_2}) = \sigma(z_1 - z_2), which is binary logistic regression with w=w1−w2w = w_1 - w_2. The multiclass note develops this model and its alternatives.

Where logistic regression reappears#

  • The first text classifier of the NLP course is multinomial logistic regression on word counts, the bag-of-words model; it reaches 0.918 accuracy on AG News and remains the baseline in the text classification note.
  • Negative sampling in word2vec trains a logistic regression that tells observed (word, context) pairs from random ones, and a language model’s output layer is a softmax over the vocabulary trained with cross-entropy, as in the n-gram note.
  • The discriminator of a GAN is a classifier trained with exactly this log-loss (GAN note), and noise-contrastive estimation learns an unnormalized density by logistic regression of data against noise (energy-based models note).

What changed#

Linear classifier with a surrogate lossLogistic regression
Outputa side of the hyperplanea probability σ(⟨w,x⟩)\sigma(\langle w, x\rangle)
Losschosen for its shapederived: the negative Bernoulli log-likelihood
What the score meansdepends on the lossthe log-odds; ewje^{w_j} is an odds ratio
Fittinggradient methodsthe same, plus Newton’s method (IRLS) with ∇=X⊤(p−y)\nabla = X^\top(p - y)
Separable datamany solutionsno maximum likelihood estimate; the penalty (a Gaussian prior) restores one
Probabilitiesnot availablecalibrated if the model is right; checked by a reliability diagram, repaired by Platt or isotonic maps

Limitations#

  • Linear log-odds. Interactions and nonlinear effects must be supplied as features; otherwise the model is miscalibrated where the true log-odds bend.
  • Separation and near-separation. Unpenalized fits diverge or give huge, unstable coefficients; the penalty strength then matters and must be chosen by validation, with features on comparable scales.
  • Coefficients are fragile to interpret. Correlated features share and trade weight, and an odds ratio is conditional on all the other features in the model.
  • Calibration is not discrimination. A calibrated model can still separate the classes poorly, and calibration measured on one population breaks when the base rate or the feature distribution shifts.
  • Rare classes. The probabilities can be right while the default threshold of 0.5 almost never fires; choosing the threshold is a separate decision.

What you should remember#

  • Logistic regression models the log-odds as linear: P(y=1∣x)=σ(⟨w,x⟩)P(y = 1 \mid x) = \sigma(\langle w, x\rangle). Gaussian classes with a common covariance give exactly this form.
  • Maximum likelihood under the Bernoulli model gives the log-loss, which is the logistic surrogate ∑ilog⁡(1+e−Mi)\sum_i \log(1 + e^{-M_i}).
  • The gradient is X⊤(p−y)X^\top(p - y), the Hessian X⊤SXX^\top S X with S=diag⁡(pi(1−pi))S = \operatorname{diag}(p_i(1 - p_i)); the loss is convex, has no closed-form minimizer, and Newton’s method is IRLS.
  • At the optimum with a bias, the mean predicted probability equals the share of positives.
  • On separable data the weights grow without bound and the probabilities go to 0 and 1; an L2 penalty, a Gaussian prior, gives a unique finite fit.
  • The log-loss is a proper scoring rule. Check calibration with a reliability diagram that shows its bin counts, and with ECE next to the log-loss or Brier score; repair it with Platt scaling or isotonic regression fitted on held-out data. Platt scaling keeps the ranking; isotonic regression can only add ties.
  • The softmax with cross-entropy is the KK-class version; for K=2K = 2 it reduces to the sigmoid.

Code#

The Gaussian log-odds, Newton’s method, separation, calibration of naive Bayes, pool adjacent violators and the two-class softmax:

import numpy as np
from scipy.special import expit as sigmoid, softmax
from scipy.stats import norm
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.calibration import CalibratedClassifierCV
from sklearn.isotonic import IsotonicRegression
from sklearn.metrics import log_loss, roc_auc_score

rng = np.random.default_rng(0)

# 1. Gaussian classes N(+1, 1) and N(-1, 1): the log-odds are exactly 2x; a prior of 0.2 adds log(0.25)
x0 = 0.5
log_odds = norm.logpdf(x0, 1) - norm.logpdf(x0, -1)
print(log_odds, round(sigmoid(log_odds), 4), round(sigmoid(log_odds + np.log(0.2 / 0.8)), 4))   # 1.0 0.7311 0.4046

# 2. maximum likelihood by Newton's method (IRLS) on two standardized breast cancer features
data = load_breast_cancer()
X = StandardScaler().fit_transform(data.data[:, :2])        # mean radius, mean texture
y = 1.0 - data.target                                       # 1 = malignant
Xb = np.c_[np.ones(len(X)), X]
w = np.zeros(3)
for _ in range(25):
    p = sigmoid(Xb @ w)
    grad = Xb.T @ (p - y)                                   # the gradient X^T (sigma - y)
    H = Xb.T @ (Xb * (p * (1 - p))[:, None])                # the Hessian X^T S X
    w -= np.linalg.solve(H, grad)
sk = LogisticRegression(C=np.inf, solver='newton-cholesky').fit(X, y)
print(w.round(4), np.r_[sk.intercept_, sk.coef_[0]].round(4))   # [-0.7076  3.722   0.9374] twice
print(f'{np.abs(grad).max():.1e}', round(p.sum(), 4), y.sum())  # 1.2e-15 212.0 212.0: mean prediction = share of positives

# 3. all 30 features: the training set is separable, and the unpenalized loss runs to zero
X30 = StandardScaler().fit_transform(data.data)
for C in (1.0, 1e2, 1e4, 1e6):
    m = LogisticRegression(C=C, max_iter=100_000).fit(X30, y)
    p = m.predict_proba(X30)[:, 1]
    print(C, round(np.linalg.norm(m.coef_), 1), f'{log_loss(y, p):.2g}', int((m.predict(X30) != y).sum()))
# C = 1: |w| 3.8, loss 0.053, 7 errors | 1e2: 21.4, 0.03, 4 | 1e4: 113.8, 0.022, 4 | 1e6: 1891.1, 0.0042, 0

# 4. gradient descent on four separable points: the weight grows like log(t/4), the loss falls like 2/t
Xs, ys = np.array([[1., -2], [1, -1], [1, 1], [1, 2]]), np.array([0, 0, 1, 1.])
w = np.zeros(2)
for t in range(1, 100_001):
    w -= 0.5 * Xs.T @ (sigmoid(Xs @ w) - ys) / 4
    if t in (1_000, 10_000, 100_000):
        print(t, round(w[1], 3), f'{np.mean(np.logaddexp(0, -(2 * ys - 1) * (Xs @ w))):.2e}')
# 1000 5.548 1.95e-03 | 10000 7.828 1.99e-04 | 100000 10.127 2.00e-05

# 5. calibration: Gaussian classes with correlated features; naive Bayes double-counts the correlated evidence
d, rho = 8, 0.7
L = np.linalg.cholesky(rho * np.ones((d, d)) + (1 - rho) * np.eye(d))
yc = rng.integers(0, 2, 20_000)
Xc = rng.normal(size=(20_000, d)) @ L.T + np.where(yc[:, None] == 1, 0.3, -0.3)
tr, te = slice(0, 10_000), slice(10_000, None)
def ece(p, y, bins=10):
    k = np.minimum((p * bins).astype(int), bins - 1)
    return sum((k == b).mean() * abs(p[k == b].mean() - y[k == b].mean()) for b in range(bins) if (k == b).any())
models = {'naive Bayes': GaussianNB(), '+ Platt': CalibratedClassifierCV(GaussianNB(), method='sigmoid', cv=5),
          '+ isotonic': CalibratedClassifierCV(GaussianNB(), method='isotonic', cv=5), 'logistic regression': LogisticRegression()}
for name, m in models.items():
    p = m.fit(Xc[tr], yc[tr]).predict_proba(Xc[te])[:, 1]
    print(name, round(ece(p, yc[te]), 3), round(log_loss(yc[te], p), 3), round(roc_auc_score(yc[te], p), 3))
# ECE, log-loss, AUC: naive Bayes 0.236 1.133 0.695 | + Platt 0.008 0.64 0.695 | + isotonic 0.01 0.634 0.695
#                     logistic regression 0.012 0.633 0.695

# 6. isotonic regression (pool adjacent violators) on six labels sorted by score
print(IsotonicRegression().fit_transform(np.arange(6), [0, 1, 0, 0, 1, 1]).round(3))   # [0. 0.333 0.333 0.333 1. 1.]

# 7. two-class softmax = sigmoid of the score difference
z = np.array([1.3, -0.4])
print(softmax(z)[0].round(4), sigmoid(z[0] - z[1]).round(4))    # 0.8455 0.8455

# 8. ECE of perfectly calibrated predictions (y ~ Bernoulli(p)) on 10,000 examples, 200 repetitions, 10 and 50 bins
p0 = rng.random((200, 10_000)); y0 = (rng.random((200, 10_000)) < p0).astype(float)
print([round(float(np.mean([ece(p, y, b) for p, y in zip(p0, y0)])), 3) for b in (10, 50)])   # [0.01, 0.022]

Next#

Logistic regression returns a probability, and a probability is not yet a decision. Somewhere a threshold turns it into “flag” or “pass”, and the right threshold depends on how rare the positives are and on what each kind of error costs. Judging the model needs a number too, and accuracy, the obvious one, says almost nothing when positives are rare: a model that never flags anything scores 99% when 1% are positive. The next note builds the vocabulary for these questions: the confusion matrix, precision and recall, the ROC and precision–recall curves, and thresholds chosen from costs.

References#