Open-source clustering · Thetan Berserker (TB)

One distance in.
Every cluster out.

Thetan Berserker reads your data twice, corrects its own mistakes, and tells you how many clusters there are. You set a distance, θ. It finds K, with no gradients and no GPU.

Hyperparameters
1 θ
Passes over the data
2
Peak memory
O(n + Kd)

01 · The algorithm

Two passes. One idea:
let the centroids arrive first.

The figure follows one real run of TB, computed in your browser on 150 points drawn from 5 Gaussians, with θ = 3.6. Scroll, and each step of Algorithm 2 plays as a short loop.

Step 01 Alg. 2 · lines 3–5

Pass 1 · Thetan Sequential

Shuffle the data, then visit each point exactly once. A point joins the nearest centroid if it is closer than θ, and that centroid's running mean moves toward it. Otherwise the point opens a new cluster.

for x in shuffle(X):
    μ ← nearest centroid
    if ‖x − μ‖ < θ:  join μ, update its mean
    else:            open a new cluster at x

Fast and online, but order-sensitive: centroids are still moving, so an early point can open a cluster between two true groups.

Step 02 Alg. 2 · lines 6–7

Cleanup · TS on the centroids

Run the very same TS on the centroids alone. Centroids within θ of each other land in one group. The oldest is kept, and the rest are flagged as spurious (ζ₁).

No new machinery: TB reuses TS on the data, on the centroids, and on both together.

Step 03 Alg. 2 · lines 8–10

Delete & reassign

Spurious centroids are deleted. Their points move to the nearest surviving centroid, and centroids are recomputed from the labels (M⋆₂).

This removes duplicates, but it cannot undo early assignments. Hence a second pass.

Step 04 Alg. 2 · lines 11–13

Pass 2 · Prepend, then re-shuffle

The key move. The cleaned centroids are placed in front of a fresh permutation of the data, M⋆₂ ∪ X₂. Every berserker claims its territory before the first data point arrives.

Each point is now compared with near-final centroids instead of whichever points happened to come first.

Step 05 Alg. 2 · lines 14–19

Second cleanup · a fixed point

A last cleanup removes the few spurious centroids that outliers create in pass 2, and the permutation is inverted. TB returns centroids M⋆ and labels λ⋆.

Two passes suffice. Running up to 14 iterations gives no further gain in NMI (paper, Fig. 8B).

02 · The ordering problem

Same data. New order.
Same answer.

Sequential clustering is fast, but shuffle the input and the partition changes. Below, both methods read the paper's simulation (a 10 × 10 grid of unit Gaussians, ℓ = 5, θ = 3.6) in a fresh random order every few seconds. Both are computed live.

TS Thetan Sequential one pass
Clusters found
– / 100
Off-centre centroids
–
Agreement with last order
–
TB Thetan Berserker two passes
Clusters found
– / 100
Off-centre centroids
–
Agreement with last order
–

off-centre: more than 1 unit from every true centre · agreement: adjusted Rand index between consecutive shuffles · shuffle 0

Paper, 30 × 10 grid over 20 orders: two orders agreed with a mean ARI of 0.956 after one pass and 0.990 after TB, removing about 77% of the order-induced disagreement.

centroid θ around it, to scale outlier cluster, fewer than 8 points

03 · One parameter

θ is the only knob.

Theorem 1 gives the rule: choose θ above the widest cluster and below the narrowest gap, and a single pass recovers every cluster with no splits and no merges. In practice, set θ to the largest expected cluster diameter. When unsure, err small.

Clusters inferred
–
True clusters
12
ARI vs truth
–
Runtime
–

Clusters inferred across θ

TB TS true K

Sweeping θ…

Twelve unit Gaussians, computed live in your browser on a single thread.

04 · Theory

Guarantees, not heuristics.

The proofs use uniform samples in d-dimensional balls under the ℓ₂ norm, with extensions to Gaussians in the appendix. Each step of TB rests on one of them.

Lemma 1

Separation is preserved

If every pair of points from different clusters is at least θ apart, TS never merges two clusters, whatever the order.

‖xi − xj‖ ≥ θ ⇒ no cross-cluster merge

Theorem 1

One-to-one recovery

When θ exceeds every cluster diameter and stays below every gap, a single TS pass returns exactly K clusters: no merges, no splits.

maxk Dk < θ ≤ ω

Propositions 1 & 2

Short edges vanish with dimension

For two balls of radius θ/2 whose centres are 3θ/2 apart, the chance that a cross-ball pair lies within θ decays exponentially in d. Order mistakes become rarer as dimension grows.

pθ ≈ 0.217 · e−0.594 d

Lemma 2 · Theorem 2

Self-correction in two passes

A centroid born from a spurious cross-cluster merge lies within θ of a true centre, so the second pass absorbs it.

‖μm − ν1‖ ≤ ½‖y − x‖ + θ/2 < θ

Theorems 3 · 4

Order independence

For balls of radius r whose centres are more than 4r apart, the cleanup and the prepended second pass give the same result for every order.

2r < θ < ℓ − 2ε

Theorems 5 · 6

Explicit cost bounds

Time is linear when there is one cluster and quadratic only when every point is its own cluster. Memory stays linear, with in-place permutations.

O(nd) … O(n²d) time · O(n + Kd) memory

05 · Performance

A million points. 2.7 seconds.

300 Gaussian clusters, 1,000,000 points, a single CPU thread (paper, Fig. 14). The KMeans variants were given the true K = 300. TB inferred it, returning 300.4 clusters on average, with the highest NMI (0.975) and ARI (0.954).

Runtime at n = 1,000,000

seconds, log scale · lower is better

Peak memory at n = 1,000,000

megabytes, log scale · lower is better

Missed centroids

40.3 → 0.8

one pass vs full TB, 300 true clusters, 20 orders

Clusters returned

321.3 → 300.8

for 300 true clusters, without being told K

Runtime growth

1.04 vs 2.02

log-log slope in n: TB is linear, HDBSCAN quadratic

Robust to θ

2.8 – 3.9

θ range where NMI and ARI stay within 2% of their peak

06 · The Thetan family

One primitive, five algorithms.

Thetan Sequential is the core primitive. Everything else is built from it.

TS

Thetan Sequential

One pass, one θ. Online and streaming-friendly. The building block.

TB

Thetan Berserker

Two passes with self-correction. Order-robust, and K is inferred. The primary method.

TSR

TS Randomized

Runs TS over several shuffles (default 10), re-clusters all centroids, and reassigns.

TBK

TB → KMeans

Seeds KMeans with the centroids of TB's K largest clusters.

TBSCAN

TB → DBSCAN

Density-based linking of TB centroids for non-linear shapes. Up to 5,000× faster than DBSCAN on 375k-point spirals.

07 · Quickstart

Three lines to a clustering.

The Thetan kernels are written in Cython and called from Python. Nine distance metrics ship with them, including euclidean, cosine, L1 and arccos.

git clone https://github.com/thetan-ai/thetan.git
cd thetan
pip install -r requirements/build.txt
pip install --no-build-isolation .

Cite

Thetan Berserker Clustering

Eleftherios Garyfallidis, Serge Koudoro, Jong Sung Park, Sai Prajwal Reddy

Indiana University Bloomington · University of Michigan

BibTeX
@article{garyfallidis2025thetan,
  title   = {Thetan Berserker Clustering},
  author  = {Garyfallidis, Eleftherios and Koudoro, Serge
             and Park, Jong Sung and Reddy, Sai Prajwal},
  journal = {IEEE Transactions on Pattern Analysis
             and Machine Intelligence},
  year    = {2025}
}