# Jaccard index

Intersection and union of two sets A and B
Intersection over Union as a similarity measure for object detection on images - an important task in computer vision.

The Jaccard index, also known as Intersection over Union and the Jaccard similarity coefficient (originally given the French name coefficient de communauté by Paul Jaccard), is a statistic used for gauging the similarity and diversity of sample sets. The Jaccard coefficient measures similarity between finite sample sets, and is defined as the size of the intersection divided by the size of the union of the sample sets:

${\displaystyle J(A,B)={{|A\cap B|} \over {|A\cup B|}}={{|A\cap B|} \over {|A|+|B|-|A\cap B|}}.}$

(If A and B are both empty, we define J(A,B) = 1.)

${\displaystyle 0\leq J(A,B)\leq 1.}$

The Jaccard distance, which measures dissimilarity between sample sets, is complementary to the Jaccard coefficient and is obtained by subtracting the Jaccard coefficient from 1, or, equivalently, by dividing the difference of the sizes of the union and the intersection of two sets by the size of the union:

${\displaystyle d_{J}(A,B)=1-J(A,B)={{|A\cup B|-|A\cap B|} \over |A\cup B|}.}$

An alternate interpretation of the Jaccard distance is as the ratio of the size of the symmetric difference ${\displaystyle A\triangle B=(A\cup B)-(A\cap B)}$ to the union.

This distance is a metric on the collection of all finite sets.[1][2][3]

There is also a version of the Jaccard distance for measures, including probability measures. If ${\displaystyle \mu }$ is a measure on a measurable space ${\displaystyle X}$, then we define the Jaccard coefficient by ${\displaystyle J_{\mu }(A,B)={{\mu (A\cap B)} \over {\mu (A\cup B)}}}$, and the Jaccard distance by ${\displaystyle d_{\mu }(A,B)=1-J_{\mu }(A,B)={{\mu (A\triangle B)} \over {\mu (A\cup B)}}}$. Care must be taken if ${\displaystyle \mu (A\cup B)=0}$ or ${\displaystyle \infty }$, since these formulas are not well defined in these cases.

The MinHash min-wise independent permutations locality sensitive hashing scheme may be used to efficiently compute an accurate estimate of the Jaccard similarity coefficient of pairs of sets, where each set is represented by a constant-sized signature derived from the minimum values of a hash function.

## Similarity of asymmetric binary attributes

Given two objects, A and B, each with n binary attributes, the Jaccard coefficient is a useful measure of the overlap that A and B share with their attributes. Each attribute of A and B can either be 0 or 1. The total number of each combination of attributes for both A and B are specified as follows:

${\displaystyle M_{11}}$ represents the total number of attributes where A and B both have a value of 1.
${\displaystyle M_{01}}$ represents the total number of attributes where the attribute of A is 0 and the attribute of B is 1.
${\displaystyle M_{10}}$ represents the total number of attributes where the attribute of A is 1 and the attribute of B is 0.
${\displaystyle M_{00}}$ represents the total number of attributes where A and B both have a value of 0.
A 0 ${\displaystyle M_{00}}$ ${\displaystyle M_{10}}$ ${\displaystyle M_{01}}$ ${\displaystyle M_{11}}$

Each attribute must fall into one of these four categories, meaning that

${\displaystyle M_{11}+M_{01}+M_{10}+M_{00}=n.}$

The Jaccard similarity coefficient, J, is given as

${\displaystyle J={M_{11} \over M_{01}+M_{10}+M_{11}}.}$

The Jaccard distance, dJ, is given as

${\displaystyle d_{J}={M_{01}+M_{10} \over M_{01}+M_{10}+M_{11}}=1-J.}$

### Difference with the simple matching coefficient (SMC)

When used for binary attributes, the Jaccard index is very similar to the simple matching coefficient. The main difference is that the SMC has the term ${\displaystyle M_{00}}$ in its numerator and denominator, whereas the Jaccard index does not. Thus, the SMC counts both mutual presences (when an attribute is present in both sets) and mutual absence (when an attribute is absent in both sets) as matches and compares it to the total number of attributes in the universe, whereas the Jaccard index only counts mutual presence as matches and compares it to the number of attributes that have been chosen by at least one of the two sets.

