B4B33RPZ · Pattern Recognition and Machine Learning

MLE, MAP, Bayes

Lab 5: estimating the parameters of a density: maximum likelihood, maximum a posteriori and Bayesian estimation
Klára JanouškováFEE CTU, Prague

Warm-up: Parzen windows

We increase the bandwidth hh of a Gaussian Parzen estimate. The estimate becomes…

p^h(x)=1n∑i1hK ⁣(x−xih)\hat p_h(x) = \frac1n \sum_i \frac1h K\!\left(\frac{x - x_i}{h}\right): each bump gets wider and lower, area still 1. Large hh = underfitting.

Why did we choose hh by cross-validation and not by the log-likelihood of the training samples?

With h→0h \to 0 every training sample sits under its own infinitely tall spike: training log-likelihood →∞\to \infty. Held-out samples punish that.

Part I

Recap

What did we know so far?

p(x∣k)p(x \mid k)how we get it
Bayes, minimax, Neyman–Pearson, Waldgiven"we know everything about everything"
Last week: Parzennot knownnon-parametric: samples define p^\hat p (histogram, Parzen)
Todaynot knownparametric: we know the shape, e.g. a Gaussian, but not its parameters θ\theta
Today: from samples x1,…,xNx_1, \dots, x_N and a model p(x∣θ)p(x \mid \theta), estimate θ\theta. Three ways: maximum likelihood (ML), maximum a posteriori (MAP), Bayesian estimation.

A known shape with unknown numbers

A parametric model p(x∣θ)p(x \mid \theta): a whole family of densities, one for each value of θ\theta.

data xxmodelunknown θ\theta
coin flips: H or TBernoulli: P(H)=θP(H) = \thetaθ\theta
serial number of a captured tankuniform on {1,…,θ}\{1, \dots, \theta\}θ\theta = how many tanks
a die: faces 1,…,K1, \dots, Kcategorical: P(x=k)=pkP(x = k) = p_kp1,…,pKp_1, \dots, p_K
a measurement (letter A)Normal N(μ,σ2)\mathcal N(\mu, \sigma^2)μ,σ2\mu, \sigma^2

All four appear today; the last two are the assignment. Once we have θ^\hat\theta for each class, p(x∣k,θ^k)p(x \mid k, \hat\theta_k) goes into the Bayes classifier from lab 2.

Part II

Parameter estimation: ML, MAP, Bayesian

A coin: how likely is heads?

We flip a coin 8 times:  H H T H H H H H  (7 heads, 1 tail).

What is θ=P(heads)\theta = P(\text{heads}) for this coin?
  • The obvious guess: 78=0.875\frac78 = 0.875. Where does it come from, and is it the best guess?
  • Would you bet that a coin from your pocket lands heads 87.5 % of the time?
Notation
θ\theta = what we want to know but cannot see: here θ=P(heads)\theta = P(\text{heads}).   D={x1,…,xN}D = \{x_1, \dots, x_N\} = what we observed.
We assume the flips are i.i.d.: independent, each from the same p(x∣θ)p(x \mid \theta).

How probable are our flips?

Our data:  H H T H H H H H  (7 heads, 1 tail). If heads has probability θ\theta, how probable is this sequence?

↓ one flip

p(H∣θ)=θ,p(T∣θ)=1−θp(H \mid \theta) = \theta, \qquad p(T \mid \theta) = 1 - \theta

↓ the flips are independent: multiply, one factor per flip

θ⋅θ⋅(1−θ)⋅θ⋯θ=θ7(1−θ)\theta \cdot \theta \cdot (1 - \theta) \cdot \theta \cdots \theta = \textcolor{orange}{\theta^7 (1 - \theta)}

↓ the same for any i.i.d. data D=(x1,…,xN)D = (x_1, \dots, x_N)

p(D∣θ)=∏i=1Np(xi∣θ)\textcolor{orange}{p(D \mid \theta)} = \prod_{i=1}^{N} p(x_i \mid \theta)

As a function of θ\theta (data fixed): the likelihood L(θ)\textcolor{orange}{L(\theta)}.

Quick check: what is a likelihood?

The likelihood L(θ)=p(D∣θ)L(\theta) = p(D \mid \theta), seen as a function of θ\theta. Which is true?

p(D∣θ)p(D \mid \theta) is a distribution over the data DD, one for each θ\theta. Over θ\theta its integral can be anything: for our flips ∫01θ7(1−θ) dθ=172\int_0^1 \theta^7(1 - \theta)\, d\theta = \frac{1}{72}. A distribution over θ\theta needs a prior and Bayes' rule (MAP, Bayes).

Which θ\theta explains the data best?

Data: H H T H H H H H (7 H, 1 T),   L(θ)=θ7(1−θ)\textcolor{orange}{L(\theta)} = \theta^7 (1 - \theta). Where is it largest?

Maximum likelihood: θ^ML=arg⁡max⁡θL(θ)\hat\theta_{\text{ML}} = \arg\max_\theta \textcolor{orange}{L(\theta)}: the θ\theta under which our data were most probable.

00.510.020.04θθ = 0.875L(θ) = θ⁷ (1 − θ)00.51θ−15−10−5θ = 0.875log L(θ) = 7 log θ + log(1 − θ)

Right: the logarithm. It has its peak at the same θ\theta; we will see why it is easier to work with.

Recap: derivatives

f(θ)f(\theta)f′(θ)f'(\theta)
cc (no θ\theta in it)00
θn\theta^nn θn−1n\, \theta^{n-1}  (also 1θ=θ−1\frac1\theta = \theta^{-1})
log⁡θ\log \theta1θ\frac{1}{\theta}
eθe^{\theta}eθe^{\theta}
a f+b ga\, f + b\, ga f′+b g′a\, f' + b\, g'
f⋅gf \cdot gf′g+fg′f' g + f g'  (product rule)
f1f2⋯fNf_1 f_2 \cdots f_N∑ifi′∏j≠ifj\sum_i f_i' \prod_{j \ne i} f_j  (one factor at a time)
fg\frac{f}{g}f′g−fg′g2\frac{f' g - f g'}{g^2}  (quotient rule)
f(g(θ))f(g(\theta))f′(g(θ)) g′(θ)f'(g(\theta))\, g'(\theta)  (chain rule)

↓ product rule on the coin likelihood itself, f=θ7f = \theta^7, g=1−θg = 1 - \theta

