B4B33RPZ · Pattern Recognition and Machine Learning

Minimax

Lab 3: decisions without priors, the minimax strategy, and how it relates to Bayes
Klára JanouškováFEE CTU, Prague

Test next week

Test 1: 16. 10. (next week), during your lab
  • Topics: Bayesian and non-Bayesian decision making: problems and theory
  • Can't come? Let us know in advance.
  • No minimum score to pass (well, the minimum is zero :)
  • Bring a calculator. Nothing else is allowed: no laptop, no notes, no phone.
Bonus tasks
  • Presented in person after the labs
  • You explain the task and convince us that your solution is correct
Part I

Recap

Warm-up quiz: Bayes from lab 2

0/1 loss, p(A∣x)=0.3p(A \mid x) = 0.3, p(C∣x)=0.7p(C \mid x) = 0.7. The Bayesian strategy says…

With the 0/1 loss, q∗(x)=argmax⁡kp(k∣x)q^*(x) = \operatorname{argmax}_k p(k \mid x): the most probable class.

To compute the Bayesian risk R(q)R(q) we need…

R(q)R(q) sums p(x,k) W(k,q(x))p(x, k)\, W(k, q(x)) and p(x,k)=p(x∣k) p(k)p(x, k) = p(x \mid k)\, p(k). Today: no priors.

σA=2\sigma_A = 2, σC=0.5\sigma_C = 0.5, equal priors. Far out in the tails we decide…

The wider Gaussian decays more slowly. Narrow C wins only near its mean: an interval [t1,t2][t_1, t_2].

The Bayesian decision problem

XXobservations: what we know
KKhidden states: what we don't know
DDdecisions
pXKp_{XK}joint probability X×K→RX \times K \to \mathbb{R}
WWloss K×D→RK \times D \to \mathbb{R}
qqstrategy X→DX \to D

Risk of a strategy: the average loss

R(q)=∑x∈X∑k∈KpXK(x,k) W(k,q(x))R(q) = \sum_{x \in X} \sum_{k \in K} p_{XK}(x, k)\, W(k, q(x))

Bayesian strategy: the smallest risk

q∗=argmin⁡q ⁣:X→DR(q)q^* = \operatorname*{argmin}_{q\colon X \to D} R(q)
Many things simplify with the 0/1 loss: R(q)R(q) = probability of a mistake, q∗q^* = most probable class.

Name the parts: two more examples

Medical diagnosisSelf-driving car: pedestrian?
XXtest results, symptoms (fever, blood values)camera and sensor readings
KKhas the disease / healthypedestrian present / not
DDtreat / don't treat / more testsbrake / keep driving
WWuntreated sick patient: very large; treated healthy one: moderatemissed pedestrian: huge; needless braking: small
qq"treat if p(disease∣x)>0.2p(\text{disease} \mid x) > 0.2""brake if p(pedestrian∣x)>0.05p(\text{pedestrian} \mid x) > 0.05"

Question: is p(pedestrian)p(\text{pedestrian}) the same outside a school at 8:00 and on a motorway at 3:00? And who sets the price of a missed pedestrian in CZK?

Bayesian strategy with a reject option

Question: the classifier may answer "not known" for a fixed price ε\varepsilon. When should it?

D=K∪{not known}D = K \cup \{\text{not known}\}, loss

