Part II · Chapter 11

Logistic regression

How to turn a straight line into a classifier that reports how sure it is — and why the sigmoid, not a threshold, is the only function that lets you train the thing.

Essentials

A linear score, squashed into a probability, trained by a loss built so the squashing cancels.

Everything below is the explanation and the practice. If you are short on time, read this box, then go to the problems.

1. Why a hard decision is not enough

Every classifier so far hands you a label — decision trees walk you to a leaf and give you one. The notes put it this way:

Key idea

Previously, we viewed classification as making an explicit decision on whether a given sample $x^{(i)}$ belongs to one of $K$ classes. But making a discrete decision on the class of $x^{(i)}$ doesn't tell us how strongly we believe $x^{(i)}$ belongs to that class.

Notation: $x^{(i)}$ is the $i$-th sample — a parenthesised superscript is an index, not an exponent. Both kinds appear below.

1.1 The concrete thing that goes wrong

A model screens chest X-rays and outputs "tumour" or "no tumour". Two patients come back "tumour": one from the middle of a cloud of obviously-cancerous scans, the other one pixel over the line, where half the training scans went each way. Same word for both, so you cannot:

1.2 Why linear, specifically

The notes' second observation: some classifiers, decision trees among them, are not linear functions of their input at all. A tree asking "is $x_1 > 3$?" then "is $x_2 > 7$?" carves feature space into axis-aligned boxes, one label each — a staircase, piecewise constant, with no coefficients $w_j$ to read.

Key idea

Logistic regression models are an intuitive extension of linear regression that let us perform classification while still building on a linear model.

Linear models give up that flexibility for three things: interpretable coefficients, easy optimisation (a convex objective), and simplicity (few parameters, so less to overfit). Logistic regression keeps all three and outputs probabilities.

2. Four stages from a line to a classifier

The notes build the model by failing three times on purpose: each failure names the property the next attempt must have.

Figure A — the four stages, aligned on the same data
Reconstruction note. The professor's PDF contains Figures 5–9 as images, and those images did not survive text extraction. Every plot on this page is therefore a reconstruction: real data, real least-squares and maximum-likelihood fits, computed in your browser when the page loads. The dataset is mine, chosen to match the description in the notes (one-dimensional $x$, labels in $\{0,1\}$, a region of overlap around $x \approx 1$), so the numbers here will not match the professor's slides digit-for-digit — but every claim the notes make about the shapes is reproduced.

2.1 Stage 1 — the dataset (Figure 5)

The setup from the notes: a dataset

$$ \D = \{(x^{(i)}, y^{(i)})\}_{i=1}^{N}, \qquad x^{(i)} \in \R, \qquad y^{(i)} \in \{0, 1\} $$

$N$ samples, each input one real number, each label $0$ or $1$. We want a linear classifier: one whose decision boundary is linear — in 1-D, a single point on the $x$-axis.

The dataset this page uses throughout — twelve points, six per class:

$y$$x$ values
0−1.4−0.8−0.30.20.71.2
11.01.31.62.12.73.3

Read the overlap off the table: the largest class-0 input is $x = 1.2$, the smallest class-1 input $x = 1.0$. They cross, so in the strip $1.0 \le x \le 1.2$ the classes are interleaved and no threshold classifies all twelve correctly. That is the point, not a defect: real data overlaps, and a model forced to be certain there is lying.

Figure 5 (reconstructed) — the dataset to train a classifier on
The shaded band is the overlap region, $1.0 \le x \le 1.2$. Any vertical line you draw — any linear decision boundary in 1-D — gets at least one point wrong. What we would like a model to say near $x = 1$ is not "class 0" or "class 1" but "about fifty-fifty".

2.2 Stage 2 — just use linear regression (Figure 6)

The notes' first idea: the labels are numbers, $0$ and $1$. Linear regression predicts numbers. So fit

$$ \hat{y} = wx + b $$

by least squares. The normal equations on the twelve points give

$$ \hat{y} = 0.2184 + 0.2913\,x $$
Figure 6 (reconstructed) — $\D$ trained on a linear regression model
The line crosses $\hat y = 0.5$ at $x = 0.967$ — right in the overlap, which is encouraging. But look at the ends: at $x = -1.4$ the model predicts $\hat y = -0.189$, and at $x = 3.3$ it predicts $\hat y = 1.180$. Both are outside $\{0,1\}$ and outside $[0,1]$.

The notes: the model "learns some relationship between the input $x$ and the class $y$, where the class will increase if the input increases" — the positive slope is right — but "this doesn't necessarily constrain the output of a model to the appropriate class that we would like, either a 0 or a 1." A linear function is unbounded: at $x = 10$ this fit predicts $1.433$, and "$\hat y = 1.433$" is not a class, nor a probability. It has no interpretation at all.

The failure mode the notes skip, and you should see

Squared loss punishes being far from the target number. A point at $x = 10$ with label $1$ is the easiest point in the world to classify, but least squares sees $\hat y = 0.2184 + 0.2913(10) = 3.13$, which is $2.13$ from the target, for a squared penalty of $4.5$ — more than all twelve real points combined. So the fit contorts to pull it down, and the only way is to flatten the slope.

Figure B — one far-away, correctly-labelled point moves the boundary
Watch the crossing point. Before: $\hat y = 0.2184 + 0.2913x$, boundary at $x = 0.967$, one point misclassified (the class-0 point at $x = 1.2$). After adding $(x{=}10,\ y{=}1)$: $\hat y = 0.3601 + 0.1073x$, boundary at $x = 1.303$, and now two points are wrong — the class-1 points at $x = 1.0$ and $x = 1.3$. The point at $x = 1.3$ was classified correctly before. We added a point the model already got right, and it made the model worse.
Intuition

Squared loss has unbounded appetite: there is no "right enough", so a few easy, extreme points can outvote all the hard, informative ones near the boundary. The loss built in §7 saturates instead — refitting the logistic model with and without the point at $x = 10$ gives $w = 5.5119$, $b = -5.8044$ both times, identical to four decimal places, because that point already has probability $0.99999$ and so contributes no gradient.

2.3 Stage 3 — threshold the line (Figure 7)

The outputs are invalid, so clamp them at the natural cut $0.5$: class $0$ below, class $1$ above. The notes write a new model,

$$ \hat{y} = g_{\text{threshold}}(wx + b), \qquad g_{\text{threshold}}(z) = \begin{cases} 0 & z < 0.5\\ 1 & z \ge 0.5\end{cases} $$

(Equivalently, absorb the $0.5$ into $b$ and threshold at $0$; nothing below depends on the convention.)

Figure 7 (reconstructed) — $\D$ trained on a thresholding model
Now every output is a legal class. But the model's belief goes from "definitely 0" to "definitely 1" across an infinitesimal sliver of the $x$-axis, and its slope is exactly zero everywhere else.

The notes name both problems; the second forces the design.

Problem (a) — the model is absurdly certain

In the notes' words: "The model is extremely certain about its predictions! It transitions rapidly from one class to the other right around input values $x = 1$. However, looking at the overlap in our data, we probably want our model to state that for $x = 1$, $P(Y = 1 \mid x) \approx 0.5$."

That is the overlap from §2.1, so the honest answer at $x = 1$ is a coin flip. The threshold model instead reports certainty $1$ for class 0 at $x = 0.9666$ and certainty $1$ for class 1 at $x = 0.9668$: it has thrown away all information about where the sample sits relative to the boundary — exactly what §1 wanted.

Problem (b) — there is nothing for gradient descent to descend

The notes: "it has a derivative of zero essentially everywhere, making it very problematic to optimize over." This is the hinge of the chapter, so make it precise.

Proof

Claim. Gradient descent cannot train $\hat y = g_{\text{threshold}}(wx+b)$.

Recall from chapter 9 the update rule:

$$ \theta \leftarrow \theta - \alpha \nabla_\theta J(\theta) $$

where $J(\theta)$ is the training objective, $\alpha > 0$ is the learning rate, and $\nabla_\theta J$ is the gradient — the vector of partial derivatives $\partial J / \partial \theta_j$. The rule has exactly one input: the gradient. If the gradient is the zero vector, the update does nothing, whatever $\alpha$ is.

Now compute that gradient. Whatever per-sample loss $\ell$ you use, the chain rule gives

