02 - Bayesian Decision Making

Pattern Recognition and Machine Learning, Lab 2

Michal Neoral

neoramic@fel.cvut.cz

2 October 2026

Motivation: licence plates

Table of Czech regions and their licence plate codes

Map of Czech regions

The second letter of an older plate tells the region (kraj).

Our simplification: recognize only the letters A (Praha) and C (Jihočeský kraj).

The data and the features

L
R
N images of 10 × 10 pixels

How do we get a (discrete) feature \(\cx\) from an image of a letter?

\(\color{#3C7DD9}{L}\) = sum of the pixel values in the left half

\(\color{#E08A00}{R}\) = sum of the pixel values in the right half

\[\cx = g(\color{#3C7DD9}{L} - \color{#E08A00}{R}) \in \cX = \{-10, \dots, 10\}\]

\(g\) quantizes the difference to a small set of integers

The Bayesian risk

How do we define the Bayesian risk \(R(\cq)\) of a strategy \(\cq\)?

\[ R(\cq) = \sum_{\cx \in \cX} \sum_{\ck \in \cK} \cpXK(\cx, \ck)\; \cW(\ck, \cq(\cx)) \]

Symbol Meaning Licence plates Mushrooms
\(\cX\) observations \(\cx \in \cX\): what we know \(\cx = g(L - R)\) cap colour, smell, ring
\(\cK\) hidden states \(\ck \in \cK\): what we don't know letters \(\{\cA, \cC\}\) edible, poisonous
\(\cD\) decisions \(\cd \in \cD\) \(\cD = \cK = \{\cA, \cC\}\) eat, leave it (\(\cD \ne \cK\))
\(\cpXK\) joint probability \(\cpXK(\cx, \ck)\) feature \(\cx\) with letter \(\ck\) a red smelly one is poisonous
\(\cW\) loss \(\cW(\ck, \cd)\): price of \(\cd\) when the truth is \(\ck\) 0/1 loss poisonous eaten: hospital
\(\cq\) strategy \(\cq: \cX \to \cD\) A or C for every \(\cx\) "red with dots: leave it"

What is the Bayesian (optimal) strategy \(\cq^*\)?

What do we know?

Known

  • \(\cp(\cx \mid \ck)\): likelihood
  • \(\cp(\ck)\): prior

Unknown

  • \(\cpXK(\cx, \ck)\): joint probability
  • it appears in the risk \(R(\cq)\)

How do we get \(\cpXK(\cx, \ck)\)?

Derivation: the joint probability

Product rule \(\cpXK(\cx, \ck) = \cp(\cx \mid \ck)\, \cp(\ck) = \cp(\ck \mid \cx)\, \cp(\cx)\)

Marginal \(\cp(\cx) = \sum_{\ck \in \cK} \cpXK(\cx, \ck) = \sum_{\ck \in \cK} \cp(\cx \mid \ck)\, \cp(\ck)\)

Bayes' rule \(\cp(\ck \mid \cx) = \dfrac{\cp(\cx \mid \ck)\, \cp(\ck)}{\cp(\cx)}\)

The risk now contains only known quantities: \[ R(\cq) = \sum_{\cx \in \cX} \sum_{\ck \in \cK} \cp(\cx \mid \ck)\, \cp(\ck)\, \cW(\ck, \cq(\cx)) \]

Discrete example

Bayes example: discrete \(\cx\)

\(\cx = g(L - R) \in \cX = \{x_1, x_2, x_3, x_4\}\), \(\quad \cK = \cD = \{\cA, \cC\}\) Priors: \(\;\cp(\cA) = \cp(\cC) = 0.5\)

Likelihoods \(\cp(\cx \mid \ck)\)

\(x_1\) \(x_2\) \(x_3\) \(x_4\)
\(\cA\) 0.3 0.2 0.1 0.4
\(\cC\) 0.7 0.2 0.1 0

Loss \(\cW(\ck, \cd)\)

\(\ck \backslash \cd\) \(\cA\) \(\cC\)
\(\cA\) 0 1
\(\cC\) 1 0

Strategy \(\cq(\cx)\)

\(x_1\) \(x_2\) \(x_3\) \(x_4\)
A A C C

1. What is the risk \(R(\cq)\) of this strategy?

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

3. How do we determine the classification error?

Derivation: risk of the given strategy

\[ R(\cq) = \sum_{\cx \in \cX} \sum_{\ck \in \cK} \cp(\cx \mid \ck)\, \cp(\ck)\, \cW(\ck, \cq(\cx)) \]

With the 0/1 loss, only the wrong classes \(\ck \ne \cq(\cx)\) contribute: \[ R(\cq) = \sum_{\cx \in \cX} \sum_{\ck \ne \cq(\cx)} \cp(\cx \mid \ck)\, \cp(\ck) \]

\(x_1\) \(x_2\) \(x_3\) \(x_4\)
decision \(\cq(\cx)\) A A C C
wrong class \(\ck\) C C A A
\(\cp(\cx \mid \ck)\, \cp(\ck)\) \(0.7 \cdot 0.5 = 0.35\) \(0.2 \cdot 0.5 = 0.1\) \(0.1 \cdot 0.5 = 0.05\) \(0.4 \cdot 0.5 = 0.2\)

\[ R(\cq) = 0.35 + 0.1 + 0.05 + 0.2 = \mathbf{0.7} \]

Derivation: the optimal strategy

\[ R(\cq) = \sum_{\cx \in \cX} \underbrace{\sum_{\ck \in \cK} \cp(\cx \mid \ck)\, \cp(\ck)\, \cW(\ck, \cq(\cx))}_{R(\cx,\, \cq(\cx)) \text{: partial risk}} \]

Each term depends only on the single decision \(\cq(\cx)\), so we minimize every term separately: \[ \min_{\cq} R(\cq) = \sum_{\cx \in \cX} \min_{\cd \in \cD} R(\cx, \cd), \qquad \cq^*(\cx) = \arg\min_{\cd \in \cD} R(\cx, \cd) \]

For the 0/1 loss and two classes: \[ R(\cx, \cA) = \cp(\cx \mid \cC)\, \cp(\cC), \qquad R(\cx, \cC) = \cp(\cx \mid \cA)\, \cp(\cA) \]

Example for \(x_1\): \[ R(x_1, \cA) = 0.7 \cdot 0.5 = 0.35, \qquad R(x_1, \cC) = 0.3 \cdot 0.5 = \mathbf{0.15}, \qquad \cq^*(x_1) = \cC \]

Discrete example: results

1. Risk of the given strategy \[ R(\cq) = \cp(\cA)\,(0.1 + 0.4) + \cp(\cC)\,(0.7 + 0.2) = \mathbf{0.7} \]

3. Classification error \[ \text{error} = \frac{\#\,\text{misclassified}}{\#\,\text{all} = N} \]

2. Partial risk and the optimal strategy \[ R(\cx, \cd) = \sum_{\ck \in \cK} \cp(\cx \mid \ck)\, \cp(\ck)\, \cW(\ck, \cd), \qquad \cq^*(\cx) = \arg\min_{\cd \in \cD} R(\cx, \cd) \]

\(x_1\) \(x_2\) \(x_3\) \(x_4\)
\(R(\cx, \cA)\) 0.35 0.1 0.05 0
\(R(\cx, \cC)\) 0.15 0.1 0.05 0.2
\(\cq^*(\cx)\) C A or C A or C A

\(R(\cq^*) = 0.15 + 0.1 + 0.05 + 0 = \mathbf{0.3}\). For \(x_2, x_3\) both decisions are optimal (a tie), a preference must be agreed on.

Continuous x

Bayes example: continuous \(\cx\)

\(\cx = L - R \in \cX = \mathbb{R}\) is now continuous

Likelihood: normal distribution \[ \cp(\cx \mid \ck) = \frac{1}{\sigma_{\ck} \sqrt{2\pi}} \exp\left(-\frac{(\cx - \mu_{\ck})^2}{2\sigma_{\ck}^2}\right) \]

Given: \(\muA,\ \muC,\ \sA,\ \sC\), \(\;\cp(\cA),\ \cp(\cC)\), \(\;\cW\) = 0/1 loss

How does the Bayesian risk \(R(\cq)\) change for a continuous \(\cx\)?

What is the optimal strategy \(\cq^*(\cx)\)?

Derivation: optimal strategy, continuous \(\cx\)

\[ R(\cq) = \int_{\cX} \sum_{\ck \in \cK} \cpXK(\cx, \ck)\, \cW(\ck, \cq(\cx))\, d\cx = \int_{\cX} \cp(\cx) \sum_{\ck \in \cK} \cp(\ck \mid \cx)\, \cW(\ck, \cq(\cx))\, d\cx \]

\(\cp(\cx) \ge 0\), so we minimize the integrand separately for every \(\cx\): \[ \cq^*(\cx) = \arg\min_{\cd \in \cD} \sum_{\ck \in \cK} \cp(\ck \mid \cx)\, \cW(\ck, \cd) \]

For the 0/1 loss: \(\;\sum_{\ck \ne \cd} \cp(\ck \mid \cx) = 1 - \cp(\cd \mid \cx)\), therefore \[ \cq^*(\cx) = \arg\max_{\cd \in \cD} \cp(\cd \mid \cx) = \arg\max_{\cd \in \cD} \frac{\cp(\cx \mid \cd)\, \cp(\cd)}{\cp(\cx)} = \arg\max_{\cd \in \cD} \cp(\cx \mid \cd)\, \cp(\cd) \]

Two classes: a quadratic inequality

We decide \(\cA\) where \(\;\cp(\cx \mid \cA)\, \cp(\cA) \ge \cp(\cx \mid \cC)\, \cp(\cC)\):

\[ \cp(\cA)\, \frac{1}{\sA \sqrt{2\pi}}\, e^{-\frac{(\cx - \muA)^2}{2\sA^2}} \;\ge\; \cp(\cC)\, \frac{1}{\sC \sqrt{2\pi}}\, e^{-\frac{(\cx - \muC)^2}{2\sC^2}} \]

\(\sqrt{2\pi}\) cancels, both sides are positive, so we take the logarithm: \[ \ln \cp(\cA) - \ln \sA - \frac{(\cx - \muA)^2}{2\sA^2} \;\ge\; \ln \cp(\cC) - \ln \sC - \frac{(\cx - \muC)^2}{2\sC^2} \]

Move the terms without \(\cx\) to the right: \[ -\frac{(\cx - \muA)^2}{2\sA^2} + \frac{(\cx - \muC)^2}{2\sC^2} \;\ge\; \ln\!\left(\frac{\sA}{\sC} \cdot \frac{\cp(\cC)}{\cp(\cA)}\right) =: L \]

Two classes: collecting the powers of \(\cx\)

\[ -\frac{(\cx - \muA)^2}{2\sA^2} + \frac{(\cx - \muC)^2}{2\sC^2} \;\ge\; L \qquad \Big|\; \cdot\, 2\sA^2 \sC^2 > 0 \]

\[ -\sC^2 (\cx - \muA)^2 + \sA^2 (\cx - \muC)^2 \;\ge\; 2 \sA^2 \sC^2 L \]

Expand the squares: \[ -\sC^2 \cx^2 + 2\sC^2 \muA \cx - \sC^2 \muA^2 + \sA^2 \cx^2 - 2\sA^2 \muC \cx + \sA^2 \muC^2 - 2 \sA^2 \sC^2 L \;\ge\; 0 \]

Group by the powers of \(\cx\): \[ \bbox[#FFF1A8,4px]{(\sA^2 - \sC^2)}\, \cx^2 + \bbox[#C9F0D4,4px]{2(\sC^2 \muA - \sA^2 \muC)}\, \cx + \bbox[#C6EEF6,4px]{\sA^2 \muC^2 - \sC^2 \muA^2 - 2 \sA^2 \sC^2 L} \;\ge\; 0 \]

\(a = \sA^2 - \sC^2\) \(b = 2(\sC^2 \muA - \sA^2 \muC)\) \(c = \sA^2 \muC^2 - \sC^2 \muA^2 - 2 \sA^2 \sC^2 L\)

Decision regions

Class likelihoods with their means and standard deviations

Likelihoods multiplied by the priors

Decision regions C, A, C with thresholds t1 and t2

The same thresholds are the roots of the quadratic

Likelihoods \(\cp(\cx \mid \ck)\) with means \(\mu_{\ck}\) and standard deviations \(\sigma_{\ck}\)

Multiply by the priors: \(\cp(\cx \mid \ck)\, \cp(\ck)\)

Decide the class with the larger \(\cp(\cx \mid \ck)\, \cp(\ck)\): thresholds \(t_1, t_2\)

The thresholds are the roots of \(a\cx^2 + b\cx + c = 0\), class \(\cA\) where it is \(\ge 0\)

Decision regions: try it

Watch out: all the cases of \(a\cx^2 + b\cx + c \ge 0\)

All cases of a quadratic inequality: a > 0, a < 0, a = 0 with two, one or no roots

Which intervals belong to class \(\cA\) in each case?

Handle every case in your code, including \(a = 0\) and no real roots.

Output of the assignment

Number line with thresholds t1 and t2 and three decisions

The strategy is stored as two thresholds q['t1'] <= q['t2'] and the decisions for the three intervals, e.g. q['decision'] = ['C', 'A', 'C'].

The risk for continuous \(\cx\)

Decision regions C, A, C with thresholds t1 and t2

The same regions with the integrals of the decided class under each region

Compute the risk \(R(\cq)\) of your strategy.

Hint: the area under a normal density up to \(t\) is scipy.stats.norm.cdf(t, mu, sigma)

\[ R(\cq) = 1 - \Big( \int_{-\infty}^{t_1} \!\cp(\cC)\,\cp(\cx|\cC)\,d\cx + \int_{t_1}^{t_2} \!\cp(\cA)\,\cp(\cx|\cA)\,d\cx + \int_{t_2}^{\infty} \!\cp(\cC)\,\cp(\cx|\cC)\,d\cx \Big) \]

Derivation: the risk for continuous \(\cx\)

\[ R(\cq) = \int_{-\infty}^{\infty} \sum_{\ck \in \cK} \cp(\ck)\, \cp(\cx \mid \ck)\, \cW(\ck, \cq(\cx))\, d\cx \]

0/1 loss: all classes except the decided one \(\cq(\cx)\): \[ R(\cq) = \int_{-\infty}^{\infty} \Big( \sum_{\ck \in \cK} \cp(\ck)\, \cp(\cx \mid \ck) \;-\; \cp(\cq(\cx))\, \cp(\cx \mid \cq(\cx)) \Big)\, d\cx \]

The first part integrates to one: \[ \int_{-\infty}^{\infty} \sum_{\ck} \cp(\ck)\, \cp(\cx \mid \ck)\, d\cx = \sum_{\ck} \cp(\ck) \int_{-\infty}^{\infty} \cp(\cx \mid \ck)\, d\cx = \sum_{\ck} \cp(\ck) = 1 \]

\[ R(\cq) = 1 - \int_{-\infty}^{\infty} \cp(\cq(\cx))\, \cp(\cx \mid \cq(\cx))\, d\cx \] The integral is the probability of a correct decision.

Derivation: splitting into intervals

\(\cq\) is constant on the intervals \((t_{i-1}, t_i)\) with decisions \(\cd_1, \cd_2, \cd_3\), where \(t_0 = -\infty\), \(t_3 = \infty\): \[ R(\cq) = 1 - \sum_{i=1}^{3} \cp(\cd_i) \int_{t_{i-1}}^{t_i} \cp(\cx \mid \cd_i)\, d\cx \]

Cumulative distribution function of class \(\ck\), in code norm.cdf(t, mu, sigma): \[ F_{\ck}(t) = \int_{-\infty}^{t} \cp(\cx \mid \ck)\, d\cx, \qquad \int_{a}^{b} \cp(\cx \mid \ck)\, d\cx = F_{\ck}(b) - F_{\ck}(a) \] with \(F_{\ck}(-\infty) = 0\) and \(F_{\ck}(\infty) = 1\).

For the decisions \(\cC \mid \cA \mid \cC\): \[ R(\cq) = 1 - \Big( \cp(\cC)\, F_{\cC}(t_1) + \cp(\cA) \big( F_{\cA}(t_2) - F_{\cA}(t_1) \big) + \cp(\cC) \big( 1 - F_{\cC}(t_2) \big) \Big) \]

Derivation: a numerical example

Parameters: \(\muA = 0,\ \sA = 1,\ \muC = 2,\ \sC = 2,\ \cp(\cA) = 0.5\)

Thresholds: \(t_1 = -2.571,\ t_2 = 1.238\), decisions \(\cC \mid \cA \mid \cC\)

\[ F_{\cC}(t_1) = 0.0111,\quad F_{\cA}(t_1) = 0.0051,\quad F_{\cA}(t_2) = 0.8921,\quad F_{\cC}(t_2) = 0.3515 \]

\[ R(\cq) = 1 - 0.5\,\big( 0.0111 + (0.8921 - 0.0051) + (1 - 0.3515) \big) = 1 - 0.7733 = \mathbf{0.2267} \]

Check: the areas of the wrong class give the same result: \[ R(\cq) = \cp(\cA) \big( F_{\cA}(t_1) + 1 - F_{\cA}(t_2) \big) + \cp(\cC) \big( F_{\cC}(t_2) - F_{\cC}(t_1) \big) = 0.2267 \]

Take-away for the lab

  • Discrete \(\cx\): risk of a given strategy, the optimal strategy, classification error
  • Continuous \(\cx\): coefficients \(a\), \(b\), \(c\) of the quadratic inequality
  • Thresholds t1, t2 and the decisions for the three intervals
  • Risk of the strategy using scipy.stats.norm.cdf
  • Watch out for all the cases (\(a = 0\), one or no real root)