ddθ θ7(1−θ)=7θ6(1−θ)−θ7=θ6(7−8θ)\frac{d}{d\theta}\, \theta^7 (1 - \theta) = 7\theta^6 (1 - \theta) - \theta^7 = \theta^6 (7 - 8\theta)

Zero at θ=78\theta = \frac78. A product of NN factors gives NN terms of NN factors each. With the log it is a sum of simple terms:

(log⁡∏ifi)′=∑i(log⁡fi)′=∑ifi′fi\Big(\log \prod_i f_i\Big)' = \sum_i \big(\log f_i\big)' = \sum_i \frac{f_i'}{f_i}

↓ chain rule: outer log⁡u\log u, inner u=1−θu = 1 - \theta, u′=−1u' = -1

ddθlog⁡(1−θ)=11−θ⋅(−1)=−11−θ\frac{d}{d\theta} \log(1 - \theta) = \frac{1}{1 - \theta} \cdot (-1) = -\frac{1}{1 - \theta}

Several parameters (μ,σ2\mu, \sigma^2): partial derivatives, one at a time, the others held fixed. Treat σ2\sigma^2 as one variable vv: ddv1v=−1v2\frac{d}{dv} \frac1v = -\frac{1}{v^2}.

Where can a maximum be?

θinside, smooth: slope 0θnotallowedθ⁻⁴, θ ≥ 60at the edge: slope ≠ 0xx³ at 0slope 0, but no maximum

Candidates: points where f′=0f' = 0, the edges of the allowed range, and points where f′f' does not exist.

Then compare ff at the candidates, or check f′′<0f'' < 0 at a point with f′=0f' = 0.

How do we find the maximum?

General coin: NHN_H heads, NTN_T tails, N=NH+NTN = N_H + N_T. Where can the maximum be?

↓ edges: L(0)=0L(0) = 0 if NH≥1N_H \ge 1, L(1)=0L(1) = 0 if NT≥1N_T \ge 1; inside L>0L > 0. So the maximum is inside (0,1)(0, 1), where the slope is 0. (H H H: NT=0N_T = 0, maximum at the edge θ=1\theta = 1.)

↓ take the log: products become sums, and log is increasing, so the argmax does not move

ℓ(θ)=log⁡L(θ)=NHlog⁡θ+NTlog⁡(1−θ)\ell(\theta) = \log L(\theta) = N_H \log \theta + N_T \log(1 - \theta)

↓ derivative: (log⁡θ)′=1θ(\log\theta)' = \frac1\theta, (log⁡(1−θ))′=−11−θ(\log(1 - \theta))' = -\frac{1}{1 - \theta}; set it to 0

dℓdθ=NHθ−NT1−θ=0\frac{d\ell}{d\theta} = \frac{N_H}{\theta} - \frac{N_T}{1 - \theta} = 0

↓ multiply by θ(1−θ)\theta(1 - \theta)

NH(1−θ)=NT θ  ⇒  NH=(NH+NT) θN_H (1 - \theta) = N_T\, \theta \;\Rightarrow\; N_H = (N_H + N_T)\, \theta

↓ divide by NN

θ^ML=NHN\hat\theta_{\text{ML}} = \frac{N_H}{N}
Our coin: 7 H, 1 T
θ^ML=78=0.875\hat\theta_{\text{ML}} = \frac{7}{8} = 0.875

The "obvious" relative frequency is the ML estimate. The log trick is used in every derivation today (and in the assignment).

Bonus: in code, a product of 1000 small numbers underflows to 0, a sum of logs does not.

Quick check

You flip a new coin 3 times: H H H. The ML estimate of θ\theta is…

θ^ML=NHN=33=1\hat\theta_{\text{ML}} = \frac{N_H}{N} = \frac{3}{3} = 1: the model now says tails is impossible.

What would make the estimate after H H H more sensible?

The log does not move the maximum. What is missing is prior knowledge: most coins are close to fair. That is what MAP adds.

How can we use what we knew before?

Treat θ\theta as a random variable with a prior p(θ)\textcolor{blue}{p(\theta)}: our belief before seeing DD. Then ask for the most probable θ\theta after seeing DD.

↓ Bayes' rule, as in lab 2 with H=θH = \theta, E=DE = D

p(θ∣D)=p(D∣θ) p(θ)p(D)\textcolor{purple}{p(\theta \mid D)} = \frac{\textcolor{orange}{p(D \mid \theta)}\, \textcolor{blue}{p(\theta)}}{p(D)}

↓ p(D)p(D) does not depend on θ\theta: it does not move the argmax

θ^MAP=arg⁡max⁡θp(θ∣D)=arg⁡max⁡θp(D∣θ) p(θ)\hat\theta_{\text{MAP}} = \arg\max_\theta \textcolor{purple}{p(\theta \mid D)} = \arg\max_\theta \textcolor{orange}{p(D \mid \theta)}\, \textcolor{blue}{p(\theta)}

↓ take the log

=arg⁡max⁡θ[log⁡p(D∣θ)+log⁡p(θ)]= \arg\max_\theta \big[\textcolor{orange}{\log p(D \mid \theta)} + \textcolor{blue}{\log p(\theta)}\big]
Names

prior p(θ)\textcolor{blue}{p(\theta)}: before the data

likelihood p(D∣θ)\textcolor{orange}{p(D \mid \theta)}: what the data say

posterior p(θ∣D)\textcolor{purple}{p(\theta \mid D)}: after the data

ML = MAP with a flat prior: if p(θ)\textcolor{blue}{p(\theta)} is constant, it does not move the argmax either.

The coin with a prior

Same data (7 H, 1 T). Prior: p(θ)∝θa−1(1−θ)b−1\textcolor{blue}{p(\theta)} \propto \theta^{a-1}(1-\theta)^{b-1} (a Beta(a,b)(a, b) density); here a=b=5a = b = 5: "most coins are roughly fair".

↓ likelihood times prior: the exponents add up

p(θ∣D)∝θNH+a−1(1−θ)NT+b−1\textcolor{purple}{p(\theta \mid D)} \propto \theta^{N_H + a - 1} (1 - \theta)^{N_T + b - 1}

↓ same shape as the likelihood, with NH+a−1N_H + a - 1 heads and NT+b−1N_T + b - 1 tails: same derivation as before

θ^MAP=NH+a−1N+a+b−2\hat\theta_{\text{MAP}} = \frac{N_H + a - 1}{N + a + b - 2}
=7+48+8=1116=0.688= \frac{7 + 4}{8 + 8} = \frac{11}{16} = 0.688   (ML: 0.875)

The prior acts like a−1=4a - 1 = 4 extra heads and b−1=4b - 1 = 4 extra tails.

00.20.40.60.811234θpriorlikelihood (area 1)posteriorMLMAPBayes

Why choose a single θ\theta at all?

The posterior says many values of θ\theta are plausible. To predict the next flip, average the prediction of every θ\theta, weighted by how plausible it is.

↓ the expectation you know: E[f(z)]=∫p(z) f(z) dz\mathbb E[f(z)] = \int p(z)\, f(z)\, dz, with z=θz = \theta and f(θ)=p(x∣θ)f(\theta) = p(x \mid \theta)

p(x∣D)=∫p(x∣θ) p(θ∣D) dθp(x \mid D) = \int p(x \mid \theta)\, \textcolor{purple}{p(\theta \mid D)}\, d\theta

↓ coin: p(H∣θ)=θp(H \mid \theta) = \theta, so we need the mean of the posterior

p(H∣D)=∫θ p(θ∣D) dθ=E[θ∣D]p(H \mid D) = \int \theta\, \textcolor{purple}{p(\theta \mid D)}\, d\theta = \mathbb E[\theta \mid D]

↓ the posterior is Beta(NH+a, NT+b)(N_H + a,\ N_T + b); the mean of Beta(α,β)(\alpha, \beta) is αα+β\frac{\alpha}{\alpha + \beta} (derived on the next slide)

p(H∣D)=NH+aN+a+bp(H \mid D) = \frac{N_H + a}{N + a + b}
Our coin: 7 H, 1 T
7+58+10=1218=0.667\frac{7 + 5}{8 + 10} = \frac{12}{18} = 0.667

The result is a predictive distribution p(x∣D)p(x \mid D), not a parameter value. For the coin it is again "heads with prob. 0.667"; for a Gaussian it is not a Gaussian any more (assignment).

Where does the posterior mean come from?

Posterior Beta(α,β)(\alpha, \beta), for our coin α=NH+a=12\alpha = N_H + a = 12, β=NT+b=6\beta = N_T + b = 6:

p(θ∣D)=θα−1(1−θ)β−1B(α,β),B(α,β)=∫01θα−1(1−θ)β−1 dθ\textcolor{purple}{p(\theta \mid D)} = \frac{\theta^{\alpha - 1} (1 - \theta)^{\beta - 1}}{B(\alpha, \beta)}, \qquad B(\alpha, \beta) = \int_0^1 \theta^{\alpha - 1} (1 - \theta)^{\beta - 1}\, d\theta

↓ definition of the mean; θ⋅θα−1=θα\theta \cdot \theta^{\alpha - 1} = \theta^{\alpha}

E[θ∣D]=1B(α,β)∫01θα(1−θ)β−1 dθ\mathbb E[\theta \mid D] = \frac{1}{B(\alpha, \beta)} \int_0^1 \theta^{\alpha} (1 - \theta)^{\beta - 1}\, d\theta

↓ the integral is the same normaliser, with α+1\alpha + 1 instead of α\alpha

=B(α+1,β)B(α,β)= \frac{B(\alpha + 1, \beta)}{B(\alpha, \beta)}

↓ for whole numbers B(α,β)=(α−1)! (β−1)!(α+β−1)!B(\alpha, \beta) = \frac{(\alpha - 1)!\,(\beta - 1)!}{(\alpha + \beta - 1)!}; most factorials cancel

=α! (β−1)!(α+β)!⋅(α+β−1)!(α−1)! (β−1)!=αα+β= \frac{\alpha!\,(\beta - 1)!}{(\alpha + \beta)!} \cdot \frac{(\alpha + \beta - 1)!}{(\alpha - 1)!\,(\beta - 1)!} = \frac{\alpha}{\alpha + \beta}
Our coin: 7 H, 1 T, prior Beta(5, 5)
E[θ∣D]=1212+6=1218=0.667\mathbb E[\theta \mid D] = \frac{12}{12 + 6} = \frac{12}{18} = 0.667

Sanity check: flat Beta(1,1)(1, 1), no data: 12\frac12. The trick "the integral is a normaliser you already know" comes back with the Dirichlet and the NIG in the assignment.

Three estimators side by side

what it computesprior?resultcoin
MLarg⁡max⁡θp(D∣θ)\arg\max_\theta \textcolor{orange}{p(D \mid \theta)}noone θ^\hat\theta, plug in p(x∣θ^)p(x \mid \hat\theta)0.875
MAParg⁡max⁡θp(D∣θ)p(θ)\arg\max_\theta \textcolor{orange}{p(D \mid \theta)}\textcolor{blue}{p(\theta)}yesone θ^\hat\theta, plug in p(x∣θ^)p(x \mid \hat\theta)0.688
Bayes∫p(x∣θ)p(θ∣D) dθ\int p(x \mid \theta)\textcolor{purple}{p(\theta \mid D)}\, d\thetayesa distribution p(x∣D)p(x \mid D)0.667
  • Lots of data: the likelihood dominates the prior, all three agree.
  • Little data: ML can be extreme (θ^=1\hat\theta = 1 after H H H), the prior keeps MAP and Bayes sensible, if the prior is sensible.

Try it: prior, likelihood, posterior

Two ways to think about θ\theta

xkcd 1132: a detector lies only if two dice both show six; it says the sun exploded. The frequentist concludes it did, since p < 0.05; the Bayesian bets it did not.
xkcd.com/1132 (Randall Munroe, CC BY-NC 2.5)

Frequentist: θ\theta is a fixed unknown number; only the data are random. Report the θ\theta that makes the data most likely: ML.

Bayesian: θ\theta is uncertain, so give it a distribution. The prior p(sun exploded)p(\text{sun exploded}) is tiny, and one "YES" from a detector that lies 1/36 of the time does not beat it.

What each one thinks about, in order of "evolution":

θ\thetaa priori
→
xxpragmatic
→
x∣θx \mid \thetafrequentist
→
x,θx, \thetajoint
→
θ∣x\theta \mid xBayesian
Part III

German tank problem

German tank problem

Given: tanks are marked with consecutive whole numbers 1,2,…,θ1, 2, \dots, \theta.

Goal: estimate German tank production θ\theta from a few captured tanks.

Captured: tanks no. 19, 40, 42, 60. How many tanks were produced?

Model: every produced tank is equally likely to be captured:

p(x∣θ)={1θx∈{1,…,θ}0otherwisep(x \mid \theta) = \begin{cases} \frac{1}{\theta} & x \in \{1, \dots, \theta\} \\ 0 & \text{otherwise} \end{cases}
Black-and-white photo: a German tank assembly line, 1940s, several hulls in a row.
Panther production line (Bundesarchiv, CC BY-SA 3.0 DE)

The likelihood of θ\theta

Sketch L(θ)L(\theta) first: what happens for θ<60\theta < 60? For θ≥60\theta \ge 60?

↓ if θ<60\theta < 60, tank 60 could not exist: one factor is 0

↓ otherwise every factor is 1θ\frac1\theta; k=4k = 4 tanks, m=max⁡ixi=60m = \max_i x_i = 60

L(θ)=∏i=1kp(xi∣θ)={θ−kθ≥m0θ<mL(\theta) = \prod_{i=1}^{k} p(x_i \mid \theta) = \begin{cases} \theta^{-k} & \theta \ge m \\ 0 & \theta < m \end{cases}

↓ θ−k\theta^{-k} decreases: take the smallest allowed θ\theta (the edge case from the recap: no slope 0 anywhere)

θ^ML=max⁡ixi=60\hat\theta_{\text{ML}} = \max_i x_i = 60
020406080100120140160θL(θ)L = 0θ < 60 impossibleθML  = 60captured tanks (serial number)likelihood (scaled)

Is 60 a good guess?

The largest captured number can never exceed θ\theta: θ^ML≤θ\hat\theta_{\text{ML}} \le \theta always, so on average it is too small (biased).

↓ the kk numbers split 1..m1..m into gaps of average size mk−1\frac{m}{k} - 1; assume one more gap above mm

θ^=m+mk−1\hat\theta = m + \frac{m}{k} - 1
=60+604−1=74= 60 + \frac{60}{4} - 1 = 74

This estimator is unbiased (on average exactly θ\theta) and has the smallest variance among unbiased ones.

monthstatisticalintelligenceGerman records
June 19401691000122
June 19412441550271
August 19423271550342

Monthly tank production estimates (Wikipedia, "German tank problem").

How was this really used?

1940s: serial numbers as intelligence

Allied economists collected serial numbers from captured and destroyed German tanks. Gearboxes came in two unbroken sequences; chassis and engine numbers and other parts were used to cross-check.

Road wheels of two Panthers

64 road wheels carried mould numbers → how many moulds were in use. British wheel makers said how many wheels a mould makes per month → ≈ 270 Panthers in February 1944. German records: 276.

Before D-Day, and after the war

Rumours of huge numbers of Panthers were checked against the serial-number estimates. After the war, the records of Speer's armaments ministry showed for June 1940 – Sept. 1942: intelligence ≈ 1400 tanks/month, serial numbers 246, true 245.

Afterwards

Published by Ruggles & Brodie (1947), the estimator "sample maximum + average gap" only in a footnote. The same trick estimated V-2 rocket production, Soviet equipment in the Korean War, Israeli Merkava tanks, and ≈ 12.5 million Commodore 64 computers.

R. Ruggles, H. Brodie, An Empirical Approach to Economic Intelligence in World War II, JASA 42 (1947); Wikipedia, "German tank problem".

What does Bayes say about θ\theta?

Prior: no idea, except "at most 1000 tanks": p(θ)=11000\textcolor{blue}{p(\theta)} = \frac{1}{1000} for θ=1,…,1000\theta = 1, \dots, 1000.

↓ Bayes' rule; a flat prior is a constant, so the posterior has the shape of the likelihood

p(θ∣D)=θ−kZ(m≤θ≤1000)\textcolor{purple}{p(\theta \mid D)} = \frac{\theta^{-k}}{Z} \quad (m \le \theta \le 1000)

Z=∑t=m1000t−kZ = \sum_{t = m}^{1000} t^{-k} makes it sum to 1.

↓ summarise the posterior by its middle (median) and its mean

median 75,   mean 89   (ML: 60)

And an honest range: P(θ≤161∣D)=0.95P(\theta \le 161 \mid D) = 0.95. ML gives one number; Bayes also says how sure we are.

4060801001201401601802000.020.040.06θ95 %: θ ≤ 161median 75mean 89posterior p(θ | D)medianmean

Which serial number will the next tank have?

Plug in θ^ML=60\hat\theta_{\text{ML}} = 60: "the next tank has a number from 1 to 60".

Is that believable after seeing only 4 tanks?

↓ Bayes: average the uniform p(x∣θ)p(x \mid \theta) over the posterior, as for the coin

p(x∣D)=∑θp(x∣θ) p(θ∣D)p(x \mid D) = \sum_{\theta} p(x \mid \theta)\, \textcolor{purple}{p(\theta \mid D)}
P(x>60∣D)=0.24P(x > 60 \mid D) = 0.24   (ML plug-in: 0)

The ML plug-in is overconfident: it rules out every tank it has not seen. Same story as face 4 of the die and σML2\sigma^2_{\text{ML}} for 5 samples.

0204060801001201401600.0050.010.0150.02xP(x > 60) = 0.24capturedML plug-in p(x | θ = 60)Bayes p(x | D)

Try it: capture some tanks

Part IV

Exercises

Exercise: socks

You have red and blue socks. The proportion of red socks is π\pi. You draw socks with replacement.

  1. What does it mean to draw with replacement?
  2. What is the probability of drawing two red socks in a row?
  3. In N=10N = 10 draws you got R=2R = 2 red socks. What is the probability of drawing RR red socks in NN draws?
  4. Compute the ML estimate of π\pi.

RPZ exercise book: "Binomial Socks".

A loaded die: what are p1,…,p6p_1, \dots, p_6?

We roll a die N=50N = 50 times. Counts NkN_k of each face:

face kk123456
NkN_k51223028

Model: categorical, P(x=k)=pkP(x = k) = p_k, with pk≥0p_k \ge 0 and ∑kpk=1\sum_k p_k = 1.

↓ i.i.d. product; group equal faces: face kk contributes pkp_k exactly NkN_k times

L(p1,…,p6)=∏i=1Npxi=∏k=16pkNk\textcolor{orange}{L(p_1, \dots, p_6)} = \prod_{i=1}^{N} p_{x_i} = \prod_{k=1}^{6} p_k^{N_k}

Why not just set every derivative to 0? Each pkNkp_k^{N_k} grows with pkp_k: without the constraint ∑kpk=1\sum_k p_k = 1, the "maximum" is pk→∞p_k \to \infty.

Maximize with the constraint ∑kpk=1\sum_k p_k = 1

↓ log-likelihood, plus a Lagrange multiplier λ\lambda times the constraint

L(p,λ)=∑kNklog⁡pk+λ(1−∑kpk)\mathcal L(p, \lambda) = \sum_{k} N_k \log p_k + \lambda \Big(1 - \sum_k p_k\Big)

↓ derivative with respect to one pkp_k: only one term of each sum depends on it

∂L∂pk=Nkpk−λ=0  ⇒  pk=Nkλ\frac{\partial \mathcal L}{\partial p_k} = \frac{N_k}{p_k} - \lambda = 0 \;\Rightarrow\; p_k = \frac{N_k}{\lambda}

↓ derivative with respect to λ\lambda gives back the constraint; plug in pkp_k

∑kNkλ=1  ⇒  λ=∑kNk=N\sum_k \frac{N_k}{\lambda} = 1 \;\Rightarrow\; \lambda = \sum_k N_k = N

↓ put λ=N\lambda = N back

p^k=NkN\hat p_k = \frac{N_k}{N}
Our die
p^=(0.10, 0.24, 0.46, 0, 0.04, 0.16)\hat p = (0.10,\ 0.24,\ 0.46,\ \mathbf{0},\ 0.04,\ 0.16)

Again the relative frequencies. Face 4 gets probability 0: never seen, so "impossible".

A prior for probabilities: Dirichlet

↓ same shape as the likelihood, with αk−1\alpha_k - 1 instead of NkN_k ("pseudo-counts")

Dir(p;α)∝∏kpkαk−1\textcolor{blue}{\text{Dir}(p; \alpha)} \propto \prod_k p_k^{\alpha_k - 1}

↓ MAP: maximize likelihood × prior, same steps as ML (you derive it in the assignment)

p^kMAP=Nk+αk−1∑m(Nm+αm−1)\hat p_k^{\text{MAP}} = \frac{N_k + \alpha_k - 1}{\sum_m (N_m + \alpha_m - 1)}
αk=2\alpha_k = 2: face 4: 0+150+6=0.018\frac{0 + 1}{50 + 6} = 0.018

↓ Bayes: the posterior is Dir(Nk+αk)(N_k + \alpha_k); average over it

p(x=k∣D)=Nk+αk∑m(Nm+αm)p(x = k \mid D) = \frac{N_k + \alpha_k}{\sum_m (N_m + \alpha_m)}
αk=2\alpha_k = 2: face 4: 0+250+12=0.032\frac{0 + 2}{50 + 12} = 0.032
0.10.20.30.40.51N = 52N = 123N = 234N = 005N = 26N = 8MLMAP, αk  = 2Bayes, αk  = 2

Try it: pseudo-counts α\alpha

Quick check

Dirichlet prior with αk=1\alpha_k = 1 for all kk. The MAP estimate of p4p_4 for our die (N4=0N_4 = 0) is…

p^k=Nk+αk−1∑m(Nm+αm−1)=0+050=0\hat p_k = \frac{N_k + \alpha_k - 1}{\sum_m (N_m + \alpha_m - 1)} = \frac{0 + 0}{50} = 0: a flat prior adds nothing to the maximum.

Same prior, Bayesian predictive: p(x=4∣D)p(x = 4 \mid D) is…

p(x=k∣D)=Nk+αk∑m(Nm+αm)=0+150+6=156p(x = k \mid D) = \frac{N_k + \alpha_k}{\sum_m (N_m + \alpha_m)} = \frac{0 + 1}{50 + 6} = \frac{1}{56}. Averaging over all plausible pp never gives an unseen face exactly 0.

Part V

The assignment

The assignment in three parts

1 · Normal

Data from N(0,1)\mathcal N(0, 1). Estimate μ,σ2\mu, \sigma^2 by ML, MAP (normal-inverse-gamma prior) and Bayes.

plots for 1, 5, 50 samples; an outlier

2 · Categorical

The die from today. ML, MAP and Bayes with a Dirichlet prior.

samples from the prior and the posterior

3 · Classifier

Letters A vs. C (measurement from lab 2): Bayes classifier with each of the three estimates.

20, 200, 2000 training samples

Two handwritten derivations to submit as photos: MAP for the Normal (μ∗,σ2∗\mu^*, \sigma^{2*}) and MAP for the categorical. The ML ones are practice for the tests (not graded).

Exercise: ML for a Normal distribution

Samples x1,…,xNx_1, \dots, x_N i.i.d. from N(μ,σ2)\mathcal N(\mu, \sigma^2):

p(x∣μ,σ2)=12πσ2exp⁡ ⁣(−(x−μ)22σ2)p(x \mid \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\!\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)
Task: derive μ^ML\hat\mu_{\text{ML}} and σ^ML2\hat\sigma^2_{\text{ML}}. Then compute them for x=(1,4,6,3,4,7)x = (1, 4, 6, 3, 4, 7).

Recipe from the coin: log-likelihood, derivative with respect to each parameter, set to 0.

How much should we trust σ^ML2\hat\sigma^2_{\text{ML}}?

−4−20240.40.8N = 2: σ²ML  = 0.29−4−2024N = 5: σ²ML  = 0.41−4−2024xN = 50: σ²ML  = 0.93true N(0, 1)ML fitsamples
  • Few samples: the fit is too narrow (too confident), and it changes a lot from sample to sample.
  • One sample: σ^ML2=0\hat\sigma^2_{\text{ML}} = 0, an infinitely thin spike. A prior prevents that.

Is an estimator right on average?

An estimator is a recipe: data in, number out. New data set, new number. Imagine repeating the experiment many times.

↓ average the estimate over all possible data sets DD of size NN

bias(θ^)=ED[θ^(D)]−θ\text{bias}(\hat\theta) = \mathbb E_D\big[\hat\theta(D)\big] - \theta

Bias 0: unbiased, right on average.

E[σ^ML2]=N−1N σ2\mathbb E\big[\hat\sigma^2_{\text{ML}}\big] = \frac{N - 1}{N}\, \sigma^2:  \ 0.8 for N=5N = 5

μ^\hat\mu sits in the middle of the samples, closer to them than the true μ\mu, so the spread around it is too small. Divide by N−1N - 1: unbiased.

00.511.522.530.40.81.2σ̂²true σ² = 1average 0.80average 0.99σ̂²ML : divide by Ndivide by N − 1

Which of our estimators are biased?

estimatorbiased?why / the fix
mean μ^=1N∑ixi\hat\mu = \frac1N \sum_i x_inoeach xix_i is right on average
σ^ML2\hat\sigma^2_{\text{ML}} (divide by NN)yes, lowdivide by N−1N - 1
tank: θ^ML=max⁡ixi\hat\theta_{\text{ML}} = \max_i x_iyes, lownever above θ\theta; fix: m+mk−1m + \frac{m}{k} - 1
MAP μ∗=νμ0+∑ixiN+ν\mu^* = \frac{\nu\mu_0 + \sum_i x_i}{N + \nu}yes, to μ0\mu_0on purpose: pulled towards the prior
relative frequency NkN\frac{N_k}{N}nobut 0 for unseen faces in many single data sets

All biases above vanish as N→∞N \to \infty: the estimators are consistent. Bias matters for small NN.

Is unbiased always better?

What we care about: how far the estimate is from the truth, on average.

↓ expand the square around the average estimate E[θ^]\mathbb E[\hat\theta]; the cross term averages to 0

E[(θ^−θ)2]=bias2+variance\mathbb E\big[(\hat\theta - \theta)^2\big] = \text{bias}^2 + \text{variance}

σ2\sigma^2 from N=5N = 5 Gaussian samples, S=∑i(xi−μ^)2S = \sum_i (x_i - \hat\mu)^2:

estimatebiasvariancemean sq. error
S/(N−1)S / (N - 1)00.500.50
S/NS / N (ML)−0.200.320.36
S/(N+1)S / (N + 1)−0.330.220.33
Bias–variance trade-off

The unbiased estimate is the worst of the three here: a little bias buys a lot less variance.

Same idea as MAP and as the Bayesian average for films: pull towards a sensible value, accept some bias, get a much steadier estimate.

Try it: repeat the experiment

MAP for a Normal: a prior on (μ,σ2)(\mu, \sigma^2)

Conjugate prior: normal-inverse-gamma NIG(μ,σ2∣μ0,ν,α,β)\textcolor{blue}{\text{NIG}(\mu, \sigma^2 \mid \mu_0, \nu, \alpha, \beta)} (formula in the notebook). Read its parameters as imaginary data:

  • μ\mu was estimated from ν\nu samples with mean μ0\mu_0
  • σ2\sigma^2 from 2α2\alpha samples with sum of squared deviations 2β2\beta
  • peak of the prior: μ=μ0\mu = \mu_0, σ2=βα+1.5\sigma^2 = \frac{\beta}{\alpha + 1.5}

↓ MAP (you derive these, photo in the submission)

μ∗=νμ0+∑ixiN+ν,σ2∗=2β+ν(μ0−μ∗)2+∑i(xi−μ∗)2N+3+2α\mu^* = \frac{\nu \mu_0 + \sum_i x_i}{N + \nu}, \quad \sigma^{2*} = \frac{2\beta + \nu(\mu_0 - \mu^*)^2 + \sum_i (x_i - \mu^*)^2}{N + 3 + 2\alpha}
Unit test

x=(2,7,3,5,1,0)x = (2, 7, 3, 5, 1, 0)

μ0=0, ν=5, α=1, β=2.5\mu_0 = 0,\ \nu = 5,\ \alpha = 1,\ \beta = 2.5

μ∗=5⋅0+186+5=1.636\mu^* = \frac{5 \cdot 0 + 18}{6 + 5} = 1.636
σ2∗=5+13.39+45.1611=5.777\sigma^{2*} = \frac{5 + 13.39 + 45.16}{11} = 5.777
01234μ₀ = 0 (ν = 5)x̄ = 3 (N = 6)μ* = 1.64

Likelihood and prior over (μ,σ2)(\mu, \sigma^2)

First 5 samples of N(0,1)\mathcal N(0, 1); prior μ0=1\mu_0 = 1, ν=2\nu = 2, α=2\alpha = 2, peak at (μ,σ2)=(1,1)(\mu, \sigma^2) = (1, 1). The same three maps as in your notebook.

−2−101212μσ²likelihood p(D | μ, σ²)−2−101212μσ²prior NIG(μ, σ²)−2−101212μσ²p(D | μ, σ²) · priorMLprior peakMAPtrue (0, 1)

Peak of the likelihood = ML ✕: σ2\sigma^2 too small for 5 samples.

The prior: our guess ○, a wide blob around it.

Product: its peak is MAP ◆, between ✕ and ○. Not normalised (we dropped p(D)p(D)).

Try it: the (μ,σ2)(\mu, \sigma^2) maps

Bayes for a Normal: average over (μ,σ2)(\mu, \sigma^2)

Conjugacy: the posterior is again NIG, only the parameters change (formulas in the notebook):

ν~=ν+N,α~=α+N2,μ~0=νμ0+∑ixiν+N,β~=…\tilde\nu = \nu + N, \quad \tilde\alpha = \alpha + \frac N2, \quad \tilde\mu_0 = \frac{\nu\mu_0 + \sum_i x_i}{\nu + N}, \quad \tilde\beta = \dots

ν\nu and 2α2\alpha just add the NN real samples to the imaginary ones.

↓ predictive: integrate both parameters out

p(x∗∣D)=∬N(x∗∣μ,σ2) NIG(μ,σ2∣μ~0,ν~,α~,β~) dμ dσ2p(x^* \mid D) = \iint \mathcal N(x^* \mid \mu, \sigma^2)\, \textcolor{purple}{\text{NIG}(\mu, \sigma^2 \mid \tilde\mu_0, \tilde\nu, \tilde\alpha, \tilde\beta)}\, d\mu\, d\sigma^2

The notebook gives the closed form (κ\kappa). It is not a Normal: heavier tails, wider for small NN.

What you implement
  • bayes_posterior_params_normal: the four new parameters
  • bayes_estim_pdf_normal: p(x∗∣D)p(x^* \mid D) on a grid of x∗x^*

The posterior plot looks exactly like the MAP-objective plot: they differ only by the constant p(D)p(D), and matplotlib rescales the colours.

Try it: ML, MAP and Bayes for a Normal

What should you see in your plots?

  • Many samples: ML, MAP and Bayes agree and approach the true parameters. ML is the simplest.
  • Few samples: ML jumps around and σ^ML2\hat\sigma^2_{\text{ML}} is too small (too confident).
  • MAP pulls towards the prior; replaces degenerate answers (σ2=0\sigma^2 = 0 for one sample) by sensible defaults.
  • Bayes is the most careful: heavier tails, some probability even for values not seen yet.
  • Outliers move ML and MAP: the model (one Gaussian) is wrong, not the method.
  • Priors must be chosen; a bad prior hurts (part 3).

Part 3: a Bayes classifier for A and C

Measurement xx = compute_measurement_lr_cont (lab 2), 0/1 loss, priors p(A),p(C)p(A), p(C) = relative frequencies in the training set.

ML, MAP: two Gaussians, as in the Bayes lab: thresholds t1,t2t_1, t_2 from the quadratic.

Bayes: the predictive is not Gaussian, so decide point by point:

p(A) p(x∣A,DA)>p(C) p(x∣C,DC)  ⇒  Ap(A)\, p(x \mid A, D_A) > p(C)\, p(x \mid C, D_C) \;\Rightarrow\; A
Watch out
  • xx is in the thousands: the prior (μ0=0,σ2=1)(\mu_0 = 0, \sigma^2 = 1) from part 1 is useless (next slide).
  • One prior per class.
  • With 20 training samples: ML gives 7.5 % test error; MAP and Bayes should beat it.

How do we choose a prior for A and C?

−4000−200002000xCAprior of part 1:μ₀ = 0, σ² = 1 (a needle)class Cclass Apart-1 prior

Approximate class shapes, read off the notebook's test-set plot.

  1. Plot a histogram of each class's training samples.
  2. μmy\mu_{my} = where the class sits.  σmy2\ \sigma^2_{my} = (typical spread)²: it is a variance, so a spread of 500 means 2.5⋅1052.5 \cdot 10^5.
  3. Keep it weak: small ν\nu and α\alpha (1 to 3). The prior is then worth a few samples and the 20 real ones can still move it.
  4. μ0=μmy\mu_0 = \mu_{my},  β=σmy2(α+1.5)\ \beta = \sigma^2_{my}(\alpha + 1.5). Plot it with plot_prior on the data's range, one prior per class.

Quick check

Class C sits around −2000-2000; most samples are within a few hundred of that. A sensible σmy2\sigma^2_{my} is about…

A spread (standard deviation) of a few hundred, e.g. σ≈300\sigma \approx 300, gives σ2≈9⋅104\sigma^2 \approx 9 \cdot 10^4. 400 would be a standard deviation of 20.

You switch from 20 to 2000 training samples, same prior. The MAP estimates…

μ∗=νμ0+∑ixiN+ν\mu^* = \frac{\nu\mu_0 + \sum_i x_i}{N + \nu}: with N=2000N = 2000 and ν=2\nu = 2 the prior is 2 imaginary samples against 2000 real ones. (Only the Bayes predictive may overflow numerically: use logs.)

Submission and tips

Upload one .zip (no folders)
  • mle_map_bayes.py with all 11 functions
  • map_normal_derivation.jpg, map_categorical_derivation.jpg
  • the 14 figures from the notebook
  • the notebook (not checked)
Tips
  • Copy the functions you need from bayes.py into mle_map_bayes.py: BRUTE cannot import them.
  • Variance: divide by NN (np.var, ddof=0).
  • 200 or 2000 samples: products and Γ(⋅)\Gamma(\cdot) overflow. Think in logs.
Part VI

Where is this actually used?

Which seller would you buy from?

Same product, same price. Which seller is most likely to give you a good experience?

Bayes with a flat prior: s+1n+2\frac{s + 1}{n + 2} gives a) 0.917, b) 0.942, c) 0.926.

