03 - Minimax

Pattern Recognition and Machine Learning, Lab 3

Michal Neoral

neoramic@fel.cvut.cz

9 October 2026

Test next week

Test: 16 October

  • At the beginning of the lab
  • Topics: Bayesian and non-Bayesian tasks
  • Theory and worked problems
  • Also things from the assignments, e.g. what a Python function computes
  • Bring a calculator
  • No phone, laptop or notes

Bonus tasks

  • Handed in in person after the lab
  • You explain the task and convince us that the solution is correct

Cannot come to the test? Let me know in advance.

Recap

Recap: Bayesian decision making

What do we need to know to find the Bayesian strategy?

\[ R(\cq) = \sum_{\cx \in \cX} \sum_{\ck \in \cK} \cpXK(\cx, \ck)\; \cW(\ck, \cq(\cx)), \qquad \cq^* = \arg\min_{\cq:\, \cX \to \cD} R(\cq) \]

We need

  • \(\cp(\cx \mid \ck)\): likelihoods
  • \(\cp(\ck)\): priors
  • \(\cW(\ck, \cd)\): the loss

With the 0/1 loss

\(\cq^*(\cx) = \arg\max_{\ck} \cp(\ck \mid \cx)\)

\(\phantom{\cq^*(\cx)} = \arg\max_{\ck} \cp(\cx \mid \ck)\, \cp(\ck)\)

The prior is inside the posterior.

Recap: the reject option

When is it better to answer "I do not know"?

Decisions \(\cD = \cK \cup \{\text{not known}\}\) and the loss

\[ \cW(\ck, \cd) = \begin{cases} 0 & \text{if } \cd = \ck \\ 1 & \text{if } \cd \ne \ck \text{ and } \cd \ne \text{not known} \\ \varepsilon & \text{if } \cd = \text{not known} \end{cases} \]

The Bayesian strategy becomes

\[ \cq^*(\cx) = \begin{cases} \arg\max_{\ck} \cp(\ck \mid \cx) & \text{if } 1 - \max_{\ck} \cp(\ck \mid \cx) < \varepsilon \\ \text{not known} & \text{otherwise} \end{cases} \]

When Bayes is not enough

Priors are unknown

A plate reader used anywhere in the country: every crossroad has different \(\cp(\ck)\).

The loss is hard to set

What is the price of an overlooked tumour compared with a false alarm?

A guarantee is required

"At most 1 % of dangerous states may be overlooked."

What can we still do when we know only the likelihoods \(\cp(\cx \mid \ck)\)?

Non-Bayesian tasks

Error on class \(\ck\): \(\quad \epsilon(\ck) = \sum_{\cx:\, \cq(\cx) \ne \ck} \cp(\cx \mid \ck)\)

Neyman-Pearson

\[\min_{\cq}\; \etwo\]

subject to \(\;\eone \le \bar\epsilon\)

\(\cone\) dangerous, \(\ctwo\) normal: few false alarms, overlooked danger bounded

Wald

\[\min_{\cq:\, \cX \to \cD}\; \max_{\ck \in \{\cone, \ctwo\}}\; \kappa(\ck)\]

subject to \(\;\eone \le \epsilon,\ \etwo \le \epsilon\)

decisions \(\cD = \cK \cup \{?\}\), the answer "?" is no error

undecided: \(\kappa(\ck) = \sum_{\cx:\, \cq(\cx) = ?} \cp(\cx \mid \ck)\)

Minimax

\[\min_{\cq}\; \max_{\ck \in \cK}\; \epsilon(\ck)\]

the worst class is as good as possible

today

For two classes, all three are solved by thresholding the likelihood ratio \(\;\cr(\cx) = \frac{\cp(\cx \mid \cone)}{\cp(\cx \mid \ctwo)}\)

Discrete minimax

The minimax task

\[ \cq^* = \arg\min_{\cq:\, \cX \to \cK}\; \max_{\ck \in \cK}\; \epsilon(\ck), \qquad \epsilon(\ck) = \sum_{\cx:\, \cq(\cx) \ne \ck} \cp(\cx \mid \ck) \]

What is \(\epsilon(\ck)\) in words? What do we need, and what is missing compared with Bayes?

For two classes: decide by the likelihood ratio \[ \cr(\cx) = \frac{\cp(\cx \mid \cone)}{\cp(\cx \mid \ctwo)}, \qquad \cq(\cx) = \begin{cases} \cone & \text{if } \cr(\cx) > \cl \\ \ctwo & \text{if } \cr(\cx) \le \cl \end{cases} \]

One number gives the whole strategy: the threshold \(\cl\).

Lecture: the inverse ratio \(\cp(\cx \mid \ctwo) / \cp(\cx \mid \cone)\) with the threshold \(\mu\). Assignment: this form.

Discrete example: female or male?

