Quant Memo
Core

DBSCAN Density Clustering

DBSCAN finds clusters by looking for regions where points are packed densely together, letting clusters take any shape and automatically labelling sparse, isolated points as noise instead of forcing them into a group.

Prerequisites: K-Means Clustering

K-means clustering assumes clusters are round blobs and forces every single point into one of a fixed number of groups, even the stray point that clearly belongs to no group at all. Real data is rarely so obliging — clusters can be curved, elongated, or wrap around each other, and some points genuinely are noise that shouldn't be forced anywhere. DBSCAN clusters by a different rule entirely: not "which centre is closest" but "how crowded is the neighbourhood around me."

The analogy: finding crowds in a stadium from above

Imagine looking down at a stadium's crowd from a drone. Dense knots of people standing shoulder to shoulder are clearly "a group," regardless of what shape that group traces on the ground — a group could be a long snaking queue, a ring around a vendor, or a blob. Meanwhile a lone person standing far from everyone else is obviously not part of any group. You don't need to know how many groups there are in advance, and you don't need every person to belong to one — you just need a rule for "how many people nearby counts as a crowd." DBSCAN applies exactly this rule to data points instead of people.

The maths: core points, reachability, and noise

DBSCAN needs two parameters: ε\varepsilon, a distance defining "nearby," and minPts\text{minPts}, the number of neighbours required to count as dense. A point pp is a core point if it has at least minPts\text{minPts} neighbours within distance ε\varepsilon:

{q:dist(p,q)ε}minPts|\{q : \text{dist}(p, q) \le \varepsilon\}| \ge \text{minPts}

In words: count how many other points sit within a small radius of pp; if that count clears the threshold, pp is sitting in a dense region. A cluster is then built by chaining core points together: any core point directly reachable from another core point joins the same cluster, and non-core points that fall within ε\varepsilon of a core point are added to that cluster as border members without needing density of their own. Any point that is neither a core point nor within ε\varepsilon of one is labelled noise — it belongs to nothing.

Worked example 1: three points, minPts = 2

Points at positions 1,2,3,101, 2, 3, 10 on a line. Set ε=1.5\varepsilon = 1.5, minPts=2\text{minPts} = 2. Check point 22: neighbours within 1.51.5 are 11 and 33 (distances 11 and 11), so it has 2 neighbours — that meets minPts=2\text{minPts}=2, so 22 is a core point. Point 11: only neighbour within 1.51.5 is 22 (distance 11); that's 1 neighbour, just short of minPts=2\text{minPts}=2 on its own, but since 11 falls within ε\varepsilon of core point 22, it joins as a border point. Same logic makes 33 a border point of the same cluster. Point 1010 has no neighbours within 1.51.5 of anything — it's noise. Result: one cluster {1,2,3}\{1,2,3\}, one noise point {10}\{10\}, with no need to have specified "2 clusters" in advance.

Worked example 2: why shape doesn't matter

Consider points tracing a thin crescent shape plus a small round blob nested inside the crescent's curve. K-means with k=2k=2, which only understands round clusters built around a centre point, would slice straight through both shapes and badly misassign points — a centre placed in the middle of the crescent isn't actually close to most of the crescent's own points. DBSCAN instead walks along the crescent: each point along the arc has enough densely-packed neighbours along the arc to count as core, chaining the whole crescent into one cluster regardless of its curve, while the round blob's points chain into a separate cluster by the same local-density logic. Neither cluster ever needed to be "round" or centred anywhere — density decided the shape, not distance-to-a-centre.

Decision boundary
flexibility 3.0points on wrong side 9reasonable

The explorer above shows how classifier flexibility changes what regions get grouped together — DBSCAN's clusters are the unsupervised analogue: dense regions get outlined in whatever shape they actually take, not forced into a preset geometry.

Function explorer
-221.1
x = 1.00f(x) = 0.731

Density-based membership is a threshold effect, not unlike the steep transition of a logistic curve shown here — a point is either "in a dense enough neighbourhood" or it isn't, with the ε\varepsilon/minPts pair setting where that line falls.

DBSCAN groups points by local density rather than distance to a centre, which lets clusters take any shape and automatically flags sparse points as noise instead of forcing them into the nearest group.

What this means in practice

DBSCAN is useful wherever clusters aren't round and where "not everything belongs to a group" is a real possibility — detecting clusters of correlated trading anomalies, grouping similar market regimes from irregularly shaped feature clouds, or finding genuine outlier trades that shouldn't be lumped into any behavioural cluster. Its Achilles' heel is a single global ε\varepsilon: if clusters in the data have very different densities, one ε\varepsilon that works for the dense cluster will swallow the sparse one into noise, or vice versa.

The classic mistake is treating ε\varepsilon and minPts\text{minPts} as safe defaults rather than data-dependent choices. Too small an ε\varepsilon turns almost everything into noise; too large an ε\varepsilon merges genuinely separate clusters into one blob. Unlike k-means, DBSCAN doesn't need you to guess the number of clusters — but it trades that for needing you to guess the right notion of "nearby," which is not obviously easier.

Related concepts

Practice in interviews

Further reading

  • Ester, Kriegel, Sander & Xu, A Density-Based Algorithm for Discovering Clusters (1996)
  • Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 14.3
ShareTwitterLinkedIn