The Jaccard similarity measures the fraction of shared elements between set. In particular, given set AA and BB, the Jaccard similarity between them is defined as

J(A,B)=∣A∩B∣∣A∪B∣ J(A, B) = \frac{|A \cap B|}{|A \cup B|}

The naive calculation of J(A,B)J(A, B) has time complexity O(∣A∪B∣)O(|A \cup B|) by scanning all elements in the two sets once. Here we discuss how randomization might save time.

Lemma One

If we select an item xx uniformly at random from ∣A∪B∣|A \cup B|, then Pr⁡[x∈A∩B]=∣A∩B∣∣A∪B∣\Pr[x \in A \cap B] = \frac{|A \cap B|}{|A \cup B|}.

Define the random variable

X={1, if x∈ A∩B0, otherwise X = \begin{cases} 1, \ if \ x \in \ A \cap B \\ 0, \ otherwise \end{cases}

Then we see that XX is an unbiased estimator of J(A,B)J(A, B), i.e., E[X]=J(A,B)E[X] = J(A, B).

Suppose that we have a sequence of i.i.d. copies of XX, denoted X1,X2,...,XkX_1, X_2, ..., X_k, then the average μ=1k∑i=1kXi\mu = \frac{1}{k} \sum_{i = 1}^k X_i is also an unbiased estimator of J(A,B)J(A, B). Moreover, by law of large number, μ→J(A,B)\mu \rightarrow J(A, B) as k→∞k \rightarrow \infty. The problem is, how many XiX_i's are enough.

The answer depends on a few factors:

  1. Whether the value of the required error is relative to J(A,B)J(A, B) or independent to J(A,B)J(A, B).
  2. What is the tolerance of failure probability.

In the following discussion we consider only the case of finding an estimator μ\mu such that J(A,B)∈[μ±ϵ]J(A, B) \in [\mu \pm \epsilon] with probability at least 1−δ1 - \delta, where both ϵ\epsilon and δ\delta are given parameters. Then the convergence behavior with respect to the number of samples kk can be captured by Hoeffding inequality:

Pr⁡[∣μ−J(A,B)∣≥ϵ]≤2exp⁡(−2ϵ2k)=δ \Pr[ |\mu - J(A, B)| \ge \epsilon] \le 2\exp \left( -\frac{2\epsilon^2}{k} \right) = \delta

Solving the equation gives

k=2ϵ2log⁡2δ k = \frac{2}{\epsilon^2}\log \frac{2}{\delta}

Or equivalent, when we interpret ϵ\epsilon as the width of confidence interval,

ϵ=log⁡2δk \epsilon = \sqrt \frac{\log \frac{2}{\delta} }{k}

which has order O(1k)O(\sqrt \frac{1}{k}).