B4B33RPZ · Pattern Recognition and Machine Learning

Exercise lab I

Lab 7: solving exam-style exercises, test 1 feedback, and nearest neighbours
Klára JanouškováFEE CTU, Prague

Warm-up: logistic regression

Logistic regression models directly…

p(k∣x)=σ(k w⊤x)p(k \mid x) = \sigma(k\,\mathbf{w}^\top\mathbf{x}): it is a discriminative model. It never says how the xx of a class are distributed, so it cannot generate data.

The training data are linearly separable. Without regularisation, the ML weights w\mathbf{w}…

Making ∥w∥\|\mathbf{w}\| larger pushes every σ(k w⊤x)\sigma(k\,\mathbf{w}^\top\mathbf{x}) closer to 1, so the loss keeps falling and never reaches a minimum. A prior (L2 penalty) stops it.

What does the kk in σ(k w⊤x)\sigma(k\,\mathbf{w}^\top \mathbf{x}) do?

Last week's fit gives each student a score a=w⊤x=1.79 x−5.83a = \mathbf{w}^\top \mathbf{x} = 1.79\,x - 5.83; a>0a > 0 predicts "pass".

Passed students (k=+1k = +1):  p(+1∣x)=σ(a)\ p(+1 \mid \mathbf{x}) = \sigma(a)

↓ the two probabilities sum to 1, and σ\sigma is symmetric: 1−σ(a)=σ(−a)1 - \sigma(a) = \sigma(-a)

Failed students (k=−1k = -1):  p(−1∣x)=1−σ(a)=σ(−a)\ p(-1 \mid \mathbf{x}) = 1 - \sigma(a) = \sigma(-a)

One formula for both:  p(k∣x)=σ(k a)\ p(k \mid \mathbf{x}) = \sigma(k\,a). Multiplying by kk mirrors the failed students: the margin is m=k am = k\,a

Now all students lie on one curve σ(m)\sigma(m) = probability of their true label: m>0m > 0 correct, m<0m < 0 wrong side (2 students).

So one loss −ln⁡σ(m)-\ln \sigma(m) works for both classes: next slide.

−6−4−202460.510ascore a = w·x = 1.79 x − 5.83σ(a)σ(−a)−6−4−202460.510mmargin m = k · aσ(m)failed students: a → −am < 0: wrong side

Recap: how do we score w\mathbf{w} on the training data?

Labels ki∈{−1,+1}k_i \in \{-1, +1\}, x\mathbf{x} with a 1 appended. The model: p(k∣x)=σ(k w⊤x)p(k \mid \mathbf{x}) = \sigma(k\, \mathbf{w}^\top \mathbf{x}).

Likelihood of the training labels:

L(w)=∏i=1Nσ(ki w⊤xi)L(\mathbf{w}) = \prod_{i=1}^{N} \sigma(k_i\, \mathbf{w}^\top \mathbf{x}_i)

↓ maximising LL = minimising −1Nln⁡L-\tfrac1N \ln L, and −ln⁡σ(m)=ln⁡(1+e−m)-\ln \sigma(m) = \ln(1 + e^{-m})

E(w)=1N∑i=1Nln⁡(1+e−mi),mi=ki w⊤xiE(\mathbf{w}) = \frac{1}{N} \sum_{i=1}^{N} \ln\big(1 + e^{-m_i}\big), \qquad m_i = k_i\, \mathbf{w}^\top \mathbf{x}_i

The margin mi>0m_i > 0: example on the correct side. Correct side: loss →0\to 0; wrong side: loss ≈∣m∣\approx |m|.

With L2: minimise E(w)+λ∥w∥2E(\mathbf{w}) + \lambda \|\mathbf{w}\|^2 (bias not penalised).

−4−20241234mlosswrong sidecorrect sideloss ≈ −mln 2loss of one example: ln(1 + e^(−m))−m

Three students, three margins

Last week's ML fit: w0=−5.83w_0 = -5.83, w1=1.79w_1 = 1.79.

