Practice / Regression

Logistic regression: log-odds, decision boundaries and thresholds

Ten problems on what a logistic regression model says, rather than how it is trained: probabilities from a logit, weights read as odds ratios, the decision boundary as a line with its normal and a point's distance from it, a threshold other than one half, the scale of the weights, separable data where the weights grow without bound and a ridge penalty that stops them, the linear boundary of two Gaussian classes with unequal priors, XOR and the feature that fixes it, a circular boundary from squared features, and the boundaries between three softmax classes, with worked solutions and the mistakes of multiplying a probability by an odds ratio, leaving out the norm in a distance, putting the threshold on the logit, trusting a small gradient on separable data and placing the boundary halfway between the classes when the priors differ.

Before you start

Logistic regression turns a linear score into a probability. The score z=w⊤x+bz = w^\top x + b is the log-odds of the positive class, the sigmoid turns it into a probability, and the model predicts the positive class where the probability is high enough. The set of points where it is exactly on the fence is the decision boundary, and because zz is linear in xx that boundary is a line, a plane or a hyperplane, with ww as its normal. The regression-gradients page trains the model; these ten problems read it. They compute probabilities and odds ratios, draw the boundary and measure distances to it, move it with a threshold, separate the role of the direction of ww from its size, follow the weights on separable data, derive the boundary from two Gaussian classes, and then bend the boundary with new features and split the plane among three classes. The five mistakes at the end are the ones that give a believable number: a probability multiplied by an odds ratio, a distance without the norm of ww, a threshold applied to the logit, a small gradient taken as convergence, and a boundary placed halfway between the classes when one class is rarer.

  • The model. For features x∈Rdx \in \mathbb{R}^d, weights w∈Rdw \in \mathbb{R}^d and bias bb, the logit is z=w⊤x+bz = w^\top x + b and the model's probability of class 11 is p=σ(z)p = \sigma(z), with σ(t)=1/(1+e−t)\sigma(t) = 1/(1 + e^{-t}). Labels are y∈{0,1}y \in \{0, 1\}.
  • Log-odds. The odds of class 11 are p/(1−p)p/(1 - p), and ln⁡p1−p=z\ln\dfrac{p}{1 - p} = z: the logit is the log-odds. ln⁡\ln is the natural log.
  • Decision rule. With threshold τ\tau in (0,1)(0, 1), predict 11 when p≥τp \ge \tau. Since σ\sigma is increasing, this is z≥ln⁡τ1−τz \ge \ln\dfrac{\tau}{1 - \tau}; for τ=12\tau = \tfrac12 it is z≥0z \ge 0.
  • Decision boundary. The set {x:w⊤x+b=c}\{x : w^\top x + b = c\} for the cutoff cc on the logit is a hyperplane with normal vector ww, pointing toward the side where class 11 is predicted. The signed distance from a point xx to {w⊤x+b=0}\{w^\top x + b = 0\} is (w⊤x+b)/∥w∥(w^\top x + b)/\|w\|.
  • Loss. The training loss is the mean binary cross-entropy L=−1N∑n[ynln⁡pn+(1−yn)ln⁡(1−pn)]L = -\tfrac1N\sum_n\big[y_n\ln p_n + (1 - y_n)\ln(1 - p_n)\big], as on the regression-gradients page. For one example, −ln⁡σ(z)=ln⁡(1+e−z)-\ln\sigma(z) = \ln(1 + e^{-z}) and −ln⁡(1−σ(z))=ln⁡(1+ez)-\ln(1 - \sigma(z)) = \ln(1 + e^{z}).
  • Softmax. With kk classes, each class has its own wiw_i and bib_i, the logits are zi=wi⊤x+biz_i = w_i^\top x + b_i, and the probabilities are softmax⁡(z)i=ezi/∑jezj\operatorname{softmax}(z)_i = e^{z_i}/\sum_j e^{z_j}.

Builds on: Regression gradients: linear, logistic and softmax

