---
title: Logistic regression
heading: Modeling a class probability with the logistic sigmoid of a linear function, log-odds and the odds ratio per unit of input, the cross-entropy error and its strikingly simple gradient, Newton's method for the weights (iteratively reweighted least squares), and the decision boundary
eyebrow: S14 · Lesson 16 · Logistic regression
xp: 50
---

::: hook
Fisher's discriminant and the perceptron draw a line but don't say how confident to be on each side. Logistic regression does: it passes a linear function of the input through the logistic sigmoid, which turns any real number into a probability, and fits the weights by maximizing the likelihood of the labels. Its gradient has the same form as linear regression's, prediction minus target times input, and it's the building block of every neural network's output layer for classification.
:::

::: play
- The predicted probability $\sigma(w_0 + w_1x)$ of the positive class against the input x.
- The decision boundary, where the probability crosses 1/2, is at $x = -w_0/w_1$. A larger $w_1$ makes the transition sharper.
- Each unit increase in x multiplies the odds $p/(1 - p)$ by $e^{w_1}$.
:::

```sim plot
view: graph
x: x = -4..8
y: p = 0..1.05
slider: a = -8..4 step 0.1 = -3
slider: b = 0.1..4 step 0.1 = 1.5
plot: 1/(1 + exp(-(a + b*x))) | samples: 400 | label: P(positive | x)
readout: decision boundary = -a/b | digits: 3
readout: odds ratio per unit x = exp(b) | digits: 4
readout: probability at x of 3 = 1/(1 + exp(-(a + 3*b))) | digits: 4
```

::: predict
## A logistic model has $w_0$ = −3 and $w_1$ = 1.5. What probability does it give the positive class at x = 3?

- [ ] 0.5
- [ ] 1.5
- [x] About 0.82
- [ ] About 0.95

::: reveal
The activation is −3 + 1.5 × 3 = 1.5, and $\sigma(1.5) = 1/(1 + e^{-1.5})$ = 0.818. The boundary, where the activation is 0, is at x = 2; one unit past it, the odds have grown by $e^{1.5}$ = 4.48 from even.
:::
:::

::: nameit
## The model

$$p(\mathcal{C}_1|\mathbf{x}) = \sigma(\mathbf{w}^T\boldsymbol{\phi}(\mathbf{x})), \qquad \sigma(a) = \frac{1}{1 + e^{-a}}$$

The activation a is the **log-odds**, $\ln\frac{p}{1 - p}$; increasing a feature by one multiplies the odds by $e^{w}$. The sigmoid's derivative is $\sigma(1 - \sigma)$.

## Fitting

With targets $t_n \in \{0, 1\}$ and predictions $y_n$, the negative log-likelihood is the cross-entropy

$$E(\mathbf{w}) = -\sum_n\left[t_n\ln y_n + (1 - t_n)\ln(1 - y_n)\right], \qquad \nabla E = \sum_n(y_n - t_n)\boldsymbol{\phi}_n$$

There's no closed-form minimum, but E is convex, so gradient descent or **Newton's method** finds the unique optimum. Newton's Hessian is $H = \sum_ny_n(1 - y_n)\boldsymbol{\phi}_n\boldsymbol{\phi}_n^T$, and each step is a weighted least-squares problem: **iteratively reweighted least squares** (IRLS).

## Separable data

If a hyperplane separates the classes perfectly, the likelihood keeps rising as $\|\mathbf{w}\| \to \infty$: the weights blow up and the probabilities become overconfident 0s and 1s. Regularization (a prior on w) keeps them finite.
:::

::: example
## For the model above and a training example at x = 3 with t = 1, find the error gradient and the Hessian contribution.

::: solution
1. **Prediction.** At x = 3 the model gives y = 0.818, so $y - t = -0.182$.
2. **Gradient.**
   $$\begin{aligned} \nabla E &= (y - t)(1, x) \\ &= (-0.182, -0.547) \end{aligned}$$
   Gradient descent raises both weights, pushing the probability at x = 3 toward 1.
