Practice / Regression
Logistic regression: log-odds, decision boundaries and thresholds
Before you start
Logistic regression turns a linear score into a probability. The score 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 is linear in that boundary is a line, a plane or a hyperplane, with 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 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 , 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 , weights and bias , the logit is and the model's probability of class is , with . Labels are .
- Log-odds. The odds of class are , and : the logit is the log-odds. is the natural log.
- Decision rule. With threshold in , predict when . Since is increasing, this is ; for it is .
- Decision boundary. The set for the cutoff on the logit is a hyperplane with normal vector , pointing toward the side where class is predicted. The signed distance from a point to is .
- Loss. The training loss is the mean binary cross-entropy , as on the regression-gradients page. For one example, and .
- Softmax. With classes, each class has its own and , the logits are , and the probabilities are .
Builds on: Regression gradients: linear, logistic and softmax
Problems
- ·
A model has and . Find the logit, the probability of class and the prediction at for , and .
- ·
For the model of Problem 1, show that the logit is the log-odds, and say what happens to the odds when increases by and when increases by . Check with the odds at and .
- ··
For the model of Problem 1, write the decision boundary at as a line in the plane, find where it crosses the axes, say which side is class , and find the signed distances of and from it.
- ··
False positives are expensive, so the model of Problem 1 is used with threshold . Find the new boundary, how far it moves and in which direction, and the new prediction for .
- ··
Multiply and of Problem 1 by . Show that the decision boundary does not change, and find at for and . Over what distance across the boundary does go from to ?
- ···
Train a one-feature model without bias on two points: with and with . Show that the mean loss is , that it has no minimizer, and what gradient descent does. Then add the penalty and find the minimizer for .
- ···
Class has features Normal and class has Normal, with prior probabilities and . Show that is linear in , and find and . Find the boundary for , , with equal priors, and with .
- ··
The four points and of class and and of class are the XOR pattern. Show that no line separates them. Then add the feature and check that separates them.
- ··
A model uses the features and , with logit . Describe the decision boundary at and the class- region, and find at , and .
- ···
A three-class softmax model has , , and all biases . Find the boundary between each pair of classes, describe the region of each class, and find the probabilities at .
Answers
- : , , class ; : , , class ; : , , on the boundary
- The logit is the log-odds; a unit step in multiplies the odds by , and a unit step in by
- Boundary , through and , with class on the side points to; signed distances and
- Boundary , moved along ; is now predicted class
- The boundary is the same for every ; for and for ; goes from to over a band of width , which is for
- has no minimizer, and gradient descent sends to infinity; with the minimizer is
- with and ; the boundary is for equal priors and for
- No linear boundary in separates XOR; with the product feature, does
- The boundary is the circle , with class outside it; , and
- Boundaries , and , meeting at the origin; class owns the negative quadrant and classes and split the rest along ; at the probabilities are
Worked solutions
Problem 1
A model has and . Find the logit, the probability of class and the prediction at for , and .
- : , .
- : , A negative logit gives .
- : , A zero logit is even odds.
- : , , class ; : , , class ; : , , on the boundaryThe sign of the logit decides the class at ; its size says how sure the model is. With the rule , a point on the boundary goes to class .
Problem 2
For the model of Problem 1, show that the logit is the log-odds, and say what happens to the odds when increases by and when increases by . Check with the odds at and .
- , so Divide the two fractions; the denominators cancel.
- Take logs.
- Increasing by adds to , so it multiplies the odds by Adding to a log multiplies the quantity.
- Increasing by multiplies the odds by .
- Odds at : ; at : Their ratio is .
- The logit is the log-odds; a unit step in multiplies the odds by , and a unit step in by is the odds ratio of feature , the same at every starting point. The probability itself changes by different amounts at different points: from to here (Mistake 1).
Problem 3
For the model of Problem 1, write the decision boundary at as a line in the plane, find where it crosses the axes, say which side is class , and find the signed distances of and from it.
- , that is The points where .
- It crosses the axis at and the axis at Set , then .
- The normal points to the side with , below and to the right of the lineMoving along raises by per unit of .
- The length of the normal.
- : ; : The logit divided by .
- Boundary , through and , with class on the side points to; signed distances and The logit is times the signed distance, so a point's logit grows linearly as it moves away from the boundary. The point 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 . Find the new boundary, how far it moves and in which direction, and the new prediction for .
- exactly when is increasing, so invert it at .
- New boundary: A line parallel to the old one, with the same normal.
- It is from the old oneTwo parallel lines and are apart.
- It moves along , into the old class- sideThe class- region shrinks.
- has Problem 1.
- Boundary , moved along ; is now predicted class A threshold never tilts a logistic boundary, it only slides it along . Raising trades recall for precision, and sweeping it from to traces the model's ROC curve.
Problem 5
Multiply and of Problem 1 by . Show that the decision boundary does not change, and find at for and . Over what distance across the boundary does go from to ?
- exactly when Divide by .
- : , ; : , The logit scales with .
- at signed distances The logit is times the signed distance (Problem 3).
- The boundary is the same for every ; for and for ; goes from to over a band of width , which is for The direction of and the ratio fix where the boundary is; the size of fixes how sharp it is. A larger is a more confident model with the same predictions at .
Problem 6
Train a one-feature model without bias on two points: with and with . Show that the mean loss is , that it has no minimizer, and what gradient descent does. Then add the penalty and find the minimizer for .
- At : Class with logit .
- At : Class with logit , and .
- and Both points give the same loss; differentiate with the chain rule.
- decreases for every and tends to without reaching itNo finite is a minimizer, and gradient descent increases forever, more and more slowly.
- Penalized: gives The gradient of is .
- For : Solve numerically; the left side falls and the right side rises, so there is one root.
- has no minimizer, and gradient descent sends to infinity; with the minimizer is On separable data the boundary is already right, and the loss rewards only confidence, so grows without bound (Problem 5). A ridge penalty, early stopping or label smoothing keeps it finite; with the minimizer is .
Problem 7
Class has features Normal and class has Normal, with prior probabilities and . Show that is linear in , and find and . Find the boundary for , , with equal priors, and with .
- Bayes' rule; the normalizing constants of the two densities are equal and cancel.
- The terms cancel because the variances are equal.
- and Collect the coefficient of and the constant.
- Equal priors: , , boundary Halfway between the means.
- : The log prior odds shift the bias.
- with and ; the boundary is for equal priors and for Two 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 terms survive and the boundary is quadratic.
Problem 8
The four points and of class and and of class are the XOR pattern. Show that no line separates them. Then add the feature and check that separates them.
- Suppose separates them, with on class and on class A separating line.
- Class : and , which add to and .
- Class : and , which add to and .
- 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.
- With : and Class , both negative.
- Class , both positive.
- No linear boundary in separates XOR; with the product feature, doesThe boundary is linear in and curved, a hyperbola, in the original plane. A hidden layer learns features like instead of having them supplied.
Problem 9
A model uses the features and , with logit . Describe the decision boundary at and the class- region, and find at , and .
- on A circle of radius about the origin.
- Class where , outside the circle grows with the distance from the origin.
- The center is the most confidently class .
- and and .
- The boundary is the circle , with class outside it; , and In the feature space the boundary is the straight line with normal . A model linear in its features can draw any boundary those features can express.
Problem 10
A three-class softmax model has , , and all biases . Find the boundary between each pair of classes, describe the region of each class, and find the probabilities at .
- Classes and tie where , that is Equal logits give equal probabilities; the softmax denominator is shared.
- vs : ; vs : ; vs : , , .
- Class where and ; class where and ; class where and The predicted class has the largest logit, so it must beat both others.
- At the logits are .
- The denominator is about .
- Boundaries , and , meeting at the origin; class owns the negative quadrant and classes and split the rest along ; at the probabilities are Only differences of weights matter: adding the same vector to every changes no boundary and no probability. With two classes the one boundary is , which is logistic regression with .
Where this goes wrong
1. Multiplying a probability by an odds ratio
Problem 2 says a unit step in multiplies something by , and the probability is the number on the screen.
- At , , and Right so far.
- “One more unit of multiplies the probability by .”The slip that causes the mistake: multiplies the odds , not .
- A probability above is the clue. The odds go from to , and the probability from to .
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.
- has logit Right so far: Problem 1.
- “So it lies from the boundary.”The slip that causes the mistake: the logit is times the distance, and here.
- distance The distance is . Doubling and would double this “distance” without moving the point or the line (Problem 5).
3. Putting the threshold on the logit
A threshold of is a probability, and the boundary equation has a number on its right side.
- Predict class when Right so far: Problem 4.
- “The boundary moves to .”The slip that causes the mistake: the right side is a value of the logit, so it is .
- predict class when At , and , below , yet this rule predicts class . The two scales differ even at , whose cutoff on the logit is , not .
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.
- Right so far: Problem 6, step 3.
- “At the gradient is , so has converged.”The habit that causes the mistake: reading a small gradient as a nearby minimum, when this loss has no minimum.
- is the fitted weightAt the loss falls from to , 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.
- Means and , variance , Right so far.
- “The boundary is at the midpoint, .”The term that is missing: the log prior odds in the bias.
- predict class for At the posterior is , not . The even-odds point is , pushed toward the rarer class's mean.
Print this set: logistic-regression-and-decision-boundaries.pdf (problems, answers, and worked solutions on separate pages).
More at eulerpad.app. This is one of 82 free practice sets with worked solutions at eulerpad.app/practice, alongside guides to the topics behind them.
EulerPad, the app, reads your handwritten math on iPad, iPhone and Android and checks it line by line. It's free to start.
This set is also Chapter 8 of Deep Learning Math by Hand, a 250-problem workbook with worked solutions: eulerpad.app/workbooks