Sampling Methods for Inner Product Sketching

Majid Daliri, Juliana Freire, Christopher Musco, Aécio S. R. Santos, Haoxiang Zhang

Here is a blog-style summary of the paper.

Sampling Methods for Inner Product Sketching

TL;DR

Estimating inner products between high-dimensional vectors is fundamental to databases and machine learning, but classic linear sketches (like Johnson-Lindenstrauss or CountSketch) suffer when sparse vectors have small support overlap. This paper introduces two coordinated sampling sketches—based on Threshold Sampling and Priority Sampling—that achieve the tighter theoretical error bounds of Weighted MinHash (WMH) while running orders of magnitude faster (in linear and near-linear time).

What Problem Does This Paper Address?

Given two vectors $\mathbf{a}$ and $\mathbf{b}$ of dimension $n$, computing their inner product $\langle \mathbf{a}, \mathbf{b} \rangle = \sum_{i=1}^n a_i b_i$ takes $O(n)$ time. When computing all-pairs inner products across a dataset of $D$ vectors, the brute-force cost scales as $O(D^2 n)$, which becomes prohibitive. Sketching algorithms compress each vector into a small footprint $S(\mathbf{a})$ of size $m \ll n$ such that $\langle \mathbf{a}, \mathbf{b} \rangle \approx \langle S(\mathbf{a}), S(\mathbf{b}) \rangle$ in $O(D^2 m)$ time.

Standard linear sketches guarantee an error of $\epsilon \|\mathbf{a}\|_2 \|\mathbf{b}\|_2$. However, in real-world database applications (e.g., estimating correlations or join sizes between unjoined tables), vectors are sparse and share only a tiny fraction of non-zero entries (the intersection support $\mathcal{I}$). In this regime, linear sketching errors are dominated by non-overlapping entries. While recent work showed that Weighted MinHash (WMH) improves the error bound to $\epsilon \max(\|\mathbf{a}_{\mathcal{I}}\|_2 \|\mathbf{b}\|_2, \|\mathbf{a}\|_2 \|\mathbf{b}_{\mathcal{I}}\|_2)$, WMH is computationally heavy ($O(Nm \log n)$ construction time for $N$ non-zeroes), complex to implement, and samples with replacement.

Methods

The authors introduce two coordinated, weighted sampling-without-replacement sketching methods that draw entries with probability proportional to their squared magnitudes ($a_i^2 / \|\mathbf{a}\|_2^2$):

  1. Threshold Sampling (TS-weighted):
  2. For each non-zero entry $i$, assign a threshold $\tau_i = m \cdot a_i^2 / \|\mathbf{a}\|_2^2$.
  3. Using a shared hash function $h(i) \in [0, 1]$, include entry $i$ in sketch $S(\mathbf{a})$ if $h(i) \le \tau_i$.
  4. Cost & Sketch Size: Runs in linear $O(N)$ time. Sketch size is $m$ in expectation (randomly varying with variance $O(\sqrt{m})$).
  5. Priority Sampling (PS-weighted):
  6. Eliminates sketch size variability by computing ranks $R_i = h(i) / a_i^2$ and retaining only the $m$ smallest values.
  7. The $(m+1)$-th smallest rank serves as a data-dependent threshold $\tau_a$.
  8. Cost & Sketch Size: Runs in $O(N \log m)$ time and guarantees a strict sketch size of at most $m$.

Unified Unbiased Estimator: Both methods use the same estimator over the intersection of sampled indices $\mathcal{T} = K_a \cap K_b$: $$W = \sum_{i \in \mathcal{T}} \frac{a_i b_i}{\min(1, a_i^2 \tau_a, b_i^2 \tau_b)}$$ The authors also formulate a black-box reduction that translates join-correlation estimation (Pearson’s $r$ over unjoined tables) into six inner products between values, squared values, and indicator vectors, unlocking weighted sketches for dataset discovery.

Datasets

