B4B33RPZ · Pattern Recognition and Machine Learning

Bayesian Decision Making

Lab 2: Bayesian risk, the optimal strategy, and two worked examples
Klára JanouškováFEE CTU, Prague
Part I

Recap

Warm-up quiz: probability from lab 1

A test finds 90 % of sick patients. Which probability is 0.9?

We know the patient is sick (right of the bar) and ask about the test result.

P(H)=0.5P(H) = 0.5, P(E∣H)=0.6P(E \mid H) = 0.6, P(E∣¬H)=0.2P(E \mid \neg H) = 0.2. What is P(H∣E)P(H \mid E)?

P(H∣E)=P(E∣H) P(H)P(E∣H) P(H)+P(E∣¬H) P(¬H)P(H \mid E) = \frac{P(E \mid H)\,P(H)}{P(E \mid H)\,P(H) + P(E \mid \neg H)\,P(\neg H)} =0.6⋅0.50.6⋅0.5+0.2⋅0.5=0.30.4=0.75= \frac{0.6 \cdot 0.5}{0.6 \cdot 0.5 + 0.2 \cdot 0.5} = \frac{0.3}{0.4} = 0.75 The wolf and the pens from lab 1.

Warm-up quiz, continued

Which of these always sums to 1?

A conditional distribution sums to 1 over the variable left of the bar.

You roll a fair die and win the number shown in CZK. Expected win?

∑i=1616 i=3.5\sum_{i=1}^{6} \tfrac{1}{6}\, i = 3.5. Today's risk is the same kind of average, of penalties.

Can a probability density p(x)p(x) be larger than 1?

Only the area under it must be 1. A narrow Gaussian peaks above 1.

Statistical pattern recognition: the mushroom case

You find a mushroom in the forest. Eat it or not? What is each symbol here?

SymbolMeaningMushroom example
XXobservations x∈Xx \in X: what we know (measurement, feature vector)cap colour, smell, ring… and context: rain, temperature, month, place
KKhidden states (classes) k∈Kk \in K: what we don't knowedible / poisonous
DDpossible decisions (actions)eat / leave it
pXKp_{XK}joint probability pXK(x,k)p_{XK}(x, k) of seeing xx when the state is kkhow often a red smelly one is poisonous
WWpenalty (loss) W(k,d)W(k, d): the price of decision dd when the truth is kkate a poisonous one: hospital
qqstrategy (decision function) q ⁣:X→Dq\colon X \to Dyour rule: "red with white dots → leave it"
rain 12 mm14 °COctoberŠumava

Why do we need a penalty WW?

Penalty W(k,d)W(k, d) for the mushroom:

truth kk \ decision ddeatleave it
edible01 (no dinner)
poisonous1000 (hospital)0

Question: the mushroom is 95 % likely edible. Should you eat it?

No: 5 % × 1000 = 50 is far worse than 95 % × 1 = 0.95.
Picking the most probable class is not enough. We need the expected penalty of each decision.

How good is a strategy qq? The Bayesian risk

Question: over many mushrooms, what penalty does strategy qq pay on average?

One pair (x,k)(x, k): we decide q(x)q(x) and pay W(k,q(x))W(k, q(x)).

This pair occurs with probability pXK(x,k)p_{XK}(x, k).

Average = sum over all pairs, weighted by probability: the Bayesian risk

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))

This is the standard expectation Ez∼p[f(z)]=∑zp(z) f(z)\mathbb{E}_{z \sim p}[f(z)] = \sum_z p(z)\, f(z), with z=(x,k)z = (x, k):

R(q)⏟E[f(z)]=∑x∈X∑k∈K⏟∑z  pXK(x,k)⏟p(z)  W(k,q(x))⏟f(z)\underbrace{R(q)}_{\mathbb{E}[f(z)]} = \underbrace{\sum_{x \in X} \sum_{k \in K}}_{\sum_z}\; \underbrace{p_{XK}(x, k)}_{p(z)}\; \underbrace{W(k, q(x))}_{f(z)}

Is R(q)R(q) bounded?

Yes: min⁡W≤R(q)≤max⁡W\min W \le R(q) \le \max W, a weighted mean with weights summing to 1.

The Bayesian strategy

Question: we can score any strategy with R(q)R(q). Which one do we want?

