Algorithm Search⌘K

Algorithmresearch

xQuAD

Also called explicit query aspect diversification, eXplicit Query Aspect Diversification.

Greedily picks documents that are relevant to the query and to query aspects not yet covered by the documents already picked.

Time
O(n log n + n k m) worst
Space
O(n + m) extra
Where
k = the number of results; m = the number of aspects; n = the number of candidate documents
Properties
deterministiciterative
Origin
Rodrygo L. T. Santos, Craig Macdonald, Iadh Ounis, 2010

xQuAD (explicit query aspect diversification) is a greedy algorithm for search result diversification that reranks the top of a first ranking by mixing two scores: how relevant a document is to the query, and how well it covers the aspects (sub-queries) of the query that the documents already picked leave uncovered. Rodrygo Santos, Craig Macdonald and Iadh Ounis introduced it in 2010 and took the aspects from the query reformulations that web search engines suggest. Its diversity term is the coverage objective of IA-Select; xQuAD adds the plain relevance with a weight 1−λ1 - \lambda, so that λ=0\lambda = 0 keeps the first ranking and λ=1\lambda = 1 is IA-Select. Unlike maximal marginal relevance, it never compares two documents with each other.

In plain words

Suppose someone searches for "jaguar", and the search engine knows that people often rewrite that query as "jaguar car" or "jaguar animal". xQuAD treats each rewrite as one need behind the query. It builds the result list one page at a time. A page earns points for matching the original query, and more points for matching a need that the list does not cover yet. Once a car page is in the list, other car pages earn less from the car need, so a good animal page can move up. A slider sets how much the uncovered needs count against the plain match.

The problem

The input is a query qq, a first ranking RR of candidates (Santos et al. rerank the top τ=1000\tau = 1000), the relevance P(d∣q)P(d \mid q) of each candidate, a set QQ of mm sub-queries qiq_i with importances P(qi∣q)P(q_i \mid q) that sum to 1, the coverage P(d∣qi)∈[0,1]P(d \mid q_i) \in [0, 1] of each candidate for each sub-query, λ∈[0,1]\lambda \in [0, 1] and a length kk. The output is a list of min⁡(k,∣R∣)\min(k, |R|) documents. In the paper, both P(d∣q)P(d \mid q) and P(d∣qi)P(d \mid q_i) come from a retrieval model, such as BM25 or a query likelihood model, run once for the query and once for each sub-query.

The algorithm

With SS the list chosen so far, the next document maximizes Equation 5 of the paper:

(1−λ) P(d∣q)+λ∑qi∈QP(qi∣q) P(d∣qi)∏dj∈S(1−P(dj∣qi)).(1 - \lambda)\, P(d \mid q) + \lambda \sum_{q_i \in Q} P(q_i \mid q)\, P(d \mid q_i) \prod_{d_j \in S} \big(1 - P(d_j \mid q_i)\big).

The product is the probability that no chosen document satisfies qiq_i, under the assumption that the documents satisfy a sub-query independently. It changes by one factor per pick, so the procedure keeps it per sub-query:

  1. XQUAD(R, rel, P_aspect, cover, λ, k)
  2. 1for each sub-query i: novel[i] ← 1
  3. 2S ← empty list
  4. 3while some document of R is not in S and |S| < k:
  5. 4for each d not in S, in id order:
  6. 5score(d) ← (1 − λ) · rel(d) + λ · Σ over i of P_aspect(i) · cover(d, i) · novel[i]
  7. 6b ← the d with the largest score, ties to the smaller id; append b to S
  8. 7for each sub-query i that b covers:
  9. 8novel[i] ← (1 − cover(b, i)) · novel[i]
  10. 9return S

Example

The query "jaguar" has two aspects, car with importance 0.6 and animal with 0.4, and four candidates:

doc P(d∣q)P(d \mid q) P(d∣car)P(d \mid \text{car}) P(d∣animal)P(d \mid \text{animal})
d1 0.9 0.9 0
d2 0.8 0.8 0
d3 0.6 0 0.7
d4 0.5 0.3 0.3

With λ=0\lambda = 0 the list is the first ranking, d1, d2, d3, d4. With λ=0.5\lambda = 0.5:

