05 - MLE, MAP, Bayes

Pattern Recognition and Machine Learning, Lab 5

Michal Neoral

neoramic@fel.cvut.cz

23 October 2026

Recap

What do we know about the classes?

Bayesian task

  • \(\cp(\cx \mid \ck)\) known
  • \(\cp(\ck)\), \(\cW(\ck, \cd)\) known

We know everything.

Parzen windows

  • \(\cp(\cx \mid \ck)\) unknown
  • any shape

Non-parametric: histogram, \(k\)-NN, kernels.

last lab

Today

  • \(\cp(\cx \mid \ck)\) unknown
  • known form \(\cp(\cx \mid \cth)\), unknown \(\cth\)

Parametric: ML, MAP, Bayes.

today

We estimate a few parameters instead of the whole density, e.g. \(\cth = (\mu, \sigma^2)\) of a normal distribution.

The setting

Known form, unknown parameters: \[ \cp(\cx \mid \ck) = \cp(\cx \mid \ck, \cth_\ck) \] Each class separately, as with Parzen: we drop \(\ck\) and write \(\cp(\cx \mid \cth)\).

Training set, samples i.i.d.: \[ \cT = \{\cx_1, \cx_2, \dots, \cx_N\} \]

Likelihood of the parameters: \[ L(\cth) = \cp(\cT \mid \cth) = \prod_{i=1}^{N} \cp(\cx_i \mid \cth) \]

Log-likelihood, same maximum: \[ \ell(\cth) = \ln L(\cth) = \sum_{i=1}^{N} \ln \cp(\cx_i \mid \cth) \]

\(L(\cth)\) is a function of \(\cth\) for fixed data, not a distribution over \(\cth\).

Likelihood: try it

Three ways to estimate \(\cth\)

Maximum likelihood (ML)

\(\cT = \{\cx_1, \dots, \cx_N\}\) training set, \(\cx_i\) i.i.d. (independent, identically distributed) \(\;\Rightarrow\; \cp(\cT \mid \cth) = \prod_{i=1}^{N} \cp(\cx_i \mid \cth)\)

Choose the parameters under which the data are most probable:

\[ \hML{\cth} = \arg\max_{\cth} \cp(\cT \mid \cth) = \arg\max_{\cth} \prod_{i=1}^{N} \cp(\cx_i \mid \cth) \]

Prediction for a new \(\cx^*\): plug in the estimate, \(\;\cp(\cx^* \mid \cT) \approx \cp(\cx^* \mid \hML{\cth})\)

Result: \(\hML{\cth}\), one number (a point estimate), not a distribution.

Pros

Simple, no extra assumptions, the best with a lot of data.

Cons

With few data it trusts them blindly: 3 tosses, 3 heads, so \(\hat\pi_\text{head} = 1\)?

Maximum a posteriori (MAP)

\(\cT = \{\cx_1, \dots, \cx_N\}\) training set, \(\cx_i\) i.i.d. (independent, identically distributed) \(\;\Rightarrow\; \cp(\cT \mid \cth) = \prod_{i=1}^{N} \cp(\cx_i \mid \cth)\)

\(\cth\) is a random variable with a prior \(\cp(\cth)\). Maximize the posterior:

\[ \hMAP{\cth} = \arg\max_{\cth} \cp(\cth \mid \cT) = \arg\max_{\cth} \frac{\cp(\cT \mid \cth)\, \cp(\cth)}{\cp(\cT)} = \arg\max_{\cth} \cp(\cT \mid \cth)\, \cp(\cth) \]

  • \(\cp(\cT)\) does not depend on \(\cth\): drop it
  • the prior \(\cp(\cth)\) has to be specified

Result: \(\hMAP{\cth}\), again one number.

Convenient prior: conjugate. The posterior has the same form as the prior.

Prior, likelihood, posterior

Prior distribution of the parameter

Prior and likelihood with the ML estimate at its maximum

Prior, likelihood and posterior with ML, MAP and MMSE estimates

Before the data: the prior \(\cp(\cth)\).

The data: likelihood \(\cp(\cT \mid \cth)\), its maximum is \(\hML{\cth}\).

Posterior \(\propto\) likelihood \(\times\) prior: a compromise between the two.

Bayesian inference: keep the whole posterior

\(\cT = \{\cx_1, \dots, \cx_N\}\) training set, \(\cx_i\) i.i.d. (independent, identically distributed) \(\;\Rightarrow\; \cp(\cT \mid \cth) = \prod_{i=1}^{N} \cp(\cx_i \mid \cth)\)