↓ the boundary is where p=0.5p = 0.5, i.e. w0+w1x=0w_0 + w_1 x = 0

b=−w0/w1=3.26b = -w_0 / w_1 = 3.26 h, so w0=−w1bw_0 = -w_1 b and m=k w1(x−b)m = k\, w_1 (x - b)

A passed, 5.5 h: m=1.79⋅2.24=4.0m = 1.79 \cdot 2.24 = 4.0, loss 0.020.02

B failed, 3 h: m=−1.79⋅(−0.26)=0.46m = -1.79 \cdot (-0.26) = 0.46, loss 0.490.49

C passed, 2.5 h: m=1.79⋅(−0.76)=−1.36m = 1.79 \cdot (-0.76) = -1.36, loss 1.581.58

Steeper σ\sigma: double w1w_1, keep bb. All margins double:

A 0.02→0.000.02 \to 0.00,  \ B 0.49→0.340.49 \to 0.34,  \ C 1.58→2.771.58 \to 2.77

Correct points gain a little, wrong ones lose ~linearly.

0123456701xp(pass | x)steeper: w₁ × 2boundary b = 3.26−4−2024681234mlossAA: 0.02BB: 0.49CC: 1.58failedpassedln(1 + e^(−m))same point, w₁ doubledsteeper σ

Separable vs. overlapping: where is the minimum?

Keep each boundary bb (w0=−w1bw_0 = -w_1 b) and drag w1w_1: only the steepness changes. Lines: how far each point is from its label.

Separable (top): every point is correct, every loss shrinks: E→0E \to 0, no minimum, ∥w∥→∞\|\mathbf{w}\| \to \infty.

Overlapping (bars): the 2 wrong-side students' losses grow ≈∣m∣∝w1\approx |m| \propto w_1, the others' shrink.

EE = falling part + growing part (thin curves): the sum has a minimum, w1≈1.8w_1 \approx 1.8.

Convex, but no closed form: gradient descent or Newton. L2 (+λw12+\lambda w_1^2) gives a minimum in both cases.

Halfway through the labs

About half of the labs are done, so you should already be able to solve about half of the exam exercises.

  • Test 1 feedback: what went well, and what we want to see in definitions
  • Typical exam exercises, solved on the whiteboards:
    • E1: maximum likelihood with missing failure times
    • E2: Parzen windows and Neyman–Pearson
    • E3: Bayes with Gaussians and a "don't know" option
  • Nearest neighbours: k-NN, Voronoi cells, k-d trees, condensation and editing, and an exam exercise on 1-NN (E4)

Where to find exam questions

Past exam questions

cmp.felk.cvut.cz/cmp/courses/recognition/Exam-questions

Old exams, grouped by topic. Today's three exercises come from there.

Exercise book (Sbírka úloh)

Course page → Labs → Sbírka úloh

Typical problems with solutions, mostly from past exams. It is updated during the semester, so download it again from time to time.

Found a mistake in the exercise book? Report it in the exercisebook repository (linked on the course page).
Part 1

Test 1: feedback

How did test 1 go?

24 students, 12 points per test.

9.5mean
10.6median
3 – 12range

Computations went well. The weak spots are the formal definitions (a) parts) and the error integrals in Neyman–Pearson / minimax.

PartAverageMax
Q1 a) definition0.941
Q1 b) missing values0.991
Q1 c) optimal strategy2.623
Q1 d) risk0.791
Q2 a) definition0.701
Q2 b) strategy2.944
Q2 c) errors0.551

Definitions: what is the Bayesian task?

Given: observations XX, hidden states KK, decisions DD, the joint probability p:X×K→[0,1]p: X \times K \to [0,1] and the loss W:K×D→RW: K \times D \to \mathbb{R}.

Strategy: any function q:X→Dq: X \to D; QQ = the set of all strategies.

Risk of a strategy (the expected loss):

R(q)=∑x∈X∑k∈Kp(x,k) W(k,q(x))R(q) = \sum_{x \in X} \sum_{k \in K} p(x, k)\, W\big(k, q(x)\big)