Observation \(\cx\) = (height, weight), \(\quad \cK = \{\cone, \ctwo\} = \{\cF, \cM\}\) (female, male)

\(\cp(\cx \mid \cF)\)

u-light light avg heavy
short .197 .145 .094 .017
normal .077 .299 .145 .017
tall .001 .008 .000 .000

\(\cp(\cx \mid \cM)\)

u-light light avg heavy
short .011 .005 .011 .011
normal .005 .071 .408 .038
tall .002 .014 .255 .169

1. What is \(\cr(\cx) = \frac{\cp(\cx \mid \cM)}{\cp(\cx \mid \cF)}\) for each cell?

2. How do we find the minimax strategy \(\cq^*\)?

3. What are its errors \(\epsilon(\cF)\) and \(\epsilon(\cM)\)?

Derivation: the likelihood ratio and its order

\(\cr(\cx) = \cp(\cx \mid \cM) \,/\, \cp(\cx \mid \cF)\)

u-light light avg heavy
short 0.056 0.034 0.117 0.647
normal 0.065 0.237 2.814 2.235
tall 2.000 1.750 \(\infty\) \(\infty\)

Index \(i\) after sorting by \(\cr\) (from 0)

u-light light avg heavy
short 1 0 3 5
normal 2 4 9 8
tall 7 6 10 11

Every threshold on \(\cr\) is one index \(i^* \in \{0, 1, \dots, 12\}\): \[ \cq(\cx_i) = \begin{cases} \cM & \text{if } i \ge i^* \\ \cF & \text{if } i < i^* \end{cases} \qquad\qquad \epsilon(\cF) = \sum_{i \ge i^*} \cp(\cx_i \mid \cF), \qquad \epsilon(\cM) = \sum_{i < i^*} \cp(\cx_i \mid \cM) \]

Derivation: errors for every threshold

threshold \(i^*\) 0 1 2 3 4 5 6 7 8 9 10 11 12
\(\cM\) if \(\cr \ge\) .034 .056 .065 .117 .237 .647 1.75 2.00 2.24 2.81 \(\infty\) \(\infty\) never
\(\epsilon(\cF)\) 1 .855 .658 .581 .487 .188 .171 .163 .162 .145 0 0 0
\(\epsilon(\cM)\) 0 .005 .016 .021 .032 .103 .114 .128 .130 .168 .576 .831 1
max 1 .855 .658 .581 .487 .188 .171 .163 .162 .168 .576 .831 1

Empty axes for the errors of both classes

Errors for the thresholds from 12 down to 12

Errors for the thresholds from 12 down to 11

Errors for the thresholds from 12 down to 10

Errors for the thresholds from 12 down to 9

Errors for the thresholds from 12 down to 8

Errors for the thresholds from 12 down to 7

Errors of both classes for every threshold

The maximum of the two errors and its minimum at index 8

Sorted by \(\cr\), decide \(\cM\) for \(i \ge i^*\): \[ \epsilon(\cF) = \sum_{i \ge i^*} \cp(\cx_i \mid \cF) \] \[ \epsilon(\cM) = \sum_{i < i^*} \cp(\cx_i \mid \cM) \]

\(i^* = 12\): nothing is decided \(\cM\), i.e. we always decide \(\cF\).

Smallest maximum: \(\mathbf{i^* = 8}\)

Derivation: the discrete minimax strategy

Strategy \(\cq^*(\cx)\) for \(i^* = 8\)

u-light light avg heavy
short F F F F
normal F F M M
tall F F M M

\[ \cq^*(\cx) = \begin{cases} \cM & \text{if } \cr(\cx) \ge 2.235 \\ \cF & \text{otherwise} \end{cases} \]

\(\epsilon(\cF) = 0.162, \quad \epsilon(\cM) = 0.130, \quad \max = \mathbf{0.162}\)

For discrete \(\cx\) the errors usually cannot be made equal: take the smallest maximum.

Assignment: ratios \(0\), \(\infty\), nan and ties are common. Thresholding the index handles them (nan: prefer the first class).

Continuous minimax

Minimax for continuous \(\cx\)

\[ \cq^* = \arg\min_{\cq:\, \cX \to \cK}\; \max_{\ck \in \cK}\; \epsilon(\ck), \qquad \epsilon(\ck) = \int_{\cx:\, \cq(\cx) \ne \ck} \cp(\cx \mid \ck)\, d\cx \]

We move the threshold \(\cl\). What happens to \(\eone\), \(\etwo\) and their maximum?

For continuous \(\cx\) the errors change continuously, one grows and the other shrinks. The optimum is where \(\;\eone = \etwo\).

Example I: two triangles

Densities p(x|1) = |x| and p(x|2) = 1 - |x| on the interval from -1 to 1

The likelihood ratio r(x) added to the densities

