HDBSCAN (Hierarchical DBSCAN) › Formula

Michael BrenndoerferAugust 17, 202511 min read
Machine Learning from ScratchAll chapters
Part 7 of 9Unsupervised Learning
Chapter 56HDBSCAN (Hierarchical DBSCAN) › Formula
Reading section 2 of 8 · Formula · 11 minSection 2 of 8 · 11 min

Book contents

Section 2 of 8 · 11 min read

HDBSCAN (Hierarchical DBSCAN) › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To understand how HDBSCAN works, we need to explore how it measures density, builds relationships between points, and selects meaningful clusters. The algorithm adapts to varying densities across the dataset, which is a capability that many traditional clustering methods lack. Let's build this understanding step by step, starting with the fundamental question: how do we measure local density in a way that works for clusters of different sizes and shapes?

Understanding Local Density Through Neighborhoods

Imagine you're standing in a crowded city square. If you look around and see many people nearby, you're in a dense area. If you see only a few people far away, you're in a sparse area. HDBSCAN uses a similar intuition, but formalizes it mathematically through the concept of neighborhoods.

For each point in the dataset, HDBSCAN asks: "How far do I need to look to find k other points?" This distance tells us about the local density around that point. Points in dense clusters will have nearby neighbors, while isolated points or noise will need to look much farther.

This idea is captured by the core distance, which measures the distance from a point to its k-th nearest neighbor:

corek(x)=d(x,kNN(x))\text{core}_k(x) = d(x, \text{kNN}(x))

where:

  • corek(x)\text{core}_k(x): core distance of point xx (distance to its k-th nearest neighbor)
  • d(x,kNN(x))d(x, \text{kNN}(x)): Euclidean distance from point xx to its k-th nearest neighbor
  • kk: the min_samples parameter, which controls how many neighbors we require (typically set to the minimum number of samples in a neighborhood for a point to be considered a core point)
  • xx: a data point in the dataset
  • kNN(x)\text{kNN}(x): the k-th nearest neighbor of point xx

The core distance is small for points in dense regions and large for isolated points. This gives us a local measure of density that adapts to each point's immediate surroundings, rather than using a single global threshold like DBSCAN does.

Creating a Density-Aware Distance Metric

Now comes the key insight: when measuring distances between points, we should account for the density of the regions they inhabit. Two points in a dense cluster should be considered "close" even if their Euclidean distance is moderate, because they're both in a region of high density. Conversely, two points in sparse regions should be considered "distant" even if they're relatively close in Euclidean space, because they're both in low-density areas.

This leads to the mutual reachability distance, which adjusts distances based on local density:

dmreach(x,y)=max⁡{corek(x),corek(y),d(x,y)}d_{\text{mreach}}(x,y) = \max\{\text{core}_k(x), \text{core}_k(y), d(x,y)\}

where:

  • dmreach(x,y)d_{\text{mreach}}(x,y): mutual reachability distance between points xx and yy (a density-aware distance metric)
  • corek(x)\text{core}_k(x): core distance of point xx (distance to its k-th nearest neighbor)
  • corek(y)\text{core}_k(y): core distance of point yy (distance to its k-th nearest neighbor)
  • d(x,y)d(x,y): Euclidean distance between points xx and yy
  • kk: the min_samples parameter (same as in the core distance calculation)
  • x,yx, y: two data points in the dataset

This formula ensures that the distance between two points is at least as large as the core distance of either point. Here's why this matters: if either point is in a sparse region (large core distance), the mutual reachability distance will reflect that sparsity, effectively "stretching" the distance between sparse regions. Points in dense regions (small core distances) remain close to each other, while points in sparse regions become farther apart.

This density-aware distance metric is the foundation that allows HDBSCAN to handle clusters of varying densities. By transforming the original distance space into a mutual reachability space, we create a representation where dense clusters are well-separated from sparse regions, making them easier to identify.

Building a Hierarchical Structure

With our density-aware distances, we can now construct a hierarchical representation of the data. HDBSCAN builds a minimum spanning tree (MST) using the mutual reachability distances. The MST connects all points using the minimum total edge weight, creating a tree structure where each edge represents a connection between points.

The MST construction finds the spanning tree TT that minimizes:

∑(x,y)∈Tdmreach(x,y)\sum_{(x,y) \in T} d_{\text{mreach}}(x,y)

where:

  • TT: a spanning tree (a connected acyclic graph that connects all points in the dataset)
  • (x,y)∈T(x,y) \in T: an edge in the spanning tree connecting points xx and yy
  • dmreach(x,y)d_{\text{mreach}}(x,y): mutual reachability distance between points xx and yy (used as the edge weight)
  • x,yx, y: data points connected by an edge in the tree

Why a tree? A tree structure captures the essential connectivity of the data while eliminating redundant connections. Each point is connected to the rest of the dataset through a unique path, and the edges in this tree represent the most "natural" connections based on our density-aware distances. This tree structure allows us to build a hierarchy by progressively removing edges.

The hierarchical nature emerges when we consider what happens as we remove edges from the MST in order of decreasing weight. Removing a long edge (representing a connection between sparse regions) separates the tree into components, which we can interpret as potential clusters. As we continue removing edges, we create a hierarchy of cluster structures at different density levels.

Measuring Cluster Persistence Through Stability