Do not pick one \(\cth\), keep the whole distribution: \[ \cp(\cth \mid \cT) = \frac{\cp(\cT \mid \cth)\, \cp(\cth)}{\cp(\cT)} \]

Prediction: average all models, weighted by their probability: \[ \cp(\cx^* \mid \cT) = \int \cp(\cx^* \mid \cth)\, \cp(\cth \mid \cT)\, d\cth \]

Result: a distribution \(\cp(\cth \mid \cT)\), no single \(\cth\). The prediction is a distribution too.

Pros

Uses all the information, knows how uncertain it is.

Cons

The integral: in closed form only for conjugate priors.

Comparing the estimates

Four samples on the x axis and the prior mean

The ML normal density, narrow around the samples

The MAP normal density, shifted towards the prior mean and wider

The Bayesian predictive density, a Student t distribution, dash-dotted

Ten normal densities drawn from the posterior in grey, one of them in yellow, behind the predictive density

\(N = 4\) samples, normal model, unknown \(\mu\) and \(\sigma^2\). The prior puts \(\mu\) around \(\mu_0 = 3\).

ML: plug in \(\hML{\cth}\). It fits the 4 samples tightly, too narrow.

MAP: the prior pulls \(\mu\) towards \(\mu_0\) and makes \(\sigma\) larger.

Bayes: the predictive \(\cp(\cx^* \mid \cT)\), not a normal any more.

It averages models from \(\cp(\cth \mid \cT)\): 10 of them drawn, one highlighted.

Bayesian parameter estimation (MMSE)

\(\cT = \{\cx_1, \dots, \cx_N\}\) training set, \(\cx_i\) i.i.d. (independent, identically distributed) \(\;\Rightarrow\; \cp(\cT \mid \cth) = \prod_{i=1}^{N} \cp(\cx_i \mid \cth)\)

Or: pick one \(\hat\cth\) with the smallest expected squared error: \[ R(\hat\cth) = \int \cp(\cth \mid \cT)\, (\cth - \hat\cth)^2\, d\cth \]

\[ \frac{\partial R}{\partial \hat\cth} = -2 \int \cp(\cth \mid \cT)\, (\cth - \hat\cth)\, d\cth = 0 \quad\Rightarrow\quad \hMMSE{\cth} = \int \cth\, \cp(\cth \mid \cT)\, d\cth \]

Result: \(\hMMSE{\cth} = \mathbb{E}[\cth \mid \cT]\), one number: the mean of the posterior. Prediction by plugging in.

Comparing the estimates: MMSE

ML, MAP and the Bayesian predictive density, dash-dotted

The MMSE normal density added as a solid line

ML, MAP and Bayes from before.

MMSE: plug in the posterior mean. Still a normal, unlike Bayes.

Three estimates, one picture

prior result prediction \(\cp(\cx^* \mid \cT)\)
ML no \(\hML{\cth}\), max of likelihood \(\cp(\cx^* \mid \hML{\cth})\)
MAP yes \(\hMAP{\cth}\), max of posterior \(\cp(\cx^* \mid \hMAP{\cth})\)
MMSE yes \(\hMMSE{\cth}\), mean of posterior \(\cp(\cx^* \mid \hMMSE{\cth})\)
Bayes yes the whole \(\cp(\cth \mid \cT)\) \(\int \cp(\cx^* \mid \cth)\, \cp(\cth \mid \cT)\, d\cth\)

With a flat prior, MAP equals ML.

With a lot of data, all four agree.

Conjugate priors

likelihood \(\cp(\cx \mid \cth)\) parameter conjugate prior posterior
binomial \(\cpi\) Beta \((a, b)\) Beta \((a + R,\; b + N - R)\)
categorical \(p_1, \dots, p_K\) Dirichlet \((\alpha_1, \dots, \alpha_K)\) Dirichlet \((\alpha_k + N_k)\)
normal \(\mu\) (\(\sigma^2\) known) normal \((\mu_0, \sigma_0^2)\) normal
normal \(\mu, \sigma^2\) NIG NIG assignment
Poisson \(\lambda\) Gamma \((\alpha, \beta)\) Gamma \((\alpha + \sum \cx_i,\; \beta + N)\)
uniform \([0, \cth]\) \(\cth\) Pareto \((\cth_0, k)\) Pareto \((\max(\cth_0, m),\; k + N)\)

The full table: Wikipedia, Conjugate prior.

The German tank problem

Counting tanks from serial numbers

A German tank factory assembly line in World War II

WW2: the Allies read serial numbers on captured tanks (gearboxes, wheels). How many tanks does Germany produce per month?

month intelligence statistics German records
June 1940 1000 169 122
June 1941 1550 244 271
August 1942 1550 327 342