Bayesian strategy: smallest risk
q∗=argmin⁡q ⁣:X→DR(q)q^* = \operatorname*{argmin}_{q\colon X \to D} R(q)
The minimum is over all strategies: whole functions q ⁣:X→Dq\colon X \to D, one decision for every xx.

How many strategies are there? Say xx is just the cap colour (4 values), DD = {eat, leave}.

In general ∣D∣∣X∣|D|^{|X|}. The lab has 21 values of xx: 221≈22^{21} \approx 2 million. Trying them all does not scale. Next: we don't have to.
observation x (cap colour)redeatleave×browneatleave×whiteeatleave×yelloweatleave2 · 2 · 2 · 2 = 16 strategiesall 16 strategies q (one cell per colour)EEEEEEELEELEEELLELEEELELELLEELLLLEEELEELLELELELLLLEELLELLLLELLLLE = eat (solid), L = leave (dashed)

Finding the strategy q∗q^*

Question: must we try all ∣D∣∣X∣|D|^{|X|} strategies? No, decide each xx separately

Start from the definition:

R(q∗)=min⁡q ⁣:X→D∑x∈X∑k∈KpXK(x,k) W(k,q(x))R(q^*) = \displaystyle\min_{q\colon X \to D} \sum_{x \in X} \sum_{k \in K} p_{XK}(x, k)\, W(k, q(x))

↓ Recall q ⁣:X→Dq\colon X \to D: a strategy is one decision q(x)∈Dq(x) \in D for every xx, and q(x)q(x) only appears in the term of its own xx. So each term can be minimised on its own: the min moves inside the sum.

=∑x∈Xmin⁡q(x)∈D∑k∈KpXK(x,k) W(k,q(x))= \displaystyle\sum_{x \in X} \min_{q(x) \in D} \sum_{k \in K} p_{XK}(x, k)\, W(k, q(x))

↓ For a fixed xx, the value q(x)q(x) is just some decision: call it dd and minimise over d∈Dd \in D. Split the joint: pXK(x,k)=p(x) pK∣X(k∣x)p_{XK}(x, k) = p(x)\, p_{K|X}(k \mid x). The factor p(x)p(x) is the same for every dd, so it can go in front of the min: multiplying all options by the same non-negative number does not change which one is smallest.

=∑x∈Xp(x)min⁡d∈D∑k∈KpK∣X(k∣x) W(k,d)⏟R(x,d)= \displaystyle\sum_{x \in X} p(x) \min_{d \in D} \underbrace{\sum_{k \in K} p_{K|X}(k \mid x)\, W(k, d)}_{R(x, d)}
Partial risk
R(x,d)=∑kpK∣X(k∣x) W(k,d)R(x, d) = \sum_{k} p_{K|X}(k \mid x)\, W(k, d)
expected penalty of dd once we have seen xx
Optimal strategy
q∗(x)=argmin⁡d∈DR(x,d)q^*(x) = \operatorname*{argmin}_{d \in D} R(x, d)
for every xx on its own

The 0/1 loss: which decision is best?

Question: if every mistake costs the same, which decision is best?

0/1 loss (D=KD = K):