step novel(car) novel(animal) d1 d2 d3 d4 pick
1 1 1 0.72 0.64 0.44 0.40 d1
2 0.1 1 0.424 0.44 0.319 d3
3 0.1 0.3 0.424 0.277 d2
4 0.02 0.3 0.2698 d4

After d1, the car aspect is 90% covered, so d2 keeps almost only its relevance, 0.40.4, plus 0.5⋅0.6⋅0.8⋅0.1=0.0240.5 \cdot 0.6 \cdot 0.8 \cdot 0.1 = 0.024, and d3 moves up because the animal aspect is still open. With λ=1\lambda = 1 the order is d1, d3, d4, d2: d4 covers a little of both aspects and passes d2, whose only aspect is already served.

Correctness

The file proofs/correctness.md proves that the loop picks, at each step, the document of Equation 5 with the tie rule. The invariant is that novel[i]=∏dj∈S(1−P(dj∣qi))\mathrm{novel}[i] = \prod_{d_j \in S}(1 - P(d_j \mid q_i)) before each pass, which line L8 keeps. The proof also shows that the score of dd is the marginal gain of the set function

F(S)=(1−λ)∑d∈SP(d∣q)+λ∑qiP(qi∣q)(1−∏d∈S(1−P(d∣qi))),F(S) = (1 - \lambda) \sum_{d \in S} P(d \mid q) + \lambda \sum_{q_i} P(q_i \mid q) \Big(1 - \prod_{d \in S} \big(1 - P(d \mid q_i)\big)\Big),

the sum of a modular term and the IA-Select objective. With non-negative relevance scores, FF is monotone and submodular, so by the Nemhauser, Wolsey and Fisher theorem the greedy list has at least 1−1/e1 - 1/e of the best value of FF. The paper does not state this bound; it is a consequence of the form of Equation 5. The bound is about FF under the model, not about measured quality.

Proof: The incremental xQuAD loop picks the document of Equation 5 at every step, and is greedy on a monotone submodular objectivefull

Let RR be a finite set of candidate ids with a total order, rel(d)=P(d∣q)\mathrm{rel}(d) = P(d \mid q) for d∈Rd \in R, a finite set QQ of aspects with weights wi=P(qi∣q)≥0w_i = P(q_i \mid q) \ge 0, coverages ci(d)=P(d∣qi)∈[0,1]c_i(d) = P(d \mid q_i) \in [0, 1], taken as 00 when not given, and λ∈[0,1]\lambda \in [0, 1]. For a chosen set SS and d∈R∖Sd \in R \setminus S, Equation 5 of Santos, Macdonald and Ounis is

xS(d)=(1−λ) rel(d)+λ∑i∈Qwi ci(d)∏j∈S(1−ci(j)).x_S(d) = (1 - \lambda)\, \mathrm{rel}(d) + \lambda \sum_{i \in Q} w_i\, c_i(d) \prod_{j \in S} \big(1 - c_i(j)\big).

The xQuAD list with the tie rule of this page is s1,…,sms_1, \ldots, s_m with m=min⁡(k,∣R∣)m = \min(k, |R|), where sts_t is the smallest id among the maximizers of xSt−1x_{S_{t-1}} over R∖St−1R \setminus S_{t-1}, and St={s1,…,st}S_t = \{s_1, \ldots, s_t\}.

Lemma 1 (invariant). At the start of each pass of the loop of line L3, with SS the set of ids chosen so far, novel[i]=∏j∈S(1−ci(j))\mathrm{novel}[i] = \prod_{j \in S} (1 - c_i(j)) for every aspect ii.

Proof. Line L1 sets every value to 11, the empty product. When bb is chosen, lines L7 and L8 multiply novel[i]\mathrm{novel}[i] by 1−ci(b)1 - c_i(b) for each aspect that bb covers; for the other aspects ci(b)=0c_i(b) = 0 and the factor is 11. □\square

Theorem 2 (output). The procedure returns s1,…,sms_1, \ldots, s_m, each with its score xSt−1(st)x_{S_{t-1}}(s_t).

