Unit 5, lesson 27

Logistic Regression

How logistic regression turns a linear score into a probability with the sigmoid function, how odds, log-odds and odds ratios read its coefficients, and where a threshold puts the decision boundary.

  • Video 27
  • Logistic Regression and Likelihood

A courier wants the probability that a parcel will arrive late, given the distance it has to travel. A least-squares line fitted to late and on-time labels coded 11 and 00 can predict numbers below 00 or above 11, and no probability can take those values. Logistic regression keeps a linear score in the features and passes it through the sigmoid function, so every output lies strictly between 00 and 11. This lesson converts between scores, odds and probabilities, reads each coefficient as a multiplier of the odds, and keeps the probability apart from the decision that a threshold makes.

Key terms

Binary classification

Definition Binary classification is a supervised learning task in which the target YY takes exactly two values, coded y=1y = 1 for the positive class and y=0y = 0 for the negative class, and the input is a feature vector x\mathbf{x} in Rp\mathbb{R}^p known at prediction time. A classifier either outputs a predicted label y^\hat{y} in {0,1}\{0, 1\} directly or estimates the conditional probability P(Y=1∣x)P(Y = 1 \mid \mathbf{x}), and a decision rule then turns that estimate into a label. Which class is called positive is a coding convention, usually the event of interest. Swapping the codes replaces every probability pp of the positive class by 1−p1 - p.

In plain words Each case has a yes or no answer. A probabilistic classifier says how likely yes is for cases with these features, and a separate rule decides what to do with that number. Positive means coded 11, not good or desirable.

Example In the delivery data a late parcel is coded 11 and an on-time parcel 00, and the only feature is the distance xx in kilometers. Eight constructed training parcels at distances 1,2,…,81, 2, \dots, 8 have labels 0,0,1,0,1,0,1,10, 0, 1, 0, 1, 0, 1, 1. The least-squares line through these labels is

y^=−128+542 x,\hat{y} = -\tfrac{1}{28} + \tfrac{5}{42}\,x ,

which gives about −0.036-0.036 at x=0x = 0 and about 1.1551.155 at x=10x = 10. Neither number can be a probability, so a probability model needs a different form.

Sigmoid function

Definition The sigmoid function, also called the logistic function, is

σ(z)=11+e−z=ez1+ez\begin{aligned} \sigma(z) &= \frac{1}{1 + e^{-z}} \\[0.4em] &= \frac{e^{z}}{1 + e^{z}} \end{aligned}

for every real number zz. Its values satisfy 0<σ(z)<10 < \sigma(z) < 1. It is strictly increasing, with σ(0)=1/2\sigma(0) = 1/2, σ(z)→1\sigma(z) \to 1 as z→∞z \to \infty and σ(z)→0\sigma(z) \to 0 as z→−∞z \to -\infty. It has the symmetry σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z), and its derivative is σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)\bigl(1 - \sigma(z)\bigr), which is largest at z=0z = 0, where it equals 1/41/4.

In plain words The sigmoid squeezes the whole number line into the interval from 00 to 11 along an S-shaped curve. A score of 00 means even chances, large positive scores give probabilities near 11 and large negative scores give probabilities near 00, but no finite score gives exactly 00 or 11. The curve bends, so equal steps in the score do not give equal steps in probability.

Example Take the declared score z=−2+0.5xz = -2 + 0.5x, with coefficients chosen to show the conversion rather than fitted to data.

Distance xx in km00224466
Score zz−2-2−1-10011
Probability σ(z)\sigma(z)0.11920.11920.26890.26890.50.50.73110.7311

Each column adds 11 to the score, yet the probability rises by about 0.14970.1497, then 0.23110.2311, then 0.23110.2311. The last two steps are equal because of the symmetry around z=0z = 0.

Odds and log-odds

Definition For a probability pp with 0≤p<10 \le p < 1, the odds of the event are p/(1−p)p/(1 - p), a number in [0,∞)[0, \infty). For 0<p<10 < p < 1 the log-odds, also called the logit, are

logit⁡(p)=ln⁡p1−p,\operatorname{logit}(p) = \ln \frac{p}{1 - p} ,