Ruggles and Brodie (1947); table from Wikipedia, German tank problem.

We have seen \(n\) tanks, the largest serial number is \(m\). How many tanks are there?

The model: uniform distribution

Serial numbers are uniform on \([0, \cth]\), \(\cth\) is the number of tanks: \[ \cp(\cx \mid \cth) = \begin{cases} \frac{1}{\cth} & 0 \le \cx \le \cth\\ 0 & \text{otherwise} \end{cases} \] Continuous for simplicity, real serial numbers are integers.

\[ L(\cth) = \prod_{i=1}^{n} \frac{1}{\cth} [\![\cx_i \le \cth]\!] \] \[ L(\cth) = \begin{cases} \cth^{-n} & \cth \ge \max_i \cx_i\\ 0 & \text{otherwise} \end{cases} \]

\[ \hML{\cth} = \max_i \cx_i = m \]

Is it a good estimate? Can the true \(\cth\) be smaller than \(m\)?

ML with more and more tanks

One observation: the ML plug-in density and the likelihood

Two observations

Three observations

Four observations

Five observations

Six observations: a new maximum moves the estimate

Seven observations

Eight observations

Nine observations

Ten observations: the likelihood is steep

Derivation: Bayes for the tanks

Prior: \(\cth\) uniform on \([0, M]\), at most \(M\) tanks. Posterior, \(m = \max_i \cx_i\): \[ \cp(\cth \mid \cT) \propto \prod_{i=1}^{n} \frac{1}{\cth} [\![\cx_i \le \cth]\!]\; \frac{1}{M} [\![\cth \le M]\!] \quad\Rightarrow\quad \cp(\cth \mid \cT) = \frac{1}{Z} \frac{1}{\cth^{n}} [\![m \le \cth \le M]\!] \]

\[ Z = \int_m^M \frac{d\cth}{\cth^{n}} = \frac{1}{n - 1} \left( \frac{1}{m^{n-1}} - \frac{1}{M^{n-1}} \right), \qquad \hMAP{\cth} = m = \hML{\cth} \;\;\text{(flat prior)} \]

\[ \hMMSE{\cth} = \frac{1}{Z} \int_m^M \frac{\cth}{\cth^{n}}\, d\cth \;\xrightarrow{M \to \infty}\; m\, \frac{n - 1}{n - 2}, \qquad \cp(\cx \mid \cT) = \int_{\max(\cx, m)}^{M} \frac{1}{\cth}\, \cp(\cth \mid \cT)\, d\cth \]

ML, MAP, MMSE and Bayes for the tanks

One observation and the ML plug-in density

The posterior over theta with the MAP estimate equal to the ML estimate

The MMSE estimate, the mean of the posterior

The Bayesian predictive density

All estimates after ten observations

One tank, \(\cx_1 = 5.15\): the ML plug-in density ends at the observation.

Posterior over \(\cth\) (prior uniform on \([0, 10]\)): its maximum is again \(m\).

Its mean \(\hMMSE{\cth} = 7.3\) lies to the right of \(m\).

Bayesian prediction \(\cp(\cx \mid \cT)\): larger numbers are still possible.

Ten tanks: the posterior is narrow, all estimates are close.

German tanks: try it

The real (discrete) problem

Serial numbers \(1, 2, \dots, N\), we see \(k\) of them, the largest is \(m\):

estimate formula \(k = 4\), \(m = 60\)
ML \(m\) 60
unbiased \(m + \dfrac{m}{k} - 1\) 74
Bayes, posterior mean \(\dfrac{(m - 1)(k - 1)}{k - 2}\) 89

ML is biased: it always underestimates. Adding the average gap \(\frac{m}{k}\) fixes it.

Why is ML the most common?

Why ML?

Under general conditions, as \(N \to \infty\)

  • consistent: converges to the true \(\cth\)
  • asymptotically unbiased and efficient (the smallest variance)
  • asymptotically normal

In practice

  • no prior to choose
  • a lot of data: MAP and Bayes give the same result
  • Bayes needs integrals: closed form only for conjugate priors

But with few data

  • overconfident: tanks (and socks, unseen categories later)
  • biased: \(\hat\sigma^2_\text{ML} = \frac{1}{N} \sum (\cx_i - \hat\mu)^2\), \(\mathbb{E}[\hat\sigma^2_\text{ML}] = \frac{N-1}{N} \sigma^2\)

A bit of history

  • 1769 Lagrange: ML for the multinomial gives relative frequencies
  • 1778 D. Bernoulli: multiply the densities of the observations, maximize
  • 1809 Gauss: posterior mode with a flat prior, gives least squares
  • 1912, 1922 Fisher: the method gets its name and its theory

