Skip to content

Evaluation, diagnostics, benchmarks, and additional tests · Evaluation, diagnostics, benchmarks, and additional hypothesis tests · EVA004 · Diagnostic

Gap statistic

Selects a candidate cluster count by comparing observed within-cluster dispersion with simulated reference data.

Runs in CodeSplash Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through discover.choose_k. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Selects a candidate cluster count by comparing observed within-cluster dispersion with simulated reference data.

Question / hypotheses

Is clustering more compact than expected under a specified no-structure reference geometry? The usual selection rule is not a universal cluster-existence test.

Architecture / computational structure

A fixed scoring/calibration pipeline applied to model outputs or supplied observations; the formula and usage sections identify its operations.

Assumptions

The reference distribution must be scientifically defensible. Uniform bounding-box reference data, with or without PCA rotation, can be inappropriate for constrained or manifold data.

Formula / computational definition

Gap⁡(k)=B−1∑b=1Blog⁡Wkb∗−log⁡Wk.\operatorname{Gap}(k)=B^{-1}\sum_{b=1}^B\log W_{kb}^{*}-\log W_k.

Parameters and notation

W_k is dispersion with k groups; B is simulation count. Reference geometry, distance power, k range, clustering starts, and the one-standard-error rule are consequential hyperparameters.

Fitting, fine-tuning, and adaptation

There is no model fine-tuning step for this evaluation procedure. Freeze the evaluated systems and evaluator before the final test. Select evaluation settings on development data; if a learned evaluator or calibration model is adapted, use separate fitting data, version it, and reassess its validity.

How to use and calibrate

Fit every candidate k to observations and independently simulated reference datasets. Use the original Tibshirani one-SE rule or explicitly document a different rule; software defaults can differ.

Example application

Assess whether adding a fourth cluster improves standardized measurements more than a uniform reference predicts.

Interpretation / pitfalls

Watch out

A null chosen after viewing the answer makes the comparison circular. Multiple software definitions of W_k and one-SE selection produce different answers.

Python and R code examples

Scope: Mathematical calculation on synthetic inputs; not a complete benchmark pipeline.

Validation: Python — Executed successfully on synthetic inputs. R — reviewed but not executed in this environment. Python dependencies are explicit imports; R uses base R unless a package/bridge is explicitly named.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Mathematical calculation on synthetic inputs; not a complete benchmark pipeline. · needs numpy

import numpy as np
log_W_observed=np.log([100.,48.,42.])
log_W_reference=np.log([[120.,81.,64.],[125.,79.,65.],[119.,80.,66.]])
gap=log_W_reference.mean(axis=0)-log_W_observed
print(gap)  # aggregation only; these are supplied fitted dispersions
R

Complete calculation — Mathematical calculation on synthetic inputs; not a complete benchmark pipeline.

logW <- log(c(100,48,42)); logWref <- log(rbind(c(120,81,64),c(125,79,65),c(119,80,66)))
print(colMeans(logWref)-logW)
# Full estimator: cluster::clusGap(X, FUNcluster=function(x,k) list(cluster=kmeans(x,k,nstart=20)$cluster), K.max=6, B=100)

Sources

From the CodeSplash reference, entry EVA004.