which can be any real number. Solving for pp gives p=odds/(1+odds)p = \text{odds}/(1 + \text{odds}). If the log-odds equal zz, then p/(1−p)=ezp/(1 - p) = e^{z}, so p=ez−p ezp = e^{z} - p\,e^{z} and p=ez/(1+ez)=σ(z)p = e^{z}/(1 + e^{z}) = \sigma(z). The logit and the sigmoid therefore undo each other, with logit⁡(σ(z))=z\operatorname{logit}(\sigma(z)) = z for every real zz and σ(logit⁡(p))=p\sigma(\operatorname{logit}(p)) = p for 0<p<10 < p < 1.

In plain words Odds compare the chance of the event with the chance of its absence. Odds of 33 mean three late parcels for every on-time parcel among cases like this one, in the long run and if the model is right. Probability, odds and log-odds carry the same information on three different scales, and logistic regression is linear on the last of them.

Example A probability of 1/41/4 has odds (1/4)/(3/4)=1/3(1/4)/(3/4) = 1/3 and log-odds ln⁡(1/3)≈−1.0986\ln(1/3) \approx -1.0986. Odds of 33 correspond to probability 3/43/4 and log-odds ln⁡3≈1.0986\ln 3 \approx 1.0986. Doubling the odds from 1/41/4 to 1/21/2 moves the probability from 1/51/5 to 1/31/3, not from 1/51/5 to 2/52/5. A multiple of the odds is not the same multiple of the probability.

Logistic regression

Definition Logistic regression is a model for a binary target. For a feature vector x\mathbf{x} in Rp\mathbb{R}^p, an intercept bb and a weight vector w\mathbf{w} in Rp\mathbb{R}^p, it states

P(Y=1∣x)=σ(z),z=b+w⊤x,\begin{aligned} P(Y = 1 \mid \mathbf{x}) &= \sigma(z) , \\[0.4em] z &= b + \mathbf{w}^{\top}\mathbf{x} , \end{aligned}

so the log-odds of the positive class are the linear score zz. Linearity of the log-odds in the chosen features is an assumption of the model, and the features may include engineered ones such as squares or products. The log loss of one example with label yy and predicted probability pp of class 11 is

L(y,p)=−yln⁡p−(1−y)ln⁡(1−p),\begin{aligned} L(y, p) &= -y \ln p \\[0.4em] &- (1 - y)\ln(1 - p) , \end{aligned}

which equals −ln⁡p-\ln p when y=1y = 1 and −ln⁡(1−p)-\ln(1 - p) when y=0y = 0. Given training examples (xi,yi)(\mathbf{x}_i, y_i) for i=1,…,ni = 1, \dots, n, the parameters are usually estimated by minimizing the mean log loss, the empirical risk

R^(b,w)=1n∑i=1nL(yi,pi),\hat{R}(b, \mathbf{w}) = \frac{1}{n}\sum_{i=1}^{n} L(y_i, p_i) ,

where pi=σ(b+w⊤xi)p_i = \sigma(b + \mathbf{w}^{\top}\mathbf{x}_i). The next lesson derives this loss from the likelihood. The function R^\hat{R} is convex in (b,w)(b, \mathbf{w}), and its gradient with respect to (b,w)(b, \mathbf{w}) is

1n∑i=1n(pi−yi)[1xi].\frac{1}{n}\sum_{i=1}^{n} (p_i - y_i) \begin{bmatrix} 1 \\ \mathbf{x}_i \end{bmatrix} .

The empirical risk has exactly one minimizer when no nonzero (b,w)(b, \mathbf{w}) gives b+w⊤xi≥0b + \mathbf{w}^{\top}\mathbf{x}_i \ge 0 for every example with yi=1y_i = 1 and b+w⊤xi≤0b + \mathbf{w}^{\top}\mathbf{x}_i \le 0 for every example with yi=0y_i = 0. If a hyperplane separates the two training classes in this way, with at least one score not zero, the loss keeps decreasing as those separating coefficients are multiplied by larger and larger numbers, and it has no minimizer.

In plain words The model is a line or flat plane on the log-odds scale and an S-shaped curve on the probability scale. In the gradient each example contributes its error pi−yip_i - y_i, weighted by its features, so a gradient descent step tends to raise the probabilities of late parcels and lower those of on-time parcels. The outputs are model probabilities. Whether parcels given probability 0.30.3 are late about 3030 percent of the time is a separate property, calibration, and it is checked on held-out data rather than assumed.