A threshold on the likelihood ratio gives the decision regions 1, 2, 1 and the two errors

\(\cp(\cx \mid \cone) = |\cx|, \quad \cp(\cx \mid \ctwo) = 1 - |\cx|, \quad \cx \in [-1, 1]\). Find the minimax strategy.

Step 1: draw the likelihood ratio \(\cr(\cx)\)

Step 2: a threshold \(\cl\) gives the regions \(\cone \mid \ctwo \mid \cone\) and the errors. Which \(\cl\) is optimal?

Example I: try it

Derivation: example I

By symmetry the regions are \(\cone \mid \ctwo \mid \cone\) with the boundaries \(\pm t\):

\[ \eone = \int_{-t}^{t} |\cx|\, d\cx = 2 \int_{0}^{t} \cx\, d\cx = t^2 \]

\[ \etwo = 2 \int_{t}^{1} (1 - \cx)\, d\cx = (1 - t)^2 \]

\[ \eone = \etwo: \quad t^2 = (1 - t)^2 \quad \text{gives} \quad t = \tfrac{1}{2} \]

\[ \cl^* = \cr(\tfrac{1}{2}) = 1, \qquad \eone = \etwo = \mathbf{\tfrac{1}{4}} \]

\[ \cq^*(\cx) = \begin{cases} \ctwo & \text{if } |\cx| \le \tfrac{1}{2} \\ \cone & \text{otherwise} \end{cases} \]

The densities and the likelihood ratio

Boundaries at -t and t with the two error areas

The optimal threshold: boundaries at -0.5 and 0.5, both errors equal 0.25

Example II: your turn

Densities p(x|1) = 1 and p(x|2) = x + 0.5 on the interval from 0 to 1

\(\cp(\cx \mid \cone) = 1, \quad \cp(\cx \mid \ctwo) = \cx + 0.5\)

on the interval \(\cx \in (0, 1)\)

1. What does \(\cr(\cx)\) look like?

2. Where do we decide \(\cone\) and where \(\ctwo\)?

3. Which boundary \(t\) gives \(\eone = \etwo\)?

Derivation: example II

\(\cr(\cx) = \frac{1}{\cx + 0.5}\) is decreasing: we decide \(\cone\) for \(\cx < t\).

\[ \eone = \int_{t}^{1} 1\, d\cx = 1 - t \]

\[ \etwo = \int_{0}^{t} (\cx + 0.5)\, d\cx = \tfrac{1}{2} t^2 + \tfrac{1}{2} t \]

\[ t^2 + 3t - 2 = 0, \qquad t = \tfrac{1}{2} \big( \sqrt{17} - 3 \big) \approx 0.562 \]

\[ \eone = \etwo \approx \mathbf{0.438}, \qquad \cl^* = \cr(t) \approx 0.942 \]

\[ \cq^*(\cx) = \begin{cases} \cone & \text{if } \cx < 0.562 \\ \ctwo & \text{otherwise} \end{cases} \]

The densities and the decreasing likelihood ratio

A boundary t with the regions 1 and 2 and the two error areas

The optimal boundary t = 0.562 with both errors equal to 0.438

Example III: two normal distributions

Two normal densities with means -5 and 1 and standard deviations 2 and 5

The scaled likelihood ratio with a threshold and the boundaries t1 and t2

Decision regions 2, 1, 2 and the errors of both classes

\(\cp(\cx \mid \ck)\) normal with \(\mone = -5,\ \sone = 2,\ \mtwo = 1,\ \stwo = 5\). Find the minimax strategy.

A threshold \(\cl\) on \(\cr(\cx)\) gives two boundaries \(t_1, t_2\). How do we compute them?

Regions \(\ctwo \mid \cone \mid \ctwo\) and the errors for \(\cl = 1\). How do we get \(\eone = \etwo\)?

Derivation: from \(\cl\) to the boundaries

We decide \(\cone\) where \(\;\dfrac{\cp(\cx \mid \cone)}{\cp(\cx \mid \ctwo)} \ge \cl\):

\[ \frac{\stwo}{\sone}\, \exp\!\left( -\frac{(\cx - \mone)^2}{2\sone^2} + \frac{(\cx - \mtwo)^2}{2\stwo^2} \right) \;\ge\; \cl \]

Take the logarithm: the same inequality \(a\cx^2 + b\cx + c \ge 0\) as in Lab 2 \[ -\frac{(\cx - \mone)^2}{2\sone^2} + \frac{(\cx - \mtwo)^2}{2\stwo^2} \;\ge\; \ln\!\left( \frac{\sone}{\stwo} \cdot \cl \right) =: L \]

\(a = \sone^2 - \stwo^2\) \(b = 2(\stwo^2 \mone - \sone^2 \mtwo)\) \(c = \sone^2 \mtwo^2 - \stwo^2 \mone^2 - 2 \sone^2 \stwo^2 L\)

