2.5.13 · D2Unsupervised Learning

Visual walkthrough — Anomaly detection methods

1,753 words8 min readBack to topic

We only need one idea: normal data lives in a fuzzy cloud. Anything far from the cloud is suspicious. Everything else is turning that sentence into arithmetic.


Step 1 — Start with a cloud of dots

WHAT. Draw the data. Each server-reading is a dot. We use two numbers per reading so we can see it: = CPU usage, = memory usage. So one reading is a point on a flat sheet.

WHY. Before any formula, we must look. The whole method is "learn what normal looks like", and normal is literally the shape of this cloud. If you can't see the cloud you can't say what's far from it.

PICTURE. The white dots below cluster in the middle. The lonely amber dot at the top-right is the reading we suspect — it sits far from the pack.

Figure — Anomaly detection methods

Step 2 — Find the centre of the cloud

WHAT. Compute the mean : the average position of all the dots.

  • ::: the number of training dots.
  • ::: the -th dot (the little up top is just a label, not a power).
  • ::: "add them all up, then split evenly" — the balance point.

WHY. Normal behaviour has a "typical" value. The mean is the single point that sits closest (in squared-distance) to everything at once — the natural stake-in-the-ground for "centre of normal".

PICTURE. The cyan cross marks . Notice it lands right in the thick of the white dots, not on any single dot.

Figure — Anomaly detection methods

Step 3 — Measure how the cloud is stretched and tilted

WHAT. Compute the covariance matrix .

  • ::: the arrow pointing from the centre out to one dot.
  • The little (transpose) ::: turns a standing column into a lying-down row so the multiplication produces a table, not a single number.
  • Averaging these tables ::: gives one table describing the cloud's overall spread.

Read the table as:

WHY. A round cloud and a stretched, tilted cloud both have the same centre but very different "shapes". The mean alone can't tell a point that's far along the fat direction (still normal) from one that's far across the thin direction (very abnormal). records the shape so we can judge distance fairly.

PICTURE. The cyan ellipse traces the shape describes. Its long axis is the direction of biggest spread; its short axis, the tightest.

Figure — Anomaly detection methods

Step 4 — Turn "far from the cloud" into one honest number

WHAT. Compute the Mahalanobis distance squared:

  • ::: arrow from centre to the point we're testing.
  • ::: the inverse of the shape matrix — it undoes the stretch and tilt, so distance is measured in "cloud units" not raw units.
  • The whole sandwich ::: one number: how many cloud-widths away sits.

WHY. Plain straight-line distance is unfair: 50 units along a fat direction is barely unusual, but 50 across a thin direction is wildly unusual. Multiplying by squeezes the fat directions and stretches the thin ones, so every direction is scored on the same "how surprising is this?" scale. We use (not ) precisely because we want to cancel the spread, not re-apply it.

PICTURE. Two dots sit the same ruler-distance from centre — but one is inside the cloud, one outside. In cloud-units (the dashed rings) they land on different rings: Mahalanobis sees what your eye sees.

Figure — Anomaly detection methods

Step 5 — Convert distance into a probability

WHAT. Feed into the Gaussian density:

  • ::: a hill that is tall at the centre () and shrinks fast as you walk out.
  • ::: the "volume" of the cloud; dividing by it keeps the total probability equal to no matter how big the cloud.
  • ::: number of features.

WHY use ? We need something that (a) is largest at the centre, (b) never goes negative (probabilities can't), and (c) fades smoothly and quickly far away. does exactly this — small distance → near 1, large distance → crashes toward 0. No polynomial fades this cleanly.

PICTURE. The surface is a smooth bell sitting over the cloud: high in the middle, hugging zero at the edges. Height = probability = "how normal".

Figure — Anomaly detection methods

Step 6 — Slice the bell with a threshold

WHAT. Pick a small cutoff . Flag as an anomaly when .

  • ::: a tiny probability floor we choose (e.g. ).
  • ::: "the bell is too low here — the point lives where almost no normal data does".

WHY. Because shrinks as grows, " below a floor" is exactly the same as " above a ceiling". So the threshold on probability is really a ring drawn on the plane: inside the ring = normal, outside = anomaly. Choosing = choosing how tight that ring is.

PICTURE. The amber contour is the level . Dots inside are green (normal); the amber dot outside is flagged.

Figure — Anomaly detection methods

Step 7 — The edge and degenerate cases

WHAT. Three ways the picture can break, and what happens.

Case A — a dead feature ( has a zero-spread direction). If every training reading has, say, the same value in some direction, the cloud is a flat line, its "volume" , and doesn't exist — you'd divide by zero. Fix: drop the useless feature, or add a tiny to the diagonal () so the ellipse always has thickness.

Case B — a lonely point on the fat axis. A point can be far from centre yet still land inside the amber ring, because it lies along the cloud's long direction. Straight-line distance would panic; Mahalanobis correctly says "normal". This is the whole reason we bothered with .

Case C — too large or too small. Huge → tiny ring → half the normal cloud gets flagged (false alarms). Tiny → giant ring → real anomalies slip through. The ring's radius is the false-alarm dial.

WHY. The reader must never meet a scenario we hid. Zero-variance, along-axis outliers, and threshold extremes are the three traps in real deployments.

PICTURE. Left: a collapsed line-cloud (Case A) with the amber warning. Middle: the along-axis point sitting safely inside the ring (Case B). Right: same data, two rings for two values (Case C).

Figure — Anomaly detection methods

The one-picture summary

Everything above, on a single frame: the cloud (Step 1), its centre (Step 2), its shape ellipse (Step 3), the cloud-distance rings (Step 4), the bell shrinking outward (Step 5), and the amber threshold ring that flags the outsider (Step 6).

Figure — Anomaly detection methods
Recall Feynman retelling — say it back in plain words

We dumped all the normal readings as dots and found their middle. Then we noticed the dots weren't a neat circle — they were a stretched, tilted blob, and we wrote that shape down in a little table. To ask "is a new reading weird?", we didn't just measure straight-line distance to the middle, because that's unfair to the blob's shape. Instead we measured distance through the blob — squeezing the fat directions, stretching the thin ones — so one honest number says how many blob-widths away the point is. We fed that number into a bell curve: near the middle it's tall (very normal), far out it drops to almost nothing (very rare). Finally we drew a ring at a chosen height; anything landing outside the ring — living where normal data essentially never goes — gets flagged. The only trap: never forget to measure through the blob's shape, or a normal point lying along the long axis will scare you for no reason.


Prev: Parent · Anomaly detection methods