Problems

  1. ·

    A model has w=(2,−1)w = (2, -1) and b=−1b = -1. Find the logit, the probability of class 11 and the prediction at τ=12\tau = \tfrac12 for x=(1,0.5)x = (1, 0.5), x=(0,1)x = (0, 1) and x=(0.5,0)x = (0.5, 0).

  2. ·

    For the model of Problem 1, show that the logit is the log-odds, and say what happens to the odds when x1x_1 increases by 11 and when x2x_2 increases by 11. Check with the odds at (1,0.5)(1, 0.5) and (2,0.5)(2, 0.5).

  3. ··

    For the model of Problem 1, write the decision boundary at τ=12\tau = \tfrac12 as a line in the (x1,x2)(x_1, x_2) plane, find where it crosses the axes, say which side is class 11, and find the signed distances of (1,0.5)(1, 0.5) and (0,1)(0, 1) from it.

  4. ··

    False positives are expensive, so the model of Problem 1 is used with threshold τ=0.8\tau = 0.8. Find the new boundary, how far it moves and in which direction, and the new prediction for (1,0.5)(1, 0.5).

  5. ··

    Multiply ww and bb of Problem 1 by c>0c > 0. Show that the decision boundary does not change, and find pp at (1,0.5)(1, 0.5) for c=3c = 3 and c=10c = 10. Over what distance across the boundary does pp go from σ(−1)\sigma(-1) to σ(1)\sigma(1)?

  6. ···

    Train a one-feature model without bias on two points: x=−1x = -1 with y=0y = 0 and x=1x = 1 with y=1y = 1. Show that the mean loss is L(w)=ln⁡(1+e−w)L(w) = \ln(1 + e^{-w}), that it has no minimizer, and what gradient descent does. Then add the penalty λ2w2\tfrac\lambda2 w^2 and find the minimizer for λ=0.1\lambda = 0.1.

  7. ···

    Class 00 has features x∼x \sim Normal(μ0,σ2)(\mu_0, \sigma^2) and class 11 has x∼x \sim Normal(μ1,σ2)(\mu_1, \sigma^2), with prior probabilities π0\pi_0 and π1\pi_1. Show that ln⁡P(y=1∣x)P(y=0∣x)\ln\dfrac{P(y = 1 \mid x)}{P(y = 0 \mid x)} is linear in xx, and find ww and bb. Find the boundary for μ0=0\mu_0 = 0, μ1=2\mu_1 = 2, σ=1\sigma = 1 with equal priors, and with π1=0.2\pi_1 = 0.2.

  8. ··

    The four points (0,0)(0, 0) and (1,1)(1, 1) of class 00 and (0,1)(0, 1) and (1,0)(1, 0) of class 11 are the XOR pattern. Show that no line separates them. Then add the feature x3=x1x2x_3 = x_1x_2 and check that z=x1+x2−2x3−0.5z = x_1 + x_2 - 2x_3 - 0.5 separates them.

  9. ··

    A model uses the features x12x_1^2 and x22x_2^2, with logit z=x12+x22−4z = x_1^2 + x_2^2 - 4. Describe the decision boundary at τ=12\tau = \tfrac12 and the class-11 region, and find pp at (0,0)(0, 0), (1,1)(1, 1) and (2,2)(2, 2).

  10. ···

    A three-class softmax model has w1=(1,0)w_1 = (1, 0), w2=(0,1)w_2 = (0, 1), w3=(0,0)w_3 = (0, 0) and all biases 00. Find the boundary between each pair of classes, describe the region of each class, and find the probabilities at x=(2,1)x = (2, 1).

Worked solutions

Problem 1

A model has w=(2,−1)w = (2, -1) and b=−1b = -1. Find the logit, the probability of class 11 and the prediction at τ=12\tau = \tfrac12 for x=(1,0.5)x = (1, 0.5), x=(0,1)x = (0, 1) and x=(0.5,0)x = (0.5, 0).

  1. x=(1,0.5)x = (1, 0.5): z=2−0.5−1=0.5z = 2 - 0.5 - 1 = 0.5, p=σ(0.5)≈0.6225p = \sigma(0.5) \approx 0.6225w⊤x=2⋅1+(−1)⋅0.5w^\top x = 2 \cdot 1 + (-1)\cdot 0.5.
  2. x=(0,1)x = (0, 1): z=0−1−1=−2z = 0 - 1 - 1 = -2, p=σ(−2)≈0.1192p = \sigma(-2) \approx 0.1192A negative logit gives p<12p < \tfrac12.
  3. x=(0.5,0)x = (0.5, 0): z=1−0−1=0z = 1 - 0 - 1 = 0, p=12p = \tfrac12A zero logit is even odds.
  4. (1,0.5)(1, 0.5): z=0.5z = 0.5, p≈0.6225p \approx 0.6225, class 11; (0,1)(0, 1): z=−2z = -2, p≈0.1192p \approx 0.1192, class 00; (0.5,0)(0.5, 0): z=0z = 0, p=0.5p = 0.5, on the boundaryThe sign of the logit decides the class at τ=12\tau = \tfrac12; its size says how sure the model is. With the rule p≥τp \ge \tau, a point on the boundary goes to class 11.