The challenge now is: which clusters should we select from this hierarchy? Not all clusters are equally meaningful. Some clusters appear only at very specific density thresholds and disappear quickly as we change the threshold. Others persist across a wide range of density levels, suggesting they represent stable, meaningful structures in the data.

HDBSCAN introduces the concept of stability to quantify how persistent a cluster is across different density thresholds. For each point in a cluster, we track when it first joins the cluster (its "birth" threshold) and when it leaves (its "death" threshold). A cluster that persists across many density levels, and contains many points that remain in the cluster for a long time, is considered more stable and therefore more meaningful.

The stability of a cluster CC is defined as:

stability(C)=∑x∈C(λdeath(x)−λbirth(x))\text{stability}(C) = \sum_{x \in C} (\lambda_{\text{death}}(x) - \lambda_{\text{birth}}(x))

where:

-stability(C)\text{stability}(C): stability score of cluster CC (a measure of how persistent the cluster is across different density thresholds)

  • λbirth(x)\lambda_{\text{birth}}(x): density threshold (λ value) at which point xx first joins the cluster (the "birth" threshold)
  • λdeath(x)\lambda_{\text{death}}(x): density threshold (λ value) at which point xx leaves the cluster (the "death" threshold), with λdeath(x)>λbirth(x)\lambda_{\text{death}}(x) > \lambda_{\text{birth}}(x)
  • CC: a cluster (a set of data points that form a coherent group)
  • x∈Cx \in C: a data point belonging to cluster CC
  • ∑x∈C\sum_{x \in C}: sum over all points in cluster CC

This formula sums the "lifespan" of each point within the cluster across density thresholds. Since death occurs at a higher threshold than birth, the difference is positive, representing how long each point remains in the cluster. Clusters with high stability are those where many points remain together across a wide range of density levels, indicating a coherent structure that persists as density changes.

Selecting Optimal Clusters

The final step is to select the set of clusters that maximizes total stability while ensuring no cluster is a subset of another. This optimization problem can be written as:

optimal clusters=arg⁡max⁡C∑C∈Cstability(C)\text{optimal clusters} = \arg\max_{\mathcal{C}} \sum_{C \in \mathcal{C}} \text{stability}(C)

where:

-C\mathcal{C}: a set of clusters selected from the hierarchy (a collection of non-overlapping clusters)

  • C∈CC \in \mathcal{C}: a cluster in the selected set
  • stability(C)\text{stability}(C): stability score of cluster CC (as defined in the stability formula above)
  • ∑C∈Cstability(C)\sum_{C \in \mathcal{C}} \text{stability}(C): total stability across all selected clusters
  • arg⁡max⁡\arg\max: the argument (set of clusters) that maximizes the total stability

subject to the constraint that no cluster is a subset of another selected cluster. This ensures we select the most stable, meaningful clusters without redundancy.

The constraint prevents us from selecting both a parent cluster and its child cluster, which would double-count stability. Instead, we choose the cluster configuration that maximizes total stability across all selected clusters, giving us the representation that best preserves persistent structure in the data.

Why This Mathematical Framework Works

The mathematical foundation of HDBSCAN provides several important guarantees. The mutual reachability distance satisfies the triangle inequality, making it a proper metric. This property enables the use of efficient algorithms for MST construction, such as Kruskal's or Prim's algorithm.

The stability measure provides a principled, data-driven way to select clusters, addressing one of the main limitations of traditional DBSCAN: the need to manually choose a single density threshold. Instead of making this difficult choice, HDBSCAN explores all possible density thresholds and selects clusters based on how long they persist.

The hierarchical approach naturally captures nested cluster structures and provides a way to handle clusters of varying densities within the same dataset. The algorithm's theoretical foundation ensures convergence to a stable solution and provides guarantees about cluster quality under reasonable assumptions about the data distribution.

Visualizing Cluster Stability

Let's create a visualization that demonstrates how the stability measure works in practice:

Out[3]:
Visualization
Horizontal bar chart showing cluster lifespans across density thresholds.
Cluster hierarchy showing birth and death thresholds for different clusters. Each horizontal bar represents a cluster's lifespan across density thresholds (λ values). The width of each bar indicates how long the cluster persists, with longer bars representing more stable clusters. Here's how HDBSCAN tracks cluster stability across different density levels.
Bar chart showing stability scores for each cluster.
Stability scores for each cluster calculated using the formula: stability = Σ(λ_death - λ_birth) for all points in the cluster. Higher stability scores indicate clusters that are both large and persistent across density thresholds. The algorithm selects clusters with the highest stability scores, as these represent the most robust and meaningful structures in the data. This quantitative approach addresses the main limitation of traditional DBSCAN by giving a principled method for cluster selection.

Here are the key concepts of HDBSCAN's stability measure:

  1. Cluster Lifespan: Each cluster is born at a specific density threshold (λ_birth) and dies at another threshold (λ_death). The width of each bar represents how long the cluster persists across different density levels.

  2. Stability Calculation: The stability score for each cluster is proportional to both the number of points in the cluster and how long it persists across density thresholds. Clusters that are both large and persistent receive higher stability scores.

  3. Cluster Selection: The algorithm selects clusters with the highest stability scores, as these represent structures that persist across the widest range of density thresholds. In this example, Cluster A would be selected due to its high stability score.