Lab 2 had \(\dfrac{\cp(\ctwo)}{\cp(\cone)}\) in place of \(\cl\): this is the Bayesian strategy for \(\;\cp(\cone) = \dfrac{1}{1 + \cl}\)

Derivation: the errors of the strategy

For \(\sone < \stwo\) the regions are \(\ctwo \mid \cone \mid \ctwo\) with the boundaries \(t_1 < t_2\).

With the distribution functions \(F_{\cone}\), \(F_{\ctwo}\) (norm.cdf): \[ \eone = \int_{-\infty}^{t_1} \cp(\cx \mid \cone)\, d\cx + \int_{t_2}^{\infty} \cp(\cx \mid \cone)\, d\cx = 1 - \big( F_{\cone}(t_2) - F_{\cone}(t_1) \big) \]

\[ \etwo = \int_{t_1}^{t_2} \cp(\cx \mid \ctwo)\, d\cx = F_{\ctwo}(t_2) - F_{\ctwo}(t_1) \]

\(\eone = \etwo\) has no closed-form solution for normal distributions. We have to search for \(\cl\) numerically.

Minimax and Bayes

The risk of a fixed strategy

We fix a strategy \(\cq\). How does its risk change when the prior changes?

\[ R(\cq) = \sum_{\ck} \int_{\cx:\, \cq(\cx) \ne \ck} \cpXK(\cx, \ck)\, d\cx = \sum_{\ck} \cp(\ck) \underbrace{\int_{\cx:\, \cq(\cx) \ne \ck} \cp(\cx \mid \ck)\, d\cx}_{\epsilon(\ck)} \]

For two classes, with \(\cp(\ctwo) = 1 - \cp(\cone)\): \[ R(\cq) = \cp(\cone)\, \eone + \big( 1 - \cp(\cone) \big)\, \etwo = \cp(\cone)\, \big( \eone - \etwo \big) + \etwo \]

Linear in \(\cp(\cone)\), from \(\etwo\) at \(\cp(\cone) = 0\) to \(\eone\) at \(\cp(\cone) = 1\). Worst case: \(\max(\eone, \etwo)\)

Minimax as the worst-case Bayes

Risk of the optimal Bayesian strategy as a function of the prior

A strategy designed for prior 0.6 evaluated for all priors is a line touching the curve

The ends of the line are the class errors, marked above the prior 0.6

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The same construction for another prior, the points trace the two class errors

The errors of both classes for every prior and their maximum

The minimax strategy is the Bayesian strategy with a horizontal risk line

For each prior \(\cp(\cone)\): the risk of its own optimal Bayesian strategy (example III)

The strategy designed for \(\cp(\cone) = 0.6\), used everywhere: a line touching the curve

Its ends are \(\etwo\) (left) and \(\eone\) (right). We mark both above its prior

The same for the strategies of all priors from 0 to 1: the points draw \(\eone\) and \(\etwo\)

The worst case of every Bayesian strategy: \(\max(\eone, \etwo)\)

Minimax: the Bayesian strategy for \(p^*(\cone)\), its line is horizontal, \(\eone = \etwo\)

Minimax and Bayes: try it

The minimax strategy for example III

The minimax strategy for two normal distributions: both errors equal 0.187

Search over the prior instead of \(\cl\)

from scipy.optimize import fminbound
p1 = fminbound(worst_risk, 0, 1)

\(p^*(\cone) = 0.312, \quad \cl^* = 2.20\)

\(t_1 = -9.20, \quad t_2 = -3.08\)

\(\eone = \etwo = \mathbf{0.187}\)

\[ \cq^*(\cx) = \begin{cases} \cone & \text{if } t_1 < \cx < t_2 \\ \ctwo & \text{otherwise} \end{cases} \]

A grid of priors is too coarse.

Output of the assignment

Part 1: discrete

  • minimax_strategy_discrete: strategy, \(i^*\) and the errors for all \(N + 1\) thresholds
  • classify_discrete: labels for test images

Part 2: continuous

  • risk_fix_q_cont: fixed strategy, all priors
  • worst_risk_cont: worst risk for one prior
  • minimax_strategy_cont: the result

Copy your Lab 2 functions into minimax.py. Imports do not work in the BRUTE evaluation.

Take-away for the lab

  • Minimax needs only \(\cp(\cx \mid \ck)\): no priors, no loss
  • The strategy is a threshold on the likelihood ratio \(\cr(\cx)\)
  • Discrete \(\cx\): sort by \(\cr\), try all \(N + 1\) thresholds, take the smallest maximum
  • Continuous \(\cx\): the optimum is where \(\eone = \etwo\)
  • Normal distributions: minimax is the Bayesian strategy for the worst prior
  • Next week: test! Bring a calculator