ML ranks by sn\frac{s}{n}: seller a), with 100 %. Same trap as H H H.

↓ the coin with a flat prior Beta(1, 1): p(good∣D)=s+an+a+bp(\text{good} \mid D) = \frac{s + a}{n + a + b}

p(good∣D)=s+1n+2p(\text{good} \mid D) = \frac{s + 1}{n + 2}

"Add one good and one bad review to everyone" (Laplace's rule of succession), then compare.

Idea: 3Blue1Brown, Probabilities of probabilities

Ranking films with few votes

A film with 3 votes, all 10/10: best film ever? For years IMDb published how its Top 250 avoids that:

WR=vv+m R+mv+m C\text{WR} = \frac{v}{v + m}\, R + \frac{m}{v + m}\, C

RR = the film's mean rating from vv votes, CC = mean over all films, mm = a fixed number of votes.

Seen it before?

The MAP mean of the Normal: μ∗=NN+ν xˉ+νN+ν μ0\mu^* = \frac{N}{N + \nu}\,\bar x + \frac{\nu}{N + \nu}\,\mu_0

Every film starts with mm imaginary votes at the global mean CC: a prior worth mm samples. Few votes → close to CC; many votes → its own mean.

C=6.9C = 6.9. Slider is on a log scale.

Counting things you have never seen

Spam filter (naive Bayes)

p(word∣spam)p(\text{word} \mid \text{spam}) estimated by counting words in known spam. A word never seen in spam gets ML estimate 0, so the product over the email's words is 0: one word decides everything.

Autocomplete, n-gram models

p(next word∣previous words)p(\text{next word} \mid \text{previous words}) from counts in a corpus. Most word pairs never occur in the corpus, but they are not impossible.

Fix in both: add-one (Laplace) smoothing Nk+1N+K\frac{N_k + 1}{N + K} = the Bayes estimate with a Dirichlet prior, αk=1\alpha_k = 1. That is face 4 of our die.

Your loss function is a likelihood

Training a network = minimizing a loss over the data. Which loss?

↓ ML for a model of the output yy given the input xx, written as a minimisation

θ^ML=arg⁡min⁡θ∑i−log⁡p(yi∣xi,θ)\hat\theta_{\text{ML}} = \arg\min_\theta \sum_i -\log p(y_i \mid x_i, \theta)

↓ classification: p(yi∣xi,θ)p(y_i \mid x_i, \theta) = predicted probability of the true class

−log⁡p(yi∣xi,θ)-\log p(y_i \mid x_i, \theta) is the cross-entropy loss.

↓ regression with Gaussian noise: y=fθ(x)+εy = f_\theta(x) + \varepsilon, ε∼N(0,σ2)\varepsilon \sim \mathcal N(0, \sigma^2)

−log⁡p(y∣x,θ)=(y−fθ(x))22σ2+const-\log p(y \mid x, \theta) = \frac{(y - f_\theta(x))^2}{2\sigma^2} + \text{const}

The squared error (MSE) loss.

So

Image classifiers, detectors and the next-word prediction that trains large language models are all trained by maximum likelihood.

Least squares (camera calibration, 3D reconstruction, line fitting) = ML with Gaussian errors: Gauss's own argument, 1809.

Bridge to next week: what is regularisation?

↓ MAP in logs, written as a minimisation

θ^MAP=arg⁡min⁡θ[−log⁡p(D∣θ)⏟loss on the data−log⁡p(θ)⏟penalty]\hat\theta_{\text{MAP}} = \arg\min_\theta \Big[ \underbrace{-\textcolor{orange}{\log p(D \mid \theta)}}_{\text{loss on the data}} \underbrace{- \textcolor{blue}{\log p(\theta)}}_{\text{penalty}} \Big]

↓ Gaussian prior on the weights: p(θ)∝exp⁡ ⁣(−∥θ∥22s2)\textcolor{blue}{p(\theta)} \propto \exp\!\big(-\frac{\|\theta\|^2}{2 s^2}\big)

−log⁡p(θ)=12s2∥θ∥2+const- \textcolor{blue}{\log p(\theta)} = \frac{1}{2 s^2} \|\theta\|^2 + \text{const}

L2 regularisation (weight decay) = MAP with a Gaussian prior: "weights are probably small". A narrow prior = a strong penalty.

Next week: logistic regression is trained by ML (cross-entropy). Add λ∥w∥2\lambda \|w\|^2 and it becomes MAP.

The epic story of maximum likelihood (TL;DR)

1700sBernoulli, Lagrange, Laplace: "the most probable value"
1809Gauss: normal errors → least squares
1912–1922Fisher: names it, claims it is the best
1948–1953counterexamples; Wald's proof; Le Cam
  • An "obvious" idea, used intuitively long before anyone could say when it works.
  • Fisher's claims (consistent, most efficient) took decades and several failed proofs; they hold under regularity conditions, not always.
  • It can fail: many parameters with few samples each (Neyman–Scott), or a maximum at the edge like the tank, where the usual theory does not apply.

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

Part VII

Before the next test

Exercise: Parzen windows

Measurements X={1.5, −1, 1.5, 3, 2, −0.5}X = \{1.5,\ -1,\ 1.5,\ 3,\ 2,\ -0.5\}. Kernel K(x,y)=k(x−y)K(x, y) = k(x - y) with

k(z)={1h∣z∣≤h20∣z∣>h2,h=3k(z) = \begin{cases} \frac1h & |z| \le \frac h2 \\ 0 & |z| > \frac h2 \end{cases}, \qquad h = 3
Task: plot the Parzen estimate p^(x)\hat p(x). What is p^(2.5)\hat p(2.5)?

RPZ exercise book: "Parzen Window Density Estimation".

−3−2−1012345x1/182/183/184/18

Test 1: how did it go?

24 students, 12 points: mean 9.5, median 10.1, three full scores.

Q1 (Bayes) went well. Points were lost mostly in Q2 (Neyman–Pearson in 1A, minimax in 1B): the definition and the final error.

Pure slips (arithmetic, copying) cost only 0.25. The deductions on the next slide are about understanding.

partaveragemax
Q1 a) definition0.921
Q1 b), c) computation3.584
Q1 d) Bayesian risk0.791
Q2 a) definition0.731
Q2 b) strategy2.884
Q2 c) error0.561

