---
title: Decision theory
heading: Turning posterior probabilities into decisions, minimizing the probability of error, the Bayes decision boundary between two Gaussian classes and how priors shift it, unequal costs and the decision threshold they set, the Bayes error, and the best predictor under squared and absolute loss
eyebrow: S14 · Lesson 3 · Decision theory
xp: 50
---

::: hook
A classifier that outputs "faulty with probability 0.3" hasn't made a decision yet. Whether to stop the production line depends on what a missed fault costs against what a false alarm costs, and on how common faults are. Decision theory separates the two jobs cleanly: inference estimates the probabilities, and a decision rule, set by the costs, turns them into actions. S8 used the same idea to choose a receiver's threshold; here it's general.
:::

::: play
- Two classes with Gaussian densities of unit variance, centered at 0 and 2, weighted by their prior probabilities (class 1 has prior P). The decision boundary where the weighted densities cross moves away from the more probable class.
- The shaded probability of error is the area of each weighted density on the wrong side of the boundary.
- With equal priors the boundary is the midpoint, and the error is Q(1) = 0.159.
:::

```sim plot
view: graph
x: x = -3..5
y: p = 0..0.4
slider: P = 0.05..0.95 step 0.01 = 0.8
plot: P*exp(-x^2/2)/sqrt(2*pi) | samples: 400 | label: class 1 × prior
plot: (1 - P)*exp(-(x - 2)^2/2)/sqrt(2*pi) | samples: 400 | color: ch3 | label: class 2 × prior
marker: x = 1 + ln(P/(1 - P))/2 | label: boundary
readout: boundary = 1 + ln(P/(1 - P))/2 | digits: 3
readout: probability of error = P*Q(1 + ln(P/(1 - P))/2) + (1 - P)*Q(1 - ln(P/(1 - P))/2) | digits: 4
```

::: predict
## Two equally likely classes have unit-variance Gaussian densities centered 2 apart. What's the lowest possible error rate for deciding between them from one measurement?

- [ ] 0
- [ ] 0.023
- [x] 0.159
- [ ] 0.5

::: reveal
The best rule picks the class with the larger posterior, which puts the boundary at the midpoint, one standard deviation from each mean. Each class then spills past the boundary with probability Q(1) = 0.159. No classifier using this measurement can do better: that's the Bayes error, set by how much the classes overlap.
:::
:::

::: nameit
## Minimum error

To minimize the probability of a wrong decision, choose the class with the largest posterior $p(\mathcal{C}_k|x) \propto p(x|\mathcal{C}_k)P(\mathcal{C}_k)$. For two Gaussian classes with equal variance σ², means $\mu_1 < \mu_2$ and priors $P_1$, $P_2$, the boundary is

$$x^* = \frac{\mu_1 + \mu_2}{2} + \frac{\sigma^2}{\mu_2 - \mu_1}\ln\frac{P_1}{P_2}$$

and the error is $P_1Q\left(\frac{x^* - \mu_1}{\sigma}\right) + P_2Q\left(\frac{\mu_2 - x^*}{\sigma}\right)$, the **Bayes error**.

## Unequal costs

With a cost $c_M$ for missing a positive and $c_F$ for a false alarm, deciding positive has expected cost $c_F(1 - p)$ and deciding negative $c_Mp$, where p is the posterior of the positive class. So decide positive when

$$p > \frac{c_F}{c_F + c_M}$$

A rare but expensive miss drives the threshold far below 0.5.

## Regression

Under squared loss the best prediction is the conditional mean $E[t|x]$; under absolute loss, the conditional median.
:::

::: example
## The sim's classes (means 0 and 2, σ = 1) with priors 0.8 and 0.2: where's the boundary and what's the error?

::: solution
1. **Boundary.**
   $$\begin{aligned} x^* &= 1 + \frac{1}{2}\ln 4 \\ &= 1.693 \end{aligned}$$
2. **Error.**
   $$\begin{aligned} P_e &= 0.8\,Q(1.693) \\ &\quad + 0.2\,Q(0.307) \\ &= 0.112 \end{aligned}$$
3. **Compare equal priors.** That's lower than the 0.159 of the same classes with equal priors: the more common class gets more of the axis.

