# Is the Jaccard distance a distance?

Wikipedia defines the Jaccard distance between sets A and B as $$J_\delta(A,B)=1-\frac{|A\cap B|}{|A\cup B|}.$$ There's also a book claiming that this is a metric. However, I couldn't find any explanation of why $J_\delta$ obeys the triangle inequality. The naive approach of writing the inequality with seven variables (e.g., $x_{001}$ thru $x_{111}$, where $x_{101}$ is the number of elements in $(A\cap C) \backslash B$) and trying to reduce it seems hopeless for pen and paper. In fact it also seems hopeless for Mathematica, which is trying to find a counterexample for 20 minutes and is still running. (It's supposed to say if there isn't any.)

Is there a simple argument showing that this is a distance? Somehow, it feels like the problem shouldn't be difficult and I'm missing something.

-
Would it be possible (and/or appropriate) to put a link on the Wikipedia page to this MO question and answer? I'm hoping someone here knows more about Wikipedia guidelines than I do regarding such links. – Joe Silverman Jul 3 '15 at 15:16

The trick is to use a transform called the Steinhaus Transform. Given a metric $(X, d)$ and a fixed point $a \in X$, you can define a new distance $D'$ as

$$D'(x,y) = \frac{2D(x,y)}{D(x,a) + D(y,a) + D(x,y)}$$

It's known that this transformation produces a metric from a metric. Now if you take as the base metric $D$ the symmetric difference between two sets, what you end up with is the Jaccard distance (which actually is known by many other names as well).

For more information and references, check out Ken Clarkson's survey (Section 2.3)

-
You forgot to specify what $a$ should be, but the empty set seems to do the job. – Harald Hanche-Olsen Mar 13 '10 at 19:31
Steinhaus Transform, is it a standard term? – Anton Petrunin Mar 14 '10 at 0:24
I believe so, at least among the right folks :). It's possible it shows up under other names as well – Suresh Venkat Mar 14 '10 at 2:10
It seems that proving that Steinhaus Transform gives a metric is just as hard as the original problem... – Anton Petrunin Mar 18 '10 at 2:26

Here is an elementary proof of the Steinhaus transform (from which said metricity follows as a special case, as noted in Suresh's answer).

Lemma. Let $p,q,r > 0$ such that $p \le q$. Then, $\frac{p}{q} \le \frac{p+r}{q+r}.$

Corollary. Let $d(x,y)$ be a metric. Then, for arbitrary (but fixed) $a$, \begin{equation*} \delta(x,y) := \frac{2d(x,y)}{d(x,a)+d(y,a)+d(x,y)}, \end{equation*} is a metric.

Proof. Only the triangle inequality for $\delta$ is nontrivial. Let $p=d(x,y)$, $q=d(x,y)+d(x,a)+d(y,a)$, and $r=d(x,z)+d(y,z)-d(x,y)$. Applying the lemma, we obtain \begin{eqnarray*} \delta(x,y) &=& \frac{2d(x,y)}{d(x,a)+d(y,a)+d(x,y)} \le \frac{2d(x,z)+2d(y,z)}{d(x,a)+d(y,a)+d(x,z)+d(y,z)}\\ &=& \frac{2d(x,z)}{d(x,a)+d(z,a)+d(x,z)+d(y,z)+d(y,a)-d(z,a)} + \frac{2d(y,z)}{d(y,a)+d(z,a)+d(y,z)+d(x,z)+d(x,a)-d(z,a)}\\ &\le& \delta(x,z)+\delta(y,z), \end{eqnarray*} where the last inequality again uses triangle inequality for $d$.

-

It is possible to prove this directly too, without invoking the Steinhaus Transform. But that would probably make the proof longer. However, I did once prove it directly, and I think it went a bit like this:

Assume there exist A, B ,C such that d(A,B) + d(B,C) < d(A,C). For such a counterexample, note that A, C and $A\cap C$ have to be nonempty. Now since the right hand side remains unchanged on changing B, we can remove all elements in B which are not in A or C, since that would only further decrease the left hand side. Thus B is contained in $A\cup C$. The final step involves arguing that we can also remove all those elements in B which are only in A or C, as this operation will also only decrease the left hand side. Finally, we will have a B that is supposedly a counterexample to the metric distance claim, but it lies completely in $A \cap C$. This can also be shown to be not possible.

I hope I remember it right, I haven't worked this out recently.

-

We permute all the elements of $A \cup B \cup C$ and denote by $p_{A,B}$ the probability that the first element of the permutation that is in $A$ or $B$ is not in both. This probability is equal to $1-\frac{A \cap B}{A \cup B}$, which is the Jaccard distance, because we look at the first element which is in $A \cup B$ and the probability that it is in both sets is $\frac{A \cap B}{A \cup B}$.

Now we are only left to prove that $p_{A,B}+p_{B,C} \geq p_{A,C}$. That's true because if the first element of the permutation that is in $A$ is in index $i(A) \neq i(C)$, then it means that $i(A) \neq i(B)$ or $i(B) \neq i(C)$.

-
The definition of $p_{A,B}$ is asymmetric in $A$ and $B$, unlike Jaccard distance. The error in the proof occurs in "we look at the first element which in in $A\cup B$" since the first element which is in $A\cup B$ might be in $B$, but not in $A$; even though the first element in $A$ is also in $B$. – Boris Bukh Jul 3 '15 at 7:33
Howver, the proof can be salvaged. Just define $i(A)$ to be the first element of $A$, $i(B)$ to be the first element of $B$, etc. Define $p_{A,B}$ as "$i(A)\neq i(B)$", and so on. – Boris Bukh Jul 3 '15 at 7:38