W(k,d)={0if d=k1if d≠kW(k, d) = \begin{cases} 0 & \text{if } d = k \\ 1 & \text{if } d \ne k \end{cases}

↓ Plug WW into R(x,d)=∑kp(k∣x) W(k,d)R(x, d) = \sum_k p(k \mid x)\, W(k, d): the term k=dk = d is multiplied by 0, all others by 1.

R(x,d)=∑k≠dpK∣X(k∣x)R(x, d) = \sum_{k \ne d} p_{K|X}(k \mid x)

↓ The posterior sums to 1 over all kk, so the sum over k≠dk \ne d is 1 minus the missing term.

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

↓ Minimising 1−p(d∣x)1 - p(d \mid x) is the same as maximising p(d∣x)p(d \mid x).

q∗(x)=argmax⁡k∈KpK∣X(k∣x)q^*(x) = \operatorname*{argmax}_{k \in K} p_{K|X}(k \mid x)

↓ Bayes' rule: p(k∣x)=p(x∣k) p(k)/p(x)p(k \mid x) = p(x \mid k)\, p(k) / p(x). p(x)p(x) is the same for every kk, so it does not change the argmax.

q∗(x)=argmax⁡k∈Kp(x∣k) p(k)q^*(x) = \operatorname*{argmax}_{k \in K} p(x \mid k)\, p(k)
With the 0/1 loss, R(q)R(q) = probability of a wrong decision.

Quick check

In q∗=argmin⁡qR(q)q^* = \operatorname{argmin}_q R(q), the argmin runs over…

q∗q^* is a whole function. It just happens that we can find it one xx at a time.

With the 0/1 loss, R(q)R(q) is…

An average of 0s and 1s is a probability.

p(poisonous∣x)=0.01p(\text{poisonous} \mid x) = 0.01, penalties as before (1000 / 1). Eat?

R(x,eat)=0.01⋅1000=10R(x, \text{eat}) = 0.01 \cdot 1000 = 10, R(x,leave)=0.99⋅1=0.99R(x, \text{leave}) = 0.99 \cdot 1 = 0.99.

Part II

Exercise: letters on licence plates

CZ region recognition by licence plates

The second character of a plate is the region (kraj):

1A7 4551A = Praha
7C2 5025C = Jihočeský kraj
3J1 2272J = Vysočina
Our simplification: recognise only A and C.

Question: you stand at a crossroads in South Bohemia. Before looking at a plate: A or C?

C is much more likely: that is the prior p(k)p(k).

Feature design: where does p(x∣k)p(x \mid k) come from?

So far p(x∣k)p(x \mid k) was given for every discrete xx. We only have images: N×10×10N \times 10 \times 10 pixels.

Question: which single number xx would separate A from C?

  • sum of all pixels (ink)
  • the central pixel
  • top − bottom
  • left − right
Left vs. right: C is open on the right, A is symmetric.

Today, a toy version: x=g(R−L)x = g(R - L), where gg normalises, clips and rounds to X={0,1,2,3}X = \{0, 1, 2, 3\}.

In the lab: L−RL - R, normalised and clipped to 21 values {−10,…,10}\{-10, \dots, 10\}.

LRL = 24, R = 24R − L = 0LRL = 22, R = 10R − L = -12

Bayes example: discrete xx

x=g(R−L)x = g(R - L),   X={0,1,2,3}X = \{0, 1, 2, 3\}
K={A,C}K = \{A, C\}
q ⁣:X→Dq\colon X \to D,   D=KD = K
W ⁣:K×D→RW\colon K \times D \to \mathbb{R}, 0/1 loss
P(A)=P(C)=0.5P(A) = P(C) = 0.5 (uniform, uninformed priors)
xx0123
P(x∣A)P(x \mid A)0.30.20.10.4
P(x∣C)P(x \mid C)0.70.20.10
W0/1W_{0/1}: kk \ ddAC
A01
C10
Question: for each xx, which letter should we answer? And how often will we be wrong?

Quick check

Now P(A)=0.25P(A) = 0.25, P(C)=0.75P(C) = 0.75. What is q∗(1)q^*(1)?

0.2⋅0.25=0.05<0.2⋅0.75=0.150.2 \cdot 0.25 = 0.05 < 0.2 \cdot 0.75 = 0.15: the prior breaks the tie.

Still P(A)=0.25P(A) = 0.25. What is q∗(3)q^*(3)?

p(3∣C)=0p(3 \mid C) = 0: C never produces x=3x = 3, so any prior loses.

Uniform priors, but saying C when it is A costs 10. What is q∗(0)q^*(0)?

p(A∣0)=0.3p(A \mid 0) = 0.3: R(0,C)=0.3⋅10=3R(0, C) = 0.3 \cdot 10 = 3, R(0,A)=0.7⋅1=0.7R(0, A) = 0.7 \cdot 1 = 0.7.

Part III

Continuous xx: Gaussian classes

Bayes example: continuous xx

Question: x∈Rx \in \mathbb{R} now. What changes in the risk?

Sums over xx become integrals: R(q)=∫X∑k∈KpXK(x,k) W(k,q(x)) dxR(q) = \int_X \sum_{k \in K} p_{XK}(x, k)\, W(k, q(x))\, dx
Each class is a Gaussian: p(x∣k)=12πσk2exp⁡ ⁣(−(x−μk)22σk2)p(x \mid k) = \frac{1}{\sqrt{2\pi\sigma_k^2}} \exp\!\left(-\frac{(x - \mu_k)^2}{2\sigma_k^2}\right)

Given: μA,μC,σA,σC,p(A),p(C)\mu_A, \mu_C, \sigma_A, \sigma_C, p(A), p(C), 0/1 loss. Still q∗(x)=argmax⁡kp(x∣k) p(k)q^*(x) = \operatorname{argmax}_k p(x \mid k)\,p(k).

Where do we switch from A to C?

Where the two weighted curves cross.

xp(x|A) p(A)p(x|C) p(C)decide Adecide Cthreshold

Quadratic equations and inequalities

Roots of ax2+bx+c=0a x^2 + b x + c = 0:

x1,2=−b±Δ2a,Δ=b2−4acx_{1,2} = \frac{-b \pm \sqrt{\Delta}}{2a}, \qquad \Delta = b^2 - 4ac

Where is ax2+bx+c>0a x^2 + b x + c > 0? It depends on the sign of aa (picture).

In our case: a>0⇔σA>σCa > 0 \Leftrightarrow \sigma_A > \sigma_C. The wider class wins on both tails.

x₁x₂a > 0> 0 outside the rootsx₁x₂a < 0> 0 between the roots
  • Δ>0\Delta > 0: two roots x1<x2x_1 < x_2, the sign changes at both: two thresholds.
  • Δ=0\Delta = 0: one double root x0=−b2ax_0 = -\tfrac{b}{2a}; the parabola only touches the axis, so the sign does not change: one class everywhere (a tie only at x0x_0).
  • Δ<0\Delta < 0: no roots, the sign never changes: one class everywhere.

Lab (find_strategy_2normal): thresholds t1≤t2t_1 \le t_2 and a decision for each of the 3 intervals. Handle a=0a = 0, Δ=0\Delta = 0 and Δ<0\Delta < 0.  Play with it: desmos.com/calculator/qa60j7hx6u

Try it: two Gaussian classes

How do we compute the risk?

Question: with the 0/1 loss, R(q)R(q) = P(mistake). How do we compute it?

ii numbers the intervals X1,X2,X3X_1, X_2, X_3 (right); in XiX_i we answer qiq_i: q1=Aq_1 = A, q2=Cq_2 = C, q3=Aq_3 = A.

↓ Intuitive: in XiX_i we are wrong if the true class is any k≠qik \ne q_i (hatched):

R(q)=∑i=13⏟intervals  ∑k≠qi⏟wrong classesp(k)∫Xip(x∣k) dxR(q) = \underbrace{\sum_{i=1}^{3}}_{\text{intervals}} \; \underbrace{\sum_{k \ne q_i}}_{\text{wrong classes}} p(k) \int_{X_i} p(x \mid k)\, dx

Problem: K−1K - 1 integrals per interval (below: K=3K = 3).

↓ In XiX_i exactly one class is right, qiq_i (shaded); right + wrong = 1:

R(q)=1−∑i=13p(qi)∫Xip(x∣qi) dxR(q) = 1 - \sum_{i=1}^{3} p(q_i) \int_{X_i} p(x \mid q_i)\, dx
xshaded = correcthatched = mistakesp(x|A) p(A)p(x|C) p(C)t₁t₂X₁: say AX₂: say CX₃: say A AC (say)TK = 3, one interval: 2 hatched areas to add

↓ Area between thresholds = area left of t2t_2 − area left of t1t_1: ∫t1t2p(x∣k) dx=Fk(t2)−Fk(t1)\int_{t_1}^{t_2} p(x \mid k)\, dx = F_k(t_2) - F_k(t_1)

Code: norm.cdf(t2, mu, sd) - norm.cdf(t1, mu, sd)

Quick check

σA=σC\sigma_A = \sigma_C, p(A)=p(C)p(A) = p(C). Where is the threshold?

a=0a = 0 and the ln⁡\ln term is 0: halfway between the means.

Now p(A)p(A) grows. The threshold moves…

A becomes more likely a priori, so A's region grows.

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

The wider Gaussian decays more slowly: a>0a > 0, A wins outside the roots. The lab data is this case.

Take-away for the lab

  • Bayesian risk R(q)R(q) = expected penalty of a strategy.
  • Minimise it per observation: q∗(x)=argmin⁡dR(x,d)q^*(x) = \operatorname{argmin}_d R(x, d) (partial risk).
  • 0/1 loss: q∗(x)=argmax⁡kp(x∣k) p(k)q^*(x) = \operatorname{argmax}_k p(x \mid k)\,p(k), and R(q∗)R(q^*) is the error probability.
  • Gaussian classes: the boundary solves a quadratic inequality; one threshold if σA=σC\sigma_A = \sigma_C, up to two otherwise.
In bayes.py
Discrete (21 values of xx):
bayes_risk_discrete, find_strategy_discrete (ties → A), classify_discrete, classification_error
Continuous (two Gaussians):
find_strategy_2normal (t1t_1, t2t_2, 3 decisions), bayes_risk_2normal, classify_2normal
Try both losses: W1=[0110]W_1 = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} and W2=[0510]W_2 = \begin{bmatrix} 0 & 5 \\ 1 & 0 \end{bmatrix}.
Optional