Test 1: common mistakes

Risk of a strategy vs. partial risk

Most wrote only the partial risk R(x,d)=∑kp(x,k) W(k,d)R(x, d) = \sum_k p(x, k)\, W(k, d) and a pointwise argmin. The task asks for the risk of a strategy: R(q)=∑x∑kp(x,k) W(k,q(x))R(q) = \sum_x \sum_k p(x, k)\, W(k, q(x)),  q∗=arg⁡min⁡qR(q)\ q^* = \arg\min_q R(q).

Conditional vs. joint

p(x∣k)p(x \mid k) instead of p(x,k)p(x, k) drops the priors. And p(k∣x)=p(x,k)p(x)p(k \mid x) = \frac{p(x, k)}{p(x)}: divide by p(x)p(x), never multiply. Bayesian risk: weight the partial risks by p(x)p(x) again.

Integrate over the other region

6 of 23 integrated each density over its own decision region and got 1−ε1 - \varepsilon. The error of class 1 is the mass of p(x∣1)p(x \mid 1) where we decide 2:  ε(1)=∫X2p(x∣1) dx\ \varepsilon(1) = \int_{X_2} p(x \mid 1)\, dx.

Likelihood ratio + sanity checks

Write ℓ(x)=p(x∣1)p(x∣2)\ell(x) = \frac{p(x \mid 1)}{p(x \mid 2)} first: it gives the shape of the region (two tails in Neyman–Pearson: the constraint is on their sum). An error above 0.5 for two classes? Probably 1−ε1 - \varepsilon.