3. **Hessian contribution.** The slope entry from this example is
   $$\begin{aligned} y(1 - y)x^2 &= 0.149 \times 9 \\ &= 1.34 \end{aligned}$$

**Answer:** the gradient is (−0.182, −0.547), and the example adds 1.34 to the Hessian's slope entry.
:::
:::

::: example
## A model predicts 0.9 for some input. What's the log-odds, and how far must x move (with $w_1$ = 1.5) to reach 0.5?

::: solution
1. **Log-odds.**
   $$\begin{aligned} a &= \ln(0.9/0.1) \\ &= 2.197 \end{aligned}$$
2. **Move x.** The activation must fall by 2.197, so x must decrease by
   $$2.197/1.5 = 1.46$$

**Answer:** the log-odds are 2.197, and x must decrease by 1.46.
:::
:::

::: example
## A three-class softmax model gives activations (2, 1, 0) for an example whose true class is the second. Find the probabilities, the loss, and the gradient of the loss with respect to the activations.

::: solution
1. **Probabilities.** Normalize the exponentials:
   $$e^2 + e + 1 = 11.11$$
   so the probabilities are 0.665, 0.245 and 0.090.
2. **Loss.**
   $$\begin{aligned} L &= -\ln 0.245 \\ &= 1.41\ \text{nats} \end{aligned}$$
3. **Gradient.** With respect to activation k it's $y_k - t_k$:
   $$\mathbf y - \mathbf t = (0.665, -0.755, 0.090)$$
4. **What it does.** It has the same simple form as logistic regression's: each activation moves in proportion to how far its probability is from the target, so training pulls the second activation up and pushes the first down hardest.

**Answer:** the probabilities are 0.665, 0.245 and 0.090, the loss is 1.41 nats, and the gradient is (0.665, −0.755, 0.090).
:::
:::

::: check
given: a = -2 | 0.5 | 3
answer: 1/(1 + exp(-a))
tolerance: 0.5%

## What's σ({a})?

::: right
Right. $1/(1 + e^{-a})$ = {answer}.
:::

::: misses
- [1/(1 + exp(a))] That's σ(−a), the other class.
- [any] Not yet. The logistic sigmoid.
:::
:::

::: check
given: p = 0.1 | 0.6 | 0.95
answer: ln(p/(1 - p))
tolerance: 0.5%

## A logistic model outputs {p}. What's its activation (the log-odds)?

::: right
Right. $\ln(p/(1 - p))$ = {answer}.
:::

::: misses
- [ln(p)] The log-odds uses the ratio $p/(1 - p)$.
- [any] Not yet. Invert the sigmoid.
:::
:::

::: check
given: a = -4 | -1; b = 0.5 | 2; x = 1 | 4; t = 0 | 1
answer: (1/(1 + exp(-(a + b*x))) - t)*x
tolerance: 0.5%

## A model with $w_0$ = {a} and $w_1$ = {b} sees an example at x = {x} with target {t}. What's the cross-entropy gradient with respect to $w_1$?

::: right
Right. $(y - t)x$ = {answer}.
:::

::: misses
- [(t - 1/(1 + exp(-(a + b*x))))*x] Check the sign: the gradient is (y − t)x.
- [any] Not yet. Prediction minus target, times the input.
:::
:::

::: check
given: a = -6 | -2 | 3; b = 0.5 | 2 | 4
answer: -a/b
tolerance: 0.5%

## Where is the decision boundary of a logistic model with $w_0$ = {a} and $w_1$ = {b}?

::: right
Right. $-w_0/w_1$ = {answer}.
:::

::: misses
- [a/b] The activation is zero at $x = -w_0/w_1$.
- [any] Not yet. Where the activation is zero.
:::
:::

