Skip to content

Add Ward linkage to hierarchical clustering - #336

Merged
josevalim merged 1 commit into
elixir-nx:mainfrom
RicardoSantos-99:fix-ward-linkage
Jul 11, 2026
Merged

Add Ward linkage to hierarchical clustering#336
josevalim merged 1 commit into
elixir-nx:mainfrom
RicardoSantos-99:fix-ward-linkage

Conversation

@RicardoSantos-99

Copy link
Copy Markdown
Contributor

Adds :ward linkage support to Scholar.Cluster.Hierarchical.

The ward/6 update computes the new distances on squared distances, and
merge_clades now tracks the per-cluster sizes so the update has the size of
each remaining cluster (which Ward needs, unlike the other linkages).

Enables :ward in fit/2, adds it to the docs, and includes a ward test
against the SciPy reference values.

Related to #213

Ward linkage was disabled in the parallel nearest-neighbor dendrogram
because it produced incorrect dissimilarities. Two things were missing:

  * Ward's Lance-Williams recurrence needs the size of the third cluster
    (the one whose distance is being updated), which differs per column of
    the pairwise matrix. The other linkages only need the sizes of the two
    clusters being merged, so those sizes were the only ones threaded
    through merge_clades. We now also track the per-cluster sizes and pass
    them to the update function.

  * The recurrence runs on squared distances, so the update squares the
    pairwise distances before combining them and takes the square root of
    the result.

With both in place, the dissimilarities match SciPy's
scipy.cluster.hierarchy.linkage(method: "ward"). Re-enables the linkage
in fit/2, documents it, and restores the ward test.
@RicardoSantos-99

Copy link
Copy Markdown
Contributor Author
image image

@josevalim
josevalim merged commit 1742ef3 into elixir-nx:main Jul 11, 2026
1 of 2 checks passed
@josevalim

Copy link
Copy Markdown
Contributor

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants