HDBSCAN
DBSCAN finds clusters of any shape but needs one density threshold for the whole dataset — HDBSCAN builds a hierarchy across every possible threshold at once and picks out the clusters that survive longest, so one region can be dense and another sparse without either breaking the result.
Prerequisites: DBSCAN Density Clustering, Hierarchical Agglomerative Clustering
DBSCAN finds clusters by density — a point belongs to a cluster if enough other points sit within some radius of it — and unlike k-means it never assumes clusters are round or forces every point into one. But it needs a single for the entire dataset, and financial data rarely obliges: a group of large-cap tech stocks might move together tightly (dense) while a group of small-cap energy names moves together loosely (sparse). One that fits the tight cluster will split the loose one into noise; one that fits the loose cluster will merge the tight one into everything nearby.
HDBSCAN doesn't pick one density threshold — it runs DBSCAN conceptually at every threshold at once, builds a tree of how clusters merge and split as the threshold relaxes, and then keeps whichever clusters in that tree are the most stable across a wide range of thresholds, rather than whichever exist at one arbitrarily chosen threshold.
Building and pruning the hierarchy
HDBSCAN starts by re-scaling distances using mutual reachability distance, which inflates the distance between two points if either sits in a sparse region — so an isolated point is pushed even further from everything else before clustering starts. It then builds a minimum spanning tree on these adjusted distances and progressively cuts the longest edges, producing a hierarchy: at a loose threshold everything is one cluster, and as the threshold tightens, that cluster splits into smaller ones, some of which keep splitting further and some of which just shrink and vanish into noise.
The last step is cluster stability selection: for every candidate cluster in that hierarchy, HDBSCAN measures how large a range of thresholds it survives across before either splitting or disappearing. Clusters that persist across a wide range are kept as the final answer; clusters that only ever existed briefly, at one narrow threshold, are absorbed back into their parent or discarded as noise. This is what lets a tight cluster and a loose cluster both survive in the same final result — each is evaluated on its own persistence, not against a shared, dataset-wide radius.
Worked example
Clustering 800 global equities by co-movement, plain DBSCAN with finds a clean 40-stock cluster of mega-cap semiconductor names (tightly correlated) but lumps 300 unrelated mid-caps into one giant blob, because is loose enough to bridge them. Tightening to breaks up the blob sensibly but now shatters the semiconductor cluster into six fragments, since 0.08 is too strict for how those 40 stocks actually co-move. Running HDBSCAN on the same data (with min_cluster_size = 15) recovers the 40-stock semiconductor cluster intact — because it's dense and stable across a narrow range — while also splitting the mid-cap blob into several sensible, looser sector groupings, because each is separately stable at its own, wider range.
What this means in practice
HDBSCAN is the more forgiving default whenever cluster density varies across a dataset, which is the normal case in cross-sectional finance — large caps, small caps, and different sectors simply don't co-move with the same tightness. Its main cost is a less interpretable single knob: min_cluster_size replaces , and it controls the smallest group HDBSCAN is willing to call a cluster rather than noise, which takes some experimentation to set well.
Unlike k-means, both DBSCAN and HDBSCAN can leave points unassigned to any cluster ("noise"). In a portfolio context, that's often useful information on its own — a stock that doesn't fit any co-movement group is a genuine idiosyncratic name, not an error to be forced into the nearest cluster.
Related concepts
Practice in interviews
Further reading
- Campello, Moulavi & Sander, 'Density-Based Clustering Based on Hierarchical Density Estimates' (2013)
- McInnes, Healy & Astels, 'hdbscan: Hierarchical Density Based Clustering' (2017)