2.5.13 · D3Unsupervised Learning

Worked examples — Anomaly detection methods

3,138 words14 min readBack to topic

This page is the "hands get dirty" companion to the parent topic (also available in Hinglish). We are not learning new theory here — we are stress-testing the three methods from the parent (Gaussian density, Isolation Forest, One-Class SVM) against every kind of input they can meet. Before you read a solution, you will be asked to Forecast — guess the outcome. Guessing first is what turns reading into learning.

Everything is built from the ground up. If a symbol appears, it was defined either in the parent or right here.


The scenario matrix

Think of anomaly detection as a machine with a big lever (the threshold) and some knobs (the model parameters). The matrix below lists every kind of situation that lever-and-knobs machine can be handed. Our worked examples must touch every cell.

Cell What makes it tricky Which method Example
A. Point far in ALL directions Big deviation in every feature — the "obvious" anomaly Gaussian Ex 1
B. Point that looks normal per-feature but not jointly Each coordinate is fine alone; only the correlation betrays it Gaussian Ex 2
C. Exactly at the mean (degenerate/zero distance) , distance , must give the highest possible density Gaussian Ex 3
D. Threshold boundary case sits right at — which side wins? Gaussian Ex 4
E. Easy-to-isolate point (short path) Anomaly that a random split slices off immediately Isolation Forest Ex 5
F. Deep-in-the-crowd point (long path) Normal point needing many splits Isolation Forest Ex 5
G. Limiting behaviour of the score What score means "as anomalous as possible" and "as normal as possible" Isolation Forest Ex 6
H. One-Class SVM sign flip Decision score crosses zero — inside vs outside the learned region One-Class SVM Ex 7
I. Real-world word problem Translating messy language into Gaussian Ex 8
J. Exam twist: give the answer, find the knob Reverse-engineer (or ) from a required detection Gaussian Ex 9

Every numeric result below is machine-checked in the verify block.


Warm-up: the three quantities we keep reusing

Before any example, let us pin down the three objects, in plain words and pictures.

First, the object every Gaussian example compares against a threshold: the probability density . In plain words, is a height — how "tall" the bell-shaped hill of normal data is above the point . Tall (large ) = crowded, typical spot; near-zero (small ) = lonely, suspicious spot. Its formula, straight from the parent note, is

where is the number of features. Notice the entire exponent is built from one scalar, the thing inside the parentheses — which we now name.

The picture below is the whole intuition in one frame.

Figure — Anomaly detection methods

Look at the concentric ellipses: each is a constant- ring. Walking outward, grows and falls. The Gaussian test simply asks: "which ring is on, and is that ring past our fence ?"

Throughout the Gaussian examples we reuse the server-monitoring covariance from the parent:

We will need . Its determinant is , so


Ex 1 — Cell A: far in every direction

Forecast: both numbers are way above normal — expect a huge and a microscopic . Anomaly, surely.

  1. Deviation vector. . Why this step? Everything downstream measures distance from the center, so we must subtract the center first.

  2. Apply to the deviation. Why this step? re-weights each direction by how unusual movement there is. Movement along a low-variance direction is punished more.

  3. Finish the quadratic form. Why this step? is the single scalar the density depends on.

  4. Density. With : Why this step? This is the actual number we compare to .

  5. Decide. anomaly.

Verify: Sanity — is enormous (a 1-sigma point has ), so a near-zero density is exactly what we expect. Units: is dimensionless (it was scaled by ), has units of , and only the ordering against matters. ✓


Ex 2 — Cell B: each feature fine, joint value not

This is the case that makes covariance earn its keep.

Forecast: feature-by-feature this looks borderline-OK. But the two features are positively correlated (): normally, high CPU ⇒ high memory together. Here CPU is up while memory is down — anti-correlated. Predict a surprisingly large .

  1. Deviation. . Why this step? Same as always: distance is from center.

  2. Per-feature sanity (the trap). and . So each is a few sigma out — not crazy alone. Why this step? Shows what a naive per-feature detector would (mis)judge.

  3. Joint distance with . Why this step? The off-diagonal terms of inject the correlation penalty. Going against the natural correlation inflates .

  4. Compare to axis-aligned intuition. If we had ignored correlation (pretended diagonal), we'd get . The true is bigger — the anti-correlation adds ~.

Verify: confirms correlation raised the alarm. ✓ (Checked numerically below.)


Ex 3 — Cell C: degenerate input,

Forecast: zero distance ⟹ maximum density ⟹ never an anomaly.

  1. Deviation is zero. . Why this step? This is the degenerate case the matrix demanded — the exact center.

  2. Quadratic form vanishes. . Why this step? Any quadratic form of the zero vector is .

  3. Density is the peak. Why this step? is the largest the exponential can be, so this is the global maximum of .

  4. Decide. For any sensible (like ), never flagged. Good — the model must trust its own center most.

Verify: is the largest value ever takes; every other point has . Numeric check below. ✓


Ex 4 — Cell D: exactly on the threshold

