TL;DR
Two collections can be compared by asking what share of everything they contain is shared. The Jaccard index does this exactly. It divides the number of items the two sets have in common by the number of distinct items across both. It is 1 when the sets are identical, 0 when they have nothing in common, and a fraction in between otherwise.
Where you'll see it: in spotting near-duplicate web pages by their words, in judging how alike two users' liked items are in a recommender system, and in comparing molecules by their fingerprints.
Intuition
Two playlists
Ana's playlist has 40 songs and Ben's has 60. They have 20 songs in common. How similar are their tastes? Count the songs that appear on at least one list: . Of those 80 distinct songs, 20 are shared, so the overlap is one quarter.
That quarter is the Jaccard index. It answers a simple question: of everything these two lists mention between them, what fraction do they both mention?
The denominator matters. Sharing 20 songs is a strong signal if both lists are short, and a weak one if both are huge. Dividing by the combined collection keeps the score in the same units whatever the sizes are.
- J for two sets of size 10
flowchart LR A[Set A] --> I[Shared items: intersection] B[Set B] --> I A --> U[All distinct items: union] B --> U I --> J[Jaccard = shared divided by all distinct] U --> J
What it ignores
The index looks only at membership. It does not care how often an item appears, what order the items came in, or how big the collection is beyond the two sets being compared. Two sets are alike under this measure when most of what they touch is common ground.
The math
Definition
For finite sets and , the Jaccard index is
If both sets are empty, is defined to be by convention.
Here is the number of elements in , as in set operations. The numerator is the set of shared elements. The denominator is every element that appears in at least one set, counted once. The empty-set convention is needed because has no meaning, and two empty sets are certainly identical.
Basic properties
- Range: . The intersection is never bigger than the union, so the fraction never exceeds 1.
- Symmetry: , because both and are unchanged when the sets swap places.
- Identity: exactly when .
- No overlap: exactly when and are disjoint, unless both are empty.
Inclusion-exclusion form
Using the rule from the Set Operations lesson, write . Then
This is the form to use when you know the two sizes and the overlap but not the sets themselves.
Jaccard distance
The complement of similarity is
This is zero only for identical sets, symmetric, and satisfies the triangle inequality. So it is a genuine distance in the sense of a Metric Space, which is why it works with clustering methods and nearest-neighbour search. (The triangle inequality says a detour through a third set is never shorter than the direct distance.)
Binary vectors
Suppose each set is a row of 0s and 1s, where a 1 means the feature is present. Then is the number of positions where both rows have a 1, and is the number of positions where at least one row has a 1. The index is then
This is the Tanimoto coefficient used in chemistry. Cosine similarity also compares binary vectors, but it divides by the vectors' lengths rather than by the union, so the two scores give different numbers for the same pair.
Worked examples
Let and .
- Shared elements: , so .
- Everything in either set: , so .
- Index: .
- Distance: .
Interpretation: 40 percent of the combined elements are shared. The sets are related but far from identical.
Compare the vocabularies of two short documents, ignoring word order and repeats. Document A has the words . Document B has .
- Shared words: , so 3.
- All distinct words across both: , so 7.
- Index: .
- Distance: about .
Interpretation: the documents share a little under half their vocabulary. The Sørensen-Dice score (twice the overlap divided by the sum of the two set sizes) for the same pair is , which is always at least the Jaccard value. Choosing between them is a matter of convention, not correctness.
A streaming service has two users. The first has liked 40 titles, the second 60, and 20 titles appear in both lists.
- Union: distinct titles liked by at least one user.
- Intersection: 20 titles.
- Index: .
Interpretation: one in four titles either user liked is shared. A recommender would treat this as moderate overlap. Comparing every pair of millions of users this way is too slow, which is why systems use MinHash (a method that compresses each set into a short signature using a Hash Function, so that the share of matching signature entries estimates the index) to estimate it.
A molecule is encoded as a long string of bits, each marking whether a small chemical pattern is present. Molecule X has 12 bits set, molecule Y has 10, and 6 of the set bits coincide at the same positions.
- Both set: 6 positions, so .
- At least one set: positions.
- Index: .
Interpretation: the molecules share just over a third of their features. The strings are often over a thousand bits long. Most are zeros, and the index ignores positions where both molecules have a zero. Counting shared absences would make almost every pair look similar.
Tradeoffs and failure modes
The index is simple and fair for sparse data, but it can mislead when its assumptions do not hold.
| Situation | What goes wrong | Better choice |
|---|---|---|
| One set is inside a much larger one | inside of 100 items gives , though is wholly contained | Overlap coefficient: shared count divided by the size of the smaller set, which equals 1 here |
| Counts matter, not just presence | Repeated words or ratings are flattened into a single membership | Weighted Jaccard, which sums the smaller counts over the sum of the larger ones |
| Both sets are empty | has no value | Use the convention , and handle empty inputs separately |
| Very large collections | Exact union and intersection cost grows with the size of the data | Estimate the index with MinHash signatures |
| Shared absences should not count | Vectors full of zeros inflate similarity if zeros are counted | Use the binary form, counting only positions where at least one set has a 1 |
When the question is "does one set contain the other?", use the overlap coefficient rather than the Jaccard index.
History
In 1901 the Swiss botanist Paul Jaccard published a comparison of the plant species found at different sites in the Alps. He needed a number for how alike two lists of species were, and the shared-over-combined fraction was the natural answer. It spread through ecology under his name.
In the late 1950s T. T. Tanimoto, working at IBM, used a closely related coefficient to compare sets of features, and in chemistry the same formula on bit fingerprints took his name. In the late 1990s Andrei Broder used Jaccard similarity to find near-duplicate web pages, and the MinHash technique he worked with made it possible to estimate the index quickly for very large collections. Those uses explain why the same number appears in ecology, cheminformatics and search engines.
Recap
The Jaccard index divides the size of the overlap of two sets by the size of their union. It lies between 0 and 1, equals 1 for identical sets, and its complement is a distance. Written with the inclusion-exclusion formula, it needs only the two sizes and the overlap. For binary vectors it becomes the Tanimoto coefficient, and it leads to Cosine Similarity for vectors, to the Metric Space view of distances, and to MinHash for large-scale comparisons.
Check yourself
QuestionRecall: what do the numerator and the denominator of the Jaccard index count, and what value does the index take for identical sets?Show answer
The numerator counts the elements the two sets share, the intersection. The denominator counts all distinct elements in either set, the union. For identical sets the intersection and the union are the same, so the index is 1.
QuestionComputation: and . What is ?Show answer
The shared elements are , and , so . The union is , so . Therefore .
QuestionWhy: why is the denominator the union rather than the size of one set, such as ?Show answer
Using would make the index depend on which set you name first, since generally differs from . Using the union keeps the measure symmetric, keeps it between 0 and 1, and gives identical sets a score of 1 whatever their size.
QuestionApplied: one item has a list of 10 features, all of which also appear in another item's list of 1,000 features, yet the Jaccard index is only 0.01. Why, and which measure would you use to ask 'is one contained in the other?'Show answer
The intersection is 10 and the union is 1,000, so the index is 10/1,000 = 0.01. The union includes the 990 extra features of the larger list, so containment is diluted. The overlap coefficient, the intersection divided by the size of the smaller set, equals 10/10 = 1 in this case and answers the containment question directly.
Sources
- Jaccard index (Wikipedia): https://en.wikipedia.org/wiki/Jaccard_index
- Set (mathematics) (Wikipedia): https://en.wikipedia.org/wiki/Set_(mathematics)
- MinHash (Wikipedia): https://en.wikipedia.org/wiki/MinHash