Task: q∗=argmin⁡q∈QR(q)\displaystyle q^* = \operatorname*{argmin}_{q \in Q} R(q)

The partial risk R(x,d)=∑kp(x,k) W(k,d)R(x, d) = \sum_k p(x,k)\,W(k,d) and q∗(x)=argmin⁡dR(x,d)q^*(x) = \operatorname{argmin}_d R(x,d) is how we solve the task, not its definition. From test 2 on, definitions are graded strictly.

Quick check: risks and errors

Which one is the risk of a strategy qq?

b) is the partial risk of one decision dd at one xx. c) uses p(x∣k)p(x \mid k), which ignores how often each class occurs (the priors).

NP: only p(x∣1)p(x \mid 1), p(x∣2)p(x \mid 2) are known (no priors). Class 1 is dangerous; qq says "danger" on X1X_1, "safe" on X2X_2. The overlooked danger ε1=P(safe∣1)\varepsilon_1 = P(\text{safe} \mid 1) is…

ε1=P(x∈X2∣1)\varepsilon_1 = P(x \in X_2 \mid 1): a class-1 xx in the other region. a) is 1−ε11 - \varepsilon_1 (6 of 23 in test 1).

Exercise 1 · 8 points

Maximum likelihood: light bulbs

E1: how long do light bulbs live?

The lifetime tt of a bulb has the density

p(t)=1θ e−t/θ,t∈[0,∞),p(t) = \frac{1}{\theta}\, e^{-t/\theta}, \qquad t \in [0, \infty),

where p(t) δp(t)\,\delta is the probability that a bulb fails in a short interval δ\delta around tt.

(a) 3 points. Six bulbs were tested and failed at

t0=0.0, t1=1.0, t2=2.3, t3=2.7, t4=6.0, t5=12.0.t_0 = 0.0,\ t_1 = 1.0,\ t_2 = 2.3,\ t_3 = 2.7,\ t_4 = 6.0,\ t_5 = 12.0.

What is the maximum likelihood estimate of the reliability parameter θ\theta?

(0) Definition first. Write the formal definition of the maximum likelihood estimate.

E1: the test stops early

Same bulbs, same lifetimes 0.0, 1.0, 2.3, 2.7, 6.0, 12.00.0,\ 1.0,\ 2.3,\ 2.7,\ 6.0,\ 12.0, same density p(t)=1θe−t/θp(t) = \frac{1}{\theta} e^{-t/\theta}.

(b) 3 points. The test finishes at tend=4.0t_{\text{end}} = 4.0, so two bulbs are still on: their failure times are unknown. What is the ML estimate of θ\theta now?

  • Directly, using the probability that a bulb still works at tendt_{\text{end}} (2 points).
  • With the EM algorithm, which replaces each unknown failure time by its expected value given that it is after tendt_{\text{end}} (1 point). Optional for now: EM is in lecture 12 (11. 12.).

(c) 2 points. The logging device is broken. We only know that at tendt_{\text{end}}, GG bulbs are still good and FF bulbs have failed. Find θ\theta.

Recap: the exponential distribution

How long until something fails, if failures happen at a constant average rate?

Density, with θ\theta = the mean lifetime:

p(t)=1θ e−t/θ,t≥0p(t) = \tfrac{1}{\theta}\, e^{-t/\theta},\quad t \ge 0

Mean lifetime:

E[T]=θ\mathbb{E}[T] = \theta

Often written with the rate λ=1/θ\lambda = 1/\theta: p(t)=λe−λtp(t) = \lambda e^{-\lambda t},  E[T]=1/λ\ \mathbb{E}[T] = 1/\lambda. The continuous version of the geometric distribution P(X=k)=(1−p)k−1pP(X = k) = (1-p)^{k-1} p.

024680.51tp(t) = (1/θ) e^(−t/θ)θ = 1, λ = 1θ = 2, λ = 0.5θ = 4, λ = 0.25mean, t = θ

Where does E[T]=θ\mathbb{E}[T] = \theta come from?