Example Fit the model to the eight training parcels. Gradient descent on the standardized distance reaches a point where the gradient is zero to machine precision, and converting back to kilometers gives the estimates b^≈−2.6734\hat{b} \approx -2.6734 and w^≈0.5941\hat{w} \approx 0.5941 per kilometer. A finite minimizer exists because the classes overlap. A late parcel at 33 km lies below an on-time parcel at 44 km and a late parcel at 55 km lies above it, so no cut point separates the classes in either direction. The mean training log loss is about 0.52810.5281, below the value ln⁡2≈0.6931\ln 2 \approx 0.6931 of predicting 0.50.5 for every parcel, and the fitted probabilities rise from about 0.1110.111 at 11 km to about 0.8890.889 at 88 km. These are training results on constructed data and say nothing yet about performance on new parcels.

Odds ratio

Definition The odds ratio comparing two feature vectors is the odds at the first divided by the odds at the second. In logistic regression the odds at x\mathbf{x} are eb+w⊤xe^{b + \mathbf{w}^{\top}\mathbf{x}}. If feature xjx_j enters the model only through the term wjxjw_j x_j, then raising xjx_j by cc units while every other feature stays fixed multiplies the modeled odds by

odds(x+c ej)odds(x)=ec wj\frac{\text{odds}(\mathbf{x} + c\,\mathbf{e}_j)}{\text{odds}(\mathbf{x})} = e^{c\,w_j}

at every starting point x\mathbf{x}, where ej\mathbf{e}_j has 11 in position jj and 00 elsewhere. The odds ratio depends on the unit of xjx_j, since measuring the same quantity in a unit kk times smaller divides its coefficient by kk. If xjx_j also appears in a squared or product term, the odds ratio for a change in xjx_j depends on the starting values.

In plain words A coefficient is a fixed amount added to the log-odds, and its exponential is a fixed multiplier of the odds. It is not a fixed change in probability and not a fixed ratio of probabilities. With one feature, the probability changes at the rate w p(1−p)w\,p(1 - p) per unit at a point where it equals pp, which is fastest near p=1/2p = 1/2 and slow near 00 or 11. An odds ratio from a prediction model describes an association given the other modeled features. It can be read as the effect of changing the feature only when the way the data were collected supports that causal reading.

Example In the declared model z=−2+0.5xz = -2 + 0.5x, each extra kilometer multiplies the odds by e0.5≈1.6487e^{0.5} \approx 1.6487, while the probability moves by different amounts.

StepProbabilityChangeRatio of probabilities
00 to 11 km0.11920.1192 to 0.18240.18240.06320.06321.5301.530
44 to 55 km0.50.5 to 0.62250.62250.12250.12251.2451.245
88 to 99 km0.88080.8808 to 0.92410.92410.04330.04331.0491.049

The odds ratio is 1.64871.6487 in every row. Written per 100100 meters, the same model has coefficient 0.050.05 and odds ratio e0.05≈1.0513e^{0.05} \approx 1.0513 per 100100 meters, with exactly the same probabilities. The fitted model above has odds ratio e0.5941≈1.811e^{0.5941} \approx 1.811 per kilometer.

Decision boundary

Definition A decision rule with threshold tt, where 0<t<10 < t < 1, predicts class 11 when the estimated probability satisfies p^(x)≥t\hat{p}(\mathbf{x}) \ge t and class 00 otherwise. The sigmoid is strictly increasing, so for logistic regression p^(x)≥t\hat{p}(\mathbf{x}) \ge t holds exactly when b+w⊤x≥logit⁡(t)b + \mathbf{w}^{\top}\mathbf{x} \ge \operatorname{logit}(t). The decision boundary is the set of feature vectors with

b+w⊤x=logit⁡(t).b + \mathbf{w}^{\top}\mathbf{x} = \operatorname{logit}(t) .

When w≠0\mathbf{w} \ne \mathbf{0} it is a hyperplane in feature space, a point for one feature and a line for two. When w=0\mathbf{w} = \mathbf{0} every case gets the same probability and the same decision. For t=1/2t = 1/2 the boundary is the set where z=0z = 0. Points on the boundary are assigned to class 11 under this rule. If the features include engineered terms such as x12x_1^2, the boundary is a hyperplane in those features and can be curved in the original inputs.

