# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Compute the Jaccard dissimilarity of two multisets

Contributed by:
Robert B. Nachbar (Wolfram Solutions)

ResourceFunction["MultisetJaccardDissimilarity"][ gives the Jaccard dissimilarity between multisets | |

ResourceFunction["MultisetJaccardDissimilarity"][ gives the Jaccard dissimilarity between multisets |

If the *list*_{i} are considered as multisets, ResourceFunction["MultisetJaccardDissimilarity"] gives their dissimilarity.

The *list*_{i} must have the same head, but it need not be List.

The values of *assoc*_{i} must be counts—that is, non-negative Integer values.

ResourceFunction["MultisetJaccardDissimilarity"][*A*,*B*] is equivalent to .

Jaccard dissimilarity between two List multisets:

In[1]:= |

Out[1]= |

Jaccard dissimilarity between two Association multisets:

In[2]:= |

Out[2]= |

The number of elements of each distinct kind affects the result:

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

The Jaccard dissimilarity measure, sometimes called Tanimoto dissimilarity, has its origins in ecology. It was developed by Paul Jaccard as a measure of similarity of plant distribution in different regions.

The measure can be used in a number of fields, as shown by the following examples. The role of object and attribute can also be reversed, and the first application demonstrates this duality.

Here are some ground-based animal index counts:

In[7]:= |

Out[7]= |

Here the different blocks (habitats) are compared using the animals present as characters. The arrangement of the table in this manner originated in the field of psychology and was later adopted by numerical taxonomy.

The MultisetJaccardDissimilarity distance matrix of the blocks:

In[8]:= |

Out[8]= |

A Dendrogram showing how the blocks cluster:

In[9]:= |

Out[9]= |

The preceding analysis that compares the columns of the table is known as a Q-type (deriving from early factor analysis studies in the area of psychology). Conversely, comparing the rows is known as an R-type analysis and in this case compares the animals. Begin by first transposing the data table:

In[10]:= |

Out[10]= |

A Dendrogram shows that elephants and zebras, for example, are distributed similarly:

In[11]:= |

Out[11]= |

This example compares households on a single city block using the composition of household members (head, wife, daughter, brother-in-law, etc.). The data was compiled from the 1920 US Census for the 300 block of Wyoming Ave., Buffalo, NY. The households are labeled by street number.

Load the data:

In[12]:= |

Out[8]= |

In[13]:= |

Out[13]= |

The distance matrix using MultisetJaccardDissimilarity can be displayed with an ArrayPlot (note the presence of off-diagonal 0s colored yellow):

In[14]:= |

Out[14]= |

The clustering of the households as shown by a Dendrogram:

In[15]:= |

Out[15]= |

The 14 yellow-colored, off-diagonal 0s of the preceding distance matrix represent pairs of identical households. They also appear in the preceding dendrogram as leaves with no initial height. Here they are extracted as clusters:

In[16]:= |

In[17]:= |

Out[17]= |

Mapping back to the original household data gives:

In[18]:= |

Out[18]= |

Because the MultisetJaccardDissimilarity is a distance metric, the distance matrix can be used to generate a set of 3D coordinates with distance geometry methods:

In[19]:= |

In[20]:= |

Out[20]= |

Understanding market forces and characteristics of the competition are essential components of profitable business decisions. This example shows how shopping malls might be compared using the numbers of kinds of businesses in each. The data was collected from the websites of each mall using the self-reported number of businesses in each category. These malls are all managed by the same organization, so the categories can be assumed to be consistent.

Load the data:

In[21]:= |

Out[21]= |

The distance matrix using MultisetJaccardDissimilarity can be displayed with an ArrayPlot:

In[22]:= |

Out[22]= |

The Dendrogram shows that The Mills at Jersey Gardens is very different from the others:

In[23]:= |

Out[23]= |

Similarity analysis of chemical structures goes back to the original work of Carhart et al. at Lederle Laboratories. It is used extensively to search though chemical databases to find compounds of interest and to cluster chemical structures into similar groups.

Here is small set of entities from the ChemicalData collection:

In[24]:= |

Out[24]= |

They can be converted into computable Molecule objects:

In[25]:= |

Out[23]= |

These are the topological features that will characterize each chemical structure (they are not the same as those used by Carhart et al., but are easily computable with MoleculeValue):

In[26]:= |

Using Dataset, a searchable database can be made:

In[27]:= |

Out[27]= |

Estradiol, for example, can be taken as the query molecule:

In[28]:= |

Out[28]= |

In[29]:= |

Out[29]= |

In[30]:= |

Out[30]= |

Jaccard multiset dissimilarity is bounded by 0 and 1:

In[31]:= |

Out[31]= |

In[32]:= |

Out[32]= |

The result is the same as JaccardDissimilarity when the multisets are sets:

In[33]:= |

Out[33]= |

In[34]:= |

Out[34]= |

The MultisetJaccardDissimilarity is a true distance measure, as it does obey the triangle inequality:

In[35]:= |

Out[30]= |

In[36]:= |

Out[37]= |

The same is true for JaccardDissimilarity:

In[38]:= |

Out[41]= |

In[42]:= |

Out[45]= |

- 1.0.0 – 01 July 2019

This work is licensed under a Creative Commons Attribution 4.0 International License