Proof. By Lemma 1, line L5 computes xS(d)x_S(d), where the sum skips the aspects with ci(d)=0c_i(d) = 0, whose terms are 00. The ids are scanned in increasing order and the best is replaced only on a strictly larger score, so line L6 records the smallest id among the maximizers, which is sts_t. The loop runs while ids remain and fewer than kk are chosen, so it makes mm passes. □\square

Corollary 3 (lambda = 0). For λ=0\lambda = 0 the output is the first mm ids in the order rel descending, then id ascending, the first ranking.

Proof. The score is rel(d)\mathrm{rel}(d) and does not depend on SS. □\square

Theorem 4 (the set objective). Define, for S⊆RS \subseteq R,

F(S)=(1−λ)∑d∈Srel(d)+λ∑i∈Qwi(1−∏j∈S(1−ci(j))).F(S) = (1 - \lambda) \sum_{d \in S} \mathrm{rel}(d) + \lambda \sum_{i \in Q} w_i \Big(1 - \prod_{j \in S} \big(1 - c_i(j)\big)\Big).

Then xS(d)=F(S∪{d})−F(S)x_S(d) = F(S \cup \{d\}) - F(S) for d∉Sd \notin S, so xQuAD is the greedy algorithm on FF. If rel(d)≥0\mathrm{rel}(d) \ge 0 for every dd, FF is monotone and submodular with F(∅)=0F(\emptyset) = 0, and the list SmS_m satisfies F(Sm)≥(1−1/e)max⁡∣S∣=mF(S)F(S_m) \ge (1 - 1/e) \max_{|S| = m} F(S).

Proof. The first sum grows by (1−λ)rel(d)(1 - \lambda)\mathrm{rel}(d). In the second, the product of aspect ii gains the factor 1−ci(d)1 - c_i(d), so its term grows by wici(d)∏j∈S(1−ci(j))w_i c_i(d) \prod_{j \in S}(1 - c_i(j)). Together this is xS(d)x_S(d). The first sum is modular, so its marginal gain does not depend on SS, and it is non-negative when rel is. The second is the objective of IA-Select with P(c∣q)=wiP(c \mid q) = w_i and V(d∣q,c)=ci(d)V(d \mid q, c) = c_i(d), whose marginal gain is a product of numbers in [0,1][0, 1] and can only shrink when SS grows. A non-negative combination of monotone submodular functions with value 00 on the empty set has the same properties, and F(∅)=0F(\emptyset) = 0 because the empty product is 11. The theorem of Nemhauser, Wolsey and Fisher gives the bound for the greedy algorithm, since by Theorem 2 each pick maximizes the marginal gain. □\square

Santos et al. do not state Theorem 4; they present Algorithm 1 as an instance of the greedy approximation and note that the general problem is NP-hard by a reduction from maximum coverage. The bound concerns FF under the model, and says nothing about measured effectiveness.

Corollary 5 (lambda = 1). For λ=1\lambda = 1, xQuAD and IA-Select with P(c∣q)=wiP(c \mid q) = w_i and V(d∣q,c)=ci(d)V(d \mid q, c) = c_i(d) pick the same list under the same tie rule.

Proof. Both scores are the marginal gain of the same function, the second term of FF. □\square

Cost. Let n=∣R∣n = |R|, mQ=∣Q∣m_Q = |Q|, and ee the number of pairs with ci(d)>0c_i(d) > 0. Each of the at most kk passes computes nn scores in O(n+e)O(n + e) total time and updates at most mQm_Q values, so the procedure runs in O(nlog⁡n+k(n+e))=O(nlog⁡n+nkmQ)O(n \log n + k(n + e)) = O(n \log n + nk m_Q) time with O(n+mQ)O(n + m_Q) extra space. The direct form, which recomputes each product over SS, takes O(nk2mQ)O(nk^2 m_Q) time.

The test file checks Theorem 2 step by step against the direct form on random instances, half of them on a grid of values so that ties are frequent, and checks for λ=1\lambda = 1 that each score is the largest marginal gain of the coverage term.

Complexity

