COMP90073 · Trustworthy Machine Learning · Week 5

Anomaly Detection Workbook

The Week 5 tutorial, made interactive. Each question starts with the one idea from the lectures you need, then walks to the official answer in small steps.

Lecture 7 · Intro & iForestLecture 8 · Clustering, DBSCAN & LOF9 questions
FIRST
Measure mistakes
Precision, recall, F-score, AUROC, AUPRC — Q1, Q2, Q4
THEN
Define “anomaly”
Anomaly vs. noise vs. novelty — Q5, Q6
THEN
Detect by distance
k-means distance to centroid — Q3
FINALLY
Detect by density
DBSCAN and LOF — Q7, Q8, Q9
Q1

Which metric, when?

How do precision, recall, F-score, AUROC and AUPRC guide us in anomaly detection? Give a scenario for each.

Remember from Lecture 7

Accuracy fails on rare anomalies: with 9,990 normal and 10 anomalous samples, a model that predicts “normal” for everything scores 99.9% accuracy and catches zero anomalies. So we use metrics built from the four cells below.

Tap a metric — see which cells it uses, what it asks, and when to use it
Actual anomalyActual normal
FlaggedTP
caught correctly
FP
false alarm
Not flaggedFN
missed
TN
correctly ignored
Q2

F1-score for cluster B

6,000 points were clustered into A, B, C. What is the F1-score with respect to cluster B?

Remember

“With respect to B” means: B is the positive class, everything else is negative. Then Precision = , Recall = , F1 = .

Step through — the table lights up as you go
Actual
ABCSUM
PredictedA6004002001200
B100012002002400
C40040016002400
SUM200020002000
Primer

How k-means works — and how it scores anomalies

Before Q3 questions this scheme, watch it run once: cluster first, then score by distance.

Remember from Lecture 8

k-means summarises the data with k centres: assign every point to its nearest centre, move each centre to the mean of its points, repeat until nothing moves. Then each point gets an anomaly score — your distance to your centre, relative to your cluster-mates. ≈ 1 means typical; ≫ 1 means anomaly.

One full run · 24 points · k = 3
✚ centre assigned point not assigned yet high score

Q3

k-means as an outlier detector

Points are scored by distance to their nearest cluster centre. Three questions about the figure below.

Remember from Lecture 8

Anomaly score of a point = its distance to the nearest centroid (relative to the average distance in that cluster). Big distance → likely anomaly. But the centroid is the mean of its cluster — and means get pulled by outliers.

The figure — toggle the ideas
Q4

Detection rate & false alarm rate

P(normal flagged) = 0.01, P(anomaly flagged) = 0.99, and 99% of objects are normal. Find both rates.

Remember

Detection rate =  ·  False alarm rate = . Think in counts, not probabilities — take 10,000 objects.

Count it out — 10,000 objects
Q5

Supervised vs. unsupervised, when fraud keeps evolving

Fraudsters invent new tricks all the time. What are the merits of each approach here?

Remember from Lecture 7

Supervised = learn from labelled examples (like signature detection: great on known patterns). Unsupervised = model normal behaviour and flag deviations (can catch new patterns).

Two detectors, two blind spots

Supervised

Trained on labelled past fraud.

Strong: accurate on known fraud types.
Blind: a brand-new trick matches no training label — it slips through.

Unsupervised

Models normal transactions; flags deviations.

Strong: can flag fraud types never seen before.
Blind: more false alarms; no notion of “known fraud”.

Q6

Noise vs. outliers

Five quick true-or-false style checks. Tap each card for the answer.

Remember from Lecture 7

Noise = random error or variance — not interesting. Anomalies = interesting, because they may come from a different mechanism than the rest of the data.

a · Is noise ever interesting or desirable? Anomalies?
b · Can noise objects be outliers?
c · Are noise objects always outliers?
d · Are outliers always noise?
e · Can noise turn a typical value unusual, or vice versa?
Primer

How DBSCAN works, step by step

Before Q7 and Q8 ask you to reason about DBSCAN, watch it run once on 16 points.

Remember from Lecture 8

Two knobs: ε — the radius of the circle around each point — and MinPts — how many points (itself included) must sit inside that circle to make a point a core. Clusters grow by chaining core points; the algorithm visits every point once.

One full run · ε = 8, MinPts = 4
core (filled) border (hollow) × anomaly not visited yet

Q7

DBSCAN: raise MinPts, what changes?

MinPts = 6, ε = 0.1 gave 4 clusters and 5% outliers. Same ε, MinPts = 8 — what do you expect?

Remember from Lecture 8
  • Core point: at least MinPts points inside its ε-ball (itself included).
  • Border point: not core, but inside a core point's ε-ball.
  • Anomaly: neither. Clusters grow by connecting core points.
Live demo — same ε, flip MinPts (different data, same effect)
core (filled) border (hollow) × outlier
–clusters
–outliers
–core points

Raising MinPts demotes core points. Fewer cores → the connection graph loses nodes and edges → sparse clusters dissolve or split, and more points end up as outliers.

Q8

Run DBSCAN by hand

8 points, MinPts = 2. Find the clusters for ε = 2, then for ε = √10.

Remember

With MinPts = 2, a point is core as soon as one other point sits within ε. So: list each point's ε-neighbourhood, then chain neighbours into clusters. Points with an empty neighbourhood are anomalies.

Step through the run, or tap any point to inspect its ε-circle
pointε-neighbourhoodrole
Primer

How LOF works, step by step

Before Q9 compares LOF to k-NN, compute one LOF by hand — the exact example from Lecture 8.

Remember from Lecture 8

LOF asks one question: am I as densely packed as my neighbours? The pipeline: distance to your k-th neighbour (dist₂) → smoothed distances (reachdist) → local density (lrd) → the ratio of your neighbours' density to yours (LOF). Ratio ≈ 1: you fit in. Ratio > 1: you are the sparse one.

a(0,0) · b(0,1) · c(1,1) · d(3,0) · k = 2 · Manhattan distance

Q9

LOF vs. k-NN on the Ionosphere data

Rank anomalies with LOF, then with k-NN distance (k = 1…5, raw and normalised). How much do the top-5 lists agree?

Remember from Lecture 8
  • k-NN score: distance to your k-th nearest neighbour. Far = anomalous. A global view.
  • LOF: ratio of your neighbours' local density to your own. LOF ≈ 1 → like your neighbours; LOF > 1 → less dense than them → anomalous. A local view — the primer above computes one by hand.
The real data — all 351 radar returns (PCA view) with each method's top-5
k-NN: k =
'good' return 'bad' return LOF top-5 ◆ k-NN top-5

–LOF ∩ k-NN
–raw ∩ normalised

Different lens, different anomalies: LOF (local density) and k-NN (global distance) pick entirely different points here. Scaling the features reshuffles rankings too, though raw and normalised k-NN still mostly agree.