Problem 2

For the model of Problem 1, show that the logit is the log-odds, and say what happens to the odds when x1x_1 increases by 11 and when x2x_2 increases by 11. Check with the odds at (1,0.5)(1, 0.5) and (2,0.5)(2, 0.5).

  1. 1−σ(z)=e−z1+e−z1 - \sigma(z) = \dfrac{e^{-z}}{1 + e^{-z}}, so σ(z)1−σ(z)=ez\dfrac{\sigma(z)}{1 - \sigma(z)} = e^{z}Divide the two fractions; the denominators cancel.
  2. ln⁡p1−p=z\ln\dfrac{p}{1 - p} = zTake logs.
  3. Increasing x1x_1 by 11 adds w1=2w_1 = 2 to zz, so it multiplies the odds by e2≈7.389e^2 \approx 7.389Adding to a log multiplies the quantity.
  4. Increasing x2x_2 by 11 multiplies the odds by e−1≈0.368e^{-1} \approx 0.368w2=−1w_2 = -1.
  5. Odds at (1,0.5)(1, 0.5): e0.5≈1.649e^{0.5} \approx 1.649; at (2,0.5)(2, 0.5): e2.5≈12.18e^{2.5} \approx 12.18Their ratio is e2e^2.
  6. The logit is the log-odds; a unit step in x1x_1 multiplies the odds by e2≈7.39e^{2} \approx 7.39, and a unit step in x2x_2 by e−1≈0.37e^{-1} \approx 0.37ewje^{w_j} is the odds ratio of feature jj, the same at every starting point. The probability itself changes by different amounts at different points: from 0.62250.6225 to σ(2.5)≈0.9241\sigma(2.5) \approx 0.9241 here (Mistake 1).

Problem 3

For the model of Problem 1, write the decision boundary at τ=12\tau = \tfrac12 as a line in the (x1,x2)(x_1, x_2) plane, find where it crosses the axes, say which side is class 11, and find the signed distances of (1,0.5)(1, 0.5) and (0,1)(0, 1) from it.

  1. 2x1−x2−1=02x_1 - x_2 - 1 = 0, that is x2=2x1−1x_2 = 2x_1 - 1The points where z=0z = 0.
  2. It crosses the x1x_1 axis at (0.5,0)(0.5, 0) and the x2x_2 axis at (0,−1)(0, -1)Set x2=0x_2 = 0, then x1=0x_1 = 0.
  3. The normal w=(2,−1)w = (2, -1) points to the side with z>0z > 0, below and to the right of the lineMoving along ww raises zz by ∥w∥2=5\|w\|^2 = 5 per unit of ww.
  4. ∥w∥=4+1=5\|w\| = \sqrt{4 + 1} = \sqrt5The length of the normal.
  5. (1,0.5)(1, 0.5): 0.55≈0.2236\dfrac{0.5}{\sqrt5} \approx 0.2236; (0,1)(0, 1): −25≈−0.894\dfrac{-2}{\sqrt5} \approx -0.894The logit divided by ∥w∥\|w\|.
  6. Boundary x2=2x1−1x_2 = 2x_1 - 1, through (0.5,0)(0.5, 0) and (0,−1)(0, -1), with class 11 on the side w=(2,−1)w = (2, -1) points to; signed distances 0.22360.2236 and −0.894-0.894The logit is ∥w∥\|w\| times the signed distance, so a point's logit grows linearly as it moves away from the boundary. The point (0,1)(0, 1) has the larger logit in size, and it is also the farther of the two from the line.

Problem 4