Quick check

A table gives p(x,k)p(x, k). The column k=1k = 1 sums to 0.30.3. The entries of that column are…

p(x,k)=p(x∣k) p(k)p(x, k) = p(x \mid k)\, p(k) and p(k=1)=∑xp(x,1)=0.3p(k = 1) = \sum_x p(x, 1) = 0.3. Divide the column by 0.3 to get p(x∣k=1)p(x \mid k = 1).

The strategy decides d=2d = 2 on X2X_2 and d=1d = 1 elsewhere. The probability of a mistake on an object of class 1 is…

A class-1 object is misclassified when its xx falls where we decide 2. c) is 1−ε(1)1 - \varepsilon(1), the most common mistake in test 1; b) is the probability that a class-2 object is classified correctly.

Optional

Extra material

The Bayes predictive of a Normal is a Student tt

p(x∗∣D)=t2α~ ⁣(x∗  |  μ~0, β~ (ν~+1)α~ ν~)p(x^* \mid D) = t_{2\tilde\alpha}\!\left(x^* \;\middle|\; \tilde\mu_0,\ \frac{\tilde\beta\, (\tilde\nu + 1)}{\tilde\alpha\, \tilde\nu}\right)
  • Degrees of freedom 2α~=2α+N2\tilde\alpha = 2\alpha + N: few samples → heavy tails, the uncertainty about σ2\sigma^2 shows.
  • N→∞N \to \infty: t→t \to Normal, the predictive becomes the MAP / ML Gaussian.
  • Same numbers as the κ\kappa formula in the notebook: a check for your implementation (the widget uses this form).

Resources

Intuition
Stuck on the assignment
  • S. Prince, Computer Vision: Models, Learning, and Inference, ch. 4: the NIG and Dirichlet formulas, udlbook.github.io/cvbook
  • The notebook's own links (conjugate priors, Murphy's notes on the Gaussian)
Before test 2
  • RPZ exercise book, "Parametric density estimation": coin, socks, hard drives, dice, Pareto (an edge maximum, like the tank)
Stories