Logistic regression: the model
When the answer is a category, predict its probability. Logistic regression squashes a straight line into an S-curve between 0 and 1. You'll learn the sigmoid's properties, prove its derivative (a past exam question), and predict by hand.
2023 Q3 was all logistic regression: the sigmoid’s domain and range, a “show that \(\sigma' = \sigma(1 - \sigma)\)” proof, the likelihood used for training, and a prediction with \(\lambda = [1, 1, -1]\). This stage covers everything except training (that’s stage 10).
Book: what to read for this stage
ISLP 4.1 and 4.2 (why not linear regression): read, short. 4.3.1 the logistic model: read. 4.3.3 making predictions: read. 4.3.4 multiple logistic regression: read, including the student paradox (lab Q1 asks you to explain it).
Slide typos
Slide 7 lists the classes as \(\{\text{yes}, \text{no}\}\); with \(Y = 1\) meaning “defaults” the order is \(\{\text{no}, \text{yes}\}\). Slide 17 says \(\hat p(x_i) \approx P(Y = 0 \mid X = x_i)\) where it means \(P(Y = 1 \mid X = x_i)\).
1. Why not just fit a line?
Classes are coded \(Y \in \{0, \dots, m - 1\}\), but the codes are labels, not numbers: brown = 0, green = 1, blue = 2 is a free choice. Two problems with linear regression on codes:
- More than two classes: a line assumes green is “halfway” between brown and blue. Renumber the classes and you get a different fit and different predictions from the same data.
- Two classes: a line fitted to 0/1 data can be read as an estimate of \(P(Y = 1 \mid X)\), but nothing stops it going below 0 or above 1.
Every classifier this week follows the same recipe: estimate \(P(Y = k \mid X = x)\) for each class, then apply the Bayes classifier rule from stage 4: pick the biggest.
A linear regression of default (0/1) on balance gives \(\hat p(x) = -0.08 + 0.0002x\). What’s wrong at a balance of 6000?
\(-0.08 + 1.2 = 1.12\). (At balance 0 it gives \(-0.08\), also impossible.)
2. The sigmoid
Keep the linear part \(t = \beta_0 + \beta_1X\) (any real number) and squash it into \((0, 1)\) with the sigmoid (logistic) function:
\[\sigma(t) = \frac{1}{1 + e^{-t}} = \frac{e^t}{1 + e^t}, \qquad p(X) = P(Y = 1 \mid X) = \sigma(\beta_0 + \beta_1X).\]
Properties to know for the exam:
- Domain: all real numbers. Range: strictly between 0 and 1. It never reaches 0 or 1, because \(e^{-t} > 0\) makes the denominator bigger than 1 and finite.
- \(\sigma(0) = 0.5\); large positive \(t\) gives nearly 1; large negative \(t\) nearly 0.
- Symmetry: \(\sigma(-t) = 1 - \sigma(t)\).
- It’s increasing, so \(\sigma(t) > 0.5\) exactly when \(t > 0\).
| \(t\) | \(-3\) | \(-2\) | \(-1\) | \(0\) | \(1\) | \(2\) | \(3\) |
|---|---|---|---|---|---|---|---|
| \(\sigma(t)\) | 0.047 | 0.119 | 0.269 | 0.5 | 0.731 | 0.881 | 0.953 |
Compute \(\sigma(-1)\).
\(1/(1 + e^{1}) = 1/3.718 = 0.269\). Check: \(\sigma(1) = 0.731\) and the two add to 1.
3. Odds, log-odds, and the derivative
The odds are \(\frac{p}{1 - p}\). For the logistic model, \(1 - p = \frac{1}{1 + e^t}\), so
\[\frac{p}{1 - p} = e^{t}, \qquad \log\frac{p}{1 - p} = \beta_0 + \beta_1X.\]
The log-odds (logit) are linear in \(X\): that’s the sense in which logistic regression is “linear”. Increasing \(X\) by 1 adds \(\beta_1\) to the log-odds, so it multiplies the odds by \(e^{\beta_1}\). The change in \(p\) itself is not fixed: it depends on where you are on the S-curve.
The derivative (2023 Q3c, a “show that”).
\[\sigma'(t) = \frac{d}{dt}(1 + e^{-t})^{-1} = -(1 + e^{-t})^{-2}\cdot(-e^{-t}) = \frac{e^{-t}}{(1 + e^{-t})^2} = \underbrace{\frac{1}{1 + e^{-t}}}_{\sigma(t)}\cdot\underbrace{\frac{e^{-t}}{1 + e^{-t}}}_{1 - \sigma(t)}.\]
The last factor is \(1 - \sigma(t)\) because \(1 - \frac{1}{1 + e^{-t}} = \frac{e^{-t}}{1 + e^{-t}}\). So \(\boxed{\sigma'(t) = \sigma(t)\big(1 - \sigma(t)\big)}\). Check at \(t = 0\): \(0.5 \times 0.5 = 0.25\), the steepest point.
By the chain rule, \(\frac{dp}{dX} = p(1 - p)\beta_1\): biggest (\(0.25\beta_1\)) where \(p = 0.5\), tiny near 0 or 1.
(4 marks, 2023 Q3c.) Show that \(\sigma'(t) = \sigma(t)(1 - \sigma(t))\). Write it out, then reveal the model answer and mark yourself.
\(\sigma(t) = (1 + e^{-t})^{-1}\). By the chain rule, with outer function \(u^{-1}\) (slope \(-u^{-2}\)) and inner \(u = 1 + e^{-t}\) (slope \(-e^{-t}\)): \(\sigma'(t) = -(1 + e^{-t})^{-2}(-e^{-t}) = \frac{e^{-t}}{(1 + e^{-t})^2} = \frac{1}{1 + e^{-t}}\cdot\frac{e^{-t}}{1 + e^{-t}}\). The first factor is \(\sigma(t)\), and the second is \(\frac{(1 + e^{-t}) - 1}{1 + e^{-t}} = 1 - \frac{1}{1 + e^{-t}} = 1 - \sigma(t)\). Hence \(\sigma'(t) = \sigma(t)(1 - \sigma(t))\).
4. Predicting, and several inputs
With \(p\) inputs, put a 1 in front of \(x\) and write \(t = \tilde x^T\beta = \beta_0 + \sum_j\beta_jx_j\); then \(p(x) = \sigma(t)\). Predict class 1 when \(\sigma(t) \ge 0.5\), i.e. when \(t \ge 0\). So the decision boundary \(\beta_0 + \beta_1x_1 + \beta_2x_2 = 0\) is a straight line in the input plane.
Everything from week 2 carries over: dummy variables, polynomial features, penalties. \(\beta_j\) is the change in log-odds per unit of \(x_j\) holding the others fixed. The student paradox (ISLP 4.3.4): alone, student has a positive coefficient (students default more); with balance in the model it turns negative (at the same balance, students default less). Students carry bigger balances. Same story as stage 6’s newspaper example.
Worked example (2023 Q3e). \(\lambda = [1, 1, -1]\), \(x = [-\tfrac13, 1]\), \(P(Y = \text{green} \mid x) = \sigma(\lambda_0 + \lambda_1x_1 + \lambda_2x_2)\).
- \(t = 1 + 1 \cdot(-\tfrac13) + (-1)\cdot 1 = -\tfrac13\).
- \(\sigma(-\tfrac13) = \frac{1}{1 + e^{1/3}} = \frac{1}{1 + 1.3956} \approx 0.417\).
- Below 0.5, so predict red. (Shortcut: \(t < 0\) already tells you.)
Slides’ example: \(\hat\beta = (-0.5, 0.001)\), balance 1000: \(t = 0.5\), \(\hat p = 0.622\), predict default. Boundary at balance \(0.5/0.001 = 500\).
Stage test
What are the domain and the range of \(\sigma(t) = 1/(1 + e^{-t})\)?
Any real \(t\) works; \(e^{-t} > 0\) keeps \(\sigma\) strictly inside \((0, 1)\).
(4 marks.) Show that \(\sigma'(t) = \sigma(t)(1 - \sigma(t))\).
\(\sigma'(t) = -(1 + e^{-t})^{-2}\cdot(-e^{-t}) = \frac{e^{-t}}{(1 + e^{-t})^2} = \frac{1}{1 + e^{-t}}\cdot\frac{e^{-t}}{1 + e^{-t}} = \sigma(t)\big(1 - \sigma(t)\big)\), since \(1 - \sigma(t) = \frac{1 + e^{-t} - 1}{1 + e^{-t}} = \frac{e^{-t}}{1 + e^{-t}}\).
A logistic model gives \(p = 0.8\). What are the log-odds?
\(\log(0.8/0.2) = \log 4 \approx 1.386\).
\(\hat\beta = (-3, 0.5)\). At what \(x\) is \(\hat p = 0.9\)?
Need \(t = \log(0.9/0.1) = \log 9 = 2.197\). So \(-3 + 0.5x = 2.197\), \(x = 10.39\). (The 50% boundary is at \(x = 6\).)
True or false: in logistic regression, increasing \(x_1\) by 1 always increases \(p\) by \(\beta_1\).
\(\beta_1\) is the change in the log-odds. The change in \(p\) is about \(p(1 - p)\beta_1\), which depends on where you are.