False positives are expensive, so the model of Problem 1 is used with threshold τ=0.8\tau = 0.8. Find the new boundary, how far it moves and in which direction, and the new prediction for (1,0.5)(1, 0.5).

  1. p≥0.8p \ge 0.8 exactly when z≥ln⁡0.80.2=ln⁡4≈1.386z \ge \ln\dfrac{0.8}{0.2} = \ln 4 \approx 1.386σ\sigma is increasing, so invert it at 0.80.8.
  2. New boundary: 2x1−x2−1=ln⁡42x_1 - x_2 - 1 = \ln 4A line parallel to the old one, with the same normal.
  3. It is ln⁡4∥w∥=1.3865≈0.620\dfrac{\ln 4}{\|w\|} = \dfrac{1.386}{\sqrt5} \approx 0.620 from the old oneTwo parallel lines w⊤x+b=0w^\top x + b = 0 and =c= c are ∣c∣/∥w∥|c|/\|w\| apart.
  4. It moves along ww, into the old class-11 sideThe class-11 region shrinks.
  5. (1,0.5)(1, 0.5) has z=0.5<1.386z = 0.5 < 1.386Problem 1.
  6. Boundary 2x1−x2−1=ln⁡42x_1 - x_2 - 1 = \ln 4, moved 0.6200.620 along ww; (1,0.5)(1, 0.5) is now predicted class 00A threshold never tilts a logistic boundary, it only slides it along ww. Raising τ\tau trades recall for precision, and sweeping it from 00 to 11 traces the model's ROC curve.

Problem 5

Multiply ww and bb of Problem 1 by c>0c > 0. Show that the decision boundary does not change, and find pp at (1,0.5)(1, 0.5) for c=3c = 3 and c=10c = 10. Over what distance across the boundary does pp go from σ(−1)\sigma(-1) to σ(1)\sigma(1)?

  1. cw⊤x+cb=0cw^\top x + cb = 0 exactly when w⊤x+b=0w^\top x + b = 0Divide by c>0c > 0.
  2. c=3c = 3: z=1.5z = 1.5, p≈0.8176p \approx 0.8176; c=10c = 10: z=5z = 5, p≈0.9933p \approx 0.9933The logit scales with cc.
  3. z=±1z = \pm1 at signed distances ±1/(c∥w∥)\pm1/(c\|w\|)The logit is c∥w∥c\|w\| times the signed distance (Problem 3).
  4. The boundary is the same for every c>0c > 0; p≈0.8176p \approx 0.8176 for c=3c = 3 and 0.99330.9933 for c=10c = 10; pp goes from 0.2690.269 to 0.7310.731 over a band of width 2/(c∥w∥)2/(c\|w\|), which is 2/5≈0.8942/\sqrt5 \approx 0.894 for c=1c = 1The direction of ww and the ratio b/∥w∥b/\|w\| fix where the boundary is; the size of ww fixes how sharp it is. A larger ∥w∥\|w\| is a more confident model with the same predictions at τ=12\tau = \tfrac12.

Problem 6

Train a one-feature model without bias on two points: x=−1x = -1 with y=0y = 0 and x=1x = 1 with y=1y = 1. Show that the mean loss is L(w)=ln⁡(1+e−w)L(w) = \ln(1 + e^{-w}), that it has no minimizer, and what gradient descent does. Then add the penalty λ2w2\tfrac\lambda2 w^2 and find the minimizer for λ=0.1\lambda = 0.1.

  1. At x=1x = 1: −ln⁡σ(w)=ln⁡(1+e−w)-\ln\sigma(w) = \ln(1 + e^{-w})Class 11 with logit ww.
  2. At x=−1x = -1: −ln⁡(1−σ(−w))=−ln⁡σ(w)=ln⁡(1+e−w)-\ln(1 - \sigma(-w)) = -\ln\sigma(w) = \ln(1 + e^{-w})Class 00 with logit −w-w, and 1−σ(−w)=σ(w)1 - \sigma(-w) = \sigma(w).
  3. L(w)=ln⁡(1+e−w)L(w) = \ln(1 + e^{-w}) and L′(w)=−σ(−w)<0L'(w) = -\sigma(-w) < 0Both points give the same loss; differentiate with the chain rule.
  4. LL decreases for every ww and tends to 00 without reaching itNo finite ww is a minimizer, and gradient descent increases ww forever, more and more slowly.
  5. Penalized: L′(w)+λw=0L'(w) + \lambda w = 0 gives σ(−w)=λw\sigma(-w) = \lambda wThe gradient of λ2w2\tfrac\lambda2 w^2 is λw\lambda w.
  6. For λ=0.1\lambda = 0.1: w≈1.634w \approx 1.634Solve numerically; the left side falls and the right side rises, so there is one root.
  7. L(w)=ln⁡(1+e−w)L(w) = \ln(1 + e^{-w}) has no minimizer, and gradient descent sends ww to infinity; with λ=0.1\lambda = 0.1 the minimizer is w≈1.634w \approx 1.634On separable data the boundary is already right, and the loss rewards only confidence, so ∥w∥\|w\| grows without bound (Problem 5). A ridge penalty, early stopping or label smoothing keeps it finite; with λ=0.01\lambda = 0.01 the minimizer is w≈3.36w \approx 3.36.

