Pattern Recognition and Machine Learning, Lab 3
neoramic@fel.cvut.cz
9 October 2026
Test: 16 October
Bonus tasks
Cannot come to the test? Let me know in advance.
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
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.
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} \]
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)\)?
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)}\)
\[ \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.
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)\)?
\(\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) \]
| 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 |
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}\)
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).
\[ \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\).
\(\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?
triX = d3.range(-1, 1.0001, 0.01)
Plot.plot({
width: 1350, height: 430, marginLeft: 70,
style: {fontSize: "20px"},
x: {label: "x", domain: [-1, 1], ticks: 4},
y: {label: "p(x | k)", domain: [0, 1.1], ticks: 2},
marks: [
Plot.areaY(triX.filter(x => Math.abs(x) <= tt + 1e-9), {x: d => d, y: d => Math.abs(d), fill: "#3C7DD9", fillOpacity: 0.3}),
Plot.areaY(triX.filter(x => x <= -tt + 1e-9), {x: d => d, y: d => 1 - Math.abs(d), fill: "#C0392B", fillOpacity: 0.3}),
Plot.areaY(triX.filter(x => x >= tt - 1e-9), {x: d => d, y: d => 1 - Math.abs(d), fill: "#C0392B", fillOpacity: 0.3}),
Plot.line(triX, {x: d => d, y: d => Math.abs(d), stroke: "#3C7DD9", strokeWidth: 4}),
Plot.line(triX, {x: d => d, y: d => 1 - Math.abs(d), stroke: "#C0392B", strokeWidth: 4}),
Plot.ruleX([-tt, tt], {stroke: "#202729", strokeDasharray: "8 6", strokeWidth: 2}),
Plot.text([{x: -(1 + tt) / 2, t: "1", c: "#3C7DD9"}, {x: 0, t: "2", c: "#C0392B"}, {x: (1 + tt) / 2, t: "1", c: "#3C7DD9"}],
{x: "x", y: 1.04, text: "t", fill: "c", fontSize: 40, fontWeight: "bold"}),
Plot.ruleY([0])
]
})triT = d3.range(0, 1.0001, 0.01)
Plot.plot({
width: 1350, height: 300, marginLeft: 70,
style: {fontSize: "20px"},
x: {label: "t", domain: [0, 1], ticks: 10},
y: {label: "error", domain: [0, 1], grid: true, ticks: 4},
marks: [
Plot.line(triT, {x: d => d, y: d => Math.max(d * d, (1 - d) * (1 - d)), stroke: "#2F7A52", strokeWidth: 12, strokeOpacity: 0.3}),
Plot.line(triT, {x: d => d, y: d => d * d, stroke: "#3C7DD9", strokeWidth: 3.5}),
Plot.line(triT, {x: d => d, y: d => (1 - d) * (1 - d), stroke: "#C0392B", strokeWidth: 3.5}),
Plot.ruleX([tt], {stroke: "#202729", strokeDasharray: "8 6", strokeWidth: 2}),
Plot.dot([{y: tt * tt, c: "#3C7DD9"}, {y: (1 - tt) * (1 - tt), c: "#C0392B"}], {x: tt, y: "y", fill: "c", r: 9}),
Plot.ruleY([0])
]
})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} \]
\(\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\)?
\(\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} \]
\(\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\)?
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}\)
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.
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)\)
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\)
md`λ = **${pFix > 0 ? ((1 - pFix) / pFix).toFixed(2) : "∞"}**
t₁ = **${Number.isFinite(fixS.t1) ? fixS.t1.toFixed(2) : fixS.t1 < 0 ? "-∞" : "none"}**
t₂ = **${Number.isFinite(fixS.t2) ? fixS.t2.toFixed(2) : fixS.t2 > 0 ? "∞" : "none"}**
ε(1) = **${fixS.e1.toFixed(3)}**
ε(2) = **${fixS.e2.toFixed(3)}**
max = **${Math.max(fixS.e1, fixS.e2).toFixed(3)}**`g = ({m1: -5, s1: 2, m2: 1, s2: 5})
erf = x => {
const s = Math.sign(x), a = Math.abs(x), t = 1 / (1 + 0.3275911 * a);
return s * (1 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * Math.exp(-a * a));
}
cdf = (x, m, s) => 0.5 * (1 + erf((x - m) / (s * Math.SQRT2)))
bayesStrategy = p1 => {
if (p1 <= 0) return {t1: NaN, t2: NaN, e1: 1, e2: 0};
if (p1 >= 1) return {t1: -Infinity, t2: Infinity, e1: 0, e2: 1};
const L = Math.log(g.s1 / g.s2 * (1 - p1) / p1);
const a = g.s1 ** 2 - g.s2 ** 2, b = 2 * (g.s2 ** 2 * g.m1 - g.s1 ** 2 * g.m2);
const c = g.s1 ** 2 * g.m2 ** 2 - g.s2 ** 2 * g.m1 ** 2 - 2 * g.s1 ** 2 * g.s2 ** 2 * L;
const d = b * b - 4 * a * c;
if (d <= 0) return {t1: NaN, t2: NaN, e1: 1, e2: 0};
const r = [(-b - Math.sqrt(d)) / (2 * a), (-b + Math.sqrt(d)) / (2 * a)].sort((u, v) => u - v);
return {t1: r[0], t2: r[1],
e1: 1 - (cdf(r[1], g.m1, g.s1) - cdf(r[0], g.m1, g.s1)),
e2: cdf(r[1], g.m2, g.s2) - cdf(r[0], g.m2, g.s2)};
}
fixS = bayesStrategy(pFix)
riskCurve = d3.range(0, 1.0001, 0.005).map(p => {
const s = bayesStrategy(p);
return {p, e1: s.e1, e2: s.e2, bayes: p * s.e1 + (1 - p) * s.e2, worst: Math.max(s.e1, s.e2)};
})
Plot.plot({
width: 1350, height: 760, marginLeft: 70, marginBottom: 60,
style: {fontSize: "20px"},
x: {label: "prior p(1)", domain: [0, 1], ticks: 10},
y: {label: "risk", domain: [0, 0.52], grid: true, ticks: 5},
marks: [
Plot.line(riskCurve.filter(d => d.worst <= 0.52), {x: "p", y: "worst", stroke: "#2F7A52", strokeWidth: 13, strokeOpacity: 0.3}),
Plot.line(riskCurve.filter(d => d.e1 <= 0.52), {x: "p", y: "e1", stroke: "#3C7DD9", strokeWidth: 3}),
Plot.line(riskCurve.filter(d => d.e2 <= 0.52), {x: "p", y: "e2", stroke: "#C0392B", strokeWidth: 3}),
Plot.line(riskCurve, {x: "p", y: "bayes", stroke: "#202729", strokeWidth: 4}),
Plot.line([{p: 0, y: fixS.e2}, {p: 1, y: fixS.e1}], {x: "p", y: "y", stroke: "#E08A00", strokeWidth: 4, clip: true}),
Plot.dot([{p: pFix, y: pFix * fixS.e1 + (1 - pFix) * fixS.e2}], {x: "p", y: "y", fill: "#E08A00", r: 10}),
Plot.dot([{p: 0, y: fixS.e2, c: "#C0392B"}, {p: pFix, y: fixS.e2, c: "#C0392B"}, {p: 1, y: fixS.e1, c: "#3C7DD9"}, {p: pFix, y: fixS.e1, c: "#3C7DD9"}], {x: "p", y: "y", fill: "c", r: 9, clip: true}),
Plot.ruleX([pFix], {stroke: "#8A8F93", strokeDasharray: "3 5", strokeWidth: 2}),
Plot.ruleY([0])
]
})Search over the prior instead of \(\cl\)
\(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.
Part 1: discrete
minimax_strategy_discrete: strategy, \(i^*\) and the errors for all \(N + 1\) thresholdsclassify_discrete: labels for test imagesPart 2: continuous
risk_fix_q_cont: fixed strategy, all priorsworst_risk_cont: worst risk for one priorminimax_strategy_cont: the resultCopy your Lab 2 functions into minimax.py. Imports do not work in the BRUTE evaluation.