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.
Part
Average
Max
Q1 a) definition
0.94
1
Q1 b) missing values
0.99
1
Q1 c) optimal strategy
2.62
3
Q1 d) risk
0.79
1
Q2 a) definition
0.70
1
Q2 b) strategy
2.94
4
Q2 c) errors
0.55
1
Definitions: what is the Bayesian task?
Given: observations X, hidden states K, decisions D, the joint probability p:X×K→[0,1] and the loss W:K×D→R.
Strategy: any function q:X→D; Q = the set of all strategies.
Risk of a strategy (the expected loss):
R(q)=x∈X∑k∈K∑p(x,k)W(k,q(x))
Task:q∗=q∈QargminR(q)
The partial risk R(x,d)=∑kp(x,k)W(k,d) and q∗(x)=argmindR(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 q?
b) is the partial risk of one decision d at one x. c) uses p(x∣k), which ignores how often each class occurs (the priors).
NP: only p(x∣1), p(x∣2) are known (no priors). Class 1 is dangerous; q says "danger" on X1, "safe" on X2. The overlooked danger ε1=P(safe∣1) is…
ε1=P(x∈X2∣1): a class-1 x in the other region. a) is 1−ε1 (6 of 23 in test 1).
Exercise 1 · 8 points
Maximum likelihood: light bulbs
E1: how long do light bulbs live?
The lifetime t of a bulb has the density
p(t)=θ1e−t/θ,t∈[0,∞),
where p(t)δ is the probability that a bulb fails in a short interval δ around t.
(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.
What is the maximum likelihood estimate of the reliability parameter θ?
(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.0, same density p(t)=θ1e−t/θ.
(b) 3 points. The test finishes at tend=4.0, so two bulbs are still on: their failure times are unknown. What is the ML estimate of θ now?
Directly, using the probability that a bulb still works at tend (2 points).
With the EM algorithm, which replaces each unknown failure time by its expected value given that it is after tend (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 tend, G bulbs are still good and F bulbs have failed. Find θ.
Recap: the exponential distribution
How long until something fails, if failures happen at a constant average rate?
Density, with θ = the mean lifetime:
p(t)=θ1e−t/θ,t≥0
Mean lifetime:
E[T]=θ
Often written with the rate λ=1/θ: p(t)=λe−λt, E[T]=1/λ. The continuous version of the geometric distribution P(X=k)=(1−p)k−1p.
Where does E[T]=θ come from?
Mean lifetime:
E[T]=∫0∞tθ1e−t/θdt
↓ by parts: u=t, v′=θ1e−t/θ, so u′=1, v=−e−t/θ
=[−te−t/θ]0∞+∫0∞e−t/θdt
↓ the bracket is 0 at both ends; ∫0∞e−t/θdt=[−θe−t/θ]0∞=θ
=0+θ=θ
Before (b): a bulb that is still on
What do we know about the two bulbs still on, and how likely is it?
Only T>tend: P(T>t)=∫t∞θ1e−s/θds
↓ antiderivative: guess F(s)=−e−s/θ, check with the chain rule (inner derivative of −s/θ is −θ1)
F′(s)=−e−s/θ⋅(−θ1)=θ1e−s/θ✓
↓ evaluate F(∞)−F(t); as s→∞, e−s/θ→0
P(T>t)=[−e−s/θ]t∞=0−(−e−t/θ)=e−t/θ
Each bulb still on contributes this probability to the likelihood, instead of p(ti)δ.
Exercise 2 · 6 points
Parzen windows and Neyman–Pearson
E2: two classes, few points
Training sets: X1={1,−1,1,2,0} and X2={−1,0,1,3}.
(a) 3 points. Estimate p(x∣1) from X1 and p(x∣2) from X2 with Parzen windows, kernel K(x,y)=k(x−y):
k(z)={1/h0for ∣z∣≤h/2,for ∣z∣>h/2,h=2.
Plot the estimated densities. Find p(x∣1) and p(x∣2) for x=0.5.
(b) 3 points. Class 2 is dangerous. Construct the Neyman–Pearson strategy for overlooked danger <40%. What are the errors ε1 and ε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(μ2,C2):
μ1=[36],C1=[2002],μ2=[3−2],C2=[2002].
Definition first: write the formal definition of the Bayesian task with the reject option used in (b).
(2 points) Find the classifier minimising the probability of classification error.
(2 points) Reject option: saying "don't know" costs 0.1, an error costs 1. For which x∈R2 does the Bayes-optimal strategy say "don't know"?
(2 points) Find q:R2→{1,2} minimising the Bayes risk for W(1,2)=2, W(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 under a class with mean μ and covariance Σ?
p(x)=(2π)k∣Σ∣exp(−21(x−μ)⊤Σ−1(x−μ))
↓ for Σ=σ2I: Σ−1=σ21I, the quadratic form is a squared distance
p(x)∝exp(−2σ2∥x−μ∥2)
Contours are circles around μ. Same Σ in both classes: the normalising constant is the same and cancels in every ratio.
Find the class with the largest posterior, pmax(x).
If 1−pmax(x)<ε, answer that class.
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 and a distance d. Look at what the closest training points say.
1. Find the K points S of T closest to x in the metric d.
2. Vote: the class with the majority in S,
k=largmaxi=1∑K[[ki′=l]],(xi′,ki′)∈S
K=1: the nearest point is a triangle → class 2
K=3: two circles, one triangle → class 1
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 N 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.
Finding the nearest neighbour fast: k-d tree
Six points in the plane (labelled in the figure).
Split at the median in x: (7,2)
Each half at its median in y
Then in x 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(logN) per query in low dimensions.
Pick an element: the pivot (at random works well in practice).
Split the list: elements ≤ pivot (lesser), elements > pivot (greater).
We look for the k-th element (k=n/2 for the median). If lesser has at least k elements, recurse into it; otherwise recurse into greater for element k−∣lesser∣.
Only one side is searched: n+n/2+n/4+⋯≈2n on average, but O(n2) 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) even in the worst case.
Condensation: keep only the points that matter
Goal: a smaller training set A that classifies all of T correctly with 1-NN.
Put one random sample of T into A, the rest into B.
Classify the samples of B by 1-NN with training set A. Move every misclassified x∈B to A.
If anything moved in step 2, repeat step 2.
Output:A, the condensed training set for 1-NN.
Condensation on an example
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).
Split T into two sets A and B: T=A∪B, A∩B=∅.
Classify the samples of B by K-NN with training set A. Remove from B every misclassified sample.
Output:B, 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
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)=21 on [−1,1], p(x∣2)=41 on [0,4], priors p(1)=41, p(2)=43.
(5 points) Find the optimal classifier q∗(x), the one minimising the classification error. Write down the result.
(1 point) Compute the classification error ε∗.
(6 points) The 1-NN classifier, asymptotic case: the training and test sets grow to infinity. Compute its error εNN.
Definition first: write the formal definition of the task in (a).
Wrap-up
Solving an exam exercise
Write the definitions (what is given, what is the strategy, what is minimised).
Sketch the densities and the regions.
Formula first, then the numbers.
Check: errors in [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)=a for x∈[−1,0], p(x)=b for x∈(0,0.5], zero elsewhere. Training data: K points in [−1,0], L points in (0,0.5].
(1 point) Constraints on a,b so that p is a density.
(2 points) The likelihood of the data.
(4 points) ML formulas for a,b; compute them for XA={−0.1,0.2} and XB={−0.8,−0.5,−0.3,0.3}.
(6 points) Prior P(a)=4a3, 0≤a≤1. MAP formulas for a,b; compute them for the same sets.
Definition first: write the definitions of the ML and the MAP estimate.
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.