# How to calculate the Jaccard index [closed]

I want to calculate the Jaccard index between two compounds. What is the algorithm? I have searched for it, it just gives the formula but how to apply it on compounds is not known to me. Can you help?

• Exactly how do you want to use it? In ecology, it is usually used to compare how similar communities of species are, but you are referring to only two compounds. What entities do you want to compare? As for coding it, you could probably find much useful information in the currently 345 StackOverflow questions on the Jaccard coefficient/index – fileunderwater May 29 '15 at 12:49
• I missed the detail about "two compounds". My answer includes a reference for two sets of compounds but it would be good if you clarify what you mean exactly. – ddiez May 29 '15 at 14:24
• How do you denote a compound? just by its composition?? For e.g. C₃H₆O₃ can be lactic acid, glyceraldehyde or trioxane. – WYSIWYG May 30 '15 at 10:34
• Yes, denote a compound by their atoms that consists them.. – girl101 May 31 '15 at 2:28
• or by SMILES format – girl101 Jun 3 '15 at 4:50

The Jaccard index is a measure of similarity between two sets. Take a look at the Wikipedia article here. It is very easy to compute:

The Jaccard similarity coefficient for sets X and Y is defined as:

J(X,Y) = |intersection(X,Y)| / |union(X,Y)|


Where | | indicates the size (number of elements) of the set. Imagine you have two sets X and Y defined as follows:

X = {A, B, C, D}
Y = {C, D, E, F, G}


Then:

intersection(X,Y) = {C, D} => |intersection(X,Y)| = 2
union(X,Y) = {A,B,C,D,E,F} => |union(X,Y)| = 5


Therefore: J(X,Y) = 2/5

Alternatively, the Jaccard distance would be D(X,Y) = 1 - J(X,Y) = 1 - 2/5 = 3/5

In Biology the Jaccard index has been used to compute the similarity between networks, by comparing the number of edges in common (e.g. Bass, Nature methods 2013)

Regarding applying it to compounds, if you have two sets with different compounds, you can find how similar the two sets are using this index. The elements on the sets, in this case the compounds, correspond to A, B, C, etc. in my example.

• chemical compounds would have atoms, so basically for each compound we create a set of atoms and then find the index. Will that do? – girl101 May 31 '15 at 2:27