Each of the at most kk passes computes nn scores, each a sum over the sub-queries that the document covers, and updates at most mm products. The time is O(nlog⁡n+nkm)O(n \log n + nkm), including the sort of the ids, and O(nlog⁡n+k(n+e))O(n \log n + k(n + e)) when only ee pairs have nonzero coverage. The extra space is O(n+m)O(n + m). In practice, the main cost is retrieval: one ranking for the query and one for each sub-query. Santos et al. point out that, unlike implicit methods, xQuAD needs no comparisons between the term vectors of documents.

Properties

  • Explicit. The aspects are part of the input. The paper takes them from the related and suggested queries of three web search engines.
  • Probabilistic mixture. The score mixes relevance and novel coverage with one weight λ\lambda.
  • Greedy on a submodular objective. Each pick has the largest marginal gain of FF, as shown in the proof.
  • The ends of the range. λ=0\lambda = 0 gives the first ranking and λ=1\lambda = 1 gives IA-Select with the sub-queries as intents.
  • Deterministic. The tie rule fixes the output.

Variants and improvements

  • Sub-query importance. Section 4.2.3 of the paper compares a uniform importance (Equation 6) with estimators from the result sets of the sub-queries, including one inspired by the CRCS method of resource selection.
  • Other aspect sources. Sub-queries can come from query logs, click graphs, query expansion, clusters of the first ranking, or a taxonomy. Santos et al. also ran xQuAD with the official sub-topics of TREC, as a best case.
  • Implicit counterpart. MMR uses the largest similarity to the chosen documents in the place of the per-aspect novelty.
  • Without relevance. Setting λ=1\lambda = 1 turns the method into IA-Select, and with coverages of 00 or 11 into weighted greedy max coverage.

When to use it

Use xQuAD when you can produce a few reformulations or sub-topics for an ambiguous query and can score the candidates against each of them, and when you want to keep most of a good first ranking while making room for missed aspects. In the paper, on the 50 queries of the TREC 2009 Web track diversity task, xQuAD with uniform importance improved alpha-nDCG over the BM25, DPH and language model baselines, by up to 30% (BM25, alpha-nDCG@5), and did better than MMR and simulated versions of IA-Select and of the method of Radlinski and Dumais in most settings. These are measurements on one collection, with λ\lambda trained by 5-fold cross validation; the learned value was small, with a peak near λ=0.15\lambda = 0.15 for BM25 and DPH in Figure 2.

Pitfalls

  • The meaning of lambda. In xQuAD, λ\lambda weights diversity; in MMR, it weights relevance. A value tuned for one does not carry over.
  • Probabilities from scores. Equation 5 treats P(d∣q)P(d \mid q) and P(d∣qi)P(d \mid q_i) as probabilities. Raw BM25 scores are not in [0,1][0, 1] and are not comparable across sub-queries; normalize them first, or the products and the mix lose their meaning.
  • Bad sub-queries. A sub-query that is only a paraphrase of the query adds no diversity, and a wrong one promotes off-topic documents. The quality of the aspects limits the method.
  • Coverage of 1. A document with P(d∣qi)=1P(d \mid q_i) = 1 closes the aspect for good: every later document gets nothing from it.
  • Evaluation leakage. Running xQuAD with the official sub-topics of a test collection, as the best case in the paper does, uses the judgments' own aspect structure. Report such runs apart from runs with real sub-queries.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

xquad.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/xquad/. The authors are listed in prov/ZHZBK5.jsonl.
"""xQuAD (Santos, Macdonald and Ounis, 2010).

Given the candidates R of a query q, their relevance P(d | q), the aspects
(sub-queries) q_i of q with importance P(q_i | q), and the coverage
P(d | q_i) of each candidate for each aspect, xQuAD builds the list S by
repeatedly taking the candidate with the largest

    (1 - lam) * P(d | q) + lam * sum over i of P(q_i | q) P(d | q_i) * prod over d_j in S of (1 - P(d_j | q_i)).

The product, the probability that no chosen document covers aspect q_i,
is kept per aspect and updated after each pick. Ties go to the smaller id.
"""