Forecast: the fence is a specific ring; inside it (smaller ) = normal, outside = anomaly. A point exactly on the fence needs a stated convention.

  1. Set density equal to threshold. Why this step? The boundary of the decision rule is exactly where .

  2. Solve for . Multiply out and take logs: Why this step? The undoes the exponential — it is the tool that answers "what exponent produced this value?"

  3. Classify (a): exactly on the fence, . Here exactly. The parent's rule is "anomaly if " — a strict inequality. So a point with is not flagged; it is (by convention) counted as normal. The fence ring itself belongs to the normal side. Why this step? The boundary case must have a stated tie-break; the strict "" means equality ⟹ keep as normal.

  4. Classify (b): . Since , we have anomaly. Why this step? Larger = lower density = strictly past the threshold.

Verify: Plug back: . Boundary consistent, and the tie-break (" ⟹ normal") is the strict-inequality convention. ✓

Figure — Anomaly detection methods

The figure shows the fence ring for Ex 4: the point at (pink) sits outside it, the center (yellow) sits deep inside, and the on-fence point lives on the yellow ring (normal side).


Ex 5 — Cells E & F: Isolation Forest short vs long path

Forecast: short path (2.1) ⟹ high score ⟹ anomaly; long path (7.2) ⟹ lower score, but with this small both might be above — watch out.

  1. Normalizer. . Why this step? is the expected path length for points, our "normal baseline," so dividing by it makes scores comparable across dataset sizes.

  2. Normal score. Why this step? Apply the score formula; maps path length onto .

  3. Anomaly score. Why this step? Same formula, shorter path ⟹ larger exponent-magnitude reduction ⟹ higher score.

  4. Decide. ⟹ anomaly (correct). But too — the normal point is also flagged! The threshold is too loose for this .

Verify: anomaly score () normal score () — ordering is right, the method ranks correctly even when the fixed threshold misfires. ✓


Ex 6 — Cell G: limiting behaviour of the isolation score

Forecast: instant isolation should give the most anomalous score (near 1); average depth should give exactly ; buried-forever should approach .

  1. Instant isolation, . . Why this step? means "isolated with zero questions" — the most extreme outlier imaginable, so the score maxes out at .

  2. Average depth, . . Why this step? When a point behaves exactly like the baseline, the score is a neutral — the natural "undecided" midpoint.

  3. Buried forever, . . Why this step? Deeper than everyone ⟹ maximally normal ⟹ score floors at .

Verify: The score's range is exactly with at the baseline — matches the parent's claim "near 1 = anomaly, near 0 = normal." ✓


Ex 7 — Cell H: One-Class SVM sign flip

Recall from the parent: a point is an anomaly when its decision score is negative (on the origin side of the learned hyperplane). We build a tiny concrete instance.

Forecast: sits right among the support vectors ⟹ high kernel sums ⟹ , normal. is far ⟹ kernels ≈ 0 ⟹ , anomaly.

  1. Kernel of to each SV. Distances-squared: to : ; to : . Why this step? The RBF kernel measures closeness — near points give values near .

  2. Decision score of . Why this step? Positive = inside the enclosing region.

  3. Kernel of . Distances-squared: to : ; to : . Why this step? Far points give near-zero kernel values — the "influence" of each SV decays fast.

  4. Decision score of . Why this step? Sum of tiny kernels can't reach , so goes negative — outside the region.

Verify: flipped sign () exactly as the geometry predicts. ✓

Figure — Anomaly detection methods

The blue-shaded blob is the region where (normal). (yellow) is inside; (pink) is well outside.


Ex 8 — Cell I: real-world word problem

Forecast: temperature is sigma high, humidity sigma low; independent, so just adds their squares. — expect an anomaly.

  1. Translate words to model. Independent ⟹ is diagonal: Why this step? "Independent" is the plain-English word for "zero off-diagonal covariance."

  2. Deviation. . Why this step? Distance from the normal operating point.

  3. Diagonal = sum of standardized squares. Why this step? With a diagonal , Mahalanobis distance is just each feature measured in its own sigma-units, squared and summed.

  4. Decide. anomaly — flag the oven.

Verify: (temp is ), (humidity ); sum . Units check: each term is (units)/(units) = dimensionless. ✓


Ex 9 — Cell J: exam twist, reverse-engineer the threshold

Forecast: we work backwards through the density formula to solve for . Then check that a much smaller gives .

  1. Density at the required point. Why this step? The fence is defined by " at that point ," so compute that .

  2. Set . . Why this step? Choosing equal to the density there makes that exact point the boundary.

  3. Check a normal point, . Since , it is safely normal. Why this step? A tighter (smaller ) point should have far higher density than the fence — confirms monotonicity.

Verify: reproduces on the boundary, and the point has density ~ larger. ✓


Recall Self-test (reveal after guessing)

What single scalar decides the Gaussian test? ::: The Mahalanobis distance squared — density is a monotone-decreasing function of it. Why can a point be normal per-feature yet anomalous jointly? ::: Because the off-diagonal terms of penalize movements that violate the learned correlation (Ex 2). In Isolation Forest, what score corresponds to "average / baseline" behaviour? ::: Exactly , when (Ex 6). For One-Class SVM, which sign of is the anomaly? ::: — the origin side, outside the enclosing region (Ex 7).