MinHash › Worked Example

Michael BrenndoerferJanuary 10, 20262 min read
Language AI HandbookAll chapters
Part 20 of 67Data Curation
Chapter 143MinHash › Worked Example
Reading section 5 of 13 · Worked Example · 2 minSection 5 of 13 · 2 min

Book contents

Section 5 of 13 · 2 min read

MinHash › Worked ExampleOne section · Sections in this chapter5 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's walk through a concrete MinHash computation with a small example that you can follow step by step. This will solidify both the mechanics of the algorithm and the intuition behind why it works.

Suppose we have two documents:

  • Document A: "data science is great"
  • Document B: "data science is wonderful"

We extract 2-word shingles:

  • A's shingles: {data science, science is, is great}
  • B's shingles: {data science, science is, is wonderful}

The intersection is {data science, science is}, with 2 elements. The union has 4 distinct shingles: {data science, science is, is great, is wonderful}. So the exact Jaccard similarity is:

J(A,B)=24=0.5J(A, B) = \frac{2}{4} = 0.5

Now suppose we assign integer IDs to the 4 distinct shingles: data science = 0, science is = 1, is great = 2, is wonderful = 3. We use two hash functions:

h1(x)=(3x+1) mod 5h_1(x) = (3x + 1) \bmod 5 h2(x)=(x+2) mod 5h_2(x) = (x + 2) \bmod 5

Computing hash values for each shingle ID:

  • h1(0)=1h_1(0) = 1, h1(1)=4h_1(1) = 4, h1(2)=2h_1(2) = 2, h1(3)=0h_1(3) = 0
  • h2(0)=2h_2(0) = 2, h2(1)=3h_2(1) = 3, h2(2)=4h_2(2) = 4, h2(3)=0h_2(3) = 0

Document A contains shingle IDs {0,1,2}\{0, 1, 2\}, so its MinHash signature is:

sig(A)=[min⁡(h1(0),h1(1),h1(2)),  min⁡(h2(0),h2(1),h2(2))]\text{sig}(A) = \left[\min(h_1(0), h_1(1), h_1(2)),\; \min(h_2(0), h_2(1), h_2(2))\right] =[min⁡(1,4,2),  min⁡(2,3,4)]=[1,2]= \left[\min(1, 4, 2),\; \min(2, 3, 4)\right] = [1, 2]

Document B contains shingle IDs {0,1,3}\{0, 1, 3\}, so its MinHash signature is:

sig(B)=[min⁡(h1(0),h1(1),h1(3)),  min⁡(h2(0),h2(1),h2(3))]\text{sig}(B) = \left[\min(h_1(0), h_1(1), h_1(3)),\; \min(h_2(0), h_2(1), h_2(3))\right] =[min⁡(1,4,0),  min⁡(2,3,0)]=[0,0]= \left[\min(1, 4, 0),\; \min(2, 3, 0)\right] = [0, 0]

The estimated Jaccard similarity is the fraction of positions where the signatures agree:

J^(A,B)=02=0\hat{J}(A, B) = \frac{0}{2} = 0

Both positions disagree, giving an estimate of 0. The true Jaccard is 0.5. This is a bad estimate, but that's expected: with only 2 hash functions, variance is enormous. The standard deviation at J=0.5J = 0.5 with k=2k = 2 is 0.25/2≈0.35\sqrt{0.25 / 2} \approx 0.35, so getting an estimate anywhere from 0 to 1 is quite plausible.

With k=128k = 128 hash functions, the standard deviation would be 0.25/128≈0.044\sqrt{0.25 / 128} \approx 0.044, and the estimate would almost certainly fall close to 0.5. This variance reduction is why practical MinHash implementations use 128 to 256 hash functions.