def _check(docs, rel, p_aspect, cover, lam):
    if not 0.0 <= lam <= 1.0:
        raise ValueError("lam must lie in [0, 1]")
    for d in docs:
        if d not in rel:
            raise ValueError("every candidate needs a relevance score")
        for a, v in cover.get(d, {}).items():
            if a not in p_aspect or not 0.0 <= v <= 1.0:
                raise ValueError("P(d | q_i) must lie in [0, 1] for a known aspect")


def xquad_steps(docs, rel, p_aspect, cover, lam, k):
    """Return the list of (document, score at the time it was picked)."""
    _check(docs, rel, p_aspect, cover, lam)
    novel = {a: 1.0 for a in p_aspect}  # @line L1
    rest = sorted(set(docs))
    chosen = []  # @line L2
    while rest and len(chosen) < k:  # @line L3
        best, best_score = None, None
        for d in rest:  # @line L4
            div = sum(p_aspect[a] * v * novel[a] for a, v in cover.get(d, {}).items())
            score = (1.0 - lam) * rel[d] + lam * div  # @line L5
            if best_score is None or score > best_score:  # ties keep the smaller id
                best, best_score = d, score
        chosen.append((best, best_score))  # @line L6
        rest.remove(best)
        for a, v in cover.get(best, {}).items():  # @line L7
            novel[a] *= 1.0 - v  # @line L8
    return chosen  # @line L9


def xquad(docs, rel, p_aspect, cover, lam, k):
    """Return the documents that xQuAD picks, in order."""
    return [d for d, _ in xquad_steps(docs, rel, p_aspect, cover, lam, k)]
test_xquad.py 115 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/xquad/. The authors are listed in prov/ZHZBK5.jsonl.

import math
import random

import pytest

from xquad import xquad, xquad_steps

TOL = 1e-12

# The example of the page: "jaguar" with a car aspect and an animal aspect.
REL = {1: 0.9, 2: 0.8, 3: 0.6, 4: 0.5}
ASPECTS = {"car": 0.6, "animal": 0.4}
COVER = {1: {"car": 0.9}, 2: {"car": 0.8}, 3: {"animal": 0.7}, 4: {"car": 0.3, "animal": 0.3}}


def direct_steps(docs, rel, p_aspect, cover, lam, k):
    """Equation 5 of Santos et al., with the product over S recomputed from scratch."""
    chosen = []
    rest = sorted(docs)
    while rest and len(chosen) < k:
        scores = {}
        for d in rest:
            div = 0.0
            for a, p in p_aspect.items():
                miss = 1.0
                for s, _ in chosen:
                    miss *= 1.0 - cover.get(s, {}).get(a, 0.0)
                div += p * cover.get(d, {}).get(a, 0.0) * miss
            scores[d] = (1.0 - lam) * rel[d] + lam * div
        top = max(scores.values())
        best = min(d for d in rest if scores[d] == top)
        chosen.append((best, scores[best]))
        rest.remove(best)
    return chosen


def coverage(selected, p_aspect, cover):
    total = 0.0
    for a, p in p_aspect.items():
        miss = 1.0
        for d in selected:
            miss *= 1.0 - cover.get(d, {}).get(a, 0.0)
        total += p * (1.0 - miss)
    return total


def random_instance(rng, grid):
    n = rng.randint(1, 10)
    m = rng.randint(1, 4)
    pick = (lambda: rng.choice([0.0, 0.25, 0.5, 0.75, 1.0])) if grid else rng.random
    weights = [rng.random() + 0.01 for _ in range(m)]
    p_aspect = {f"a{i}": w / sum(weights) for i, w in enumerate(weights)}
    rel = {d: pick() for d in range(1, n + 1)}
    cover = {}
    for d in rel:
        cover[d] = {a: pick() for a in p_aspect if rng.random() < 0.6}
    lam = rng.choice([0.0, 0.25, 0.5, 0.75, 1.0]) if grid else rng.random()
    return list(rel), rel, p_aspect, cover, lam


def same_steps(a, b):
    assert [d for d, _ in a] == [d for d, _ in b]
    for (_, x), (_, y) in zip(a, b):
        assert math.isclose(x, y, rel_tol=1e-9, abs_tol=TOL)