"Who would rise to argue in favor of a method of minimum likelihood, or even mediocre likelihood?"

S. M. Stigler: The Epic Story of Maximum Likelihood, Statistical Science, 2007.

Example: socks

Socks

A drawer with red and green socks. We pull out a sock, note its colour and put it back. After \(N\) draws, \(R\) socks were red.

\[ \cT = \{\cx_1, \dots, \cx_N\}, \quad \cx_i \in \{\text{red}, \text{green}\} \]

\[ \cp(\text{red} \mid \cpi) = \cpi, \qquad \cp(\text{green} \mid \cpi) = 1 - \cpi \]

\(\cpi\): the ratio of red socks in the drawer

Worked out on the board in the lab.

\(\hML{\pi} = \dfrac{R}{N}, \qquad \hMAP{\pi} = \dfrac{R + a - 1}{N + a + b - 2}\)

  1. What is the likelihood \(L(\cpi)\)?
  2. Find \(\hML{\pi}\).
  3. Find \(\hMAP{\pi}\) with a prior \(\beta(\cpi \mid a, b)\).
  4. Does it matter if \(\cT\) is an ordered sequence?

More problems like this: exercise book on CourseWare.

The Beta prior: virtual observations

Six Beta distributions with different parameters a and b

\[ \hMAP{\pi} = \frac{R + a - 1}{N + a + b - 2} \]

As if we had seen \(a - 1\) extra red and \(b - 1\) extra green socks.

\(a, b\) are hyperparameters: parameters of the prior.

Socks in numbers

Prior beta(2,2), likelihood and posterior for N=2, R=2 and for N=400, R=323

Two red socks: ML says all socks are red.

400 socks: the prior does not matter any more.

Socks: try it

Categorical distribution

A die with \(K\) sides

\[ \cT = \{\cx_1, \dots, \cx_N\}, \quad \cx_i \in \{1, \dots, K\} \]

\[ \cp(\cx = k \mid p_1, \dots, p_K) = p_k \]

\[ \sum_{k=1}^{K} p_k = 1, \qquad N_k = \text{count of } k \]

Worked out on the board in the lab.

\(\hat p_k^\text{ML} = \dfrac{N_k}{N}\) (the relative frequency)

  1. Write the likelihood \(L(p_1, \dots, p_K)\).
  2. Find \(\hat p_k^\text{ML}\). Do not forget the constraint!

Hint: use a Lagrange multiplier for the constraint \(\sum_k p_k = 1\).

More problems like this: exercise book on CourseWare.

A biased die, an election, words in a text.

A category never observed

ML, MAP and Bayesian estimates of six category probabilities, category 4 has no observation

ML gives an unseen category probability 0. The prior gives it a small chance.

Practice

Exponential distribution

Waiting times \(\cT = \{t_1, \dots, t_N\}\), exponential distribution: \[ \cp(t \mid \lambda) = \frac{1}{\lambda}\, e^{-t / \lambda}, \qquad t \ge 0 \] \(\lambda\): the mean waiting time

Try it yourself, then check your result:

\(\hML{\lambda} = \dfrac{1}{N} \sum_i t_i, \qquad \hML{\cth} = \dfrac{1}{\hML{\lambda}}\)

  1. Find \(\hML{\lambda}\).
  2. Substitute \(\cth = 1 / \lambda\) and find \(\hML{\cth}\). What is the relation?

More problems like this: exercise book on CourseWare.

The assignment

Assignment: MLE, MAP, Bayes

Part 1: normal

  • ML of \(\mu, \sigma^2\)
  • MAP: normal-inverse-gamma prior
  • Bayes: posterior and predictive density

Part 2: categorical

  • ML: relative frequencies
  • MAP: Dirichlet prior
  • Bayes: predictive \(\frac{N_k + \alpha_k}{N + \sum_j \alpha_j}\)

Part 3: classifier

Letters A and C, training sets of 20, 200 and 2000 samples.

Compare ML, MAP and Bayes on the test set.

Take-away for the lab

  • Known form \(\cp(\cx \mid \cth)\): estimate the parameters from i.i.d. samples
  • ML: \(\arg\max_\cth \cp(\cT \mid \cth)\), simple, the best with a lot of data
  • MAP: \(\arg\max_\cth \cp(\cT \mid \cth)\, \cp(\cth)\), the prior as virtual observations
  • Bayes: the whole posterior, prediction averages all models
  • MMSE: the mean of the posterior
  • With few data the choice matters: socks, unseen categories, German tanks