$$ \frac{\partial \ell}{\partial w} \;=\; \underbrace{\frac{\partial \ell}{\partial \hat y}}_{\text{depends on the loss}} \cdot \underbrace{g_{\text{threshold}}'(z)}_{\text{the model}} \cdot \underbrace{\frac{\partial z}{\partial w}}_{=\,x}, \qquad z = wx+b. $$

The middle factor is the derivative of a step function. A step function is constant on $z < 0.5$ and constant on $z > 0.5$, so $g_{\text{threshold}}'(z) = 0$ at every $z \ne 0.5$. At $z = 0.5$ exactly, the function jumps, and the derivative does not exist (the left and right difference quotients blow up to $\pm\infty$).

So the middle factor is $0$ for almost every input, and undefined on a set of measure zero. Multiplying anything by that middle factor gives $0$. Therefore $\nabla_\theta J = \mathbf{0}$ almost everywhere, and

$$ \theta \leftarrow \theta - \alpha \cdot \mathbf{0} = \theta. $$

Gradient descent takes a step of length zero, forever. $\blacksquare$

The same fact as a landscape: changing $w$ a little either flips no labels or flips exactly one, so the training loss is a staircase of flat plateaus with sudden drops. Gradient descent on a staircase is a blindfolded person on a flat floor told to walk downhill.

Key idea

What kills the threshold is not "outputs must be probabilities" but "the loss must have a usable slope with respect to the parameters." Every gradient-trained model obeys this, which is why models are built out of smooth pieces. The sigmoid is chosen because it is differentiable and non-flat, not because it is pretty.

2.4 Stage 4 — replace the step with a smooth S (Figure 8)

The conclusion: keep the structure "apply a function $g$ to $wx+b$", but choose a $g$ that "smoothly maps all real input values $(-\infty, \infty)$ to the range $(0,1)$" — a probability. The standard choice is a sigmoid ("s-shaped") function, specifically the logistic function $\sigma$:

Definition $$ g_{\text{logistic}}(z) \;=\; \sigma(z) \;=\; \frac{1}{1 + e^{-z}} $$

$z \in \R$ is any real number (here $wx+b$), and $\sigma(z)$ is the output — which we now prove lies strictly between $0$ and $1$.

The notes leave it as an exercise: prove that it outputs values in $[0,1]$, and $0.5$ at $0$. So do it.

3. The logistic function, proved and differentiated

Proof

Claim 1: $0 < \sigma(z) < 1$ for every real $z$.

Upper bound. The exponential is strictly positive: $e^{-z} > 0$ for every real $z$ (it never reaches zero, it only approaches it). Therefore

$$ 1 + e^{-z} \;>\; 1 \quad\Longrightarrow\quad \sigma(z) = \frac{1}{1+e^{-z}} \;<\; \frac{1}{1} = 1. $$

The implication is legal because $t \mapsto 1/t$ is strictly decreasing on $t > 0$, and both $1+e^{-z}$ and $1$ are positive.

Lower bound. For any fixed real $z$, $e^{-z}$ is a finite positive number, so $1 + e^{-z}$ is finite and positive, so its reciprocal is positive:

$$ \sigma(z) = \frac{1}{1+e^{-z}} \;>\; 0. $$

Together: $\sigma(z) \in (0,1)$ for all $z \in \R$. $\blacksquare$

Claim 2: $\sigma(0) = 0.5$. Substitute $z = 0$. Then $e^{-0} = e^{0} = 1$, so

$$ \sigma(0) = \frac{1}{1+1} = \frac{1}{2}. \qquad \blacksquare $$

Claim 3: the asymptotes. As $z \to +\infty$, the exponent $-z \to -\infty$, so $e^{-z} \to 0$, so the denominator $\to 1$ and

$$ \lim_{z\to+\infty}\sigma(z) = \frac{1}{1+0} = 1. $$

As $z \to -\infty$, the exponent $-z \to +\infty$, so $e^{-z} \to +\infty$, the denominator grows without bound, and

$$ \lim_{z\to-\infty}\sigma(z) = 0. \qquad \blacksquare $$
Common mistake

The notes write "$[0,1]$" with closed brackets, the usual shorthand. Claim 1 shows the range is the open interval $(0,1)$: $\sigma$ approaches $0$ and $1$ but never reaches either. That matters twice — logistic regression can never be absolutely certain (a feature), and $\log\hat y$ is always finite, which is what stops the loss returning $\infty$ on real data.

3.1 The derivative — the identity that makes the model trainable

Stage 3 died of a zero derivative, so ask what $\sigma'$ is. Every gradient below rests on the answer.

Proof

Claim: $\sigma'(z) = \sigma(z)\,\bigl(1 - \sigma(z)\bigr)$.

Step 1 — rewrite as a power. $\sigma(z) = \bigl(1 + e^{-z}\bigr)^{-1}$.

Step 2 — chain rule. With $u(z) = 1 + e^{-z}$ we have $\sigma = u^{-1}$, so $\dfrac{d\sigma}{dz} = -u^{-2}\dfrac{du}{dz}$. This is legal because $u(z) > 0$ everywhere, so $u^{-1}$ is differentiable.

Step 3 — the inner derivative. $\dfrac{du}{dz} = \dfrac{d}{dz}\bigl(1 + e^{-z}\bigr) = 0 + e^{-z}\cdot(-1) = -e^{-z}$, using $\frac{d}{dz}e^{cz} = ce^{cz}$ with $c=-1$.

Step 4 — substitute.

$$ \sigma'(z) \;=\; -\bigl(1+e^{-z}\bigr)^{-2}\cdot\bigl(-e^{-z}\bigr) \;=\; \frac{e^{-z}}{\bigl(1+e^{-z}\bigr)^{2}}. $$

Step 5 — split the fraction. This is the only clever step. Write the single fraction as a product of two:

$$ \frac{e^{-z}}{\bigl(1+e^{-z}\bigr)^{2}} \;=\; \underbrace{\frac{1}{1+e^{-z}}}_{=\;\sigma(z)}\cdot\underbrace{\frac{e^{-z}}{1+e^{-z}}}_{=\;?} $$

Step 6 — identify the second factor. Add and subtract $1$ in the numerator:

$$ \frac{e^{-z}}{1+e^{-z}} \;=\; \frac{\bigl(1+e^{-z}\bigr) - 1}{1+e^{-z}} \;=\; \frac{1+e^{-z}}{1+e^{-z}} - \frac{1}{1+e^{-z}} \;=\; 1 - \sigma(z). $$

Conclusion. $\sigma'(z) = \sigma(z)\bigl(1-\sigma(z)\bigr)$. $\blacksquare$

Three consequences, each of which you will use:

  1. $\sigma' > 0$ everywhere, since $\sigma$ and $1-\sigma$ are both positive. So $\sigma$ is strictly increasing, which is what lets §5 invert $\sigma(z) \ge 0.5 \iff z \ge 0$.
  2. Largest at the boundary. $\sigma(1-\sigma)$ is a downward parabola in $\sigma$, maximised at $\sigma = 1/2$, i.e. $z = 0$, where $\sigma'(0) = 0.25$. The model is most sensitive exactly where it is most uncertain — the opposite of the threshold.
  3. It is expressed in the function's own output. Given $\hat y = \sigma(z)$, $\sigma'(z) = \hat y(1-\hat y)$ — one multiply, no recomputing $e^{-z}$, which is why backpropagation is cheap.

3.2 One more identity: $\sigma(-z) = 1 - \sigma(z)$

Proof $$ \sigma(-z) = \frac{1}{1+e^{-(-z)}} = \frac{1}{1+e^{z}}. $$

Multiply numerator and denominator by $e^{-z}$ (legal: $e^{-z} \ne 0$):

$$ \frac{1}{1+e^{z}} \cdot \frac{e^{-z}}{e^{-z}} = \frac{e^{-z}}{e^{-z} + e^{z}e^{-z}} = \frac{e^{-z}}{e^{-z} + 1} = 1 - \sigma(z), $$

where the last equality is Step 6 of the previous proof. $\blacksquare$

So the sigmoid is antisymmetric about $(0, 0.5)$, and the model therefore treats the classes symmetrically: $2$ units right of the boundary gives class 1 probability $0.8808$, and $2$ units left gives class 0 the same $0.8808$.

Figure 8 (reconstructed) — the logistic function and its derivative
Top: $\sigma(z) = 1/(1+e^{-z})$. The two dashed horizontal lines are the asymptotes $\sigma = 0$ and $\sigma = 1$ proved above; the curve approaches but never touches them. The dot marks $\sigma(0) = 0.5$. Bottom: $\sigma'(z) = \sigma(z)(1-\sigma(z))$, peaking at $0.25$ directly under the dot and decaying symmetrically. Compare this with the threshold model, whose derivative would be a flat zero line with a single infinite spike — the bottom panel is the entire reason we switched.
$z$−4−2−10124
$\sigma(z)$0.01800.11920.26890.50000.73110.88080.9820
$\sigma'(z)$0.01770.10500.19660.25000.19660.10500.0177

Worth memorising: $\sigma(0)=0.5$, $\sigma(2)\approx 0.88$, $\sigma(4)\approx 0.98$ — enough to sanity-check any logistic model in your head. The columns also confirm $\sigma(-z)=1-\sigma(z)$: $0.0180 + 0.9820 = 1$.

4. The logistic regression model (Figure 9)

Assemble it: apply $\sigma$ to the linear transformation of the input, where $g_{\text{threshold}}$ was before.

Definition

One-dimensional input ($x \in \R$):

$$ h(x) = \hat y = \sigma(wx + b) = \frac{1}{1 + e^{-(wx+b)}} $$

$M$-dimensional input ($x \in \R^M$, with the bias term folded in as a constant feature):

$$ h(x) = \hat y = \sigma(\theta^\top x) = \frac{1}{1 + e^{-\theta^\top x}} $$

Every symbol: $x \in \R^M$ is one sample's feature vector, $\theta \in \R^M$ is the parameter vector, $\theta^\top x = \sum_{j=1}^{M}\theta_j x_j$ is the dot product (a single real number, often called the logit or score), $\sigma$ is the logistic function, and $h$ is the hypothesis function.

"The bias term is included in this $x$" is chapter 10's trick: prepend a constant $1$ to every feature vector, so with $x = (1, x_1, \dots, x_{M-1})$ and $\theta = (\theta_0, \dots, \theta_{M-1})$,

$$ \theta^\top x = \theta_0 \cdot 1 + \theta_1 x_1 + \cdots + \theta_{M-1}x_{M-1} $$

and $\theta_0$ plays the role of $b$. From here on "$\theta^\top x$" always includes the bias.

4.1 Reading the output as a probability

Key idea $$ h(x) = P(Y = 1 \mid x), \qquad\text{and therefore}\qquad P(Y = 0 \mid x) = 1 - h(x). $$

The second equation is not an extra assumption — with two classes the probabilities must sum to $1$, so $P(Y=0\mid x)$ is forced.

So the output is the distribution $\bigl(1-h(x),\; h(x)\bigr)$, which answers every complaint in §1.1: rank by $h(x)$, flag $h(x)\in(0.4,0.6)$ for review, move the operating threshold, average two models.

With §3.2, $P(Y=0\mid x) = 1 - \sigma(\theta^\top x) = \sigma(-\theta^\top x)$: class 0 is the same model with $\theta$ negated, so neither class is privileged.

Figure 9 (reconstructed) — $\D$ trained on a logistic regression model
The maximum-likelihood fit on our twelve points is $h(x) = \sigma(5.5119x - 5.8044)$. Its half-way point is $x = 5.8044/5.5119 = 1.053$, inside the overlap. At $x=1$ it reports $P(Y=1\mid x) = 0.427$ — close to the "$\approx 0.5$" the notes said we wanted, which is the whole objective of the exercise. Out at $x = 3.3$ it reports $0.99998$, and at $x=-1.4$ it reports $0.0000005$. The model is uncertain where the data is ambiguous and confident where the data is clean. That is the thing the threshold model could not do.
Intuition

Keep the two steps separate: $z = \theta^\top x$ collapses the feature vector to one score, and that is where all the learning lives; $\sigma$ then squashes the score into a probability, with no parameters of its own. The squashing changes the loss surface, and so the fit you get, but never which decision rules the model can express — as §5 proves.

Our fitted model is $h(x) = \sigma(5.5119x - 5.8044)$. What is $P(Y = 0 \mid x = 1.053)$?
Why: $x=1.053$ makes the logit $z = 5.5119(1.053) - 5.8044 \approx 0$, and $\sigma(0)=0.5$, so $h(x)=0.5$ and $P(Y=0\mid x) = 1 - 0.5 = 0.5$. The decision boundary is exactly the set of points where the model is maximally unsure — not where it is certain. It is also where $\sigma' $ is largest ($0.25$), i.e. where the model is most responsive to a parameter change.

5. Why this is still a linear classifier

$\sigma$ is emphatically non-linear — it curves, it saturates — yet the notes call logistic regression a linear model for classification. Both are true:

Proof

Claim. The rule "predict class 1 when $h(x) \ge 0.5$" is identical to the rule "predict class 1 when $\theta^\top x \ge 0$".

Start from the prediction rule and substitute the model:

$$ h(x) \ge 0.5 \iff \sigma(\theta^\top x) \ge 0.5 . $$

By Claim 2 of §3, $0.5 = \sigma(0)$, so this is

$$ \sigma(\theta^\top x) \ge \sigma(0). $$

By consequence 1 of §3.1, $\sigma'(z) = \sigma(1-\sigma) > 0$ everywhere, so $\sigma$ is strictly increasing, hence injective with a strictly increasing inverse — and a strictly increasing function preserves $\ge$ in both directions:

$$ \sigma(a) \ge \sigma(b) \iff a \ge b. $$

Apply it with $a = \theta^\top x$ and $b = 0$:

$$ \boxed{\;h(x) \ge 0.5 \iff \theta^\top x \ge 0\;} $$

The set $\{x : \theta^\top x = 0\}$ is, by definition, a hyperplane through the origin of the augmented feature space — a point in 1-D, a line in 2-D, a plane in 3-D. The decision boundary is that hyperplane. $\blacksquare$

In 1-D with $z = wx+b$ the boundary is the point $x = -b/w$; in 2-D it is the straight line $\theta_0 + \theta_1x_1 + \theta_2x_2 = 0$.

Key idea

$\sigma$ is a monotone non-linearity applied after the linear part. Monotone functions cannot re-order their inputs, so they cannot change which side of the boundary a point falls on: $\sigma$ only relabels the score axis as a probability, moving nobody across the line.

So it inherits a linear classifier's limits exactly: no XOR, no circles. For curved boundaries, engineer features (chapter 10) — feed it $x_1^2, x_2^2, x_1x_2$ and the boundary is a conic in the original space, a hyperplane in the engineered one.

5.1 What $\|\theta\|$ does versus what the direction of $\theta$ does

Split $\theta = (\theta_0, w)$, with $w$ acting on the real features $\tilde x$, so $z = w^\top \tilde x + \theta_0$. The signed distance from $\tilde x$ to the boundary hyperplane is

$$ d(\tilde x) = \frac{w^\top \tilde x + \theta_0}{\|w\|} \qquad\Longrightarrow\qquad z = \|w\|\,d(\tilde x). $$

Substituting into the model:

$$ h(x) = \sigma\bigl(\|w\|\; d(\tilde x)\bigr). $$

Now everything separates cleanly:

Algebraically: replacing $\theta$ by $c\theta$ for $c > 0$ leaves the boundary set $\{x : \theta^\top x = 0\}$ unchanged — scaling $\theta$ changes every probability and not one predicted label.

Figure C — 2-D decision boundary with the probability field
The colour is $h(x) = P(Y=1\mid x)$, running from the class-0 colour to the class-1 colour. The white-ish ridge is the $h = 0.5$ contour: the decision boundary. The arrow is the direction of $w$. Things to do: (1) drag magnitude to its minimum — the whole plane goes to a flat $0.5$, the model has no opinion, but the boundary line has not moved; (2) drag it to the maximum — the gradient collapses into a knife edge, i.e. the threshold model of stage 3; (3) change angle — the line rotates while the sharpness stays put; (4) change bias — the line slides without rotating. Only the angle and bias change which points are misclassified.
Guided problemReading a 2-D logistic model off its parameters

You are handed a trained model $h(x) = \sigma(\theta^\top x)$ with $\theta = [-3,\; 1,\; 2]^\top$, where the feature vector is $x = [1,\; x_1,\; x_2]^\top$ (the leading $1$ is the bias feature).

  1. Write down the equation of the decision boundary and sketch it.
  2. Compute $h$ at the points $(x_1,x_2) = (2,1)$ and $(1, 0.5)$, and classify each.
  3. Your colleague multiplies every parameter by $2$, giving $\theta' = [-6, 2, 4]^\top$. Which predictions change? Which probabilities change?
Step 1 — The boundary

By §5, the boundary is the set where $\theta^\top x = 0$:

$$ -3 + x_1 + 2x_2 = 0 \quad\Longleftrightarrow\quad x_2 = \frac{3 - x_1}{2} = 1.5 - 0.5\,x_1 . $$

A straight line with intercept $1.5$ and slope $-0.5$. Class 1 is the side where $-3+x_1+2x_2 > 0$, i.e. above and to the right of the line; the normal direction $w = (1,2)$ points that way.

Step 2 — Evaluate the two points

Always compute the logit $z = \theta^\top x$ first; it is a plain dot product.

Point $(2,1)$: the augmented vector is $[1,2,1]^\top$, so

$$ z = (-3)(1) + (1)(2) + (2)(1) = -3 + 2 + 2 = 1, \qquad h = \sigma(1) = \frac{1}{1+e^{-1}} = 0.7311. $$

$z \ge 0$, so predict class 1, with $73.11\%$ confidence. Cross-check with the boundary: at $x_1 = 2$ the line sits at $x_2 = 0.5$, and our point has $x_2 = 1 > 0.5$, so it is on the class-1 side. ✓

Point $(1, 0.5)$: augmented $[1,1,0.5]^\top$, so

$$ z = -3 + 1 + 1 = -1, \qquad h = \sigma(-1) = 0.2689. $$

$z < 0$, so predict class 0. Note $h = 0.2689$ means $P(Y=0\mid x) = 1 - 0.2689 = 0.7311$ — the same confidence as the first point, which is the antisymmetry $\sigma(-z) = 1-\sigma(z)$ from §3.2 showing up in practice.

Step 3 — What doubling $\theta$ does

The boundary equation becomes $-6 + 2x_1 + 4x_2 = 0$. Divide by $2$: $-3 + x_1 + 2x_2 = 0$. Identical line. So no prediction changes — every point is on the same side it was before.

The probabilities all change, though. The logits double:

$$ (2,1):\ z = 2 \Rightarrow h = \sigma(2) = 0.8808, \qquad (1,0.5):\ z = -2 \Rightarrow h = \sigma(-2) = 0.1192. $$

Both points moved further from $0.5$. The model became more confident about exactly the same decisions.

Answer & the takeaway

Answers. (1) $x_2 = 1.5 - 0.5x_1$. (2) $h(2,1) = 0.7311 \to$ class 1; $h(1,0.5) = 0.2689 \to$ class 0. (3) Predictions unchanged; probabilities move to $0.8808$ and $0.1192$.

Takeaway. $\|\theta\|$ is a confidence dial that the decision rule cannot see. This has a sharp practical consequence: if your training data happens to be perfectly linearly separable, the likelihood can always be improved by scaling $\theta$ up, so the optimiser drives $\|\theta\| \to \infty$ and never converges. beyond syllabus The fix is regularisation — add a penalty $\lambda\|\theta\|^2$ to the objective, which gives $\|\theta\|$ a reason to stay finite. Our dataset has overlapping classes, so this does not bite here; it is the first thing to check when a logistic regression "won't converge".

6. Log-odds: why the coefficients mean something

Can we get back from the probability to the linear part? The inverse has a name:

Definition

For a probability $p \in (0,1)$, the odds are $\dfrac{p}{1-p}$ — "how many times more likely the event is than its complement". A probability of $0.75$ is odds of $3$, i.e. 3-to-1 on. The log-odds or logit is $\log\dfrac{p}{1-p}$, which maps $(0,1)$ onto all of $\R$.

Proof

Claim: $\displaystyle \log\frac{h(x)}{1-h(x)} = \theta^\top x$. Write $z = \theta^\top x$.

Numerator. $h = \sigma(z) = \dfrac{1}{1+e^{-z}}$.

Denominator. From Step 6 of §3.1, $1 - \sigma(z) = \dfrac{e^{-z}}{1+e^{-z}}$.

Divide. The common denominator cancels:

$$ \frac{h}{1-h} \;=\; \frac{\dfrac{1}{1+e^{-z}}}{\dfrac{e^{-z}}{1+e^{-z}}} \;=\; \frac{1}{1+e^{-z}}\cdot\frac{1+e^{-z}}{e^{-z}} \;=\; \frac{1}{e^{-z}} \;=\; e^{z}. $$

Take logs. $\log$ is the inverse of $\exp$, so

$$ \log\frac{h(x)}{1-h(x)} = \log e^{z} = z = \theta^\top x. \qquad \blacksquare $$
Key idea

Logistic regression is ordinary linear regression on the log-odds scale. What is modelled linearly is not the probability but $\log\frac{p}{1-p}$, which lives on all of $\R$ — a sensible target for an unbounded linear function. The sigmoid is just the inverse of the logit, converting back.

Hence interpretable coefficients. Hold every feature fixed but $x_j$ and raise $x_j$ by one unit: $z$ rises by exactly $\theta_j$, so

$$ \frac{\text{new odds}}{\text{old odds}} = \frac{e^{z + \theta_j}}{e^{z}} = e^{\theta_j}. $$
Example

A model predicts whether a student passes. The coefficient on hours studied per week is $\theta_j = 0.7$.

Interpretation: each extra hour multiplies the odds of passing by $e^{0.7} = 2.0138$ — it roughly doubles the odds, not the probability.

Worked numbers. A student currently at $P(\text{pass}) = 0.4$:

So $0.400 \to 0.573$: a jump of $17.3$ percentage points. The same hour for a student already at $P = 0.95$: odds $= 19$, new odds $= 38.26$, new $p = 38.26/39.26 = 0.9745$ — a jump of only $2.5$ points.

The moral: the effect on the odds is a constant multiplier for everybody; the effect on the probability depends on where you started and is largest near $p = 0.5$. So "a one-hour increase raises the pass rate by 17 points" is wrong; it holds only near $40\%$.

A logistic model of loan default has coefficient $\theta_j = -0.3$ on years of credit history. Which statement is correct?
Why: the linear part models the log-odds, so adding $1$ to $x_j$ adds $\theta_j$ to the log-odds and therefore multiplies the odds by $e^{\theta_j} = e^{-0.3} = 0.7408$. Option A confuses odds with probability and also treats a multiplicative effect as additive. Option C multiplies the wrong quantity: odds and probability only agree when $p$ is tiny (then $p/(1-p)\approx p$), so C is a decent approximation for rare events and wrong otherwise. Option D is backwards — the sign is the most interpretable part of the whole model: negative coefficient, feature pushes toward class 0.

7. The loss: cross-entropy

Three things remain: measuring this model's error, training it, and extending it to many classes.

7.0 The lecture's framing: grading an LLM

The lecture's example: grade an LLM on many tasks, recording each task's easiness ($x \in \R$) and whether it succeeded ($y \in \{0,1\}$). Tasks of equal easiness sometimes succeed and sometimes fail, so where the LLM succeeds half the time any prediction of the label is wrong half the time.

Key idea

Estimate $P(y=1)$ for each $x$ instead. That number is continuous, so classification becomes a regression problem on probabilities — which is why "logistic regression" is literally accurate. The design questions are then the usual ERM ones: which loss, and which model class (here, the sigmoid of a linear function).

The lecture chooses the loss from two principles about grading a probability $\hat p$ for an event that then happens:

  1. Monotone: a higher reported $\hat p$ should incur a lower loss.
  2. Confidence matters: a certain wrong prediction is worse than an uncertain wrong one.

From information theory (chapter 7), the surprisal of an event you gave probability $\hat p$ is $\log\frac{1}{\hat p}$ — and it satisfies both: it falls as $\hat p$ rises, and it goes to $\infty$ as $\hat p \to 0$. That is the loss. The notes write it as

$$ \ell(\hat p, y) = \1\{y=1\}\log\frac{1}{\hat p} + \1\{y=0\}\log\frac{1}{1-\hat p} = y\log\frac{1}{\hat p} + (1-y)\log\frac{1}{1-\hat p}, $$

the same as the $-[\,y\log\hat y + (1-y)\log(1-\hat y)\,]$ form below, since $\log\frac1a = -\log a$. First, why the obvious alternative fails.

7.1 Why not squared loss?

The obvious move is to reuse the loss from chapter 8:

$$ J_{\text{sq}}(\theta) = \frac{1}{N}\sum_{i=1}^{N}\Bigl(y^{(i)} - \sigma\bigl(\theta^\top x^{(i)}\bigr)\Bigr)^{2}. $$

Not absurd: bounded, differentiable, minimised when $\hat y = y$. But it fails on two counts.

Failure 1: it is not convex in $\theta$

For linear regression, squared loss gave a bowl: one global minimum, a closed form. Wrap the prediction in $\sigma$ and the bowl buckles:

Proof

Take a dataset of one point, $x = 1$ (no bias), $y = 0$. Then $\hat y = \sigma(w)$ and

$$ J(w) = \bigl(0 - \sigma(w)\bigr)^2 = \sigma(w)^2 . $$

Differentiate, using $\sigma' = \sigma(1-\sigma)$ from §3.1:

$$ J'(w) = 2\sigma\,\sigma' = 2\sigma^{2}(1-\sigma). $$

Differentiate again — product rule on $\sigma^2$ and $(1-\sigma)$, then factor out $\sigma' = \sigma(1-\sigma)$:

$$ J''(w) = 2\Bigl[2\sigma\sigma'(1-\sigma) - \sigma^{2}\sigma'\Bigr] = 2\sigma'\sigma\Bigl[2(1-\sigma) - \sigma\Bigr] = 2\sigma'\sigma\,(2 - 3\sigma). $$

Since $\sigma' > 0$ and $\sigma > 0$, the sign of $J''$ is the sign of $(2 - 3\sigma)$. So

Numerically: $J''(0) = +0.125$ and $J''(2) = -0.1188$. A convex function has $J'' \ge 0$ everywhere, so $J_{\text{sq}}$ is not convex. $\blacksquare$

With many points and parameters this gives genuine local minima and flat plateaus, so gradient descent's answer depends on where you started.

Failure 2: the gradient vanishes exactly when the model is most wrong

Differentiate one term of $J_{\text{sq}}$ with respect to $\theta$, chain rule through $\sigma$:

$$ \frac{\partial}{\partial\theta}\bigl(y - \sigma(z)\bigr)^2 = -2\bigl(y - \sigma(z)\bigr)\,\underbrace{\sigma(z)\bigl(1-\sigma(z)\bigr)}_{\sigma'(z)}\,x . $$

The worst case: $y = 1$ and the model says $\hat y = 0.001$. The error factor $(y-\hat y) = 0.999$ is large, but the $\sigma'$ factor is $0.001\times0.999 = 0.000999$, so the product is $\approx 0.000998$ — a thousandth of what a point at the boundary would give ($0.25$ there). The model is confidently wrong and the optimiser barely notices.

Stage 3's pathology, back through the side door: saturation makes $\sigma'$ small and squared loss multiplies by $\sigma'$. We need a loss whose derivative cancels that factor.

7.2 The cross-entropy (negative log-likelihood) loss

Definition

For one sample with true label $y \in \{0,1\}$ and predicted probability $\hat y = h(x) \in (0,1)$, the binary cross-entropy loss is

$$ \ell(y,\hat y) \;=\; -\Bigl[\,y\log \hat y \;+\; (1-y)\log(1-\hat y)\,\Bigr] $$

and the training objective averages it over the dataset:

$$ J(\theta) = \frac{1}{N}\sum_{i=1}^{N} -\Bigl[\,y^{(i)}\log \sigma\bigl(\theta^\top x^{(i)}\bigr) + \bigl(1-y^{(i)}\bigr)\log\bigl(1 - \sigma(\theta^\top x^{(i)})\bigr)\Bigr]. $$

Read the two branches. $y$ is $0$ or $1$, so one term is always multiplied by zero — a switch written without an if:

Both branches are the same sentence: your loss is $-\log(\text{the probability you gave the true label})$.

Intuition

Where does $-\log$ come from? Two equivalent stories.

Likelihood. The model treats the label as a Bernoulli draw, $P(y\mid x) = \hat y^{\,y}(1-\hat y)^{1-y}$. For independent samples the dataset's probability is $\prod_i P(y^{(i)}\mid x^{(i)})$; take logs — legal, since $\log$ is increasing and so does not move the maximiser — to turn the product into a sum, then flip the sign to turn maximise into minimise. You get exactly $J(\theta)$. Cross-entropy is negative log-likelihood, and fitting a logistic regression is maximum likelihood estimation.

Information. $-\log p$ is the nats of surprise in an event you gave probability $p$ (chapter 7), so cross-entropy charges your surprise at the truth: a confidently wrong model is infinitely surprised and pays accordingly.

Figure D — the cost of a prediction, for each true label
Both curves pass through $(0.5,\ 0.693)$ — the cost of shrugging. Both reach $0$ when you put full probability on the truth. And both go to $+\infty$ as you put probability $\to 0$ on the truth: $-\log(0.1) = 2.303$, $-\log(0.01) = 4.605$, $-\log(0.001)=6.908$. The explosion is the design. Squared loss caps a wrong answer's cost at $1$; cross-entropy makes it unbounded, so the optimiser can never make peace with a confidently wrong prediction. Compare the two curves' slopes near the bad end, too: squared loss flattens out there, cross-entropy gets steeper.
Guided problemCompute $\sigma(\theta^\top x)$ and the loss by hand

A model has parameters $\theta = [-1,\; 2,\; 0.5]^\top$ acting on augmented feature vectors $x = [1,\; x_1,\; x_2]^\top$. Three samples arrive:

sample$x_1$$x_2$true $y$
A1.521
B0.5−10
C210

For each: compute the logit, the predicted probability, and the cross-entropy loss. Then compute the average loss $J(\theta)$. Which sample is the model most embarrassed by, and why? (You may use $e^{-3} = 0.049787$, $e^{0.5} = 1.648721$, $e^{-3.5} = 0.030197$.)

Step 1 — Set up the recipe

Three mechanical steps per sample, never skip one:

  1. $z = \theta^\top x$ — a dot product including the bias feature $1$.
  2. $\hat y = \sigma(z) = 1/(1+e^{-z})$.
  3. $\ell = -\log\hat y$ if $y=1$, else $\ell = -\log(1-\hat y)$.

Logs here are natural logs. (Using $\log_2$ instead just rescales everything by $\log_2 e$ and changes nothing about the minimiser.)

Step 2 — Sample A

Augmented $x = [1,\;1.5,\;2]^\top$:

$$ z = (-1)(1) + (2)(1.5) + (0.5)(2) = -1 + 3 + 1 = 3. $$ $$ \hat y = \sigma(3) = \frac{1}{1+e^{-3}} = \frac{1}{1.049787} = 0.952574 . $$

True label is $1$, so we are on the $-\log\hat y$ branch:

$$ \ell_A = -\log(0.952574) = 0.048587 . $$

Small, as it should be: the model put $95.3\%$ on the correct class.

Step 3 — Sample B

Augmented $x = [1,\;0.5,\;-1]^\top$:

$$ z = -1 + (2)(0.5) + (0.5)(-1) = -1 + 1 - 0.5 = -0.5 . $$ $$ \hat y = \sigma(-0.5) = \frac{1}{1+e^{0.5}} = \frac{1}{2.648721} = 0.377541 . $$

True label is $0$, so use the other branch, with $1-\hat y = 0.622459$:

$$ \ell_B = -\log(0.622459) = 0.474077 . $$

Middling. The model leans the right way ($\hat y < 0.5$, so it would predict class 0) but only weakly — the logit is just $-0.5$, half a unit from the boundary.

Step 4 — Sample C, and the average

Augmented $x = [1,\;2,\;1]^\top$:

$$ z = -1 + 4 + 0.5 = 3.5, \qquad \hat y = \sigma(3.5) = \frac{1}{1.030197} = 0.970688 . $$

True label is $0$, so $1 - \hat y = 0.029312$ and

$$ \ell_C = -\log(0.029312) = 3.529750 . $$

Average over the three:

$$ J(\theta) = \frac{0.048587 + 0.474077 + 3.529750}{3} = \frac{4.052414}{3} = 1.350805 . $$
Answer & the takeaway

Answers. A: $z=3$, $\hat y = 0.9526$, $\ell = 0.0486$. B: $z=-0.5$, $\hat y = 0.3775$, $\ell = 0.4741$. C: $z=3.5$, $\hat y = 0.9707$, $\ell = 3.5298$. Average $J = 1.3508$.

The model is most embarrassed by C, and by a mile: $\ell_C$ is $73\times$ larger than $\ell_A$ and accounts for $87\%$ of the total loss. C is the confidently-wrong case — the model assigned $97\%$ to the wrong class.

Takeaway. Under squared loss those same three samples would have cost $(1-0.9526)^2 = 0.0022$, $(0-0.3775)^2 = 0.1425$ and $(0-0.9707)^2 = 0.9423$ — C would be only $6.6\times$ worse than B instead of $7.4\times$, and its cost would be capped at $1$ no matter how wrong it got. Cross-entropy's unbounded penalty is what makes the optimiser go and fix C first. And as §7.3 will show, C is also where the gradient is biggest — under squared loss it would have been nearly the smallest.

7.3 Training it — the gradient

To run gradient descent we need $\nabla_\theta J$. Derive it for one sample; the average follows by linearity of differentiation.

Proof

Fix one sample $(x, y)$ and write $z = \theta^\top x$, $\hat y = \sigma(z)$. The per-sample loss is

$$ \ell = -\bigl[\,y\log\sigma(z) + (1-y)\log(1-\sigma(z))\,\bigr]. $$

We want $\partial\ell/\partial\theta$. Chain through $z$:

$$ \frac{\partial \ell}{\partial \theta} = \frac{\partial \ell}{\partial z}\cdot\frac{\partial z}{\partial \theta}. $$

Line 1 — differentiate $\ell$ with respect to $z$. Use $\frac{d}{dz}\log u = \frac{1}{u}\frac{du}{dz}$ on each term, with $u = \sigma$ in the first and $u = 1-\sigma$ in the second. Note $\frac{d}{dz}(1-\sigma) = -\sigma'$:

$$ \frac{\partial \ell}{\partial z} = -\left[\, y\cdot\frac{1}{\sigma}\cdot\sigma' \;+\; (1-y)\cdot\frac{1}{1-\sigma}\cdot(-\sigma')\,\right]. $$

Line 2 — pull out the common $\sigma'$.

$$ \frac{\partial \ell}{\partial z} = -\,\sigma' \left[\, \frac{y}{\sigma} - \frac{1-y}{1-\sigma} \,\right]. $$

Line 3 — substitute the identity $\sigma' = \sigma(1-\sigma)$. This is the step the whole chapter has been building toward:

$$ \frac{\partial \ell}{\partial z} = -\,\sigma(1-\sigma) \left[\, \frac{y}{\sigma} - \frac{1-y}{1-\sigma} \,\right]. $$

Line 4 — distribute, and watch the denominators cancel. The first product cancels $\sigma$; the second cancels $(1-\sigma)$:

$$ \frac{\partial \ell}{\partial z} = -\Bigl[\, y(1-\sigma) \;-\; (1-y)\sigma \,\Bigr]. $$

Line 5 — expand the bracket.

$$ y(1-\sigma) - (1-y)\sigma = y - y\sigma - \sigma + y\sigma = y - \sigma. $$

The two $y\sigma$ terms annihilate. Therefore

$$ \frac{\partial \ell}{\partial z} = -(y - \sigma) = \sigma(z) - y = \hat y - y . $$

Line 6 — the other factor. $z = \theta^\top x = \sum_j \theta_j x_j$, so $\partial z/\partial\theta_j = x_j$, i.e. $\partial z/\partial\theta = x$.

Line 7 — multiply, and average over the dataset.

$$ \frac{\partial \ell}{\partial \theta} = \bigl(\sigma(\theta^\top x) - y\bigr)\,x, \qquad\Longrightarrow\qquad \boxed{\;\nabla_\theta J = \frac{1}{N}\sum_{i=1}^{N}\Bigl(\sigma\bigl(\theta^\top x^{(i)}\bigr) - y^{(i)}\Bigr)\,x^{(i)}\;} $$

$\blacksquare$

Key idea

Every $\log$, every $e$, every $\sigma'$ has cancelled. What is left is

$$ \nabla_\theta J = \frac{1}{N}\sum_i (\text{prediction}_i - \text{truth}_i)\cdot x^{(i)} $$

— "average the feature vectors, weighted by how wrong you were." Compare the linear-regression gradient of chapter 8:

$$ \nabla_\theta J_{\text{lin}} = \frac{1}{N}\sum_i \bigl(\theta^\top x^{(i)} - y^{(i)}\bigr)x^{(i)} \qquad\text{vs}\qquad \nabla_\theta J_{\text{log}} = \frac{1}{N}\sum_i \bigl(\sigma(\theta^\top x^{(i)}) - y^{(i)}\bigr)x^{(i)} $$

Identical form, the only difference being that the prediction passes through $\sigma$ — a general property of the exponential family. beyond syllabus

Note what this gradient does not have: the $\sigma'$ factor that crippled squared loss. The confidently-wrong point ($y=1$, $\hat y = 0.001$) contributes $(0.001-1)x = -0.999\,x$, essentially full strength; a confidently-right point ($\hat y = 0.999$) contributes $-0.001\,x$, essentially nothing. The loss and the model were designed together so the saturation cancels exactly — which is why Figure B's far-away point left the logistic fit unchanged.

This is the lecture's $\nabla_\theta J^{(i)}(\theta) = \bigl(\hat p^{(i)}_\theta - y^{(i)}\bigr)x^{(i)}$. One question remains before running GD: linear regression's objective was a parabola — this is not, so now what?

7.4 There is no closed form

Setting the gradient to zero gives the score equations

$$ \sum_{i=1}^{N}\sigma\bigl(\theta^\top x^{(i)}\bigr)\,x^{(i)} \;=\; \sum_{i=1}^{N} y^{(i)}x^{(i)} . $$

In linear regression the same step gave $X^\top X\theta = X^\top y$ — linear in $\theta$, so you invert a matrix and stop. Here $\theta$ is trapped inside an exponential, the equation is transcendental, and there is no normal equation for logistic regression.

So you iterate. $J$ is convex and differentiable, so gradient descent finds the global minimum given a small enough step size:

θ ← 0                                   # or small random values
repeat until converged:
    g ← (1/N) Σ_i ( σ(θᵀx⁽ⁱ⁾) − y⁽ⁱ⁾ ) x⁽ⁱ⁾     # full-batch gradient
    θ ← θ − α·g                         # α is the learning rate

For large $N$ use stochastic gradient descent: the same formula, averaged over one random sample or minibatch instead of all $N$. Everything from chapter 9 transfers verbatim.

Figure E — fit the model yourself (the centrepiece)
Top: the data and the current $h(x) = \sigma(wx+b)$. Each dashed stalk runs from a point's true label ($0$ or $1$) to the curve — its length is the residual $|y^{(i)} - \hat y^{(i)}|$, so a long stalk is an unhappy point. Bottom: the per-sample cross-entropy $\ell_i$ as a bar; the total is the average of the bar heights. Press "Fit it for me" and watch 200 gradient-descent steps: the curve steepens and slides until its half-way point lands in the overlap, and $J$ falls from $0.456$ to about $0.207$. Notice what it cannot fix — the two bars at $x = 1.0$ and $x = 1.2$ stay tall forever, because those points genuinely overlap and no sigmoid can make both of them happy. That residual loss is the data's irreducible ambiguity, correctly reported. Try $\alpha = 4$ to watch it converge faster, or drag $w$ to $10$ by hand to see the threshold model reappear and the overlap points' bars shoot up.
Guided problemTake one gradient-descent step by hand

A tiny dataset with one real feature, and the bias folded in so $x^{(i)} = [1,\; x_1^{(i)}]^\top$ and $\theta = [b,\; w]^\top$:

$i$$x_1^{(i)}$$y^{(i)}$
110
221
331

Start at $\theta^{(0)} = [0,\;0]^\top$ with learning rate $\alpha = 1$. Compute $J(\theta^{(0)})$, take one full-batch gradient step, and compute $J(\theta^{(1)})$. Did every point improve?

You may use $\log 2 = 0.6931$, $\sigma(0.8333)=0.6971$, $\sigma(1.5)=0.8176$, $\sigma(2.1667)=0.8972$.

Step 1 — Predictions and loss at the start

With $\theta = [0,0]^\top$ every logit is $z_i = 0\cdot 1 + 0\cdot x_1 = 0$, so every prediction is $\hat y_i = \sigma(0) = 0.5$. The model is maximally undecided about everything.

Each loss is $-\log(0.5) = \log 2 = 0.6931$ regardless of the label, so

$$ J(\theta^{(0)}) = \frac{3 \times 0.6931}{3} = 0.6931 . $$

Sanity check to remember: $\log 2 \approx 0.693$ is the loss of a model that always shrugs. Any trained binary classifier reporting average cross-entropy above $0.693$ is doing worse than a coin.

Step 2 — The residuals

The gradient formula needs only $(\hat y_i - y_i)$:

$i$$\hat y_i$$y_i$$\hat y_i - y_i$$x^{(i)}$
10.50$+0.5$$[1, 1]$
20.51$-0.5$$[1, 2]$
30.51$-0.5$$[1, 3]$

Sign check: a positive residual means the model predicted too high, so the step should push that sample's score down. Point 1 is the only one labelled $0$, so it is the only positive residual. Good.

Step 3 — Assemble the gradient
$$ \nabla_\theta J = \frac{1}{3}\sum_{i=1}^{3}(\hat y_i - y_i)\,x^{(i)} $$

Bias component (first coordinate of every $x^{(i)}$ is $1$):

$$ \frac{\partial J}{\partial b} = \frac{1}{3}\bigl[(+0.5)(1) + (-0.5)(1) + (-0.5)(1)\bigr] = \frac{-0.5}{3} = -0.1667 . $$

Weight component (second coordinate is $x_1^{(i)}$):

$$ \frac{\partial J}{\partial w} = \frac{1}{3}\bigl[(+0.5)(1) + (-0.5)(2) + (-0.5)(3)\bigr] = \frac{0.5 - 1 - 1.5}{3} = \frac{-2}{3} = -0.6667 . $$

So $\nabla_\theta J = [-0.1667,\; -0.6667]^\top$. Both components are negative, meaning both parameters should increase — which matches intuition, since the class-1 points sit at larger $x$.

Step 4 — The update and the new loss
$$ \theta^{(1)} = \theta^{(0)} - \alpha\nabla_\theta J = \begin{bmatrix}0\\0\end{bmatrix} - 1\cdot\begin{bmatrix}-0.1667\\-0.6667\end{bmatrix} = \begin{bmatrix}0.1667\\0.6667\end{bmatrix}. $$

New logits $z_i = 0.1667 + 0.6667\,x_1^{(i)}$:

$i$$z_i$$\hat y_i$$y_i$$\ell_i$
10.83330.69710$-\log(0.3029) = 1.1942$
21.50000.81761$-\log(0.8176) = 0.2014$
32.16670.89721$-\log(0.8972) = 0.1085$
$$ J(\theta^{(1)}) = \frac{1.1942 + 0.2014 + 0.1085}{3} = \frac{1.5041}{3} = 0.5014 . $$
Answer & the takeaway

Answers. $J(\theta^{(0)}) = 0.6931$; $\nabla J = [-0.1667, -0.6667]^\top$; $\theta^{(1)} = [0.1667, 0.6667]^\top$; $J(\theta^{(1)}) = 0.5014$. The loss fell by $0.192$.

No, not every point improved. Point 1 got distinctly worse: its loss rose from $0.6931$ to $1.1942$, because the step pushed the whole score line up and point 1 is the lone class-0 sample fighting against two class-1 samples at larger $x$. Points 2 and 3 improved enough to more than pay for it.

Takeaway. Gradient descent minimises the average, and the average is a negotiation. A single step can hurt individual samples; it can only ever hurt the majority-weighted sum if $\alpha$ is too large. This is also the honest answer to "why did my model get worse on my favourite example after retraining?" — because your favourite example is one term in a sum of $N$.

A point is confidently wrong: true label $y=1$, model says $\hat y = 0.002$. Compare its contribution to the gradient under cross-entropy versus under squared loss.
Why: cross-entropy's per-sample gradient is $(\hat y - y)x = (0.002 - 1)x = -0.998x$. Squared loss's is $-2(y-\hat y)\sigma(1-\sigma)x = -2(0.998)(0.002)(0.998)x = -0.00398x$ — about $250$ times smaller. Option C states the usual intuition about squared loss, which is right for linear regression and wrong here: squaring does amplify the error term, but the extra $\sigma'$ factor from the chain rule crushes it far harder. This is the single most important practical reason to use cross-entropy with a sigmoid.

7.5 Convexity: why GD still finds the global minimum

Gradient descent looks downhill only locally, so on a bumpy surface it can settle in a dent that is not the lowest point (chapter 9). What made linear regression safe was being bowl-shaped; convexity is the precise version of that.

Definition

$f:\R\to\R$ is convex if the chord joining any two points on its graph never goes below the graph:

$$ f\bigl(\lambda z_1 + (1-\lambda) z_2\bigr) \;\le\; \lambda f(z_1) + (1-\lambda) f(z_2) \qquad \forall\, z_1, z_2 \in \R,\ \lambda\in[0,1]. $$

Left to right: $\lambda z_1 + (1-\lambda)z_2$ is a point between $z_1$ and $z_2$ ($z_2$ at $\lambda=0$, $z_1$ at $\lambda=1$); the left side is the function there, the right side the same blend of the endpoint values — the height of the chord. For twice-differentiable $f$ the equivalent test is $f''(z) \ge 0$ everywhere: the slope never decreases. Both forms extend to $f:\R^d\to\R$, with $f''$ replaced by the Hessian, which must be positive semi-definite.

Key idea — the property that matters

For a convex function, every local minimum is a global minimum. Suppose $z_1$ were a local minimum but $f(z_2) < f(z_1)$ for some $z_2$. Points on the segment just next to $z_1$ lie under the chord from $(z_1, f(z_1))$ to $(z_2, f(z_2))$, which slopes downward from $z_1$ — so they are strictly lower than $f(z_1)$ arbitrarily close to it, contradicting "local minimum". Hence GD/SGD, which find local minima, find the global one.

Figure F — test the chord condition yourself
Both losses use the one-point dataset from §7.1 ($x = 1$, $y = 0$, no bias), so the curves are $-\log(1-\sigma(w))$ and $\sigma(w)^2$. What to notice: on cross-entropy and the parabola, no placement of the endpoints ever pulls the chord under the curve. On squared loss, put both endpoints to the right of $w = \log 2 \approx 0.69$ (e.g. $z_1 = 1$, $z_2 = 6$) and the chord dips below — the concave region found in §7.1.
Proof — the lecture's exercise: logistic ERM is convex (scalar case)

Take $\mathcal X = \R$, parameters $(\omega, b)$, one sample $(x, y)$, $z = \omega x + b$, $\hat p = \sigma(z)$. One term suffices, because a sum of convex functions is convex (add the chord inequalities term by term).

Step 1 — rewrite the loss without $\hat p$. Since $\frac{1}{\sigma(z)} = 1+e^{-z}$ and $\frac{1}{1-\sigma(z)} = 1 + e^{z}$,

$$ \ell = y\log(1+e^{-z}) + (1-y)\log(1+e^{z}). $$

Step 2 — first derivative in $z$. $\frac{d}{dz}\log(1+e^{-z}) = \frac{-e^{-z}}{1+e^{-z}} = -(1-\sigma(z))$ and $\frac{d}{dz}\log(1+e^{z}) = \frac{e^{z}}{1+e^{z}} = \sigma(z)$. So

$$ \frac{d\ell}{dz} = -y(1-\sigma) + (1-y)\sigma = \sigma(z) - y . $$

(Same answer as §7.3, by a shorter route.)

Step 3 — second derivative in $z$. $y$ is a constant, so $\frac{d^2\ell}{dz^2} = \sigma'(z) = \sigma(z)(1-\sigma(z)) > 0$.

Step 4 — back to the parameters. $z$ is linear in $(\omega, b)$ with $\partial z/\partial\omega = x$, $\partial z/\partial b = 1$. By the chain rule,

$$ \frac{\partial^2 \ell}{\partial\omega^2} = \sigma(1-\sigma)\,x^2 \ge 0, \qquad \nabla^2_{(\omega,b)}\,\ell = \sigma(1-\sigma)\begin{bmatrix} x^2 & x \\ x & 1\end{bmatrix}. $$

That matrix is $\sigma(1-\sigma)\,vv^\top$ with $v = [x,\ 1]^\top$, and for any $u$, $u^\top vv^\top u = (v^\top u)^2 \ge 0$ — positive semi-definite. So each term is convex in $(\omega,b)$, and the sum over $N$ samples is too. $\blacksquare$

The same argument in $\R^d$ gives the Hessian $\sum_i \sigma_i(1-\sigma_i)\,x^{(i)}x^{(i)\top}$ — a non-negative combination of such outer products, hence PSD.

Intuition — where the convexity comes from

The loss is a convex function ($\log(1+e^{\pm z})$, a softened hinge) of a linear function of the parameters, and composing a linear map inside a convex function keeps it convex. Squared loss fails because $(y-\sigma(z))^2$ is not convex in $z$: the sigmoid's flattening bends it the wrong way.

Why does the lecture care that the logistic regression objective is convex?
Why: logistic regression is convex and has no closed form (§7.4) — so option A is exactly the wrong lesson. Convexity is about the shape of the loss surface, not about differentiability of $\sigma$ (B) and not about generalisation (D: a convex model can overfit happily — see chapter 12).

8. More than two classes: softmax regression

For $K$ classes we want a distribution: $\hat y_k \ge 0$ with $\sum_k \hat y_k = 1$. Give each class a parameter vector $\theta_k \in \R^M$, compute the $K$ scores $\theta_k^\top x$, and normalise with the softmax:

Definition $$ \hat y_k \;=\; P(Y = k \mid x) \;=\; \frac{\exp\bigl(\theta_k^\top x\bigr)}{\sum_{j=1}^{K}\exp\bigl(\theta_j^\top x\bigr)} $$

$\exp$ makes every score positive and dividing by the sum makes them add to $1$, so the output is a distribution over the $K$ classes by construction.

Proof

Claim: softmax with $K=2$ is the sigmoid.

$$ \hat y_1 = \frac{e^{\theta_1^\top x}}{e^{\theta_1^\top x} + e^{\theta_0^\top x}} . $$

Divide numerator and denominator by $e^{\theta_1^\top x}$ (non-zero, so legal):

$$ \hat y_1 = \frac{1}{1 + e^{\theta_0^\top x - \theta_1^\top x}} = \frac{1}{1 + e^{-(\theta_1 - \theta_0)^\top x}} = \sigma\bigl((\theta_1-\theta_0)^\top x\bigr). \qquad\blacksquare $$

This is the lecture's exercise ("set $K=2$ and let $\theta = \theta_1 - \theta_2$"), with the second class indexed $0$ here. Only the difference of the two parameter vectors is identifiable, which is why two classes need one vector and not two — and in general why softmax has $K$ vectors but $K-1$ degrees of freedom: adding a constant vector to every $\theta_k$ cancels top and bottom.

The loss generalises the same way. With the true class $y \in \{1,\dots,K\}$, multi-class cross-entropy is

$$ \ell(y, \hat y) = -\sum_{k=1}^{K}\1\{y = k\}\log\hat y_k \;=\; -\log \hat y_{\,y} $$

where $\1\{\cdot\}$ is the indicator — the same sentence as before, minus the log of the probability you gave the true class. The gradient keeps its shape:

$$ \nabla_{\theta_k} J = \frac{1}{N}\sum_{i=1}^{N}\Bigl(\hat y^{(i)}_k - \1\{y^{(i)} = k\}\Bigr)x^{(i)} $$

— prediction minus truth, times the input, exactly as in §7.3, now once per class. Under ERM with this loss it is called softmax regression or multinomial logistic regression; in a neural network it is "a linear layer with a softmax output".

9. Common mistakes

Common mistake

1. "Logistic regression is regression, so it predicts a number." No — it is a classification model: labels in $\{0,1\}$, output used to choose a class.

The name comes from §6: $\log\frac{p}{1-p} = \theta^\top x$ is a bona fide linear regression on the log-odds, whose response happens to be a logit rather than an observed number. The name records the mathematics, not the task — and it misleads about one student a semester into fitting it with squared loss and wondering why nothing works.

Common mistake

2. Always thresholding at $0.5$. Nothing says $0.5$ is the right operating point; it is just the value making the rule coincide with $\theta^\top x \ge 0$. Two situations break it:

$$ h(x) \;>\; \frac{C_{\text{FP}}}{C_{\text{FP}} + C_{\text{FN}}} . $$

If missing a tumour is $9\times$ worse than an unnecessary biopsy, the threshold is $1/(1+9) = 0.1$, not $0.5$. This is exactly why we wanted probabilities in §1 — a probability lets you move the threshold after training; a hard label does not.

Common mistake

3. Forgetting the bias term. Write $h(x) = \sigma(w^\top \tilde x)$ with no $\theta_0$ and the boundary $w^\top\tilde x = 0$ is forced through the origin: the model can rotate it but never translate it. On our 1-D dataset it is stuck at $x = 0$ instead of $x = 1.05$, and five points are misclassified whatever $w$ you pick.

The bug: you build the design matrix and forget to prepend the column of ones (chapter 10's column), then get stubbornly mediocre accuracy with no error message. Check that $\theta$ has $M+1$ entries for $M$ features.

Common mistake

4. Assuming $\sigma(\theta^\top x)$ is a calibrated probability. The output is a number in $(0,1)$ that we interpret as $P(Y=1\mid x)$, which assumes the true log-odds really are linear in $x$. When that is roughly right (and the loss really is cross-entropy, which pushes outputs toward the empirical rates), the numbers come out well calibrated; when it is not, they can be systematically off while the rankings and the accuracy stay fine.

How to check. Bin held-out predictions — say all samples with $h(x)\in[0.7,0.8)$ — and compute what fraction were actually class 1; calibrated means about $0.75$. Plotting bin midpoint against observed frequency (a reliability diagram) should give the diagonal. beyond syllabus Post-hoc fixes exist (Platt scaling, isotonic regression), but the first step is to look.

Common mistake

5. Small numerical traps. (a) Computing $\sigma(z)$ as 1/(1+exp(-z)) overflows for very negative $z$; libraries use the branch $\sigma(z) = e^{z}/(1+e^{z})$ when $z < 0$. (b) $\log(0)$ is $-\infty$, so never round $\hat y$ to $0$ or $1$ before taking the log — this is why real implementations fuse the sigmoid and the log into one "logits" function. (c) The range is $(0,1)$ open, so a printed $\hat y$ of exactly $1.0$ means floating-point rounding, not certainty.

10. Recap

Every formula and condition is in the Essentials box at the top. Five things worth saying once more:

  1. One requirement drives the chapter: the loss must have a usable slope in the parameters. That is what kills the threshold and chooses the sigmoid.
  2. $\sigma' = \sigma(1-\sigma)$ is the load-bearing identity: it makes the gradient cancel down to $(\hat y - y)x$, and it is why confidently-correct points stop voting.
  3. Non-linear in $x$, linear as a classifier: a monotone squashing cannot move a point across $\theta^\top x = 0$.
  4. Cross-entropy, not squared loss: squared loss is non-convex here, and its gradient vanishes exactly where the model is most wrong.
  5. Convexity licenses gradient descent — every local minimum is global — and there is no closed form, so iterating is the only option.
Which single property of $\sigma$ is the reason the sigmoid replaced the threshold function?
Why: all four statements are true of $\sigma$, but only the last one is a property the threshold function lacks and that we could not do without. The threshold already outputs valid classes (so A was not the blocker — though it does not output probabilities), it is also antisymmetric about its jump, and it also induces a linear boundary. What it does not have is a usable derivative: $g'_{\text{threshold}} = 0$ almost everywhere, so $\nabla_\theta J = 0$ and $\theta \leftarrow \theta - \alpha\cdot 0$ never moves. Differentiability with non-vanishing slope is the property that makes the model trainable, and trainability is what the notes single out when they call the threshold "very problematic to optimize over."