Problem 7

Class 00 has features x∼x \sim Normal(μ0,σ2)(\mu_0, \sigma^2) and class 11 has x∼x \sim Normal(μ1,σ2)(\mu_1, \sigma^2), with prior probabilities π0\pi_0 and π1\pi_1. Show that ln⁡P(y=1∣x)P(y=0∣x)\ln\dfrac{P(y = 1 \mid x)}{P(y = 0 \mid x)} is linear in xx, and find ww and bb. Find the boundary for μ0=0\mu_0 = 0, μ1=2\mu_1 = 2, σ=1\sigma = 1 with equal priors, and with π1=0.2\pi_1 = 0.2.

  1. ln⁡P(y=1∣x)P(y=0∣x)=ln⁡π1π0+(x−μ0)2−(x−μ1)22σ2\ln\dfrac{P(y = 1 \mid x)}{P(y = 0 \mid x)} = \ln\dfrac{\pi_1}{\pi_0} + \dfrac{(x - \mu_0)^2 - (x - \mu_1)^2}{2\sigma^2}Bayes' rule; the normalizing constants of the two densities are equal and cancel.
  2. (x−μ0)2−(x−μ1)2=2x(μ1−μ0)+μ02−μ12(x - \mu_0)^2 - (x - \mu_1)^2 = 2x(\mu_1 - \mu_0) + \mu_0^2 - \mu_1^2The x2x^2 terms cancel because the variances are equal.
  3. w=μ1−μ0σ2w = \dfrac{\mu_1 - \mu_0}{\sigma^2} and b=μ02−μ122σ2+ln⁡π1π0b = \dfrac{\mu_0^2 - \mu_1^2}{2\sigma^2} + \ln\dfrac{\pi_1}{\pi_0}Collect the coefficient of xx and the constant.
  4. Equal priors: w=2w = 2, b=−2b = -2, boundary x=1x = 1Halfway between the means.
  5. π1=0.2\pi_1 = 0.2: b=−2+ln⁡0.20.8=−2−ln⁡4≈−3.386b = -2 + \ln\tfrac{0.2}{0.8} = -2 - \ln 4 \approx -3.386The log prior odds shift the bias.
  6. P(y=1∣x)=σ(wx+b)P(y = 1 \mid x) = \sigma(wx + b) with w=(μ1−μ0)/σ2w = (\mu_1 - \mu_0)/\sigma^2 and b=(μ02−μ12)/(2σ2)+ln⁡(π1/π0)b = (\mu_0^2 - \mu_1^2)/(2\sigma^2) + \ln(\pi_1/\pi_0); the boundary is x=1x = 1 for equal priors and x=1+ln⁡42≈1.693x = 1 + \tfrac{\ln 4}{2} \approx 1.693 for π1=0.2\pi_1 = 0.2Two Gaussian classes with a shared variance give exactly a logistic posterior, which is one reason the model works well beyond this case. A rarer class pushes the boundary toward its own mean (Mistake 5). With unequal variances the x2x^2 terms survive and the boundary is quadratic.

Problem 8