In plain words The model gives probabilities, and the threshold turns them into decisions. Moving the threshold moves the boundary and leaves every probability where it was. The S-shape of the sigmoid relates the score to the probability, while the shape of the boundary comes from the features, so a logistic model that uses only the raw features has a straight or flat boundary. A threshold of 1/21/2 is a convention, and a good threshold depends on the costs of the two kinds of error.

Example For the declared model with t=1/2t = 1/2, the boundary solves −2+0.5x=0-2 + 0.5x = 0, so parcels at 44 km or more are predicted late. With t=3/4t = 3/4 the boundary solves −2+0.5x=ln⁡3-2 + 0.5x = \ln 3 and moves to x=2(2+ln⁡3)≈6.197x = 2(2 + \ln 3) \approx 6.197 km. A parcel at 55 km keeps its probability 0.62250.6225 under both thresholds, but its decision changes from late to on time. For the fitted model the boundary at t=1/2t = 1/2 is x=2.6734/0.5941≈4.5x = 2.6734/0.5941 \approx 4.5 km.

Check yourself

  1. A declared model for late delivery has score z=−2+0.5xz = -2 + 0.5x, where xx is the distance in kilometers. Find the score, the odds and the probability of lateness for a parcel that travels 55 km.
Show the answer

The score is −2+0.5⋅5=0.5-2 + 0.5 \cdot 5 = 0.5, and the odds are e0.5≈1.6487e^{0.5} \approx 1.6487. The probability is

σ(0.5)=11+e−0.5≈0.6225.\begin{aligned} \sigma(0.5) &= \frac{1}{1 + e^{-0.5}} \\[0.4em] &\approx 0.6225 . \end{aligned}

As a check, 1.6487/2.6487≈0.62251.6487/2.6487 \approx 0.6225 as well. Under the model, parcels like this one are late about 165165 times for every 100100 on-time arrivals in the long run.

  1. For the same model, a colleague says that one more kilometer multiplies the probability of lateness by e0.5e^{0.5}, so a parcel that travels 66 km has probability 0.6225×1.64870.6225 \times 1.6487. What goes wrong, and what are the correct probability and odds ratio?
Show the answer

The product is about 1.0261.026, larger than 11, which no probability can be. The factor e0.5e^{0.5} multiplies the odds, not the probability. The odds rise from e0.5≈1.6487e^{0.5} \approx 1.6487 to e1≈2.7183e^{1} \approx 2.7183, so the probability is

2.71831+2.7183≈0.7311.\frac{2.7183}{1 + 2.7183} \approx 0.7311 .

The probability rises by about 0.10860.1086, a ratio of about 1.1741.174, while the odds ratio is 1.64871.6487 for every one-kilometer step. The odds ratio also describes the model’s association with distance, not by itself what would happen if a route were made longer.

  1. A model with two features has score z=−4+x1+2x2z = -4 + x_1 + 2x_2 and predicts class 11 when p^≥t\hat{p} \ge t. Describe the decision boundary for t=1/2t = 1/2 and for t=0.8t = 0.8, and classify the case (x1,x2)=(2,1)(x_1, x_2) = (2, 1) under each threshold. Did raising the threshold change its probability?
Show the answer

For t=1/2t = 1/2 the boundary is the line where the score is logit⁡(1/2)=0\operatorname{logit}(1/2) = 0, that is x1+2x2=4x_1 + 2x_2 = 4. For t=0.8t = 0.8 the score must equal logit⁡(0.8)=ln⁡4≈1.3863\operatorname{logit}(0.8) = \ln 4 \approx 1.3863, which gives the parallel line x1+2x2=4+ln⁡4≈5.386x_1 + 2x_2 = 4 + \ln 4 \approx 5.386. The case (2,1)(2, 1) has score −4+2+2=0-4 + 2 + 2 = 0 and probability exactly 0.50.5. It lies on the first boundary, so the rule p^≥1/2\hat{p} \ge 1/2 assigns it to class 11, and under t=0.8t = 0.8 it goes to class 00. Its probability is 0.50.5 in both cases, because the threshold changes only the decision.

How this lesson connects

It also uses exponential functions and natural logarithms.