The Jaccard similarity of two finite sets and is . A MinHash signature of a set is the vector of minima over independent random hash functions . For each , the two sets’ minima are equal with probability exactly , so the fraction of equal coordinates in two signatures is an unbiased estimate of with variance . Locality-sensitive hashing (LSH) with banding groups the coordinates into bands of and reports a pair as a candidate when all coordinates of some band agree, which happens with probability .
MinHash replaces each set, of any size, with numbers from which the similarity of any two sets can be estimated. Banding then finds the similar pairs without comparing all pairs. Each band’s values are hashed into a table, and only sets that share a bucket in some band are compared. The probability of sharing a bucket is a steep S-shaped function of , so pairs well above a threshold near are almost always found and pairs well below it are almost never compared.
Broder (1997) introduced MinHash to estimate the resemblance of web documents represented as sets of shingles, the overlapping runs of consecutive words. Indyk and Motwani (1998) defined locality-sensitive hash families for approximate nearest-neighbor search, and MinHash is such a family for Jaccard similarity. The same pipeline removes near-duplicate documents from the training corpora of language models.
The data
The series’ other notes use Wikimedia pageview records. Near-duplicate detection needs document text, and the Wikipedia article dumps were not reachable from the machine that ran the experiments, so the experiments use the minicrawl dataset that ClickHouse publishes. It holds the front page of each domain in a Tranco ranking of popular sites, fetched with curl, in one Parquet file dated 23 September 2023 with 7,613,078 rows. Front pages of popular sites contain many real near-duplicates, such as localized versions of one site, default server pages and the error pages of content delivery networks.
The experiments read the first 40 of the file’s 1,081 row groups, 156,432 pages from ranks 1 to 156,448. Each page’s HTML was reduced to its visible text (script, style, head and SVG elements removed), and pages with fewer than 50 words were dropped, leaving 91,548. A page’s set is its distinct word 5-shingles, each hashed to 64 bits. Pages with fewer than 50 distinct shingles were dropped and only the first page of each domain kept, leaving 91,116 pages with 100,765,252 shingles in total, a median of 732 per page, and 4,151,017,170 pairs.
An exact similarity join finds every pair with . It uses prefix filtering (Bayardo, Ma and Srikant 2007). With the tokens of every set ordered by one global order, rarest first, the prefixes of length of two sets with share at least one token, so an inverted index over the prefixes generates every qualifying pair, and each candidate is verified by merging the two full sets.
| Pairs | |
|---|---|
| 0.3 | 1,153,538 |
| 0.5 | 1,140,543 |
| 0.7 | 1,106,443 |
| 0.8 | 1,071,859 |
| 0.9 | 149,497 |
| 1.0 | 50,694 |
The distribution is bimodal. In 20,000 random pairs the mean similarity was 0.00041 and 99.13% had no shingle in common, while 93% of the pairs with have . Grouping pages connected by pairs with gives 16,222 clusters that hold 38,277 pages. The largest has 1,333 pages, nearly all the block page a security service returns to a blocked client (“Sorry, you have been blocked”), followed by 420 localized Google home pages, 260 CloudFront 403 pages and 152 Apache test pages. Because the number of pairs grows with the square of a cluster’s size, the largest cluster alone accounts for 887,397 (82.8%) of the pairs with and the four largest for 94.9%. Recall counted over pairs, as in the tables below, therefore mostly measures these clusters.
MinHash
Let be a random hash function that is injective on , and let be the element of with the smallest hash value. Each element of is equally likely to be . The minima of over and over are equal exactly when , so
With independent hash functions the number of equal coordinates is binomial with parameters and , and has mean and standard deviation . The error does not depend on the sizes of the sets. At it is at most , at .
The argument needs the minimum to be uniform over the elements of , which a fully random function provides. Broder, Charikar, Frieze and Mitzenmacher (2000) called families with this property min-wise independent; exact min-wise independence requires exponentially many permutations, and -approximate versions and -wise independent hashing give the property up to a small bias (see Hashing for sketches for the families). The experiments rehash each 64-bit shingle hash with , where is the splitmix64 finalizer and a random 64-bit seed.
The pairs found by the join overlap heavily (most lie in a few clusters), so their errors are correlated. To measure the estimator on independent pairs, the error was computed on a random maximal matching of the join’s pairs, 21,138 pairs with no page in two of them, together with 19,993 random pairs.
| Pairs | RMSE, | Theory | RMSE, | Theory | RMSE, | Theory | |
|---|---|---|---|---|---|---|---|
| 0 to 0.1 | 19,993 | 0.0012 | 0.0011 | 0.0009 | 0.0006 | 0.0004 | 0.0003 |
| 0.3 to 0.5 | 1,133 | 0.1227 | 0.1209 | 0.0621 | 0.0605 | 0.0299 | 0.0302 |
| 0.5 to 0.7 | 1,209 | 0.1234 | 0.1216 | 0.0610 | 0.0608 | 0.0296 | 0.0304 |
| 0.7 to 0.9 | 2,927 | 0.0938 | 0.0957 | 0.0504 | 0.0479 | 0.0234 | 0.0239 |
| 0.9 to 1 | 15,869 | 0.0228 | 0.0222 | 0.0111 | 0.0111 | 0.0056 | 0.0056 |
The theory column is averaged over each bin’s pairs. The mean error was below 0.005 in every bin. Computing all 256 signatures took 19.8 seconds for the 100.8 million shingles on one core, 0.77 nanoseconds per shingle and hash function.
b-bit MinHash
A signature of 64-bit minima occupies bytes. Li and König (2010) showed that keeping only the lowest bits of each minimum loses less information per bit than keeping all of them when similarities of interest are high. Two -bit values agree when the full minima agree, or by chance with probability close to when they differ. For sets that are small relative to the hash range,
For and near 0 the variance is , so one-bit values estimate the similarity of unrelated pages with standard deviation . For near 1 the full minima rarely differ, so chance agreements contribute little, and the variance approaches , at most twice the variance with full minima. At a fixed budget of bits, a few wide values estimate low similarities better and many narrow values estimate high similarities better. The table gives the root mean squared errors at 256 bits per page, on the join’s pairs with and on the random pairs.
| Join pairs, | Random pairs | ||
|---|---|---|---|
| 1 | 256 | 0.033 | 0.062 |
| 2 | 128 | 0.037 | 0.051 |
| 4 | 64 | 0.053 | 0.032 |
| 8 | 32 | 0.059 | 0.011 |
The full 64-bit signatures with , at 16,384 bits per page, give 0.021 and 0.0004. For near-duplicate detection, where the decision concerns pairs with above 0.5, one-bit values give errors 1.6 times those of 64-bit values at 1/64 of the space.
Banding
Comparing all signatures still takes time proportional to the number of pairs, 4.15 billion here. Banding, in the form described by Leskovec, Rajaraman and Ullman (chapter 3), splits the coordinates into bands of consecutive coordinates and, for each band, hashes the values of every page into a table. Two pages become a candidate pair if they share a bucket in at least one band. Their values in one band all agree with probability , and the bands use disjoint coordinates, so
As a function of this is an S-curve. Its steepest point lies near , the similarity at which the expected number of agreeing bands is 1. Larger makes the curve steeper and moves it right; larger moves it left. With 32 bands of 8 rows (), a pair with is missed with probability and a pair with becomes a candidate with probability .
The table gives each setting’s results on all 4,151,017,170 pairs, against the exact join. Candidates are distinct pairs; recall is the fraction of the 1,071,859 pairs with that became candidates.
| Candidates | Recall, | Candidates with | ||
|---|---|---|---|---|
| 64 × 4 | 0.354 | 1,156,924 | 1.0000 | 5,852 |
| 50 × 5 | 0.457 | 1,144,575 | 1.0000 | 510 |
| 32 × 8 | 0.648 | 1,120,965 | 0.9999 | 10 |
| 25 × 10 | 0.725 | 1,092,490 | 0.9914 | 0 |
| 20 × 10 | 0.741 | 1,054,173 | 0.9665 | 0 |
| 16 × 16 | 0.841 | 701,975 | 0.6506 | 0 |
| 8 × 32 | 0.937 | 154,413 | 0.1441 | 0 |
With 32 bands of 8, the candidates are 0.027% of all pairs and contain all but 68 of the pairs with . Within each similarity bin the measured fraction of pairs that became candidates matched averaged over the bin. The low counts of dissimilar candidates follow from the bimodal distribution. Of the random pairs, 99.13% have , which can never become candidates, and a pair with becomes a candidate with 50 bands of 5 with probability . The last two rows place the threshold above 0.8, and recall falls accordingly.
A candidate is a pair to verify, by comparing signatures or the full sets. Banding sets the recall; the verification step sets the precision.
The figure
Share of pairs that become candidates, by similarity
With 32 bands of 8 rows, using 256 of the 256 hash functions, 813 of the 2,000 pairs are candidates: 400 of 400 with similarity 0.8 or more and 1 of 600 below 0.3. The MinHash estimate from the same 256 functions is off by 0.025 on average (root mean square).
On the full crawl of 91,116 pages, these settings made 1,120,965 candidate pairs out of 4,151,017,170, 0.027% of all pairs, and found 99.99% of the 1,071,859 pairs with similarity 0.8 or more.
MinHash estimate from the 256 hash functions in use
The figure’s 2,000 pairs were chosen so that every similarity bin of width 0.05 is represented, which the crawl’s pairs do not provide. There are 100 pairs per bin, drawn from the join’s pairs, random pairs and pairs that share one of their first 64 MinHash values, with no page in two pairs. Their 256 MinHash agreements were recorded once from the same signatures as above, so moving a slider regroups recorded agreements and recomputes the candidates exactly as banding would. The upper chart compares each bin’s measured candidate fraction with the S-curve; the lower chart shows the MinHash estimate from the hash functions in use.
In practice
Lee et al. (2022) removed near-duplicate documents from language-model training corpora with MinHash over word 5-grams, a signature of 9,000 values and 450 bands of 20, whose S-curve has ; candidate pairs were kept as duplicates when their edit similarity exceeded 0.8. FineWeb (Penedo et al. 2024) uses 112 hash functions in 14 bands of 8 on word 5-grams, targeting documents at least 75% similar. By the formula above, that setting makes a pair with a candidate with probability 0.77 and a pair with with probability 0.988. Documents that share a bucket are merged into clusters transitively.
Computing hash values per shingle dominates the cost of signatures. One permutation hashing (Li, Owen and Zhang 2012) hashes each element once, divides the hash range into bins and keeps the minimum in each bin, with densification schemes filling empty bins for small sets. SuperMinHash (Ertl 2017) correlates the minima to reduce the estimator’s variance below for small sets.
Consequences
For near-duplicate detection on sets of shingles, MinHash with a few hundred hash functions estimates similarity within a few hundredths, at a cost independent of the size of the documents, and the measured error matched on independent pairs. Banding then reduces the 4.15 billion pairs to about a million candidates with recall above 0.99 for , when is placed about 0.1 below the similarity that matters. A steep S-curve requires both and to be large, so the number of hash functions is set by banding and can exceed what estimation alone requires, as with the 9,000 values of Lee et al.
On this crawl an exact join with prefix filtering finished in 108 seconds on one core at and 114 seconds at , because almost all pairs share no shingle and the prefixes are selective. Prefix filtering’s candidate set grows as the threshold falls and when sets share frequent tokens, while banding’s work is proportional to times the number of documents plus the number of candidates. The training-corpus pipelines above, over billions of documents, use banding.
How this was measured
The harness sources and outputs are in the
minhash-experimentsdirectory, andrun.shreproduces every number from the minicrawl Parquet file.
- Pages.
fetch_crawl.pyreads row groups 0 to 39 of the Parquet file over HTTP range requests.to_text.pyparses each page with lxml, removes script, style, noscript, template, svg and head elements, collapses whitespace, and keeps pages with at least 50 words.- Shingles.
shingle.pysplits the lowercased text into runs of word characters, hashes each window of 5 words with the first 8 bytes of BLAKE2b, drops pages with fewer than 50 distinct shingles, and keeps the first page of each domain.- Exact join.
join.cimplements prefix filtering with tokens ordered rarest first and verifies every candidate by merging the two sorted sets; at it verified 1,206,146 candidates for 1,153,538 pairs. Times are wall-clock times of single runs on one core.- Signatures and banding.
minhash.ccomputes 256 minima per page.analyze7.pycomputes the b-bit errors, the banding candidates for each setting (distinct candidate pairs are listed exactly where the band collisions number at most , which excludes 128 × 2), the clusters and the per-bin candidate fractions, written toanalysis.json.indep7.pymeasures the MinHash error on the random matching, written toindep.json.- The figure.
make_fig7.pyselects the 2,000 pairs and stores each as its similarity and 256 agreement bits, 52.5 KB compressed. The figure’s tests compare its candidates, agreement counts and bins for 8 settings pair by pair against an independent NumPy implementation (figure_ref.py), and check the binned candidate fractions against the S-curve and the estimates’ error against .- Environment. gcc 13.3 with
-O2 -march=native, Python 3.13 with NumPy 2.5, PyArrow 26 and lxml 6.1, on a 4-core Intel Xeon at 2.10 GHz with 15 GB of memory.
Potentially interesting ideas
- Recall over clusters. Recall counted over pairs is dominated by the largest clusters. Counting the fraction of pages assigned to the correct cluster after transitive merging would weight each page once.
- Lower thresholds. Near-duplicates of edited articles sit between 0.3 and 0.7, where this crawl has few pairs. A corpus with more partial overlap, such as revisions of Wikipedia articles, would test banding where the S-curve’s slope matters most.
- One permutation hashing. Measuring densified one permutation hashing against independent minima on the same pages would show the variance and speed difference at .
- Weighted sets. Shingle counts and term weights call for weighted MinHash schemes, such as consistent weighted sampling, which estimate the weighted Jaccard similarity.
Related notes
- Probabilistic data structures, the series index, with the notation
- Hashing for sketches, for the independence that the MinHash argument assumes and the families that provide it
- Sampling from streams, whose bottom- sketches keep the smallest ranks under one hash function, the single-hash form of a MinHash signature
- Expectation and concentration, for the binomial and Chernoff bounds behind the estimator’s error