**Answer:** the boundary is at x = 1.693, and the error is 0.112.
:::
:::

::: example
## A screening test's missed fault costs 1000 times as much as a false alarm. Above what posterior should it flag?

::: solution
1. **Threshold.**
   $$\begin{aligned} \frac{c_F}{c_F + c_M} &= \frac{1}{1001} \\ &\approx 0.001 \end{aligned}$$
   so flag anything with even a 0.1% chance of a fault.
2. **Compare an accuracy-trained classifier.** A classifier trained for accuracy, thresholding at 0.5, would miss almost all of them.

**Answer:** it should flag above a posterior of about 0.001.
:::
:::

::: example
## The sim's equally likely classes (means 0 and 2, σ = 1) are classified only when the larger posterior is at least 0.8; otherwise the system rejects and asks a person. Which inputs are rejected, how often, and what's the error rate on the rest?

::: solution
1. **Log-odds.** The log-odds of class 2 are
   $$2x - 2$$
2. **Reject band.** Both posteriors stay below 0.8 while $|2x - 2| < \ln 4$, so the band is
   $$0.307 < x < 1.693$$
3. **How often.** Under either class that band holds
   $$\Phi(1.693) - \Phi(0.307) = 33.4\%$$
   of the inputs.
4. **Errors left.** They are the far tails:
   $$\begin{aligned} P_e &= \tfrac12[1 - \Phi(1.693)] \\ &\quad + \tfrac12\Phi(-1.693) \\ &= 4.5\% \end{aligned}$$
   of all inputs, 6.8% of those accepted, against the 15.9% Bayes error without rejection.
5. **The trade.** Rejecting a third of the cases cuts the error rate by more than half, the trade a reject option offers.

**Answer:** inputs between 0.307 and 1.693 are rejected, 33.4% of all inputs, and the error rate on the rest is 6.8% (4.5% of all inputs).
:::
:::

::: check
given: c = 1 | 5 | 20; m = 10 | 100 | 1000
answer: c/(c + m)
tolerance: 0.5%

## A false alarm costs {c} and a miss costs {m}. Above what posterior probability of the positive class should you decide positive?

::: right
Right. $c_F/(c_F + c_M)$ = {answer}.
:::

::: misses
- [m/(c + m)] That's the threshold with the costs swapped: a costly miss should lower the threshold.
- [any] Not yet. Compare expected costs.
:::
:::

::: check
given: a = 0 | 1; d = 1 | 2 | 4; s = 0.5 | 1; P = 0.3 | 0.7 | 0.9; let b = a + d
answer: a + d/2 + s^2/d*ln(P/(1 - P))
tolerance: 0.5%

## Class 1 has mean {a} and class 2 mean {b}, both Gaussian with σ = {s}; class 1's prior is {P}. Where's the minimum-error boundary?

::: right
Right. $\frac{\mu_1 + \mu_2}{2} + \frac{\sigma^2}{\mu_2 - \mu_1}\ln\frac{P_1}{P_2}$ = {answer}.
:::

::: misses
- [a + d/2] That's the boundary for equal priors.
- [any] Not yet. Equate the prior-weighted densities.
:::
:::

::: check
given: d = 1 | 2 | 3 | 4
answer: Q(d/2)
tolerance: 1%

## Two equally likely classes have unit-variance Gaussian densities whose means are {d} apart. What's the Bayes error?

::: right
Right. $Q(d/2)$ = {answer}.
:::

::: misses
- [Q(d)] The boundary is halfway: each mean is d/2 from it.
- [any] Not yet. Midpoint boundary.
:::
:::

::: check
given: P = 0.6 | 0.8 | 0.95; d = 1 | 2 | 3
answer: P*Q(d/2 + ln(P/(1 - P))/d) + (1 - P)*Q(d/2 - ln(P/(1 - P))/d)
tolerance: 1%

## Unit-variance classes {d} apart have priors {P} and 1 − {P}. What's the Bayes error?

::: right
Right. {answer}.
:::

::: misses
- [Q(d/2)] That ignores the priors; the boundary shifts toward the rarer class.
- [any] Not yet. Shift the boundary by $\ln(P_1/P_2)/d$, then add both tails.
:::
:::

