rankseal / docs /algorithm.md
pgg-engineering's picture
Release RankSeal 0.1.0, browser workbench, wheel and verified benchmarks
bcdc840 verified
|
Raw History Blame Contribute Delete
7.49 kB

Certifying a fused ranking

Each source supplies a stable list of distinct string IDs in rank order, starting at 1. Lists may have different lengths. A page extends its source's prefix. Closing a source means that its entire configured ranking window has been consumed. Applications must enforce that window and keep each source on a consistent snapshot throughout a query. An exhausted or intentionally truncated list can close. A timeout alone cannot establish exhaustion.

For positive integer weights w_s and a positive integer rank constant c, the score for ID x is

score(x) = sum over sources containing x of w_s / (c + rank_s(x))

Ranks are local to the source. Absent IDs contribute zero. Higher scores precede lower scores, with ties broken by ascending Python string order. The result contains up to k distinct observed IDs. Before all sources close, a certificate always contains exactly k IDs. It guarantees their order, although their individual scores can still change.

Bounds

For a document already encountered, its observed contributions give a lower bound L(x). At depth d_s, the largest contribution an ID still missing from open source s can receive is a_s = w_s / (c + d_s + 1). Set a_s = 0 for closed sources. The upper bound U(x) equals L(x) plus the sum of a_s across missing sources. A document missing from every source has upper bound A = sum(a_s).

Choose tentative leaders by decreasing lower score, then by ID. Traverse the leaders in reverse, maintaining the best possible rank key of every document that would follow the current leader. Initially this includes the outsiders. At each step, the leader's lower score must outrank that best rival's upper score, using the same ID tie-break. While a source remains open, compare the weakest leader with the best possible wholly unseen ID at score A. Python's smallest nonempty string contains one U+0000 character. The smallest unseen string consists of t such characters, for the first positive t whose string has never appeared. That ID resolves equality at the unseen boundary.

Passing these checks proves every selected ID precedes both the next selected IDs and every outsider under every valid continuation. Every final score lies inside its document's interval. A certificate therefore remains valid after more pages arrive. When all sources close, every interval collapses to the exact score.

When further input is necessary

Under the API's information model, the exact pairwise test is also necessary. One valid completion closes every open list right now, so its order by observed scores is the only possible invariant answer. For any proposed leader x and distinct rival y, another valid completion appends y next to each open list in which it is still missing, then closes every list. This realizes U(y) and L(x) simultaneously. It works equally for the lexicographically first wholly unseen ID. If the rival precedes the leader under that completion, the two completions have different ordered top-k results. With fewer than k known IDs and any open source, appending a fresh ID can change the result's length.

Consequently the stop test certifies exactly the prefixes whose ordered answer is invariant across all admissible suffixes. This compares decisions at a fixed observed prefix. Different page sizes or read orders can reach different prefixes using different numbers of requests. An application that supplies extra constraints, such as a known candidate universe or required future ranks, can sometimes certify sooner.

Indexing the competitors

A bit mask records the sources that have supplied each ID. Two IDs with the same mask have the same possible future contribution. RankSeal maintains a heap of the largest observed upper scores for each occupied mask. While checking a certificate, temporarily removing the chosen leaders from each heap exposes that mask's best possible outsider. One comparison per occupied mask then suffices for all outsiders. A separate heap supplies global lower-score leaders. Changing membership makes an old heap record stale. Head cleanup and occasional rebuilding bound the storage for stale records.

Let D be distinct IDs, R be total accepted entries, m be sources, and b be occupied masks. One insertion uses amortized O(log(D + 1)) heap work. A fast certificate check costs O(m + b m + k log(D + 1)) in the worst case for its mask arithmetic and live heap operations, plus amortized removal of stale records. This counts arithmetic on weights and masks as unit cost. The mask loop visits only the set bits. For small source counts or repeated overlap patterns, b can be far smaller than D. With many different masks, b can approach D. Storage is O(R + D + m), including source ranks for exact comparisons. A second check without new input reuses the first outcome.

Exact arithmetic and ties

The fast path stores a lower and upper bound on each observed term, rounded down and up in units of 2^-64. It rounds each pending contribution up. Python integers preserve bounds for arbitrarily large integer weights. A document has at most one observed or pending term per source, so each of its scaled endpoint errors is less than or equal to m. Strict score separation or a favorable ID tie between these directed endpoints is safe. The method can defer a certificate when the intervals overlap.

Near an ambiguous tie, the fallback scans for candidates whose upper score reaches the kth lower score. At least k documents reach that floor when k documents exist. Every skipped document is already strictly below it. Exact rational arithmetic then ranks observed contributions and compares exact bounds for the retained documents. At exhaustion this produces a result even for exact ties and extremely large denominators. The fallback can scan D documents, evaluate up to R terms, and sort up to D rational scores. At the unseen equality boundary the fallback compares IDs explicitly. A wide rounded-score gap permits an immediate rejection only with a witness. For a new ID, the exact kth observed score is at most m scaled units above the tentative floor. For a known challenger, directed observed bounds can prove that it currently lies outside the top k, or that the threatened ID belongs to the top k and precedes it. A gap above 2m then survives both endpoint errors. If rounded scores leave membership or pair order ambiguous, exact comparisons resolve them before rejection.

The lower/upper-bound stopping principle builds on Fagin, Lotem and Naor's no-random-access rank aggregation work. Mamoulis, Cheng, Yiu and Cheung's LARA algorithm groups candidates by the subset of inputs where they have appeared and maintains group leaders. RankSeal uses this established structure with per-group heaps and specializes the comparisons to weighted reciprocal ranks, ordered results, stable string ties, and asynchronously advancing pages. A certificate saves retrieval work only when fetching the remaining pages incurs cost that can still be avoided.