In market basket analysis, for example, the basket of two consumers who we wish to compare might only contain a small fraction of all the available products in the store, so the SMC will usually return very high values of similarities even when the baskets bear very little resemblance, thus making the Jaccard index a more appropriate measure of similarity in that context. For example, consider a supermarket with 1000 products and two customers. The basket of the first customer contains salt and pepper and the basket of the second contains salt and sugar. In this scenario, the similarity between the two baskets as measured by the Jaccard index would be 1/3, but the similarity becomes 0.998 using the SMC.

In other contexts, where 0 and 1 carry equivalent information (symmetry), the SMC is a better measure of similarity. For example, vectors of demographic variables stored in dummy variables, such as gender, would be better compared with the SMC than with the Jaccard index since the impact of gender on similarity should be equal, independently of whether male is defined as a 0 and female as a 1 or the other way around. However, when we have symmetric dummy variables, one could replicate the behaviour of the SMC by splitting the dummies into two binary attributes (in this case, male and female), thus transforming them into asymmetric attributes, allowing the use of the Jaccard index without introducing any bias. By using this trick, the Jaccard index can be considered as making the SMC a fully redundant metric. The SMC remains, however, more computationally efficient in the case of symmetric dummy variables since it does not require adding extra dimensions.

## Weighted Jaccard similarity and distance

If ${\displaystyle \mathbf {x} =(x_{1},x_{2},\ldots ,x_{n})}$ and ${\displaystyle \mathbf {y} =(y_{1},y_{2},\ldots ,y_{n})}$ are two vectors with all real ${\displaystyle x_{i},y_{i}\geq 0}$, then their Jaccard similarity coefficient (also known then as Ruzicka similarity) is defined as

${\displaystyle J_{\mathcal {W}}(\mathbf {x} ,\mathbf {y} )={\frac {\sum _{i}\min(x_{i},y_{i})}{\sum _{i}\max(x_{i},y_{i})}},}$

and Jaccard distance (also known then as Soergel distance)

${\displaystyle d_{J{\mathcal {W}}}(\mathbf {x} ,\mathbf {y} )=1-J_{\mathcal {W}}(\mathbf {x} ,\mathbf {y} ).}$

With even more generality, if ${\displaystyle f}$ and ${\displaystyle g}$ are two non-negative measurable functions on a measurable space ${\displaystyle X}$ with measure ${\displaystyle \mu }$, then we can define

${\displaystyle J_{\mathcal {W}}(f,g)={\frac {\int \min(f,g)d\mu }{\int \max(f,g)d\mu }},}$

where ${\displaystyle \max }$ and ${\displaystyle \min }$ are pointwise operators. Then Jaccard distance is

${\displaystyle d_{J{\mathcal {W}}}(f,g)=1-J_{\mathcal {W}}(f,g).}$

Then, for example, for two measurable sets ${\displaystyle A,B\subseteq X}$, we have ${\displaystyle J_{\mu }(A,B)=J(\chi _{A},\chi _{B}),}$ where ${\displaystyle \chi _{A}}$ and ${\displaystyle \chi _{B}}$ are the characteristic functions of the corresponding set.

## Probability Jaccard similarity and distance

