Scalable Analytics: Chapter 2

High-Dimensional Data and the Problem of Finding Similar Items

Identifying similar items is a foundational challenge in scalable analytics, particularly when dealing with massive datasets and high-dimensional spaces. This problem, often exemplified by the Scene Completion Problem where the goal is to find the 10 nearest neighbors from a collection of 2 million images, appears in numerous domains:

  • Duplicate Document Detection: Finding pages with similar words or classifying them by topic to identify duplicates.

  • Recommendation Systems: Identifying customers who purchased similar products or products with similar customer sets.

  • Visual Analysis: Identifying images with similar features.

  • Web Analysis: Tracking users who visited similar websites.

Mathematically, the goal is defined as follows: given high-dimensional data points x1,x2,\mathbf{x_1, x_2, \dots} (such as an image represented as a long vector of pixel colors) and a distance function d(x1,x2)d(\mathbf{x_1, x_2}) that quantifies the distance between them, find all pairs of points (xi,xj)(\mathbf{x_i, x_j}) such that d(xi,xj)sd(\mathbf{x_i, x_j}) \le s, where ss is a specific distance threshold.

Distance Measures and Jaccard Similarity

For any application, the definition of "distance" must be established first. A primary measure used for sets is the Jaccard similarity and Jaccard distance.

  • Jaccard Similarity: The similarity of two sets C1C_1 and C2C_2 is the size of their intersection divided by the size of their union:     sim(C1,C2)=C1C2C1C2sim(C_1, C_2) = \frac{|C_1 \cap C_2|}{|C_1 \cup C_2|}     Example: If two sets have 3 elements in their intersection and 8 in their union, the Jaccard similarity is 38\frac{3}{8}.

  • Jaccard Distance: This is the complement of the similarity measure:     d(C1,C2)=1sim(C1,C2)=1C1C2C1C2d(C_1, C_2) = 1 - sim(C_1, C_2) = 1 - \frac{|C_1 \cap C_2|}{|C_1 \cup C_2|}     Example: Using the similarity above, the Jaccard distance is 58\frac{5}{8}.

The Three-Step Process for Scalable Similarity Analytics

Processing massive datasets (e.g., 1 million documents) na\u00efvely would require computing pairwise Jaccard similarities for every possible pair. For N=106N = 10^6, this results in N(N1)25×1011\frac{N(N-1)}{2} \approx 5 \times 10^{11} comparisons. Assuming a speed of 10610^6 comparisons per second, this task would take approximately 5 days. To optimize this, three steps are employed:

  1. Shingling: Converting documents into sets.

  2. Min-Hashing: Converting large sets to short signatures while preserving similarity.

  3. Locality-Sensitive Hashing (LSH): Focusing only on pairs of signatures likely to be from similar documents (candidate pairs).

Document Representation through Shingling