::: check
given: p = 0.05 | 0.2 | 0.5; c = 1 | 3; m = 10 | 50
answer: (c*(1 - p) + m*p - abs(c*(1 - p) - m*p))/2
tolerance: 0.5%

## The posterior of a fault is {p}. A false alarm costs {c} and a miss {m}. What's the expected cost of the better decision?

::: right
Right. The smaller of $c_F(1 - p)$ and $c_Mp$: {answer}.
:::

::: misses
- [any] Not yet. Compute both expected costs and take the smaller.
:::
:::

::: check
given: a = 1 | 2; b = 3 | 5; c = 10 | 20
answer: (a + b + c)/3
tolerance: 0.5%

## Given an input, the target is equally likely to be {a}, {b} or {c}. What prediction minimizes the expected squared error?

::: right
Right. The conditional mean: {answer}.
:::

::: misses
- [b] The median minimizes absolute error; squared error wants the mean.
- [any] Not yet. Minimize $E[(t - y)^2]$.
:::
:::

::: check
kind: parts
given: d = 1 | 2 | 3; h = 0.7 | 0.8 | 0.9 | 0.95

## Two equally likely unit-variance Gaussian classes have means {d} apart. The classifier rejects whenever the larger posterior is below {h}.

::: part
answer: ln(h/(1 - h))/d
tolerance: 0.5%

### How far on each side of the midpoint does the reject band reach?

::: misses
- [ln(h/(1 - h))/(2*d)] The log-odds change by d per unit of x.
- [any] Log-odds $d(x - d/2)$ reach $\ln(\theta/(1 - \theta))$.
:::
:::

::: part
answer: 0.5*erfc(-(d/2 + ln(h/(1 - h))/d)/sqrt(2)) - 0.5*erfc(-(d/2 - ln(h/(1 - h))/d)/sqrt(2))
tolerance: 0.5%

### What fraction of inputs does it reject?

::: misses
- [any] $\Phi(d/2 + h) - \Phi(d/2 - h)$ for half-width h, the same under either class.
:::
:::
:::

::: check
kind: derivation
vars: d = 0.2..5, P = 0.05..0.95

## Class 1 is Gaussian at 0 and class 2 at d, both with unit variance; class 1 has prior P.

::: step
answer: d/2 + ln(P/(1 - P))/d

### Where is the minimum-error boundary?
:::

::: step
answer: P*0.5*erfc((d/2 + ln(P/(1 - P))/d)/sqrt(2)) + (1 - P)*0.5*erfc((d/2 - ln(P/(1 - P))/d)/sqrt(2))

### What is the Bayes error?
:::

::: right
Right. A larger prior pushes the boundary toward the other class's mean.
:::
:::

::: explain
## Why should a classifier's outputs be probabilities rather than just class labels?

::: model
The right decision depends on things the classifier can't know when it's trained: the costs of different mistakes, which vary from one use to another, and the class priors in the population where it's deployed, which can differ from the training set. A classifier that outputs only a label has fixed its threshold for one particular set of costs and priors. One that outputs calibrated posterior probabilities lets the threshold be set afterward, from the costs, and lets the priors be corrected by reweighting, without retraining. It also allows a reject option: when no class's probability is high enough, defer to a human or another test.
:::
:::

::: practice

::: check
given: c = 0.5 | 50; m = 2 | 5000
answer: c/(c + m)
tolerance: 0.5%

## Threshold: false alarm {c}, miss {m}?

::: misses
- [any] $c_F/(c_F + c_M)$.
:::
:::

::: check
given: d = 0.5 | 1 | 2 | 3 | 4 | 6
answer: Q(d/2)
tolerance: 1%

## Bayes error, means {d} apart, equal priors, σ = 1?

::: misses
- [any] $Q(d/2)$.
:::
:::

::: check
given: a = -1 | 2; d = 0.5 | 3; s = 0.3 | 2; P = 0.1 | 0.6; let b = a + d
answer: a + d/2 + s^2/d*ln(P/(1 - P))
tolerance: 0.5%

## Boundary: μ₁ = {a}, μ₂ = {b}, σ = {s}, P₁ = {P}?

::: misses
- [any] Midpoint plus $\frac{\sigma^2}{d}\ln(P_1/P_2)$.
:::
:::
:::