::: check
given: b = 0.2 | 0.7 | 1.5; d = 1 | 2 | 5
answer: exp(b*d)
tolerance: 0.5%

## A logistic model has weight {b} on a feature. By what factor do the odds change when the feature increases by {d}?

::: right
Right. $e^{w\Delta x}$ = {answer}.
:::

::: misses
- [b*d] The log-odds change by $w\Delta x$; the odds multiply by its exponential.
- [any] Not yet. Odds ratio.
:::
:::

::: check
given: a = -3 | 0; b = 1 | 2; x = 1 | 3
answer: 1/(1 + exp(-(a + b*x)))*(1 - 1/(1 + exp(-(a + b*x))))*x^2
tolerance: 0.5%

## What does one example at x = {x} contribute to the Hessian's slope entry for a model with $w_0$ = {a}, $w_1$ = {b}?

::: right
Right. $y(1 - y)x^2$ = {answer}.
:::

::: misses
- [any] Not yet. The sigmoid's derivative times $x^2$.
:::
:::

::: check
kind: parts
given: a = -1 | 0.5; w = 0.5 | 1 | 2; x = 1.5 | 2 | 3; t = 0 | 1

## A logistic model with $w_0$ = {a} and $w_1$ = {w} sees one example at x = {x} with target {t}.

::: part
answer: (1/(1 + exp(-(a + w*x))) - t)*x
tolerance: 0.5%

### What is the gradient of the cross-entropy loss with respect to $w_1$?

::: misses
- [1/(1 + exp(-(a + w*x))) - t] Multiply by the input x.
- [any] $(y - t)x$.
:::
:::

::: part
answer: 1/(1 + exp(-(a + w*x)))*(1 - 1/(1 + exp(-(a + w*x))))*x^2
tolerance: 0.5%

### What is the second derivative with respect to $w_1$?

::: misses
- [any] $y(1 - y)x^2$.
:::
:::
:::

::: check
kind: derivation
vars: a = -3..3, w = -3..3, x = -3..3

## A logistic model has bias a and weight w; consider an input x.

::: step
answer: 1/(1 + exp(-(a + w*x)))

### What probability does it give class 1?
:::

::: step
answer: ln(1 + exp(-(a + w*x)))

### What is the cross-entropy loss if the target is 1?
:::

::: step
answer: ln(1 + exp(a + w*x))

### And if the target is 0?
:::

::: right
Right. Both losses are softplus functions of the activation, convex in the weights, which is why logistic regression has a single minimum.
:::
:::

::: explain
## Why do logistic regression's weights grow without limit on perfectly separable data?

::: model
Maximum likelihood rewards making every training example's predicted probability of its own class as close to 1 as possible. If some hyperplane separates the classes perfectly, scaling up the weights along it leaves the boundary where it is but makes the sigmoid steeper, pushing every correct probability closer to 1 and raising the likelihood a little more each time. Nothing stops the process, so the weights diverge and the model becomes certain of everything near the boundary, which is badly overconfident on new data. Adding a penalty on the weights, equivalently a Gaussian prior, makes the optimum finite.
:::
:::

::: practice

::: check
given: a = -5 | -2 | -1 | 0 | 1 | 2 | 5
answer: 1/(1 + exp(-a))
tolerance: 0.5%

## σ({a})?

::: misses
- [any] $1/(1 + e^{-a})$.
:::
:::

::: check
given: p = 0.02 | 0.1 | 0.3 | 0.7 | 0.9 | 0.98
answer: ln(p/(1 - p))
tolerance: 0.5%

## Log-odds of {p}?

::: misses
- [any] $\ln(p/(1 - p))$.
:::
:::

::: check
given: a = -10 | 6; b = 0.3 | 5
answer: -a/b
tolerance: 0.5%

## Boundary: $w_0$ = {a}, $w_1$ = {b}?

::: misses
- [any] $-w_0/w_1$.
:::
:::
:::
