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 (such as an image represented as a long vector of pixel colors) and a distance function that quantifies the distance between them, find all pairs of points such that , where 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 and is the size of their intersection divided by the size of their union: Example: If two sets have 3 elements in their intersection and 8 in their union, the Jaccard similarity is .
Jaccard Distance: This is the complement of the similarity measure: Example: Using the similarity above, the Jaccard distance is .
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 , this results in comparisons. Assuming a speed of comparisons per second, this task would take approximately 5 days. To optimize this, three steps are employed:
Shingling: Converting documents into sets.
Min-Hashing: Converting large sets to short signatures while preserving similarity.
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 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 , the set of 2-shingles is .
Shingle Bags: Shingles can be treated as a multiset (bag), where counts are preserved ().
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 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 and column indicates that element is a member of set .
Because boolean matrices are too large to compare in RAM, they are hashed into small signatures . The goal of Min-Hashing is to ensure that if is high, there is a high probability that .
The Min-Hash Procedure
Imagine the rows of the boolean matrix are permuted under a random permutation .
Define the hash function as the index of the first row (in the permuted order) in which column has a value of 1:
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 , the probability that the min-hash values for two columns are equal is exactly their Jaccard similarity: This holds true because any shingle in the union 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 comparison problem by hashing columns of the signature matrix such that similar columns hash to the same bucket with high probability. These become "candidate pairs."
Partitioning into Bands
The signature matrix is divided into bands, with each band containing rows.
For each band, the portion of each column is hashed to a hash table with many buckets.
Candidate pairs are those columns that hash to the same bucket in at least one band.
The parameters and are tuned to maximize the capture of similar pairs while minimizing false positives.
Mathematical Analysis of the LSH S-Curve
If columns and have similarity , the probability of their signatures being shared in a bucket is as follows:
Probability that all rows in a specific band of rows are equal:
Probability that at least one row in a band differs:
Probability that no band is identical across all bands:
Probability that at least one band is identical (the probability of being a candidate pair):
Performance Examples (b=20, r=5)
80% Similarity ():
Prob. identical in one band:
Prob. NOT a candidate pair:
Conclusion: Almost all highly similar pairs are caught.
30% Similarity ():
Prob. identical in one band:
Prob. BECOMING a candidate pair:
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 is valid if it satisfies:
iff
(Triangle Inequality)
A family of hash functions is -sensitive if for any :
If d(x, y) < d_1, then
If d(x, y) > d_2, then
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 functions. The combined function matches only if all functions match. It transforms the family into -sensitive. This shrinks probabilities, pushing lower probabilities toward 0.
OR Construction: Consists of functions. The combined function matches if at least one of the functions matches. It transforms the family into -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 results in the probability . Conversely, a b-way OR followed by an r-way AND results in .