---
title: Detection and decision thresholds
heading: Choosing the threshold that makes the fewest mistakes
eyebrow: A11 · Lesson 42 · Detection
xp: 40
---

::: hook
An optical receiver sees either no light (a 0) or a pulse of 1 V (a 1), plus noise. Where should it set the threshold between them? Halfway seems obvious, and it's right if 0s and 1s are equally common. If 1s are rare, the best threshold moves up. A radar faces the same choice with no way to know how often targets appear, so it fixes how many false alarms it can live with and catches as many targets as that allows. Both are applications of Bayes' rule (lesson 5) to a continuous measurement, and both give error rates you can compute with the Q function.
:::

::: play
- The two curves are the received voltage's density for a sent 0 and a sent 1, each weighted by how often that symbol is sent. Noise has standard deviation σ.
- Drag the threshold $t$. The shaded areas are the two kinds of mistake; the readout adds them into the error rate.
- Make 1s rare. Where do the weighted curves cross now? Put the threshold there and compare the error rate with the halfway threshold.
:::
:::

```sim plot
view: graph
x: r = -1..2 V
y: p = 0..2
slider: s = 0.1..0.4 step 0.01 = 0.2 V
slider: q = 0.05..0.95 step 0.05 = 0.5
slider: t = -0.2..1.2 step 0.01 = 0.5 V
plot: (1 - q)*exp(-r^2/(2*s^2))/(s*sqrt(2*pi)) | label: sent 0, weighted | fill: t..2
plot: q*exp(-(r - 1)^2/(2*s^2))/(s*sqrt(2*pi)) | label: sent 1, weighted | color: ch2 | fill: -1..t
marker: x = t | label: threshold
readout: error rate at t = (1 - q)*Q(t/s) + q*Q((1 - t)/s) | digits: 4
readout: best threshold, V = 0.5 + s^2*ln((1 - q)/q) | digits: 3
readout: error rate at the best threshold = (1 - q)*Q((0.5 + s^2*ln((1 - q)/q))/s) + q*Q((0.5 - s^2*ln((1 - q)/q))/s) | digits: 4
```

::: predict
## A receiver gets 0 V or 1 V plus noise of σ = 0.2 V. Only 10% of the bits are 1s. Where should the threshold go?

- [ ] At 0.5 V, halfway
- [x] A little above 0.5 V
- [ ] A little below 0.5 V
- [ ] At 0.1 V

::: reveal
A little above, at about 0.59 V. Since 0s are nine times as common, a reading near the middle is more likely to be a 0 with extra noise than a 1 with less. Moving the threshold up trades a few more missed 1s for many fewer 0s misread as 1s, and the error rate falls from 0.62% to 0.34%.
:::
:::

::: nameit
## Decisions and their errors

A receiver measures $r$ and decides between hypotheses $H_0$ (a 0 was sent, no target) and $H_1$ (a 1, a target). Its two kinds of mistake:

- **false alarm:** deciding $H_1$ when $H_0$ is true, probability $P_\text{FA}$;
- **miss:** deciding $H_0$ when $H_1$ is true, probability $P_\text{M}$; the **detection probability** is $P_\text{D} = 1 - P_\text{M}$.

These are the type I and type II errors of lesson 38. With priors $P_0$ and $P_1$, the overall error rate is $P_e = P_0P_\text{FA} + P_1P_\text{M}$.

## The minimum-error rule

To make $P_e$ as small as possible, decide whichever hypothesis has the larger posterior. By Bayes' rule that means comparing $P_1\,p(r \mid H_1)$ with $P_0\,p(r \mid H_0)$, the weighted curves in the sim. Equivalently, decide $H_1$ when the **likelihood ratio** exceeds the prior odds against it:

$$\Lambda(r) = \frac{p(r \mid H_1)}{p(r \mid H_0)} > \frac{P_0}{P_1}$$

With equal priors this is **maximum likelihood**: pick the hypothesis that best explains $r$.

## On-off signaling in Gaussian noise

For 0 or $A$ plus Gaussian noise σ, the rule becomes a threshold on $r$:

$$t = \frac{A}{2} + \frac{\sigma^2}{A}\ln\frac{P_0}{P_1}$$

With equal priors, $t = \frac{A}{2}$ and $P_e = Q\left(\frac{A}{2\sigma}\right)$. Antipodal signaling, $\pm A$, puts the levels $2A$ apart and gives $P_e = Q\left(\frac{A}{\sigma}\right)$, the result of lesson 41.

## Neyman–Pearson: fixed false alarms

A radar can't know how often targets appear, and false alarms have a cost of their own. So it fixes $P_\text{FA}$ and maximizes $P_\text{D}$. For a target of amplitude $A$ in Gaussian noise, set the threshold at $t = z\sigma$ where $Q(z) = P_\text{FA}$; then

$$P_\text{D} = Q\left(z - \frac{A}{\sigma}\right)$$

Plotting $P_\text{D}$ against $P_\text{FA}$ as the threshold moves gives the **receiver operating characteristic** (ROC), the standard picture of a detector's quality.
:::