W(k,d)={0d=k1d≠k, d≠not knownεd=not knownW(k, d) = \begin{cases} 0 & d = k \\ 1 & d \ne k,\ d \ne \text{not known} \\ \varepsilon & d = \text{not known} \end{cases}

↓ As in lab 2, decide each xx on its own: minimise the partial risk R(x,d)=∑kp(k∣x) W(k,d)R(x, d) = \sum_k p(k \mid x)\, W(k, d).

q∗(x)=argmin⁡d∈D∑k∈KpK∣X(k∣x) W(k,d)q^*(x) = \operatorname*{argmin}_{d \in D} \sum_{k \in K} p_{K|X}(k \mid x)\, W(k, d)

↓ Answer a class d=kd = k: we pay 1 whenever the truth is another class.

R(x,k)=1−pK∣X(k∣x)R(x, k) = 1 - p_{K|X}(k \mid x)

↓ Answer "not known": we pay ε\varepsilon whatever the truth, and ∑kp(k∣x)=1\sum_k p(k \mid x) = 1.

R(x,not known)=εR(x, \text{not known}) = \varepsilon

↓ The best class costs 1−max⁡kp(k∣x)1 - \max_k p(k \mid x); take it only if that is cheaper than ε\varepsilon.

q∗(x)={argmax⁡kpK∣X(k∣x)if 1−max⁡kpK∣X(k∣x)<εnot knownotherwiseq^*(x) = \begin{cases} \operatorname*{argmax}_{k} p_{K|X}(k \mid x) & \text{if } 1 - \max_{k} p_{K|X}(k \mid x) < \varepsilon \\ \text{not known} & \text{otherwise} \end{cases}
Part II

Non-Bayesian decision problems

When can't we use the Bayesian formulation?

Question: Bayes needs the priors p(k)p(k) and the loss WW. What if we don't have them?

  • Priors drift: flu cases next winter, pedestrians at 8:00 vs. 3:00. The world changes, independently of us.
  • Priors react to us: a spammer sends more of what gets through our filter. The prior depends on our own qq, so no estimate stays valid.
  • Losses not comparable: no agreed exchange rate between the two mistakes
    • missed tumour vs. unnecessary biopsy
    • convicting an innocent vs. freeing a guilty person
    • deleting a legitimate post vs. leaving a harmful one

What we can still compute without priors: how often each class is misclassified, the class error

ϵ(k)=∑x ⁣:q(x)≠kp(x∣k)\epsilon(k) = \sum_{x\colon q(x) \ne k} p(x \mid k)

↑ Bayes decided per xx with p(k∣x)=p(x∣k) p(k)/p(x)p(k \mid x) = p(x \mid k)\,p(k)/p(x), which needs p(k)p(k). Here we fix the true class kk and ask how often we are wrong: only p(x∣k)p(x \mid k).

Non-Bayesian problems: choose qq by a rule on the ϵ(k)\epsilon(k), not on ∑kp(k) ϵ(k)\sum_k p(k)\,\epsilon(k).

Three non-Bayesian formulations

Neyman–Pearson

1 = dangerous, 2 = normal (smoke detector: fire / no fire). Missing a danger is ϵ(1)\epsilon(1), a false alarm is ϵ(2)\epsilon(2).

q∗=argmin⁡q ⁣:X→K ϵ(2)⏟false alarmsq^* = \operatorname*{argmin}_{q\colon X \to K}\ \underbrace{\epsilon(2)}_{\text{false alarms}}
s.t. ϵ(1)⏟missed dangers≤ϵˉ1\text{s.t.}\ \underbrace{\epsilon(1)}_{\text{missed dangers}} \le \bar\epsilon_1

Limit missed dangers, then minimise false alarms. The limit alone is met by "always alarm" (ϵ(1)=0\epsilon(1) = 0), useless.

Minimax

Unknown mix (face unlock: how often impostors try is unknown). Make the worst class error as small as possible. In biometrics this is the equal error rate, the usual way to compare systems.

q∗=argmin⁡q ⁣:X→Kmax⁡k∈Kϵ(k)q^* = \operatorname*{argmin}_{q\colon X \to K} \max_{k \in K} \epsilon(k)
ϵ(k)=∑x ⁣:q(x)≠kp(x∣k)\epsilon(k) = \sum_{x\colon q(x) \ne k} p(x \mid k)

Whatever the mix of classes, the total error is at most max⁡kϵ(k)\max_k \epsilon(k).

Wald

A reject option without priors or prices. Both errors must stay below ϵ\epsilon; what we cannot decide safely goes to a human (a cheque reader passes unclear amounts to a clerk). Send the clerk as little as possible.

q∗=argmin⁡q ⁣:X→Dmax⁡iκiq^* = \operatorname*{argmin}_{q\colon X \to D} \max_{i} \kappa_i
s.t. ϵ1≤ϵ, ϵ2≤ϵ\text{s.t. } \epsilon_1 \le \epsilon,\ \epsilon_2 \le \epsilon

κi=∑x ⁣:q(x)=?p(x∣i)\kappa_i = \sum_{x\colon q(x) = ?} p(x \mid i): how much of class ii the clerk sees. The caps alone are met by "always ?" (no errors, all work for the clerk).

None of them needs p(k)p(k) or WW. Neyman–Pearson examples are in the lecture; today: minimax.

Quick check

Reject option, ε=0.2\varepsilon = 0.2. For some xx, max⁡kp(k∣x)=0.7\max_k p(k \mid x) = 0.7. Decide…

Rule: answer a class only if 1−max⁡kp(k∣x)<ε1 - \max_k p(k \mid x) < \varepsilon. Here 1−0.7=0.3≥0.21 - 0.7 = 0.3 \ge 0.2.

Which formulation needs the priors p(k)p(k)?

The non-Bayesian ones only use the class errors ϵ(k)\epsilon(k), built from p(x∣k)p(x \mid k).

qaq_a: ϵ(1)=0.10\epsilon(1) = 0.10, ϵ(2)=0.30\epsilon(2) = 0.30. qbq_b: ϵ(1)=ϵ(2)=0.25\epsilon(1) = \epsilon(2) = 0.25. Minimax prefers…

Compare the worst errors: max⁡=0.30\max = 0.30 for qaq_a, 0.250.25 for qbq_b. The smaller worst case wins, even though qaq_a has the smaller average.

Part III

Exercises

Discrete minimax: female or male?

x=(h,w)x = (h, w): height bin h1h_1–h3h_3, weight bin w1w_1–w4w_4. Classes: 1 = female, 2 = male.

p(x∣1)p(x \mid 1), female

w1w_1w2w_2w3w_3w4w_4
h1h_1.197.145.094.017
h2h_2.077.299.145.017
h3h_3.001.008.000.000

p(x∣2)p(x \mid 2), male

w1w_1w2w_2w3w_3w4w_4
h1h_1.011.005.011.011
h2h_2.005.071.408.038
h3h_3.002.014.255.169
Task: find the minimax strategy q ⁣:X→{1,2}q\colon X \to \{1, 2\} and its worst class error.

Hint: there are 212=40962^{12} = 4096 strategies. Which ones are worth trying?

Three strategies to start with

Question: what is the worst class error of each? ϵ(k)=∑x ⁣:q(x)≠kp(x∣k)\epsilon(k) = \sum_{x\colon q(x) \ne k} p(x \mid k)

Everyone male
w1w_1w2w_2w3w_3w4w_4
h1h_1MMMM
h2h_2MMMM
h3h_3MMMM
ϵ(1)=1\epsilon(1) = 1, ϵ(2)=0\epsilon(2) = 0
worst: 1.000

every woman is wrong, every man right

Everyone female
w1w_1w2w_2w3w_3w4w_4
h1h_1FFFF
h2h_2FFFF
h3h_3FFFF
ϵ(1)=0\epsilon(1) = 0, ϵ(2)=1\epsilon(2) = 1
worst: 1.000

every man is wrong, every woman right

Light = female, heavy = male
w1w_1w2w_2w3w_3w4w_4
h1h_1FFMM
h2h_2FFMM
h3h_3FFMM
ϵ(1)=0.273\epsilon(1) = 0.273, ϵ(2)=0.108\epsilon(2) = 0.108
worst: 0.273

women called male (w3,w4w_3, w_4): 0.094+0.017+0.145+0.0170.094 + 0.017 + 0.145 + 0.017

The extremes have worst error 1. A sensible split gets 0.273. How low can we go, and which of the 4096 splits should we try?

Discrete minimax: which strategies to try?

↓ How male-like is a cell compared with female-like? The likelihood ratio:

r(x)=p(x∣M)p(x∣F)r(x) = \frac{p(x \mid M)}{p(x \mid F)}
w1w_1w2w_2w3w_3w4w_4
h1h_10.0560.0340.1170.647
h2h_20.0650.2372.8142.235
h3h_32.0001.750∞∞

↓ Sort the cells by rr, largest first (rank 1 = most male-like):

w1w_1w2w_2w3w_3w4w_4
h1h_1111297
h2h_210834
h3h_35621

↓ Give the ii lowest-ranked cells to class 2 (male), the rest to class 1 (female). Taking a cell out of order costs more error for the same gain, so the best strategies follow this order (exactly so once we may randomise one boundary cell).

13 candidates: i=0,1,…,12i = 0, 1, \dots, 12. This is the search the assignment asks for.

Continuous minimax I

Two classes, x∈[−1,1]x \in [-1, 1]:

p(x∣1)=∣x∣,p(x∣2)=1−∣x∣p(x \mid 1) = |x|, \qquad p(x \mid 2) = 1 - |x|
Task: find the minimax strategy q∗=argmin⁡qmax⁡kϵ(k)q^* = \operatorname*{argmin}_{q} \max_{k} \epsilon(k) and its worst error.
ϵ(k)=∫x ⁣:q(x)≠kp(x∣k) dx\epsilon(k) = \int_{x\colon q(x) \ne k} p(x \mid k)\, dx

Hint: which xx look like class 1? The likelihood ratio

r(x)=p(x∣1)p(x∣2),q(x)=1 where r(x)>αr(x) = \frac{p(x \mid 1)}{p(x \mid 2)}, \qquad q(x) = 1 \text{ where } r(x) > \alpha
−1−0.500.510.51xp(x|1) = |x|p(x|2) = 1 − |x|123r(x) = |x| / (1 − |x|)

Try it: move the ratio threshold α\alpha (exercise I)

Continuous minimax II

Two classes, x∈[0,1]x \in [0, 1]:

p(x∣1)=1,p(x∣2)=x+0.5p(x \mid 1) = 1, \qquad p(x \mid 2) = x + 0.5
Task: find the minimax strategy and its worst error.

Hint: which xx look like class 1? The likelihood ratio

r(x)=p(x∣1)p(x∣2),q(x)=1 where r(x)>αr(x) = \frac{p(x \mid 1)}{p(x \mid 2)}, \qquad q(x) = 1 \text{ where } r(x) > \alpha

Question: is the threshold where the two densities cross?

00.510.511.5xp(x|1) = 1p(x|2) = x + 0.512r(x) = 1 / (x + 0.5)

Continuous minimax III: two Gaussians

p(x∣k)=1σk2πexp⁡ ⁣(−(x−μk)22σk2)p(x \mid k) = \frac{1}{\sigma_k \sqrt{2\pi}} \exp\!\left(-\frac{(x - \mu_k)^2}{2\sigma_k^2}\right)
K={1,2}K = \{1, 2\}:μ1=−5, σ1=2\mu_1 = -5,\ \sigma_1 = 2μ2=1, σ2=5\mu_2 = 1,\ \sigma_2 = 5
Task: find the minimax strategy q∗=argmin⁡qmax⁡kϵ(k)q^* = \operatorname*{argmin}_{q} \max_{k} \epsilon(k).

Remember: the best strategies are thresholds on the likelihood ratio

r(x)=p(x∣1)p(x∣2),q(x)={1r(x)>α2r(x)<αr(x) = \frac{p(x \mid 1)}{p(x \mid 2)}, \quad q(x) = \begin{cases} 1 & r(x) > \alpha \\ 2 & r(x) < \alpha \end{cases}
−15−10−50510150.10.2xp(x|1)p(x|2)

When is r(x)>αr(x) > \alpha for two Gaussians?

↓ Write out both densities:

r(x)=1σ12πexp⁡ ⁣(−(x−μ1)22σ12)1σ22πexp⁡ ⁣(−(x−μ2)22σ22)r(x) = \frac{\frac{1}{\sigma_1 \sqrt{2\pi}} \exp\!\big(-\frac{(x - \mu_1)^2}{2\sigma_1^2}\big)}{\frac{1}{\sigma_2 \sqrt{2\pi}} \exp\!\big(-\frac{(x - \mu_2)^2}{2\sigma_2^2}\big)}

↓ 2π\sqrt{2\pi} cancels; dividing two exponentials subtracts the exponents:

r(x)=σ2σ1exp⁡ ⁣(−(x−μ1)22σ12+(x−μ2)22σ22)r(x) = \frac{\sigma_2}{\sigma_1} \exp\!\Big(-\frac{(x - \mu_1)^2}{2\sigma_1^2} + \frac{(x - \mu_2)^2}{2\sigma_2^2}\Big)

↓ ln⁡\ln is increasing and r,α>0r, \alpha > 0, so r>α  ⟺  ln⁡r>ln⁡αr > \alpha \iff \ln r > \ln\alpha. The log undoes the exp:

ln⁡r(x)=ln⁡σ2σ1−(x−μ1)22σ12+(x−μ2)22σ22\ln r(x) = \ln\frac{\sigma_2}{\sigma_1} - \frac{(x - \mu_1)^2}{2\sigma_1^2} + \frac{(x - \mu_2)^2}{2\sigma_2^2}

↓ Expand the squares, collect the powers of xx:

ln⁡r(x)=ax2+bx+c\ln r(x) = a x^2 + b x + c
a=12σ22−12σ12,b=μ1σ12−μ2σ22a = \tfrac{1}{2\sigma_2^2} - \tfrac{1}{2\sigma_1^2}, \quad b = \tfrac{\mu_1}{\sigma_1^2} - \tfrac{\mu_2}{\sigma_2^2}
c=μ222σ22−μ122σ12+ln⁡σ2σ1c = \tfrac{\mu_2^2}{2\sigma_2^2} - \tfrac{\mu_1^2}{2\sigma_1^2} + \ln\tfrac{\sigma_2}{\sigma_1}

↓ Our numbers (μ1=−5,σ1=2\mu_1 = -5, \sigma_1 = 2; μ2=1,σ2=5\mu_2 = 1, \sigma_2 = 5):

ln⁡r(x)=−0.105 x2−1.29 x−2.19\ln r(x) = -0.105\,x^2 - 1.29\,x - 2.19
r(x)>α  ⟺  ax2+bx+c−ln⁡α>0r(x) > \alpha \iff a x^2 + b x + c - \ln\alpha > 0: a quadratic inequality, at most two roots t1,t2t_1, t_2.

How does a ratio threshold split xx?

↓ From the previous slide: ln⁡r\ln r is a quadratic in xx.

ln⁡r(x)=−(x−μ1)22σ12+(x−μ2)22σ22+ln⁡σ2σ1\ln r(x) = -\tfrac{(x - \mu_1)^2}{2\sigma_1^2} + \tfrac{(x - \mu_2)^2}{2\sigma_2^2} + \ln\tfrac{\sigma_2}{\sigma_1}

↓ σ1<σ2\sigma_1 < \sigma_2: a parabola opening down.

↓ Say 1 above ln⁡α\ln\alpha (here α=1\alpha = 1): class 1 gets an interval [t1,t2][t_1, t_2].

↓ Class 1 is wrong in its tails, class 2 inside:

ϵ(1)=∫−∞t1 ⁣p(x∣1) dx+∫t2∞ ⁣p(x∣1) dx\epsilon(1) = \int_{-\infty}^{t_1}\! p(x \mid 1)\,dx + \int_{t_2}^{\infty}\! p(x \mid 1)\,dx
ϵ(2)=∫t1t2p(x∣2) dx\epsilon(2) = \int_{t_1}^{t_2} p(x \mid 2)\, dx
Minimax: pick α\alpha so that ϵ(1)=ϵ(2)\epsilon(1) = \epsilon(2). No closed form: solve numerically.
−15−10−50510150.10.2xp(x|1)p(x|2)−404ln r(x)ln αt₁t₂say 2say 1say 2

How do we solve ϵ(1)=ϵ(2)\epsilon(1) = \epsilon(2) numerically?

↓ For one fixed α\alpha, everything is computable. Roots of the quadratic:

ax2+bx+(c−ln⁡α)=0  →  t1,t2a x^2 + b x + (c - \ln\alpha) = 0 \;\to\; t_1, t_2

↓ Errors from the normal CDF Φ\Phi (table, or norm.cdf):

ϵ(1)=1−[Φ(t2−μ1σ1)−Φ(t1−μ1σ1)]\epsilon(1) = 1 - \Big[\Phi\big(\tfrac{t_2 - \mu_1}{\sigma_1}\big) - \Phi\big(\tfrac{t_1 - \mu_1}{\sigma_1}\big)\Big]
ϵ(2)=Φ(t2−μ2σ2)−Φ(t1−μ2σ2)\epsilon(2) = \Phi\big(\tfrac{t_2 - \mu_2}{\sigma_2}\big) - \Phi\big(\tfrac{t_1 - \mu_2}{\sigma_2}\big)

↓ Larger α\alpha: fewer xx say 1, so ϵ(1)\epsilon(1) grows and ϵ(2)\epsilon(2) shrinks. They cross once.

↓ Bisection: α=1\alpha = 1 has ϵ(1)<ϵ(2)\epsilon(1) < \epsilon(2), α=4\alpha = 4 has ϵ(1)>ϵ(2)\epsilon(1) > \epsilon(2). Try the midpoint, keep the half where the order flips.

[αlo,αhi][\alpha_{lo}, \alpha_{hi}]α\alphat1,t2t_1, t_2ϵ(1)\epsilon(1)ϵ(2)\epsilon(2)keep half
[1, 4]2.5−9.00, −3.290.2180.173lower
[1, 2.5]1.75−9.54, −2.740.1410.210upper
[1.75, 2.5]2.13−9.26, −3.030.1780.190upper
[2.13, 2.5]2.31−9.13, −3.160.1980.181lower
[2.13, 2.31]2.22−9.19, −3.090.1880.186…
α≈2.20\alpha \approx 2.20: class 1 on [−9.20,−3.08][-9.20, -3.08], ϵ(1)=ϵ(2)≈0.187\epsilon(1) = \epsilon(2) \approx 0.187. Each step halves the interval.

The assignment searches over the prior p(1)=11+αp(1) = \tfrac{1}{1 + \alpha} instead (one number, same idea), with fminbound on max⁡kϵ(k)\max_k \epsilon(k).

Try it: move the ratio threshold α\alpha

Part IV

Minimax and Bayes

Priors exist, but we don't know them

Question: how large can the Bayesian error of a strategy qq get, if the priors are unknown?

Error of qq (0/1 loss): the mass of all pairs we get wrong.

ϵ=∑k∑x ⁣:q(x)≠kp(x,k)\epsilon = \sum_{k} \sum_{x\colon q(x) \ne k} p(x, k)

↓ Split p(x,k)=p(k) p(x∣k)p(x, k) = p(k)\,p(x \mid k); p(k)p(k) does not depend on xx, so it leaves the inner sum.

ϵ=∑kp(k)∑x ⁣:q(x)≠kp(x∣k)⏟ϵ(k)\epsilon = \sum_{k} p(k) \underbrace{\sum_{x\colon q(x) \ne k} p(x \mid k)}_{\epsilon(k)}

↓ p(k)≥0p(k) \ge 0 and ∑kp(k)=1\sum_k p(k) = 1: ϵ\epsilon is a convex combination of the ϵ(k)\epsilon(k), so it is at most the largest one (reached with p(k)=1p(k) = 1 for that class).

max⁡priors ϵ=max⁡k ϵ(k)\max_{\text{priors}}\, \epsilon = \max_{k}\, \epsilon(k)
To be safe for every prior, minimise the worst case: min⁡qmax⁡kϵ(k)\min_q \max_k \epsilon(k).
Minimax = Bayes with unknown priors and the worst prior assumed. Same solution.

Which strategies can be minimax?

Question: do we have to search all strategies, or just a few?

Bayesian strategy for a prior p(1)p(1) (lab 2, 0/1 loss): say 1 if

p(x∣1) p(1)≥p(x∣2) p(2)p(x \mid 1)\, p(1) \ge p(x \mid 2)\, p(2)

↓ Divide both sides by p(x∣2) p(1)>0p(x \mid 2)\, p(1) > 0:

p(x∣1)p(x∣2)≥p(2)p(1)=α\frac{p(x \mid 1)}{p(x \mid 2)} \ge \frac{p(2)}{p(1)} = \alpha

↓ Every prior gives one α\alpha. Backwards: which prior gives a chosen α\alpha? Use p(2)=1−p(1)p(2) = 1 - p(1):

α=1−p(1)p(1)\alpha = \frac{1 - p(1)}{p(1)}

↓ Multiply by p(1)p(1), collect the p(1)p(1) terms:

α p(1)=1−p(1)  ⇒  p(1)=11+α\alpha\, p(1) = 1 - p(1) \;\Rightarrow\; p(1) = \frac{1}{1 + \alpha}
Priors and ratio thresholds match one to one: p(1)∈(0,1)↔α∈(0,∞)p(1) \in (0, 1) \leftrightarrow \alpha \in (0, \infty).

The minimax strategy is a ratio threshold, so it is a Bayesian strategy for some prior cc:

qminimax∗=qBayes∗(p(1)=c)q^*_{\text{minimax}} = q^*_{\text{Bayes}}\big(p(1) = c\big)

→ Search over one number p(1)p(1), reusing the code from lab 2.

What if the true prior is different?

Design the Bayesian strategy qbq_b for p(1)=0.7p(1) = 0.7 (exercise III).

Its risk for that prior:

R(qb)=p(1) ϵ(1)+p(2) ϵ(2)R(q_b) = p(1)\,\epsilon(1) + p(2)\,\epsilon(2)

↓ Two classes: p(2)=1−p(1)p(2) = 1 - p(1), so RR is a convex combination of ϵ(1)\epsilon(1) and ϵ(2)\epsilon(2):

=p(1) ϵ(1)+(1−p(1)) ϵ(2)= p(1)\,\epsilon(1) + (1 - p(1))\,\epsilon(2)

↓ Collect the terms with p(1)p(1):

=p(1) (ϵ(1)−ϵ(2))+ϵ(2)= p(1)\,\big(\epsilon(1) - \epsilon(2)\big) + \epsilon(2)

↓ qbq_b is fixed, so ϵ(1),ϵ(2)\epsilon(1), \epsilon(2) are fixed: a straight line in the true prior, a⋅p(1)+ba \cdot p(1) + b.

00.20.40.60.810.10.20.30.40.5p(1)risk Rdesign p(1) = 0.7R = 0.118R of this fixed qp(1) = 0: R = ε(2) = 0.33p(1) = 1: R = ε(1) = 0.03

Where is the minimax strategy?

Dots: pick a prior, design its Bayesian strategy (lab 2), compute its risk at that prior. Repeat for a few priors.

Blue: do it for every prior: the Bayes risk curve R∗(p(1))R^*(p(1)).

Grey: a fixed strategy qbq_b is a line, never below the blue curve, touching it at its design prior p0p_0:

R(qb,p)≥R∗(p)=min⁡qR(q,p),= at p=p0R(q_b, p) \ge R^*(p) = \min_q R(q, p), \quad = \text{ at } p = p_0

↑ R∗R^* is the best any strategy can do; qbq_b is the best at p0p_0.

Orange: the worst case of qbq_b over all priors is the higher end of its line, max⁡(ϵ(1),ϵ(2))\max(\epsilon(1), \epsilon(2)). Plot it at p0p_0.

Same for p0=0.2p_0 = 0.2 (here the right end is higher), then for every p0p_0: the orange curve.

Black: the lowest worst case is a flat line. It touches the top of the blue curve.

00.20.40.60.810.10.20.30.40.5p(1)risk Rworst of q_b: 0.330.41minimax 0.187p(1) = 0.31Bayes R*designed for 0.7worst case
The minimax prior is the worst prior for Bayes: the one with the largest Bayes risk.

Try it: one strategy, two views

Why ϵ(1)=ϵ(2)\epsilon(1) = \epsilon(2)?

Question: the minimax line is flat. What does that say about its class errors?

Risk of a fixed qq when the true prior is p(1)p(1) (two slides back):

R(q)=p(1)⋅(ϵ(1)−ϵ(2))+ϵ(2)R(q) = p(1) \cdot \big(\epsilon(1) - \epsilon(2)\big) + \epsilon(2)

↓ A tilted line has its maximum at an end, max⁡(ϵ(1),ϵ(2))\max(\epsilon(1), \epsilon(2)), larger than at the flat position. Flat means slope 0:

q∗  ⇔  ϵ(1)=ϵ(2)q^* \;\Leftrightarrow\; \epsilon(1) = \epsilon(2)

↓ Put slope 0 into the line:

R(q∗)=p(1)⋅(ϵ(1)−ϵ(2))⏟0+ϵ(2)=ϵ(2)R(q^*) = p(1) \cdot \underbrace{\big(\epsilon(1) - \epsilon(2)\big)}_{0} + \epsilon(2) = \epsilon(2)
The minimax strategy has the same risk for every prior: it does not care what the prior is.

Quick check

A fixed strategy has ϵ(1)=0.1\epsilon(1) = 0.1, ϵ(2)=0.3\epsilon(2) = 0.3. Its risk if p(1)=0.5p(1) = 0.5?

R=p(1) ϵ(1)+p(2) ϵ(2)=0.5⋅0.1+0.5⋅0.3=0.2R = p(1)\,\epsilon(1) + p(2)\,\epsilon(2) = 0.5 \cdot 0.1 + 0.5 \cdot 0.3 = 0.2

The same strategy: its worst risk over all priors?

A line in p(1)p(1), largest at an end: max⁡(ϵ(1),ϵ(2))=0.3\max(\epsilon(1), \epsilon(2)) = 0.3 (at p(1)=0p(1) = 0).

How does the risk of the minimax strategy change with the true prior?

ϵ(1)=ϵ(2)=ϵ\epsilon(1) = \epsilon(2) = \epsilon, so R=p(1) ϵ+(1−p(1)) ϵ=ϵR = p(1)\,\epsilon + (1 - p(1))\,\epsilon = \epsilon for every prior: a flat line.

Quick check: minimax or Bayes?

Is the minimax strategy the Bayesian strategy for equal priors p(1)=p(2)p(1) = p(2)?

It is the Bayesian strategy for the worst prior cc. That is 0.50.5 only in special cases, e.g. a symmetric problem like exercise I. In exercises II and III it is not.

The true priors are equal. Which strategy has the smaller error?

Bayes is optimal for the prior it was designed for. Minimax pays for being safe under every prior (unless c=0.5c = 0.5).

Implementing the discrete search

Question: how do we get ϵ(1),ϵ(2)\epsilon(1), \epsilon(2) for all thresholds without a loop over strategies?

  1. Flatten both tables p(x∣1)p(x \mid 1), p(x∣2)p(x \mid 2) to vectors of length NN (21 × 21 = 441).
  2. Ratio r=p(x∣1)/p(x∣2)r = p(x \mid 1) / p(x \mid 2) per cell. 0/00/0 = NaN: treat as class 1 (assignment rule).
  3. Sort cells by rr, largest first (most class-1-like): np.argsort, reversed.
  4. Strategy ii: first ii sorted cells say 1, the rest 2; i=0,…,Ni = 0, \dots, N (N+1N + 1 strategies).
  5. i∗=argmin⁡imax⁡(ϵ1[i],ϵ2[i])i^* = \operatorname{argmin}_i \max(\epsilon_1[i], \epsilon_2[i]); map back to the 21 × 21 grid with the sort order.

↓ Moving from ii to i+1i + 1 moves one cell to class 1: running sums give all errors at once.

ϵ2[i]=∑j<ip2[sortedj],ϵ1[i]=1−∑j<ip1[sortedj]\epsilon_2[i] = \sum_{j < i} p_2[\text{sorted}_j], \qquad \epsilon_1[i] = 1 - \sum_{j < i} p_1[\text{sorted}_j]

np.cumsum with a 0 prepended: arrays of length N+1N + 1.

Our table slide used p(x∣2)/p(x∣1)p(x \mid 2)/p(x \mid 1) largest first (male side first): the same list from the other end. Cells with equal rr may come in any order.

Take-away for the lab

Discrete
  • minimax_strategy_discrete: sort cells by the likelihood ratio, try every index ii, keep the smallest max⁡(ϵ(1),ϵ(2))\max(\epsilon(1), \epsilon(2))
  • classify_discrete: apply the strategy to test images
Continuous (Gaussians)
  • risk_fix_q_cont: one strategy, all priors: a line
  • worst_risk_cont: Bayesian strategy for a prior → max⁡(ϵ(1),ϵ(2))\max(\epsilon(1), \epsilon(2))
  • minimax_strategy_cont: minimise the worst risk over p(1)p(1) (fminbound)
Copy the needed lab 2 functions into minimax.py (find_strategy_2normal, bayes_risk_2normal, classify_2normal); don't import them.
Optional

Extra: Neyman–Pearson, Wald, the error plane, games

Neyman–Pearson on the female/male tables

Same tables as the discrete minimax exercise. At most 20 % of class 1 may be misclassified; within that, as few class-2 errors as possible.

q∗=argmin⁡q ϵ(2)s.t. ϵ(1)≤0.2q^* = \operatorname*{argmin}_{q}\ \epsilon(2) \quad \text{s.t.}\ \epsilon(1) \le 0.2

Result (try it yourself first):

ϵ(1)=0.188\epsilon(1) = 0.188, ϵ(2)=0.103\epsilon(2) = 0.103: the five most female-like cells say 1

q∗(x)q^*(x)

w1w_1w2w_2w3w_3w4w_4
h1h_11112
h2h_21122
h3h_32222

Neyman–Pearson is a knapsack problem

Question: which cells should we give to class 2?

knapsackNeyman–Pearson
itemscells xx we give to class 2
weightp(x∣1)p(x \mid 1): adds to ϵ(1)\epsilon(1)
capacityϵˉ1\bar\epsilon_1
valuep(x∣2)p(x \mid 2): removes from ϵ(2)\epsilon(2)
greedy ordervalue / weight =p(x∣2)/p(x∣1)= p(x \mid 2)/p(x \mid 1): the likelihood ratio

Greedy by ratio is optimal if items may be split (fractional knapsack) = randomise one cell. That is the Neyman–Pearson lemma.

Whole cells only (0/1 knapsack): greedy can lose. Example ϵˉ1=0.1\bar\epsilon_1 = 0.1:

strategyϵ(2)\epsilon(2)
ratio threshold ii0.576
best whole cells0.511
randomised (split one cell)0.295

The heavy cell h2w3h_2 w_3 (p(x∣1)=0.145p(x \mid 1) = 0.145) does not fit; whole-cell optimum skips it and packs lighter ones.

Wald on the female/male tables

Now "?" is allowed. Both errors at most ϵ=0.15\epsilon = 0.15; within that, as few undecided as possible. Use the likelihood-ratio order: two thresholds.

q∗=argmin⁡q max⁡iκis.t. ϵ1,ϵ2≤0.15q^* = \operatorname*{argmin}_{q}\ \max_i \kappa_i \quad \text{s.t.}\ \epsilon_1, \epsilon_2 \le 0.15

Result (try it yourself first):

ϵ1=0.145\epsilon_1 = 0.145, ϵ2=0.130\epsilon_2 = 0.130, κ1=0.017\kappa_1 = 0.017, κ2=0.038\kappa_2 = 0.038: one cell is "?"

q∗(x)q^*(x)

w1w_1w2w_2w3w_3w4w_4
h1h_11111
h2h_2112?
h3h_31122

One picture for all formulations: the error plane

Each strategy qq is a point (ϵ(1),ϵ(2))(\epsilon(1), \epsilon(2)). Grey: all 212=40962^{12} = 4096 strategies.

● the 13 ratio thresholds; segments between them = randomised strategies. Nothing lies below this boundary.

□ Bayes, p(1)=0.5p(1) = 0.5: push a line of slope −p(1)/p(2)-p(1)/p(2) toward the origin.

◇ Neyman–Pearson: lowest point left of ϵ(1)=0.2\epsilon(1) = 0.2.

○ Minimax: threshold i=4i = 4 (ring); the randomised optimum sits where the boundary crosses the diagonal (0.152).

✗ a whole-cell strategy that is not a threshold, worst error 0.154: better than i=4i = 4, still above the boundary.

000.20.20.40.40.60.6ε(1)ε(2)ε(1) = 0.2ε(1) = ε(2)

Try it: the error plane

Minimax as a game against nature

Two players: we choose qq, nature chooses the prior p(1)p(1). Our loss:

R(q,p)=p(1) ϵq(1)+p(2) ϵq(2)R(q, p) = p(1)\,\epsilon_q(1) + p(2)\,\epsilon_q(2)

We move first, nature answers: min⁡qmax⁡pR\min_q \max_p R = minimax.

Nature moves first, we answer with Bayes: max⁡pmin⁡qR\max_p \min_q R = top of the Bayes-risk hill, the least favourable prior.

Always max⁡min⁡≤min⁡max⁡\max\min \le \min\max. Equal (a saddle point) once qq may be randomised: von Neumann's minimax theorem.

min⁡qmax⁡p\min_q \max_pmax⁡pmin⁡q\max_p \min_q
Gaussians (ex. III)0.1870.187
discrete, thresholds0.1620.152
discrete, whole cells0.154
discrete, randomised0.152
Minimax strategy = Bayes strategy for the least favourable prior. ϵ(1)=ϵ(2)\epsilon(1) = \epsilon(2) makes nature indifferent: every prior gives the same loss.