def test_example_lambda_zero_is_relevance_order():
    assert xquad(list(REL), REL, ASPECTS, COVER, 0.0, 4) == [1, 2, 3, 4]


def test_example_lambda_half():
    steps = xquad_steps(list(REL), REL, ASPECTS, COVER, 0.5, 4)
    assert [d for d, _ in steps] == [1, 3, 2, 4]
    for (_, got), want in zip(steps, [0.72, 0.44, 0.424, 0.2698]):
        assert math.isclose(got, want, rel_tol=1e-9)


def test_example_lambda_one():
    steps = xquad_steps(list(REL), REL, ASPECTS, COVER, 1.0, 4)
    assert [d for d, _ in steps] == [1, 3, 4, 2]
    for (_, got), want in zip(steps, [0.54, 0.28, 0.054, 0.0336]):
        assert math.isclose(got, want, rel_tol=1e-9)


@pytest.mark.parametrize("seed", range(200))
def test_matches_direct_formula_step_by_step(seed):
    rng = random.Random(seed)
    docs, rel, p_aspect, cover, lam = random_instance(rng, grid=seed % 2 == 0)
    k = rng.randint(0, len(docs) + 1)
    same_steps(xquad_steps(docs, rel, p_aspect, cover, lam, k), direct_steps(docs, rel, p_aspect, cover, lam, k))


@pytest.mark.parametrize("seed", range(50))
def test_lambda_one_is_greedy_on_coverage(seed):
    rng = random.Random(500 + seed)
    docs, rel, p_aspect, cover, _ = random_instance(rng, grid=False)
    prefix = []
    for d, score in xquad_steps(docs, rel, p_aspect, cover, 1.0, len(docs)):
        before = coverage(prefix, p_aspect, cover)
        gains = {x: coverage(prefix + [x], p_aspect, cover) - before for x in docs if x not in prefix}
        assert math.isclose(score, gains[d], rel_tol=1e-9, abs_tol=TOL)
        assert all(g <= score + TOL for g in gains.values())
        prefix.append(d)


def test_rejects_bad_input():
    with pytest.raises(ValueError):
        xquad([1], {1: 0.5}, {"a": 1.0}, {1: {"a": 0.5}}, 1.5, 1)
    with pytest.raises(ValueError):
        xquad([1], {1: 0.5}, {"a": 1.0}, {1: {"b": 0.5}}, 0.5, 1)
    with pytest.raises(ValueError):
        xquad([1], {}, {"a": 1.0}, {}, 0.5, 1)

History

Santos, Macdonald and Ounis presented xQuAD at WWW 2010 as part of their work at the University of Glasgow on the diversity task of the TREC 2009 Web track. They framed it as a probabilistic version of the greedy approximation used by most earlier methods, with explicit sub-queries in the place of comparisons between documents, and they built on the coverage idea of Agrawal et al. from 2009. xQuAD became one of the standard explicit diversification baselines, together with IA-Select.

References

  1. Rodrygo L. T. Santos, Craig Macdonald and Iadh Ounis, Exploiting query reformulations for web search result diversification, Proceedings of the 19th International Conference on World Wide Web (WWW 2010), 881-890, Section 2 (implicit and explicit diversification, and NP-hardness by reduction from maximum coverage), Section 4.1 (Algorithm 1 and Equations 1 to 5), Section 4.2 (estimation of relevance, coverage, novelty, sub-queries and their importance, with the uniform estimator of Equation 6), Section 5 (the TREC 2009 Web track setup, the baselines and the 5-fold training of lambda), Section 6 (Table 2 and Figure 2)
  2. Rakesh Agrawal, Sreenivas Gollapudi, Alan Halverson and Samuel Ieong, Diversifying search results, Proceedings of the Second ACM International Conference on Web Search and Data Mining (WSDM 2009), 5-14, Section 3 (Equation 1, the coverage objective that the diversity term of xQuAD shares, and Algorithm 1, IA-Select)
  3. Jaime Carbonell and Jade Goldstein, The use of MMR, diversity-based reranking for reordering documents and producing summaries, Proceedings of the 21st Annual International ACM SIGIR Conference (SIGIR 1998), 335-336, the MMR formula, the implicit method that xQuAD is compared with

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example