::: example
## A receiver gets 0 or 1 V with noise σ = 0.2 V, and 10% of the bits are 1s. Find the best threshold and compare its error rate with a threshold at 0.5 V.

::: solution
1. **Best threshold.**
   $$\begin{aligned} t &= 0.5 + 0.04\ln 9 \\ &= 0.588\ \text{V} \end{aligned}$$
2. **Error rate there.**
   $$\begin{aligned} P_e &= 0.9\,Q(0.588/0.2) \\ &\quad + 0.1\,Q(0.412/0.2) \\ &= 0.9\,Q(2.94) + 0.1\,Q(2.06) \\ &= 0.00148 + 0.00197 \\ &= 0.00345 \end{aligned}$$
3. **At 0.5 V.** Both tails are $Q(2.5) = 0.00621$, so $P_e = 0.00621$.
4. **Compare.** The better threshold cuts errors by 44%.

**Answer:** the best threshold is 0.588 V, with an error rate of 0.00345 against 0.00621 at 0.5 V.
:::
:::

::: example
## A radar wants $P_\text{FA} = 10^{-6}$ per look and $P_\text{D} = 0.9$. What signal-to-noise amplitude ratio does it need?

::: solution
1. **Threshold from the false-alarm rate.**
   $$Q(z) = 10^{-6} \quad\text{gives}\quad z = 4.75$$
2. **Detection.** For $P_\text{D} = 0.9$, $Q(z - A/\sigma) = 0.9$ means
   $$z - A/\sigma = -1.28$$
3. **Solve for the ratio.**
   $$\begin{aligned} \frac{A}{\sigma} &= 4.75 + 1.28 \\ &= 6.03 \end{aligned}$$
   or 15.6 dB.
4. **The cost.** Low false alarm rates are expensive: the threshold has to sit almost five noise widths up.

**Answer:** it needs $A/\sigma = 6.03$, or 15.6 dB.
:::
:::

::: example
## Bits of 0 and 1 V are equally likely, with noise σ = 0.2 V. What threshold minimizes errors, and what error rate results?

::: solution
1. **Threshold.** With equal priors and equal noise, the best threshold is halfway, 0.5 V.
2. **Distance in standard deviations.** Each symbol is 0.5 V from it:
   $$\frac{0.5}{0.2} = 2.5$$
3. **Error rate.**
   $$\begin{aligned} P_e &= Q(2.5) \\ &= 0.0062 \end{aligned}$$
4. **A better signal.** On–off signaling wastes power compared with ±0.5 V, which gives the same distance between levels with half the average power.

**Answer:** the threshold is 0.5 V, and the error rate is 0.0062.
:::
:::

::: check
given: A = 1 | 2; s = 0.2 | 0.4
answer: Q(A/(2*s))
tolerance: 1%

## On-off signaling uses 0 and {A} V, with Gaussian noise σ = {s} V and equal priors. What is the error rate with the best threshold?

::: right
Right. The threshold sits at $A/2$, so $P_e = Q\left(\frac{A}{2\sigma}\right)$ = {answer}.
:::

::: misses
- [Q(A/s)] The noise only has to cover half the gap, $A/2$.
- [2*Q(A/(2*s))] Each symbol is sent half the time: $P_e = \frac12Q + \frac12Q$.
- [any] Not yet. Threshold at $A/2$; each error needs noise of at least $A/2$.
:::
:::

::: check
given: A = 1 | 2; s = 0.2 | 0.3; q = 0.1 | 0.2
answer: A/2 + s^2/A*ln((1 - q)/q)
unit: V
tolerance: 0.5%

## On-off signaling with levels 0 and {A} V, noise σ = {s} V, and a fraction {q} of 1s. Where is the minimum-error threshold?

::: right
Right. $\frac{A}{2} + \frac{\sigma^2}{A}\ln\frac{P_0}{P_1}$ = {answer}.
:::

::: misses
- [A/2] That's for equal priors. Rare 1s push the threshold up.
- [A/2 - s^2/A*ln((1 - q)/q)] The threshold moves away from the more common symbol, so up here.
- [any] Not yet. Compare $P_1p(r \mid 1)$ with $P_0p(r \mid 0)$.
:::
:::

::: check
given: q = 0.1 | 0.25 | 0.4
answer: (1 - q)/q
tolerance: 0.5%

## 1s make up a fraction {q} of the bits. A minimum-error receiver decides 1 when the likelihood ratio exceeds what?

::: right
Right. The prior odds $\frac{P_0}{P_1}$ = {answer}.
:::

::: misses
- [q/(1 - q)] Upside down: rare 1s need stronger evidence.
- [1] That's maximum likelihood, right only for equal priors.
- [any] Not yet. $\Lambda(r) > P_0/P_1$.
:::
:::

::: check
given: A = 0.5 | 1; s = 0.2 | 0.25
answer: Q(A/s)
tolerance: 1%

## Antipodal signaling uses ±{A} V with noise σ = {s} V and equal priors. What is the error rate?

::: right
Right. $Q\left(\frac{A}{\sigma}\right)$ = {answer}.
:::