Mean lifetime:

E[T]=∫0∞t 1θe−t/θ dt\mathbb{E}[T] = \int_0^\infty t\, \tfrac{1}{\theta} e^{-t/\theta}\, dt

↓ by parts: u=tu = t, v′=1θe−t/θv' = \tfrac1\theta e^{-t/\theta}, so u′=1u' = 1, v=−e−t/θv = -e^{-t/\theta}

=[−t e−t/θ]0∞+∫0∞e−t/θ dt= \Big[-t\,e^{-t/\theta}\Big]_0^\infty + \int_0^\infty e^{-t/\theta}\, dt

↓ the bracket is 0 at both ends; ∫0∞e−t/θ dt=[−θ e−t/θ]0∞=θ\int_0^\infty e^{-t/\theta}\,dt = \Big[-\theta\, e^{-t/\theta}\Big]_0^\infty = \theta

=0+θ=θ= 0 + \theta = \theta

Before (b): a bulb that is still on

024681012ttend  = 4: test stops? they fail some time after 4failed, time knownstill on at tend 

What do we know about the two bulbs still on, and how likely is it?

Only T>tendT > t_{\text{end}}:  P(T>t)=∫t∞1θe−s/θ ds\ P(T > t) = \int_t^\infty \tfrac{1}{\theta} e^{-s/\theta}\, ds

↓ antiderivative: guess F(s)=−e−s/θF(s) = -e^{-s/\theta}, check with the chain rule (inner derivative of −s/θ-s/\theta is −1θ-\tfrac1\theta)

F′(s)=−e−s/θ⋅(−1θ)=1θ e−s/θ ✓F'(s) = -e^{-s/\theta} \cdot \Big(-\frac{1}{\theta}\Big) = \frac{1}{\theta}\, e^{-s/\theta} \ \checkmark

↓ evaluate F(∞)−F(t)F(\infty) - F(t); as s→∞s \to \infty, e−s/θ→0e^{-s/\theta} \to 0

P(T>t)=[−e−s/θ]t∞=0−(−e−t/θ)=e−t/θP(T > t) = \Big[-e^{-s/\theta}\Big]_t^\infty = 0 - \big(-e^{-t/\theta}\big) = e^{-t/\theta}

Each bulb still on contributes this probability to the likelihood, instead of p(ti) δp(t_i)\,\delta.

024680.51tP(T > t) = e^(−t/θ)all at e⁻¹ ≈ 0.37θ = 1, λ = 1θ = 2, λ = 0.5θ = 4, λ = 0.25mean, t = θ
Exercise 2 · 6 points

Parzen windows and Neyman–Pearson

E2: two classes, few points

Training sets: X1={1,−1,1,2,0}X_1 = \{1, -1, 1, 2, 0\} and X2={−1,0,1,3}X_2 = \{-1, 0, 1, 3\}.

(a) 3 points. Estimate p(x∣1)p(x \mid 1) from X1X_1 and p(x∣2)p(x \mid 2) from X2X_2 with Parzen windows, kernel K(x,y)=k(x−y)K(x, y) = k(x - y):