Extra: loss vs. prior

What does a different loss do?

Question: the lab also uses W2W_2, where one kind of mistake costs 5. How does that move the decision?

Costs W(A,C)=wACW(A, C) = w_{AC} (true A, said C), W(C,A)=wCAW(C, A) = w_{CA}, 0 when correct.

↓ In R(x,d)=∑kp(k∣x) W(k,d)R(x, d) = \sum_k p(k \mid x)\, W(k, d) only the wrong class survives:

R(x,A)=p(C∣x) wCAR(x, A) = p(C \mid x)\, w_{CA} R(x,C)=p(A∣x) wACR(x, C) = p(A \mid x)\, w_{AC}

↓ Decide A when R(x,A)<R(x,C)R(x, A) < R(x, C); write p(k∣x)=p(x∣k) p(k)/p(x)p(k \mid x) = p(x \mid k)\, p(k) / p(x) and cancel p(x)p(x):

p(x∣A) p(A) wAC  >  p(x∣C) p(C) wCAp(x \mid A)\, p(A)\, w_{AC} \;>\; p(x \mid C)\, p(C)\, w_{CA}
Divide by p(x∣C) p(C) wCAp(x \mid C)\, p(C)\, w_{CA}: decide A when p(x∣A)p(x∣C)⏟likelihood ratio⋅p(A)p(C)⏟prior odds⋅wACwCA⏟loss ratio  >  1\underbrace{\frac{p(x \mid A)}{p(x \mid C)}}_{\text{likelihood ratio}} \cdot \underbrace{\frac{p(A)}{p(C)}}_{\text{prior odds}} \cdot \underbrace{\frac{w_{AC}}{w_{CA}}}_{\text{loss ratio}} \;>\; 1 The loss ratio multiplies the prior odds: an expensive mistake behaves like a more likely class. For Gaussians only cc changes: c→c+ln⁡(wAC/wCA)c \to c + \ln (w_{AC} / w_{CA}).