The four points (0,0)(0, 0) and (1,1)(1, 1) of class 00 and (0,1)(0, 1) and (1,0)(1, 0) of class 11 are the XOR pattern. Show that no line separates them. Then add the feature x3=x1x2x_3 = x_1x_2 and check that z=x1+x2−2x3−0.5z = x_1 + x_2 - 2x_3 - 0.5 separates them.

  1. Suppose z=w1x1+w2x2+bz = w_1x_1 + w_2x_2 + b separates them, with z>0z > 0 on class 11 and z<0z < 0 on class 00A separating line.
  2. Class 00: b<0b < 0 and w1+w2+b<0w_1 + w_2 + b < 0, which add to w1+w2+2b<0w_1 + w_2 + 2b < 0(0,0)(0, 0) and (1,1)(1, 1).
  3. Class 11: w2+b>0w_2 + b > 0 and w1+b>0w_1 + b > 0, which add to w1+w2+2b>0w_1 + w_2 + 2b > 0(0,1)(0, 1) and (1,0)(1, 0).
  4. Steps 2 and 3 contradict each other, so no line worksThe two diagonals of the square cross, and a line cannot put both ends of one diagonal on one side and both ends of the other on the other side.
  5. With x3=x1x2x_3 = x_1x_2: z(0,0)=−0.5z(0, 0) = -0.5 and z(1,1)=1+1−2−0.5=−0.5z(1, 1) = 1 + 1 - 2 - 0.5 = -0.5Class 00, both negative.
  6. z(0,1)=z(1,0)=0.5z(0, 1) = z(1, 0) = 0.5Class 11, both positive.
  7. No linear boundary in (x1,x2)(x_1, x_2) separates XOR; with the product feature, z=x1+x2−2x1x2−0.5z = x_1 + x_2 - 2x_1x_2 - 0.5 doesThe boundary is linear in (x1,x2,x3)(x_1, x_2, x_3) and curved, a hyperbola, in the original plane. A hidden layer learns features like x3x_3 instead of having them supplied.

Problem 9

A model uses the features x12x_1^2 and x22x_2^2, with logit z=x12+x22−4z = x_1^2 + x_2^2 - 4. Describe the decision boundary at τ=12\tau = \tfrac12 and the class-11 region, and find pp at (0,0)(0, 0), (1,1)(1, 1) and (2,2)(2, 2).

  1. z=0z = 0 on x12+x22=4x_1^2 + x_2^2 = 4A circle of radius 22 about the origin.
  2. Class 11 where x12+x22>4x_1^2 + x_2^2 > 4, outside the circlezz grows with the distance from the origin.
  3. p(0,0)=σ(−4)≈0.0180p(0, 0) = \sigma(-4) \approx 0.0180The center is the most confidently class 00.
  4. p(1,1)=σ(−2)≈0.1192p(1, 1) = \sigma(-2) \approx 0.1192 and p(2,2)=σ(4)≈0.9820p(2, 2) = \sigma(4) \approx 0.98201+1−4=−21 + 1 - 4 = -2 and 4+4−4=44 + 4 - 4 = 4.
  5. The boundary is the circle x12+x22=4x_1^2 + x_2^2 = 4, with class 11 outside it; p≈0.018p \approx 0.018, 0.1190.119 and 0.9820.982In the feature space (x12,x22)(x_1^2, x_2^2) the boundary is the straight line u+v=4u + v = 4 with normal (1,1)(1, 1). A model linear in its features can draw any boundary those features can express.

Problem 10

A three-class softmax model has w1=(1,0)w_1 = (1, 0), w2=(0,1)w_2 = (0, 1), w3=(0,0)w_3 = (0, 0) and all biases 00. Find the boundary between each pair of classes, describe the region of each class, and find the probabilities at x=(2,1)x = (2, 1).

  1. Classes ii and jj tie where zi=zjz_i = z_j, that is (wi−wj)⊤x+(bi−bj)=0(w_i - w_j)^\top x + (b_i - b_j) = 0Equal logits give equal probabilities; the softmax denominator is shared.
  2. 11 vs 22: x1=x2x_1 = x_2; 11 vs 33: x1=0x_1 = 0; 22 vs 33: x2=0x_2 = 0w1−w2=(1,−1)w_1 - w_2 = (1, -1), w1−w3=(1,0)w_1 - w_3 = (1, 0), w2−w3=(0,1)w_2 - w_3 = (0, 1).
  3. Class 11 where x1>x2x_1 > x_2 and x1>0x_1 > 0; class 22 where x2>x1x_2 > x_1 and x2>0x_2 > 0; class 33 where x1<0x_1 < 0 and x2<0x_2 < 0The predicted class has the largest logit, so it must beat both others.
  4. At (2,1)(2, 1) the logits are (2,1,0)(2, 1, 0)zi=wi⊤xz_i = w_i^\top x.
  5. softmax⁡(2,1,0)=(e2,e,1)e2+e+1\operatorname{softmax}(2, 1, 0) = \dfrac{(e^2, e, 1)}{e^2 + e + 1}The denominator is about 11.10711.107.
  6. Boundaries x1=x2x_1 = x_2, x1=0x_1 = 0 and x2=0x_2 = 0, meeting at the origin; class 33 owns the negative quadrant and classes 11 and 22 split the rest along x1=x2x_1 = x_2; at (2,1)(2, 1) the probabilities are (0.665,0.245,0.090)(0.665, 0.245, 0.090)Only differences of weights matter: adding the same vector to every wiw_i changes no boundary and no probability. With two classes the one boundary is (w1−w2)⊤x+(b1−b2)=0(w_1 - w_2)^\top x + (b_1 - b_2) = 0, which is logistic regression with w=w1−w2w = w_1 - w_2.