k(z)={1/hfor ∣z∣≤h/2,0for ∣z∣>h/2,h=2.k(z) = \begin{cases} 1/h & \text{for } |z| \le h/2,\\ 0 & \text{for } |z| > h/2,\end{cases}\qquad h = 2.

Plot the estimated densities. Find p(x∣1)p(x \mid 1) and p(x∣2)p(x \mid 2) for x=0.5x = 0.5.

(b) 3 points. Class 2 is dangerous. Construct the Neyman–Pearson strategy for overlooked danger <40 %< 40\,\%. What are the errors ε1\varepsilon_1 and ε2\varepsilon_2 for classes 1 and 2?

Definition first: write the formal definition of the Neyman–Pearson task.

Exercise 3 · 6 points

Bayes with Gaussians and a "don't know"

E3: two Gaussian classes

Two classes with equal priors and Gaussian class-conditional densities N(μ1,C1)N(\boldsymbol\mu_1, \mathbf{C}_1), N(μ2,C2)N(\boldsymbol\mu_2, \mathbf{C}_2):

μ1=[36],  C1=[2002],μ2=[3−2],  C2=[2002].\boldsymbol\mu_1 = \begin{bmatrix}3\\6\end{bmatrix},\ \ \mathbf{C}_1 = \begin{bmatrix}2&0\\0&2\end{bmatrix},\qquad \boldsymbol\mu_2 = \begin{bmatrix}3\\-2\end{bmatrix},\ \ \mathbf{C}_2 = \begin{bmatrix}2&0\\0&2\end{bmatrix}.

Definition first: write the formal definition of the Bayesian task with the reject option used in (b).

  1. (2 points) Find the classifier minimising the probability of classification error.
  2. (2 points) Reject option: saying "don't know" costs 0.1, an error costs 1. For which x∈R2\mathbf{x} \in \mathbb{R}^2 does the Bayes-optimal strategy say "don't know"?
  3. (2 points) Find q:R2→{1,2}q: \mathbb{R}^2 \to \{1, 2\} minimising the Bayes risk for W(1,2)=2W(1,2) = 2, W(2,1)=1W(2,1) = 1: classifying a class-1 object as class 2 costs twice as much as the other error.

Recap: the multivariate normal

How likely is a point x∈Rk\mathbf{x} \in \mathbb{R}^k under a class with mean μ\boldsymbol\mu and covariance Σ\boldsymbol\Sigma?

p(x)=exp⁡ ⁣(−12(x−μ)⊤Σ−1(x−μ))(2π)k ∣Σ∣p(\mathbf{x}) = \frac{\exp\!\Big(-\tfrac12 (\mathbf{x} - \boldsymbol\mu)^\top \boldsymbol\Sigma^{-1} (\mathbf{x} - \boldsymbol\mu)\Big)}{\sqrt{(2\pi)^k\, |\boldsymbol\Sigma|}}

↓ for Σ=σ2I\boldsymbol\Sigma = \sigma^2 \mathbf{I}:  Σ−1=1σ2I\ \boldsymbol\Sigma^{-1} = \tfrac{1}{\sigma^2}\mathbf{I}, the quadratic form is a squared distance

p(x)∝exp⁡ ⁣(−∥x−μ∥22σ2)p(\mathbf{x}) \propto \exp\!\Big(-\frac{\|\mathbf{x} - \boldsymbol\mu\|^2}{2\sigma^2}\Big)

Contours are circles around μ\boldsymbol\mu. Same Σ\boldsymbol\Sigma in both classes: the normalising constant is the same and cancels in every ratio.

Recap: Bayes with a reject option

Decisions D=K∪{not known}D = K \cup \{\texttt{not known}\}, with the loss

W(k,d)={0,d=k,1,d≠k, d≠not known,ε,d=not known.W(k, d) = \begin{cases} 0, & d = k,\\ 1, & d \ne k,\ d \ne \texttt{not known},\\ \varepsilon, & d = \texttt{not known}.\end{cases}

The Bayesian strategy minimises the partial risk at each xx:

q∗(x)=argmin⁡d∈D∑k∈Kp(k∣x) W(k,d)q^*(x) = \operatorname*{argmin}_{d \in D} \sum_{k \in K} p(k \mid x)\, W(k, d)

What does each decision cost at xx?

R(x,k)=1−p(k∣x),R(x,not known)=∑kp(k∣x) ε=εR(x, k) = 1 - p(k \mid x), \qquad R(x, \texttt{not known}) = \textstyle\sum_k p(k\mid x)\,\varepsilon = \varepsilon

Recap: when is "don't know" the best answer?

Compare the cheapest class with the fixed cost ε\varepsilon of saying "not known":

q∗(x)={argmax⁡kp(k∣x),if 1−max⁡kp(k∣x)<ε,not known,if 1−max⁡kp(k∣x)≥ε.q^*(x) = \begin{cases} \operatorname*{argmax}_{k} p(k \mid x), & \text{if } 1 - \max_k p(k \mid x) < \varepsilon,\\ \texttt{not known}, & \text{if } 1 - \max_k p(k \mid x) \ge \varepsilon.\end{cases}
  1. Find the class with the largest posterior, pmax⁡(x)p_{\max}(x).
  2. If 1−pmax⁡(x)<ε1 - p_{\max}(x) < \varepsilon, answer that class.
  3. Otherwise answer not known: even the best guess is wrong too often.
Lecture recap

Nearest neighbours

Which class is the new point?

No model, just the training set T\mathcal{T} and a distance dd. Look at what the closest training points say.

1. Find the KK points SS of T\mathcal{T} closest to xx in the metric dd.

2. Vote: the class with the majority in SS,

k=argmax⁡l ∑i=1K[ ⁣[ ki′=l ] ⁣],(xi′,ki′)∈Sk = \operatorname*{argmax}_{l}\ \sum_{i=1}^{K} [\![\, k'_i = l \,]\!],\qquad (x'_i, k'_i) \in S
K=1K = 1: the nearest point is a triangle → class 2
K=3K = 3: two circles, one triangle → class 1
K = 1K = 3xclass 1class 2new point

k-NN: pros and cons

+
  • Easy to implement: a good baseline
  • General: no assumptions about the distributions
  • No training
−
  • Needs a metric; different axes may have different scales
  • Naive version is slow (compare with all NN points) and keeps the whole training set in memory

The rest of this part: making it faster (k-d tree) and smaller (condensation), and more accurate (editing).

Quick check: k-NN

Features: height in metres, weight in grams. Euclidean k-NN mostly looks at…

Weights differ by thousands of grams, heights by tenths of a metre, so the weight dominates every distance. Standardise each feature (subtract the mean, divide by the standard deviation) first.

The error of 1-NN on its own training set (no duplicate points) is…

Every training point is its own nearest neighbour. So the training error says nothing about 1-NN: measure on held-out data.

Where does 1-NN say class 1?

Every point of the plane belongs to its nearest training point.

Voronoi diagram: the cells of all points closest to one training point. Cell edges lie on the perpendicular bisectors between neighbours.

1-NN gives each cell the class of its point.

The decision boundary is made of the cell edges between points of different classes: piecewise linear.

class 1class 2cell1-NN boundary

Finding the nearest neighbour fast: k-d tree

Six points in the plane (labelled in the figure).

Split at the median in xx: (7,2)(7,2)

Each half at its median in yy

Then in xx again, until each cell has one point

Query: go down to the leaf, then back up, visiting a cell only if it can contain something closer than the best so far. About O(log⁡N)O(\log N) per query in low dimensions.

00224466881010(2,3)(5,4)(9,6)(4,7)(8,1)(7,2)(7,2)(5,4)(9,6)(2,3)(4,7)(8,1)split on xsplit on y

Animations: YouTube, Wikipedia

How fast can we find a median?

Sorting: O(nlog⁡n)O(n \log n). Can we do it in O(n)O(n)?

  1. Pick an element: the pivot (at random works well in practice).
  2. Split the list: elements ≤\le pivot (lesser), elements >> pivot (greater).
  3. We look for the kk-th element (k=n/2k = n/2 for the median). If lesser has at least kk elements, recurse into it; otherwise recurse into greater for element k−∣lesser∣k - |\texttt{lesser}|.

Only one side is searched: n+n/2+n/4+⋯≈2nn + n/2 + n/4 + \dots \approx 2n on average, but O(n2)O(n^2) with bad pivots.

Median of medians: choose the pivot as the median of the medians of groups of 5. It is always near the middle, so O(n)O(n) even in the worst case.

Condensation: keep only the points that matter

Goal: a smaller training set AA that classifies all of T\mathcal{T} correctly with 1-NN.

  1. Put one random sample of T\mathcal{T} into AA, the rest into BB.
  2. Classify the samples of BB by 1-NN with training set AA. Move every misclassified x∈Bx \in B to AA.
  3. If anything moved in step 2, repeat step 2.

Output: AA, the condensed training set for 1-NN.

Condensation on an example

training set: 220 pointsafter condensation: 89 pointsclass 1class 21-NN boundary

From 220 to 89 points, the same labels on all training points. In the overlap many points are kept, and the boundary stays ragged.

Editing: remove the points that mislead

Goal: a lower error (not a faster classifier).

  1. Split T\mathcal{T} into two sets AA and BB:  T=A∪B\ \mathcal{T} = A \cup B, A∩B=∅A \cap B = \emptyset.
  2. Classify the samples of BB by K-NN with training set AA. Remove from BB every misclassified sample.

Output: BB, the training set for 1-NN.

Edit first, then condense: the condensation then keeps only a few points along a smooth boundary.

Editing on the same example

after editing: 168 pointsedited + condensed: 25 pointsclass 1class 21-NN boundary

Editing keeps 168 of 220 points and smooths the boundary; condensing the edited set leaves only 25 points near it.

E4: optimal classifier and 1-NN (exam, Jan 2025)

Two classes 1 and 2: p(x∣1)=12p(x \mid 1) = \frac12 on [−1,1][-1, 1], p(x∣2)=14p(x \mid 2) = \frac14 on [0,4][0, 4], priors p(1)=14p(1) = \frac14, p(2)=34p(2) = \frac34.

  1. (5 points) Find the optimal classifier q∗(x)q^*(x), the one minimising the classification error. Write down the result.
  2. (1 point) Compute the classification error ε∗\varepsilon^*.
  3. (6 points) The 1-NN classifier, asymptotic case: the training and test sets grow to infinity. Compute its error εNN\varepsilon^{NN}.

Definition first: write the formal definition of the task in (a).

−2−10123450.250.5xp(x|1) = 1/2p(x|2) = 1/4class 1, p(1) = 1/4class 2, p(2) = 3/4

Wrap-up

Solving an exam exercise
  1. Write the definitions (what is given, what is the strategy, what is minimised).
  2. Sketch the densities and the regions.
  3. Formula first, then the numbers.
  4. Check: errors in [0,1][0,1], densities integrate to 1, does the boundary move the right way?
Next week, 13. 11.
  • Test 2 at the start of the lab (12 points)
  • New assignment: the perceptron
  • Practise with the exercise book
Optional

Extra exercise and nearest neighbours today

Extra: ML and MAP, two-step density (Jan 2026)

p(x)=ap(x) = a for x∈[−1,0]x \in [-1, 0],  p(x)=b\ p(x) = b for x∈(0,0.5]x \in (0, 0.5], zero elsewhere. Training data: KK points in [−1,0][-1, 0], LL points in (0,0.5](0, 0.5].

  1. (1 point) Constraints on a,ba, b so that pp is a density.
  2. (2 points) The likelihood of the data.
  3. (4 points) ML formulas for a,ba, b; compute them for XA={−0.1,0.2}\mathcal{X}_A = \{-0.1, 0.2\} and XB={−0.8,−0.5,−0.3,0.3}\mathcal{X}_B = \{-0.8, -0.5, -0.3, 0.3\}.
  4. (6 points) Prior P(a)=4a3P(a) = 4a^3, 0≤a≤10 \le a \le 1. MAP formulas for a,ba, b; compute them for the same sets.

Definition first: write the definitions of the ML and the MAP estimate.

−1−0.500.5xab

k-NN is everywhere in modern models

  • Learned features + k-NN: self-supervised encoders (e.g. DINO, DINOv2) are evaluated by k-NN on their embeddings, with no classifier trained.
  • Retrieval: image and text search (e.g. with CLIP embeddings) is nearest-neighbour search with the cosine distance.
  • Approximate search at scale: billions of vectors, so exact k-d trees are replaced by graphs (HNSW) and compressed codes (product quantisation), e.g. in the FAISS library.
  • Retrieval-augmented language models look up the nearest documents before answering.

The hard part moved from the classifier to the metric: a good embedding makes plain k-NN work.