::: misses
- [Q(A/(2*s))] The levels are $2A$ apart, so the noise must cover $A$.
- [any] Not yet. Threshold at 0; an error needs noise beyond $A$.
:::
:::

::: check
given: z = 3.09 | 4.26; r = 4 | 5
answer: Q(z - r)
tolerance: 1%

## A detector sets its threshold at {z}σ. A target arrives with amplitude {r}σ. What is the detection probability?

::: right
Right. $Q(z - A/\sigma)$ = {answer}.
:::

::: misses
- [Q(z)] That's the false alarm rate.
- [1 - Q(z - r)] That's the miss probability.
- [any] Not yet. The target's reading is Gaussian around $A$; how much lies above the threshold?
:::
:::

::: check
given: t = 2 | 3 | 3.5
answer: Q(t)
tolerance: 1%

## A detector's threshold is {t}σ above the noise's mean. What is its false alarm rate?

::: right
Right. $Q(t)$ = {answer}.
:::

::: misses
- [2*Q(t)] Only the upper tail triggers an alarm.
- [any] Not yet. Noise alone has to exceed the threshold.
:::
:::

::: check
kind: parts
given: A = 1 | 2; s = 0.2 | 0.3

## On–off bits of 0 and {A} V, equally likely, with noise {s} V.

::: part
answer: A/2
unit: V
tolerance: 0.5%

### Where is the best threshold?

::: misses
- [any] Halfway, for equal priors and equal noise.
:::
:::

::: part
answer: Q(A/(2*s))
tolerance: 0.5%

### What is the error rate?

::: misses
- [Q(A/s)] Each level is only $A/2$ from the threshold.
- [any] $Q\left(\frac{A}{2\sigma}\right)$.
:::
:::
:::

::: check
kind: derivation
vars: A = 0.5..2, s = 0.1..0.5, p = 0.05..0.5

## Levels 0 and $A$ with noise $s$, where the 1 has prior probability $p$.

::: step
answer: A/2 + s^2/A*ln((1 - p)/p)

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

::: right
Right. The rarer symbol gets a smaller region: the threshold moves toward it.
:::
:::

::: check
kind: python

## Levels 0 and 1 V, noise σ = 0.2 V, and a 1 sent with probability 0.1. Search thresholds from 0.3 to 0.9 V in steps of 0.0001 for the lowest error rate $0.9Q(T/\sigma) + 0.1Q((1 - T)/\sigma)$, and set `T_best`; set `T_formula` to $\frac12 + \frac{\sigma^2}{A}\ln\frac{1 - p}{p}$.

```python starter
import math

T_best = T_formula = 0.5
print(T_best, T_formula)
```

```python solution
import math

Q = lambda x: 0.5 * math.erfc(x / math.sqrt(2))
s = 0.2
Pe = lambda T: 0.9 * Q(T / s) + 0.1 * Q((1 - T) / s)
Ts = [0.3 + 0.0001 * k for k in range(6001)]
T_best = min(Ts, key=Pe)
T_formula = 0.5 + s**2 * math.log(0.9 / 0.1)
print(T_best, T_formula)
```

```python tests
import math
assert abs(T_formula - (0.5 + 0.04 * math.log(9))) < 1e-12, "The formula gives 0.5879 V."
assert abs(T_best - T_formula) < 0.001, f"The search should land on 0.588 V; got {T_best}."
```

::: right
Right. 0.588 V: the threshold moves toward the rarer 1, so that fewer common 0s are misread.
:::
:::

::: explain
## Why should the threshold move toward the less common symbol's level?

::: model
A reading near the middle could be either symbol pushed there by noise. The question is which is more likely, and that depends on how often each is sent as well as on the noise. If 0s are much more common, there are many more 0s being pushed up toward the middle than 1s being pushed down. So a middling reading is more probably a 0, and the boundary where the two explanations are equally likely, the best threshold, sits closer to the 1 level.
:::
:::

::: practice

::: check
given: A = 1 | 3; s = 0.25 | 0.5
answer: A/(2*s)
tolerance: 0.5%

## On-off levels 0 and {A} V, noise σ = {s} V. How many σ is the halfway threshold from each level?

::: misses
- [A/s] Half the gap.
- [any] $\frac{A}{2\sigma}$.
:::
:::

::: check
given: pfa = 0.01 | 0.001; pm = 0.05 | 0.1; q = 0.3 | 0.5
answer: (1 - q)*pfa + q*pm
tolerance: 0.5%

## A receiver has false alarm rate {pfa} and miss rate {pm}; a fraction {q} of bits are 1s. What is its overall error rate?

::: misses
- [any] $P_0P_\text{FA} + P_1P_\text{M}$.
:::
:::

::: check
given: A = 1 | 2 | 4
answer: A/sqrt(2)
unit: V
tolerance: 0.5%

## On-off signaling with levels 0 and {A} V and equal priors has average power $A^2/2$ (into 1 Ω). What antipodal amplitude ±$A'$ uses the same average power?

::: misses
- [A/2] Match powers: $A'^2 = A^2/2$.
- [any] $A' = A/\sqrt2$.
:::
:::
:::
