Live · a cluster validity test that runs in the browser.
This began as a user segmentation dashboard. It clustered a thousand ad-audience profiles into four groups, named them Power Users, Premium Engagers, Digital Natives and Casual Browsers, and reported on their behaviour. The groups do not exist.
The names were assigned by cluster index. k-means numbers its clusters
arbitrarily, so 0: "Digital Natives" attached a behavioural claim to
whichever cluster happened to come out first. Nothing checked the names
against what the clusters did.
Checked, the four segments are indistinguishable:
| Segment | hrs/day | CTR | conversion | likes |
|---|---|---|---|---|
| Power Users | 2.81 | 0.129 | 0.048 | 4,949 |
| Premium Engagers | 2.73 | 0.131 | 0.053 | 4,829 |
| Digital Natives | 2.75 | 0.126 | 0.050 | 4,986 |
| Casual Browsers | 2.75 | 0.122 | 0.049 | 5,071 |
The Power Users spend three minutes a day longer online than the Casual Browsers. The Premium Engagers have the fewest likes.
The elbow curve was computed and ignored. The training script evaluates
inertia for k from 1 to 10 and then sets optimal_k = 4 without consulting
it. Inertia falls smoothly the whole way; there is no elbow.
The data cannot support segments. Every categorical field is close to uniform, the largest correlation between any two behavioural columns is 0.08, and click-through rate does not differ by age, gender, income or education (all p > 0.38). The columns were generated independently, so there is no joint structure to find.
Any single validity measure rests on an assumption. These four rest on different ones, so agreement between them means considerably more than any of them alone, and disagreement says which assumption is carrying the result.
| Test | What it asks | Ad audience | Control |
|---|---|---|---|
| Permutation | Does it beat data with the relationships shuffled out? | p = 0.19 | p = 0.032 |
| Gap statistic | Are the clusters tighter than uniform spread gives? | selects k = 2, the degenerate minimum | selects k = 3 |
| Bootstrap stability | Does the same partition come back after resampling? | ARI 0.19, not reproducible | ARI 1.00 at k = 3 |
| Three algorithms | Do k-means, a Gaussian mixture and Ward linkage agree? | peak at k = 6, 2 and 7 | all peak at k = 3 |
All four agree in both directions. For the ad audience they agree there is nothing; for the control, constructed to contain three groups, they agree on three.
The algorithms disagreeing with each other is itself evidence: k-means assumes round equal-sized groups, a Gaussian mixture allows different widths per axis, and Ward builds by merging. When only one of them finds something, that something is usually its assumption.
k-means always returns clusters. Given uniform noise it returns tidy ones. The question is whether they are more than a partition of a featureless cloud, and the way to answer it is to compare against a null that has no structure by construction.
Each column is shuffled independently. Every column keeps its own distribution exactly; only the relationships between columns are destroyed, which is precisely what clustering claims to find. If the real data scores no better than that, there is nothing there.
Searching over seven values of k is seven chances to clear a threshold, so the comparison uses a max-statistic permutation test: each shuffled copy is scored at every k and only its best is kept, building the null distribution of "the best silhouette obtainable by searching over k". No correction is needed afterwards, and none would be possible, since with R shuffles the smallest attainable p is 1/(R+1).
The control exists because a test that only ever says "no" is indistinguishable from a broken test. Both run in the page, on the same code.
The whole assessment is arithmetic, so it runs client side in a Web Worker: 30 shuffles at each of 7 values of k, around 6 seconds, with nothing leaving the browser. You can upload your own CSV and it is never transmitted anywhere.
analysis/validity.py and analysis/methods.py are the reference
implementations; web/src/cluster.js and web/src/methods.js are the ports.
They share one small deterministic generator, so they draw the same random
numbers and agree exactly rather than approximately. scripts/verify_cluster.mjs
and scripts/verify_methods.mjs compare 104 numbers between them; the worst
relative difference is 2e-14.
That discipline caught a real difference: the Python gap statistic filled its uniform reference column by column and the JavaScript filled it row by row, consuming the same random stream in different orders. Statistically both were fine and the two would have disagreed forever.
pip install -r requirements.txt python scripts/bake_datasets.py # prepare the bundled datasets node scripts/verify_cluster.mjs # JS against the Python reference node scripts/verify_methods.mjs # gap, stability and the three algorithms cd web && npm install && npm run dev
See ARCHITECTURE.md for the method and the decisions behind it.