A k-shingle (or k-gram) for a document is a sequence of kk tokens that appears within the text. These tokens may be characters, words, or other units depending on the specific application.

  • Example (k=2): For document D1=abcabD_1 = abcab, the set of 2-shingles is S(D1)={ab,bc,ca}S(D_1) = \{ab, bc, ca\}.

  • Shingle Bags: Shingles can be treated as a multiset (bag), where counts are preserved (S(D1)={ab,bc,ca,ab}S'(D_1) = \{ab, bc, ca, ab\}).

  • Shingle Compression: To manage memory, long shingles can be hashed to a fixed size, such as 4 bytes. A document is then represented by the set of these hash values.

  • Working Assumption: Documents sharing many shingles have similar text. However, the value of kk must be large enough to prevent unrelated documents from appearing similar due to common short shingles.

Min-Hashing and the Creation of Signatures

Sets can be encoded as 0/1 boolean vectors where each dimension represents an element in the universal set. These are organized into a boolean matrix where rows represent elements (shingles) and columns represent sets (documents). A value of 1 in row ee and column ss indicates that element ee is a member of set ss.

Because boolean matrices are too large to compare in RAM, they are hashed into small signatures h(C)h(C). The goal of Min-Hashing is to ensure that if sim(C1,C2)sim(C_1, C_2) is high, there is a high probability that h(C1)=h(C2)h(C_1) = h(C_2).

The Min-Hash Procedure
  1. Imagine the rows of the boolean matrix are permuted under a random permutation π\pi.

  2. Define the hash function hπ(C)h_\pi(C) as the index of the first row (in the permuted order) in which column CC has a value of 1:     hπ(C)=argmin π(C)h_\pi(C) = \text{argmin } \pi(C)

  3. Create a signature of a column by using several (e.g., 100) independent random permutations/hash functions. The signature is a short integer vector.

The Min-Hash Property

For a random permutation π\pi, the probability that the min-hash values for two columns are equal is exactly their Jaccard similarity: Pr[hπ(C1)=hπ(C2)]=sim(C1,C2)Pr[h_\pi(C_1) = h_\pi(C_2)] = sim(C_1, C_2) This holds true because any shingle yy in the union C1C2C_1 \cup C_2 is equally likely to be the first element in the permuted order. The probability that this element belongs to both sets (the intersection) determines the equality of the min-hash.

Locality-Sensitive Hashing (LSH) for Candidate Detection

LSH aims to avoid the O(N2)O(N^2) comparison problem by hashing columns of the signature matrix MM such that similar columns hash to the same bucket with high probability. These become "candidate pairs."

Partitioning into Bands

The signature matrix MM is divided into bb bands, with each band containing rr rows.

  1. For each band, the portion of each column is hashed to a hash table with many buckets.

  2. Candidate pairs are those columns that hash to the same bucket in at least one band.

  3. The parameters bb and rr are tuned to maximize the capture of similar pairs while minimizing false positives.

Mathematical Analysis of the LSH S-Curve

If columns C1C_1 and C2C_2 have similarity tt, the probability of their signatures being shared in a bucket is as follows:

  • Probability that all rows in a specific band of rr rows are equal: trt^r

  • Probability that at least one row in a band differs: 1tr1 - t^r

  • Probability that no band is identical across all bb bands: (1tr)b(1 - t^r)^b

  • Probability that at least one band is identical (the probability of being a candidate pair):     P(candidate pair)=1(1tr)bP(\text{candidate pair}) = 1 - (1 - t^r)^b

Performance Examples (b=20, r=5)
  • 80% Similarity (t=0.8t = 0.8):

    • Prob. identical in one band: (0.8)5=0.328(0.8)^5 = 0.328

    • Prob. NOT a candidate pair: (10.328)20=0.00035(1 - 0.328)^{20} = 0.00035

    • Conclusion: Almost all highly similar pairs are caught.

  • 30% Similarity (t=0.3t = 0.3):

    • Prob. identical in one band: (0.3)5=0.00243(0.3)^5 = 0.00243

    • Prob. BECOMING a candidate pair: 1(10.00243)200.04741 - (1 - 0.00243)^{20} \approx 0.0474

    • Conclusion: Low similarity pairs are effectively filtered.

Generalized Locality-Sensitive Hash Families

LSH can be generalized beyond Jaccard similarity to other distance measures (e.g., Cosine distance, Euclidean distance). A distance measure d()d(\cdot) is valid if it satisfies:

  1. d(x,y)0d(x, y) \ge 0

  2. d(x,y)=0d(x, y) = 0 iff x=yx = y

  3. d(x,y)=d(y,x)d(x, y) = d(y, x)

  4. d(x,y)d(x,z)+d(z,y)d(x, y) \le d(x, z) + d(z, y) (Triangle Inequality)

A family HH of hash functions is (d1,d2,p1,p2)(d_1, d_2, p_1, p_2)-sensitive if for any x,yx, y:

  • If d(x, y) < d_1, then Pr[h(x)=h(y)]p1Pr[h(x) = h(y)] \ge p_1

  • If d(x, y) > d_2, then Pr[h(x)=h(y)]p2Pr[h(x) = h(y)] \le p_2

Amplification of Hash Families

To improve the S-curve effect (narrowing the transition between high and low probability), two constructions are used:

  • AND Construction: Consists of rr functions. The combined function matches only if all rr functions match. It transforms the family into (d1,d2,p1r,p2r)(d_1, d_2, p_1^r, p_2^r)-sensitive. This shrinks probabilities, pushing lower probabilities toward 0.

  • OR Construction: Consists of bb functions. The combined function matches if at least one of the bb functions matches. It transforms the family into (d1,d2,1(1p1)b,1(1p2)b)(d_1, d_2, 1-(1-p_1)^b, 1-(1-p_2)^b)-sensitive. This grows probabilities, pushing higher probabilities toward 1.

By composing an r-way AND followed by a b-way OR, the transformation of similarity ss results in the probability 1(1sr)b1 - (1 - s^r)^b. Conversely, a b-way OR followed by an r-way AND results in 1(1sb)r1 - (1 - s^b)^r.