The authors evaluated methods on both synthetic and diverse real-world benchmarks:

  • Synthetic Data: High-dimensional vectors ($n = 100{,}000$ to $250{,}000$) with controlled overlap $\mathcal{I}$ (1% to 100%), non-zero entries, and intentional outliers. Evaluated on real-valued vectors, binary vectors, and simulated join-correlation.
  • World Bank Finances Dataset: 56 tables, sampling 3,000 column pairs with temporal keys for inner product, join-correlation, and join-size estimation.
  • 20 Newsgroups Dataset: Text document similarity using TF-IDF feature vectors (evaluating all documents vs. long documents $>500$ words).
  • Twitter Graph Dataset: 14 million (user, follower) tuples used to estimate a 2-hop self-join size under extreme skew.
  • TPC-H Benchmark: Join-size estimation between LINEITEM and PARTSUPP with Zipfian skew ($z = 2$).

Key Insights and Ideas

  • Sampling Proportional to Squared Magnitude: While previous sampling techniques like End-Biased Sampling used $\ell_1$ probabilities ($|a_i| / \|\mathbf{a}\|_1$), sampling proportional to $a_i^2 / \|\mathbf{a}\|_2^2$ directly mirrors the Euclidean geometry of inner products, enabling a small constant variance bound ($2/m$).
  • Coordinated Hash Functions Drive Success: Because $h(i)$ is shared, an index that produces a low hash value in $\mathbf{a}$ produces that exact same low value in $\mathbf{b}$, maximizing the chance that matching non-zero coordinates co-occur in both sketches.
  • Uncorrelated Dependent Variables: Analyzing Priority Sampling is notoriously difficult because all weights depend on the random variable threshold $\tau_a$. The authors leverage pairwise uncorrelatedness to prove an optimal variance bound of $\frac{2}{m-1} \max(\|\mathbf{a}_{\mathcal{I}}\|_2^2 \|\mathbf{b}\|_2^2, \|\mathbf{a}\|_2^2 \|\mathbf{b}_{\mathcal{I}}\|_2^2)$.

Key Results

  • Construction Speedup: Priority Sampling constructed sketches in an average of 0.06 seconds, and Threshold Sampling in 0.21 seconds, rivaling CountSketch (0.05s). Standard WMH took 43s to 213s (omitted from plots due to scale), and optimized DartMinHash took 20× to 60× longer than Priority Sampling.
  • Accuracy vs. Linear Sketches: On real-valued synthetic vectors and World Bank data, TS-weighted and PS-weighted cut estimation errors by roughly 3× compared to Johnson-Lindenstrauss (JL) and CountSketch.
  • Join-Correlation State-of-the-Art: On World Bank data, PS-weighted achieved an $R^2$ of 0.828 (average error 0.079), beating the prior state-of-the-art KMV-based Correlation Sketches ($R^2 = 0.613$, average error 0.124) and CountSketch ($R^2 = 0.798$, average error 0.112).
  • Robustness Under Skew: In the Twitter 2-hop join experiment, weighted sampling significantly outperformed unweighted sampling methods, which struggled with variable degree distributions.

Limitations and Future Work

  • Streaming Setting: Unlike linear sketches, which support arbitrary additions and subtractions on a data stream via matrix-vector products, these sampling methods require knowing total vector norms/entry magnitudes in advance to define sampling thresholds. Adapting them to turnstile streaming via streaming $\ell_2$-sampling remains open.
  • Storage Overhead: Sampling-based sketches store both values (64-bit float) and index/hash keys (32-bit int), requiring approximately 1.5× the storage bits of a raw linear sketch entry. Advanced quantization strategies were left for future research.

Summary updated

BibTeX
@article{daliri2024sampling,
  author = {Majid Daliri and Juliana Freire and Christopher Musco and Aécio S. R. Santos and Haoxiang Zhang},
  title = {{Sampling Methods for Inner Product Sketching}},
  journal = {Proc. VLDB Endow.},
  year = {2024},
  url = {https://openreview.net/forum?id=xy831suQ2t},
}
Figure