Where this goes wrong

1. Multiplying a probability by an odds ratio

Problem 2 says a unit step in x1x_1 multiplies something by e2e^2, and the probability is the number on the screen.

  1. At (1,0.5)(1, 0.5), p≈0.6225p \approx 0.6225, and w1=2w_1 = 2Right so far.
  2. “One more unit of x1x_1 multiplies the probability by e2e^2.”The slip that causes the mistake: ewje^{w_j} multiplies the odds p/(1−p)p/(1 - p), not pp.
  3. p(2,0.5)≈0.6225⋅7.389≈4.60p(2, 0.5) \approx 0.6225 \cdot 7.389 \approx 4.60A probability above 11 is the clue. The odds go from 1.6491.649 to 12.1812.18, and the probability from 0.62250.6225 to 0.92410.9241.

2. Leaving out the norm in a distance

The logit is zero on the boundary and grows away from it, so it looks like a distance.

  1. (1,0.5)(1, 0.5) has logit z=0.5z = 0.5Right so far: Problem 1.
  2. “So it lies 0.50.5 from the boundary.”The slip that causes the mistake: the logit is ∥w∥\|w\| times the distance, and ∥w∥=5\|w\| = \sqrt5 here.
  3. distance =0.5= 0.5The distance is 0.5/5≈0.22360.5/\sqrt5 \approx 0.2236. Doubling ww and bb would double this “distance” without moving the point or the line (Problem 5).

3. Putting the threshold on the logit

A threshold of 0.80.8 is a probability, and the boundary equation has a number on its right side.

  1. Predict class 11 when p≥0.8p \ge 0.8Right so far: Problem 4.
  2. “The boundary moves to 2x1−x2−1=0.82x_1 - x_2 - 1 = 0.8.”The slip that causes the mistake: the right side is a value of the logit, so it is ln⁡(0.8/0.2)=ln⁡4≈1.386\ln(0.8/0.2) = \ln 4 \approx 1.386.
  3. predict class 11 when z≥0.8z \ge 0.8At x=(1,0)x = (1, 0), z=1z = 1 and p=σ(1)≈0.731p = \sigma(1) \approx 0.731, below 0.80.8, yet this rule predicts class 11. The two scales differ even at τ=12\tau = \tfrac12, whose cutoff on the logit is 00, not 0.50.5.

4. Taking a small gradient as convergence

Training stops when the gradient is tiny, and on the separable data of Problem 6 it gets tiny quickly.

  1. L′(w)=−σ(−w)L'(w) = -\sigma(-w)Right so far: Problem 6, step 3.
  2. “At w=10w = 10 the gradient is −4.5×10−5-4.5 \times 10^{-5}, so ww has converged.”The habit that causes the mistake: reading a small gradient as a nearby minimum, when this loss has no minimum.
  3. w≈10w \approx 10 is the fitted weightAt w=20w = 20 the loss falls from 4.5×10−54.5 \times 10^{-5} to 2.1×10−92.1 \times 10^{-9}, and it keeps falling forever. The reported weight depends only on when training stopped, and so do the model's confidences.

5. Placing the boundary halfway between the classes

With equal variances the boundary of Problem 7 sits halfway between the means, and the priors are easy to forget.

  1. Means 00 and 22, variance 11, π1=0.2\pi_1 = 0.2Right so far.
  2. “The boundary is at the midpoint, x=1x = 1.”The term that is missing: the log prior odds ln⁡(π1/π0)=−ln⁡4\ln(\pi_1/\pi_0) = -\ln 4 in the bias.
  3. predict class 11 for x≥1x \ge 1At x=1x = 1 the posterior is P(y=1∣x)=σ(−ln⁡4)=0.2P(y = 1 \mid x) = \sigma(-\ln 4) = 0.2, not 12\tfrac12. The even-odds point is x≈1.693x \approx 1.693, pushed toward the rarer class's mean.

Print this set: logistic-regression-and-decision-boundaries.pdf (problems, answers, and worked solutions on separate pages).