Other losses in the continuous case

Question: two Gaussians (μA=0\mu_A = 0, μC=3\mu_C = 3, σ=1\sigma = 1, equal priors). Calling a true A "C" now costs 5 (wAC=5w_{AC} = 5, wCA=1w_{CA} = 1). Where does the threshold go?

0/1 loss: equal σ\sigma and priors → halfway, t=1.5t = 1.5.

↓ With costs we compare the curves scaled by the cost of the other mistake (previous slide):

p(x∣A) p(A) wAC  >  p(x∣C) p(C) wCAp(x \mid A)\, p(A)\, w_{AC} \;>\; p(x \mid C)\, p(C)\, w_{CA}

↓ Take ln⁡\ln as before; only cc changes, c→c+ln⁡5c \to c + \ln 5, and −3x+c>0-3x + c > 0 gives

t=(4.5+ln⁡5)/3≈2.04t = (4.5 + \ln 5) / 3 \approx 2.04: the A region grows.

↓ Risk ≠ error probability, so no "1 − P(correct)"; weight each mistake by its cost:

R(q)=∑i∑k≠qiW(k,qi) p(k)∫Xip(x∣k) dxR(q) = \sum_{i} \sum_{k \ne q_i} W(k, q_i)\, p(k) \int_{X_i} p(x \mid k)\, dx
xp(x|A) p(A)p(x|C) p(C)0/1: 1.50× w_AC = 5w_AC = 5: 2.04