2.2.9 · D4Linear & Logistic Regression

Exercises — Logistic regression and the sigmoid function

2,745 words12 min readBack to topic

Two abbreviations used repeatedly below, spelled out once here:

  • CE = cross-entropy (the log loss ), see Cross-Entropy Loss.
  • MSE = mean squared error (the average of ), the loss from ordinary Linear Regression.

Throughout, we reuse three facts from the parent:

  • The score is a plain number on .
  • The sigmoid squashes that number into and we read it as .
  • The derivative , and the loss gradient is .

A tiny numeric table you may reuse (all to 3 decimals):


Level 1 — Recognition

Recall Solution L1.1

The sigmoid range is the open interval : it can get arbitrarily close to or but never reaches or exceeds either. Since , it cannot be a sigmoid output. The mistake is almost certainly forgetting the squash and reporting the raw score instead of .

Recall Solution L1.2

The pivot is .

  • : negative score () predict class 0.
  • : exactly the boundary, a tie.
  • : positive score () predict class 1. Rule: sign of decides the class, magnitude decides confidence.

Level 2 — Application

Recall Solution L2.1

Step 1 (score): . Why first? The linear part is computed before squashing. Step 2 (squash): . Step 3 (classify): is exactly on the boundary — a tie. By the convention , we predict class 1 (but with zero confidence). This is precisely the decision boundary .

Recall Solution L2.2

The boundary is : , i.e. . Why ? Because exactly at , and is the classification threshold.

  • Cross -axis (): .
  • Cross -axis (): . The line through and is the linear boundary.

Figure — the boundary line and the two half-planes. The figure below plots in teal. Everything on the upper-right side (shaded burnt orange) has , so and the model says class 1; the lower-left side (shaded plum) has , class 0. Notice the marked intercepts and — the two points you solved for — and confirm the line separates the plane into exactly these two straight-edged regions (that straightness is why logistic regression is called "linear").

Figure — Logistic regression and the sigmoid function
Recall Solution L2.3

Step 1: . Step 2 (error): (we over-predicted a negative example). Step 3 (gradients, single point so ): Step 4 (update, subtract gradient): Sanity: both went negative, which lowers for this positive- point, pushing toward . Correct direction.


Level 3 — Analysis

Recall Solution L3.1

Start from the definition . Now multiply numerator and denominator by : And The two expressions are identical, so . ∎ Interpretation: flipping the sign of the score swaps the two class probabilities, since . The model treats the classes symmetrically about .

Recall Solution L3.2

With : error . As , , so — the largest possible error. Cross-entropy keeps a strong, non-vanishing gradient exactly when the model is confidently wrong. Good. Squared-error loss instead has gradient , and in the tails. So a confidently-wrong point produces a near-zero gradient — learning stalls. This is the analytic reason we use log loss (see Cross-Entropy Loss).

Figure — the two loss curves for a positive example. The plot below shows, for , the CE loss in burnt orange and the MSE loss in teal, both as functions of the score . Read the far-left region ( very negative = confidently wrong): the orange CE curve keeps climbing with a steep slope (strong gradient, fast learning), while the teal MSE curve flattens out toward a constant (its slope dies, so gradient descent barely moves). This flattening is the visual meaning of "MSE saturates in the tails".

Figure — Logistic regression and the sigmoid function
Recall Solution L3.3

Odds of class 1 . Probability . Since odds , class 1 is about half as likely as class 0 — equivalently class 0 is about twice as likely as class 1 (odds of class 0 ). Log-odds negative leans class 0, consistent with .


Level 4 — Synthesis

Recall Solution L4.1

Convert each probability to its logit (undo the sigmoid):

  • , at : .
  • , at : . Subtract the equations: . Then . Model: . Check: boundary at (where ) — matches the first point. ∎
Recall Solution L4.2

Step 0: . Update 1: , . Recompute: . Moved from up toward . ✓ Step 1 error: . Update 2: , . Recompute: . Each step raises (0.5 → 0.731 → 0.823) toward the target , and the error shrinks (). This is Gradient Descent working on convex log loss.

Recall Solution L4.3

Rewrite the desired line as . We need to equal this (up to a positive scale) and be positive above the line. Take , giving .

  • On the line, e.g. : . ✓
  • A point above, e.g. : class 1. ✓ Any positive multiple works too — scaling leaves the boundary fixed but sharpens confidence. Compare to Decision Boundaries.

Level 5 — Mastery

Recall Solution L5.1

Chain rule: Piece 1 — : Piece 2 — : the sigmoid identity . Piece 3 — . Multiply: The cancels exactly — that cancellation is why logistic regression has the same clean update as Linear Regression. ∎

Recall Solution L5.2

.

  • : .
  • : .
  • : . Values fall as increases: the loss slopes steadily downhill toward the correct classification, with no bump — a picture of convexity. (As , ; the minimum is approached, never a spurious dip.)
Recall Solution L5.3

Solve : take the logit, So any already gives confidence. Push to and — a tiny gain for more than double the score. Interpretation: the sigmoid saturates; beyond the tails, extra score buys negligible probability. That flatness is exactly why in the tails (L3.2), and why we care about the log-odds scale rather than raw probability for very confident points.


Flashcards

What is the sigmoid output range, and why can't it be ?
open interval; is impossible.
Decision boundary equation for ?
(not ).
Convert probability back to a score?
Logit .
Where is maximal?
At , value (most uncertain, not most wrong).
Score needed for ?
.
Why cross-entropy (CE) over mean squared error (MSE) in the tails?
CE keeps a large gradient when the model is confidently wrong; MSE's gradient carries a factor that vanishes in the tails, so learning stalls.

Recall Feynman recap

Every exercise here was the same three moves in disguise: score → squash → compare. Compute , run it through the S-slide to get a confidence , then measure how wrong you were with and nudge the dials. Going backward (probability to score) is the log-odds. Everything else is bookkeeping.

Connections