The weighted Jaccard similarity described above generalizes the Jaccard Index to positive vectors, where a set corresponds to a binary vector given by the indicator function, i.e. ${\displaystyle x_{i}\in \{0,1\}}$. However, it does not generalize the Jaccard Index to probability distributions, where a set corresponds to a uniform probability distribution, i.e. ${\displaystyle x_{i}={\frac {1}{|X|}}}$ if ${\displaystyle i\in X}$ and 0 otherwise. It is always less if the sets differ in size. If ${\displaystyle |X|>|Y|}$, and ${\displaystyle x_{i}=\mathbf {1} _{X}(i)/|X|,y_{i}=\mathbf {1} _{Y}(i)/|Y|}$ then ${\displaystyle J_{\mathcal {W}}(x,y)={\frac {|X\cap Y|}{|X\setminus Y|+|X|}}.

Instead, a generalization that is continuous between probability distributions and their corresponding support sets is

${\displaystyle J_{\mathcal {P}}(x,y)=\sum _{x_{i}\neq 0,y_{i}\neq 0}{\frac {1}{\sum _{j}\max \left({\frac {x_{j}}{x_{i}}},{\frac {y_{j}}{y_{i}}}\right)}}}$

which is called the "Probability" Jaccard[4]. It has the following bounds against the Weighted Jaccard on probability vectors.

${\displaystyle J_{\mathcal {W}}(x,y)\leq J_{\mathcal {P}}(x,y)\leq {\frac {2J_{\mathcal {W}}(x,y)}{1+J_{\mathcal {W}}(x,y)}}}$

The corresponding distance, ${\displaystyle 1-J_{\mathcal {P}}(x,y)}$, is a metric over probability distributions, and a pseudo-metric over non-negative vectors.

Here is another interesting connection of probability Jaccard index with Coupling (probability). Given two distributions ${\displaystyle x,y}$ one can construct random variables ${\displaystyle X,Y}$ on the same probability space with marginal distributions given by ${\displaystyle x,y}$ respectively. The maximal collision probability of ${\displaystyle x,y}$ achievable, ${\displaystyle \Pr \left[X=Y\right]}$, is given by ${\displaystyle 1-{\text{TV}}(x,y)}$ where ${\displaystyle {\text{TV}}}$ is the Total Variation distance. More generally, on a single probability space one could construct an infinity number of random variables one for each distribution ${\displaystyle x}$, and seek to maximize ${\displaystyle \Pr \left[X=Y\right]}$ for all pair ${\displaystyle x,y}$. Of course there are many notions of vector maxima. One common (weak) notion is Pareto optimality. The probability Jaccard similarities furnish a Pareto optimum for the pairwise collision probabilities in the following sense:

For any sampling method ${\displaystyle G}$ and discrete distributions ${\displaystyle x,y}$, if ${\displaystyle \Pr \left[G(x)=G(y)\right]>J_{\mathcal {P}}(x,y)}$ then for some ${\displaystyle z}$ where ${\displaystyle J_{\mathcal {P}}(x,z)>J_{\mathcal {P}}(x,y)}$ and ${\displaystyle J_{\mathcal {P}}(y,z)>J_{\mathcal {P}}(x,y)}$, either ${\displaystyle \Pr \left[G(x)=G(z)\right] or ${\displaystyle \Pr \left[G(y)=G(z)\right]. That is, no sampling method can achieve more collisions than ${\displaystyle J_{\mathcal {P}}}$ on one pair without achieving fewer collisions than ${\displaystyle J_{\mathcal {P}}}$ on another pair, where the reduced pair is more similar under ${\displaystyle J_{\mathcal {P}}}$ than the increased pair. This theorem is true for the Jaccard Index of sets (if interpreted as uniform distributions) and the probability Jaccard, but not of the weighted Jaccard.

## Tanimoto similarity and distance

Various forms of functions described as Tanimoto similarity and Tanimoto distance occur in the literature and on the Internet. Most of these are synonyms for Jaccard similarity and Jaccard distance, but some are mathematically different. Many sources[5] cite an IBM Technical Report[6] as the seminal reference. The report is available from several libraries.

In "A Computer Program for Classifying Plants", published in October 1960,[7] a method of classification based on a similarity ratio, and a derived distance function, is given. It seems that this is the most authoritative source for the meaning of the terms "Tanimoto similarity" and "Tanimoto Distance". The similarity ratio is equivalent to Jaccard similarity, but the distance function is not the same as Jaccard distance.

### Tanimoto's definitions of similarity and distance

In that paper, a "similarity ratio" is given over bitmaps, where each bit of a fixed-size array represents the presence or absence of a characteristic in the plant being modelled. The definition of the ratio is the number of common bits, divided by the number of bits set (i.e. nonzero) in either sample.

Presented in mathematical terms, if samples X and Y are bitmaps, ${\displaystyle X_{i}}$ is the ith bit of X, and ${\displaystyle \land ,\lor }$ are bitwise and, or operators respectively, then the similarity ratio ${\displaystyle T_{s}}$ is

${\displaystyle T_{s}(X,Y)={\frac {\sum _{i}(X_{i}\land Y_{i})}{\sum _{i}(X_{i}\lor Y_{i})}}}$

If each sample is modelled instead as a set of attributes, this value is equal to the Jaccard coefficient of the two sets. Jaccard is not cited in the paper, and it seems likely that the authors were not aware of it.

Tanimoto goes on to define a "distance coefficient" based on this ratio, defined for bitmaps with non-zero similarity:

${\displaystyle T_{d}(X,Y)=-\log _{2}(T_{s}(X,Y))}$

This coefficient is, deliberately, not a distance metric. It is chosen to allow the possibility of two specimens, which are quite different from each other, to both be similar to a third. It is easy to construct an example which disproves the property of triangle inequality.

### Other definitions of Tanimoto distance

Tanimoto distance is often referred to, erroneously, as a synonym for Jaccard distance ${\displaystyle 1-T_{s}}$. This function is a proper distance metric. "Tanimoto Distance" is often stated as being a proper distance metric, probably because of its confusion with Jaccard distance.

If Jaccard or Tanimoto similarity is expressed over a bit vector, then it can be written as

${\displaystyle f(A,B)={\frac {A\cdot B}{\vert A\vert ^{2}+\vert B\vert ^{2}-A\cdot B}}}$

where the same calculation is expressed in terms of vector scalar product and magnitude. This representation relies on the fact that, for a bit vector (where the value of each dimension is either 0 or 1) then ${\displaystyle A\cdot B=\sum _{i}A_{i}B_{i}=\sum _{i}(A_{i}\land B_{i})}$ and ${\displaystyle {\vert A\vert }^{2}=\sum _{i}A_{i}^{2}=\sum _{i}A_{i}}$.

This is a potentially confusing representation, because the function as expressed over vectors is more general, unless its domain is explicitly restricted. Properties of ${\displaystyle T_{s}}$ do not necessarily extend to ${\displaystyle f}$. In particular, the difference function ${\displaystyle 1-f}$ does not preserve triangle inequality, and is not therefore a proper distance metric, whereas ${\displaystyle 1-T_{s}}$ is.

There is a real danger that the combination of "Tanimoto Distance" being defined using this formula, along with the statement "Tanimoto Distance is a proper distance metric" will lead to the false conclusion that the function ${\displaystyle 1-f}$ is in fact a distance metric over vectors or multisets in general, whereas its use in similarity search or clustering algorithms may fail to produce correct results.

Lipkus[2] uses a definition of Tanimoto similarity which is equivalent to ${\displaystyle f}$, and refers to Tanimoto distance as the function ${\displaystyle 1-f}$. It is, however, made clear within the paper that the context is restricted by the use of a (positive) weighting vector ${\displaystyle W}$ such that, for any vector A being considered, ${\displaystyle A_{i}\in \{0,W_{i}\}}$. Under these circumstances, the function is a proper distance metric, and so a set of vectors governed by such a weighting vector forms a metric space under this function.

## Notes

1. ^ Kosub, Sven; "A note on the triangle inequality for the Jaccard distance" arXiv:1612.02696
2. ^ a b Lipkus, Alan H. (1999), "A proof of the triangle inequality for the Tanimoto distance", Journal of Mathematical Chemistry, 26 (1–3): 263–265
3. ^ Levandowsky, Michael; Winter, David (1971), "Distance between sets", Nature, 234 (5): 34–35, doi:10.1038/234034a0
4. ^ Moulton, Ryan; Jiang, Yunjiang (2018), "Maximally Consistent Sampling and the Jaccard Index of Probability Distributions", International Conference on Data Mining, Workshop on High Dimensional Data Mining
5. ^ For example Qian, Huihuan; Wu, Xinyu; Xu, Yangsheng (2011). Intelligent Surveillance Systems. Springer. p. 161. ISBN 978-94-007-1137-2.
6. ^ Tanimoto, Taffee T. (17 Nov 1958). "An Elementary Mathematical theory of Classification and Prediction". Internal IBM Technical Report. 1957 (8?).
7. ^ Rogers, David J.; Tanimoto, Taffee T. (1960). "A Computer Program for Classifying Plants". Science. 132 (3434): 1115–1118. doi:10.1126/science.132.3434.1115.

## References

• Tan, Pang-Ning; Steinbach, Michael; Kumar, Vipin (2005), Introduction to Data Mining, ISBN 0-321-32136-7
• Jaccard, Paul (1901), "Étude comparative de la distribution florale dans une portion des Alpes et des Jura", Bulletin de la Société vaudoise des sciences naturelles, 37: 547–579
• Jaccard, Paul (1912), "The Distribution of the flora in the alpine zone", New Phytologist, 11: 37–50, doi:10.1111/j.1469-8137.1912.tb05611.x