Algorithmadvanced
Team-draft interleaving
Also called team draft interleave.
Lets two rankers pick documents in turns like team captains, with a coin flip for the first pick of each round, and credits each click to the team that picked the document.
- Time
- O(n) worst
- Space
- O(n) extra
- Where
- n = the sum of the lengths of the two rankings
- Model
- Word RAM
- Properties
- randomizediterative
- Evidence
- Proof sketchCode in 1 languages
Team-draft interleaving is an interleaved evaluation method that builds the shown list the way two captains pick players for a team: in each round a fair coin decides which ranking picks first, and then each ranking adds its highest-ranked document that is not yet shown. Each shown document belongs to the team of the ranking that picked it, and a click counts for that team only. Radlinski, Kurup and Joachims introduced it in 2008 to remove the bias of balanced interleaving: when both rankings hold the same documents, each position goes to each team with probability 1/2, so clicks that ignore the documents give a tie in expectation.
In plain words
Think of two team captains who choose players from a line. In each round a coin decides who chooses first. Each captain takes the best player who is still free, by the captain's own order of preference. The players go into one line in the order they were chosen, and each player wears the shirt of the captain who chose them.
Here the captains are two search engines, the players are results, and the line is the result page the user sees. When the user clicks a result, the engine whose shirt that result wears gets one point. The engine with more points wins this query. If both engines put the same result first, only one of them can take it, and the other takes its next result.
The problem
The input is two rankings and for one query, each with no repeated document, and a source of fair coins. The output is a shown list with no repeated document and a split of its documents into two teams and . After the user clicks the set of documents of , the output is a preference , where 1 means that A is preferred. Over many queries, a sign test on the queries with a preference decides if one ranking wins more often than chance.
The algorithm
TEAM-DRAFT-INTERLEAVE(A, B)- 1
I ← empty list, TA ← ∅, TB ← ∅ - 2
while some A[i] ∉ I and some B[j] ∉ I do - 3
if |TA| < |TB| or (|TA| = |TB| and coin() = 1) then - 4
d ← the first document of A that is not in I - 5
append d to I, TA ← TA ∪ {d} - 6
else - 7
d ← the first document of B that is not in I - 8
append d to I, TB ← TB ∪ {d} - 9
return I, TA, TB CREDIT(TA, TB, C) ▷ C: the clicked documents of I- 10
ha ← |C ∩ TA|, hb ← |C ∩ TB| - 11
return 1 if ha > hb, -1 if ha < hb, else 0
The coin in line L3 is drawn only when the teams have equal size, that is, at the start of each round of two picks. The second pick of a round goes to the smaller team. This is Algorithm 2 of Radlinski, Kurup and Joachims, and lines L10 and L11 are their equations (4) and (5). To find the first document not in in constant amortized time, keep one pointer per ranking and move it past documents that are already shown.
Example
Take the rankings of the bias example of balanced interleaving: and . Let the coin of round 1 be 1 and the coin of round 2 be 0.
| pick | round | coin | sizes before | team | first document not in I | I |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 0, 0 | A | a | a |
| 2 | 1 | 1, 0 | B | b | a, b | |
| 3 | 2 | 0 | 1, 1 | B | c | a, b, c |
| 4 | 2 | 1, 2 | A | d (a is shown) | a, b, c, d |
Now every document is shown and the loop stops, with and . The four coin results give:
| coins | I | TA | TB | one click on a, b, c, d |
|---|---|---|---|---|
| 1, 1 | a, b, c, d | a, c | b, d | A, B, A, B |
| 1, 0 | a, b, c, d | a, d | b, c | A, B, B, A |
| 0, 1 | b, a, c, d | a, c | b, d | B, A, A, B |
| 0, 0 | b, a, c, d | a, d | b, c | B, A, B, A |
The shown lists are the same two as in balanced interleaving, but in each row the four positions split two and two between the teams. So one click on a random position is a tie in expectation, while balanced interleaving gives B a win with probability .
The figure of Radlinski, Kurup and Joachims uses and . If A wins every coin, with and . A user who clicks b and e gives and , a preference for B.
Correctness
The proof is in proofs/correctness.md. Before each test of line L2, has no repeated document, the teams are disjoint and together hold the documents of , the team sizes differ by at most 1, and they are equal after an even number of picks. So positions and always hold one document of each team. When and order the same set of documents, the coin of round gives position to A with probability and position to the other team. Flipping all coins swaps the teams at every position, so for any click model that depends only on positions, the outcomes under a coin vector and under its flip are negatives of each other, and the expected outcome is 0.
Proof: Team-draft interleaving builds teams that alternate in pairs and is fair under clicks that ignore the documentsfull
Let and be rankings with no repeated document. Lines refer to the pseudocode on the page. A pick is one run of lines L4 and L5 or of lines L7 and L8.
Invariant. Before each test of line L2:
(J1) has no repeated document, , and is the set of documents of .
(J2) , and it is 0 after an even number of picks.
(J3) Each document was, at the pick that added it, the first document of not yet in ; the same holds for and .
Initialization. All three sets are empty and no pick was made.
Maintenance. The body runs only when and each have a document not in , so lines L4 and L7 are defined. The picked document is not in , so (J1) still holds after it is appended and added to one team, and (J3) holds by the choice in line L4 or L7. For (J2): if the teams have equal size, either team grows and the difference becomes ; if one team is smaller, line L3 gives the pick to the smaller team (to A when , and to B otherwise, since then ), and the difference becomes 0. So the difference is 0 after picks 0, 2, 4, and so on, and after an odd number of picks.
Termination. Each pick adds a new document of to , so there are at most picks.
Claim 1 (pairs). For each with , one of and is in and the other is in .
Proof. By (J2) the teams have equal size before pick . That pick goes to one team, and by (J2) after it the other team is smaller by 1, so line L3 gives pick to the other team.
The coin is called only when the teams have equal size, that is, once at the start of each pair of picks. Call these calls the coins of rounds The coin of round decides the team of position : A for 1 and B for 0, and position , if it exists, goes to the other team.
Claim 2 (fair coin per position). Let and be orderings of the same set of documents, and let the coins be independent fair bits. Then , and for each position , .
Proof. As both rankings hold the same set, has a document not in exactly when has one, exactly when . So the loop runs until , and . Position is in exactly when the coin of round is 1, which has probability . Position is in exactly when that coin is 0, also with probability .
Claim 3 (no preference under random clicks). Under the hypothesis of Claim 2, let the user click a set of positions drawn from any distribution that does not depend on the shown documents or on the coins. Then the expected outcome of CREDIT is 0.
Proof. Let be the number of rounds, and let flip all coins. By Claim 2's proof, the set of positions in under coins is , and is the set of the other positions, . The documents at these positions can differ between and , but the clicks depend only on positions. So for a fixed , and , and the outcome under is the negative of the outcome under . Since is a bijection on and all coin vectors have probability , the sum of the outcomes over all coin vectors is 0 for each , and so is its expectation over .
Claim 4 (insensitivity). There are rankings and and a click model where clicks go only to relevant documents, ranks the only relevant document higher than , and the expected outcome is still 0.
Proof. Take and with only relevant. In round 1 one ranking picks its first document and the other picks its first document not yet shown, so positions 1 and 2 hold and in some order. In round 2 the only document left is , and it goes to the team that the coin of round 2 picks. So is always at position 3, in with probability . A click model that clicks only therefore gives each ranking the win with probability times the click probability at position 3.
Cost. The pointers of and only move forward past documents already in , so all the scans together take steps, and each pick costs expected time with a hash set. CREDIT reads the clicks once.
Complexity
- Time. Each pick appends one new document, so there are at most picks. The two pointers only move forward, so all the scans in lines L4 and L7 take steps in total. With a hash set of shown documents, the whole interleaving costs time, and CREDIT costs .
- Space. , the teams and the hash set hold documents.
- Coins. One coin per round, so at most coins.
Properties
- Pairs of positions. Positions and hold one document of each team.
- Fair under random clicks. For two orderings of the same documents, each position belongs to each team with probability exactly , and clicks that depend only on positions give an expected outcome of 0. This is the property Radlinski, Kurup and Joachims state for the method.
- Insensitive to some differences. Let and , with only relevant. ranks higher, but is shown at position 3 for every coin result and goes to each team with probability , so the expected outcome is 0. Hofmann, Whiteson and de Rijke give this kind of example with four documents.
- A dominating ranking does not lose here. Call A dominant over B if, at every depth, the top of A holds at least as many relevant documents as the top of B, and more at some depth. Let the user click shown position with probability for a relevant document, and or 0 for a document that is not relevant. For all pairs of rankings of at most 4 documents, the expected outcome is then at least 0 when A dominates B. It can be exactly 0, as in the example above. The sources give no proof of this for longer lists. Under the same click model, balanced interleaving can prefer the dominated ranking.
Variants and improvements
- Balanced interleaving. The older method, with the same kind of shown list but credit by the top of each ranking. See balanced interleaving.
- Probabilistic interleaving. Replace the deterministic pick by a draw from a softmax over each ranking, which gives credit to rank differences that team draft cannot see. See probabilistic interleaving.
- More than two rankings. The draft extends to several captains, who pick in a random order in each round. This is the basis of multileaved comparisons.
When to use it
Team draft is the default choice for online comparison of two rankers that differ by small changes, because random clicks cannot bias it and the code is short. It cannot see a difference when the relevant document lands at the same shown position whichever team picks it, as in the insensitivity example, so for very close rankers, or to reuse logged clicks for a new pair of rankers, use probabilistic interleaving.
Pitfalls
- A coin that is not fresh. Line L3 must draw a new fair coin in each round. If the coin is fixed, for example by a random generator that is seeded again with the same value for every query, A always picks first, its documents sit at positions 1, 3, 5 and so on, and position bias alone makes A win. In the example, coins 1, 1 on every query give A the clicks on positions 1 and 3.
- Credit by rank in the input ranking. Line L10 must count clicks by team. Counting a click for both rankings when the document appears in both top parts is the credit of balanced interleaving and brings back its bias.
- Skipping the shown test. Lines L4 and L7 must take the first document not yet in . Taking A's next document by rank only puts the same document on the page twice when both rankings share it.
- Rankings with different documents. The fairness claim needs both rankings to cover the same documents up to the page length. If one ranking is much shorter, the loop in line L2 stops when it is used up, and the last positions can go to one team.
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.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/team-draft-interleaving/. The authors are listed in prov/8FBQBW.jsonl.
from typing import Callable, Collection, Hashable, Sequence
def interleave(
a: Sequence[Hashable], b: Sequence[Hashable], coin: Callable[[], int]
) -> tuple[list[Hashable], set[Hashable], set[Hashable]]:
"""Return the shown list and the teams of a and b for team-draft interleaving.
Precondition: a and b have no repeated documents. coin() returns 0 or 1 and is
called only when the two teams have the same size, once per round. The pointers
ia and ib only move forward, so the time is O(len(a) + len(b)) with hash sets,
and the extra space is O(len(a) + len(b)).
"""
shown: list[Hashable] = []
seen: set[Hashable] = set()
team_a: set[Hashable] = set()
team_b: set[Hashable] = set() # @line L1
ia, ib = 0, 0
while True:
while ia < len(a) and a[ia] in seen:
ia += 1
while ib < len(b) and b[ib] in seen:
ib += 1
if ia == len(a) or ib == len(b): # @line L2
break
if len(team_a) < len(team_b) or (len(team_a) == len(team_b) and coin() == 1): # @line L3
d = a[ia] # @line L4
shown.append(d) # @line L5
seen.add(d)
team_a.add(d)
else: # @line L6
d = b[ib] # @line L7
shown.append(d) # @line L8
seen.add(d)
team_b.add(d)
return shown, team_a, team_b # @line L9
def credit(team_a: Collection[Hashable], team_b: Collection[Hashable], clicked: Collection[Hashable]) -> int:
"""Return 1 if the clicks prefer a, -1 if they prefer b, and 0 for a tie.
Precondition: team_a and team_b are disjoint sets. O(len(clicked)) time.
"""
ha = sum(1 for d in clicked if d in team_a) # @line L10
hb = sum(1 for d in clicked if d in team_b)
return (ha > hb) - (ha < hb) # @line L11
test_team_draft_interleaving.py 189 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/team-draft-interleaving/. The authors are listed in prov/8FBQBW.jsonl.
import itertools
import random
from fractions import Fraction
from team_draft_interleaving import credit, interleave
def coins_from(bits):
it = iter(bits)
return lambda: next(it)
def outcomes(a, b):
"""All results of the coin, as (probability, shown, team_a, team_b), by enumeration."""
n = len(set(a) | set(b))
rounds = (n + 1) // 2 + 1
dist = {}
for bits in itertools.product((0, 1), repeat=rounds):
shown, ta, tb = interleave(a, b, coins_from(bits))
key = (tuple(shown), frozenset(ta))
p, _, _ = dist.get(key, (Fraction(0), ta, tb))
dist[key] = (p + Fraction(1, 2**rounds), ta, tb)
return [(p, list(key[0]), ta, tb) for key, (p, ta, tb) in dist.items()]
def oracle(a, b, bits):
"""Round by round: a coin picks the captain who chooses first in the round."""
shown, ta, tb = [], set(), set()
k = 0
def pick(lst, team):
d = next(x for x in lst if x not in shown)
shown.append(d)
team.add(d)
def both_left():
return any(x not in shown for x in a) and any(x not in shown for x in b)
while both_left():
first = bits[k]
k += 1
order = [(a, ta), (b, tb)] if first == 1 else [(b, tb), (a, ta)]
pick(*order[0])
if both_left():
pick(*order[1])
return shown, ta, tb
def click_sets(shown, prob):
for pat in itertools.product((0, 1), repeat=len(shown)):
p = Fraction(1)
for i, bit in enumerate(pat):
q = prob(i, shown[i])
p *= q if bit else 1 - q
if p:
yield {shown[i] for i in range(len(shown)) if pat[i]}, p
def expected_outcome(a, b, prob):
total = Fraction(0)
for q, shown, ta, tb in outcomes(a, b):
for clicked, p in click_sets(shown, prob):
total += q * p * credit(ta, tb, clicked)
return total
def pbm(rel, eps):
return lambda i, d: Fraction(1, i + 1) * (1 if rel[d] else eps)
def dominates(a, b, rel):
ca = cb = 0
strict = False
for x, y in zip(a, b):
ca += rel[x]
cb += rel[y]
if ca < cb:
return False
strict = strict or ca > cb
return strict
def test_paper_example():
a = list("abcdgh")
b = list("beafgh")
shown, ta, tb = interleave(a, b, coins_from([1, 1, 1, 1]))
assert shown == list("abcedfgh")
assert ta == set("acdg") and tb == set("befh")
assert credit(ta, tb, {"b", "e"}) == -1
def test_trace_on_page():
a = list("abcd")
b = list("bcda")
shown, ta, tb = interleave(a, b, coins_from([1, 0]))
assert shown == list("abcd")
assert ta == {"a", "d"} and tb == {"b", "c"}
wins = Fraction(0)
for q, shown, ta, tb in outcomes(a, b):
assert [credit(ta, tb, {d}) for d in shown].count(1) == 2
wins += q * sum(credit(ta, tb, {d}) for d in shown)
assert wins == 0
def test_edge_cases():
assert interleave([], ["x"], coins_from([])) == ([], set(), set())
assert interleave(["x"], ["x"], coins_from([0])) == (["x"], set(), {"x"})
assert interleave(["x"], ["y", "z"], coins_from([1])) == (["x"], {"x"}, set())
shown, ta, tb = interleave(["x"], ["y", "z"], coins_from([0]))
assert shown == ["y", "x"] and ta == {"x"} and tb == {"y"}
assert credit({"x"}, {"y"}, set()) == 0
def test_pairs_of_positions_and_coin_symmetry_up_to_five():
"""Positions 2m-1 and 2m hold one document of each team, and over the coins each
position goes to a with probability exactly 1/2."""
for n in range(1, 6):
perms = list(itertools.permutations(range(n)))
for a in perms:
for b in perms:
share = [Fraction(0)] * n
for q, shown, ta, tb in outcomes(a, b):
assert len(shown) == n
for m in range(0, n - 1, 2):
assert (shown[m] in ta) != (shown[m + 1] in ta)
for i, d in enumerate(shown):
if d in ta:
share[i] += q
assert all(s == Fraction(1, 2) for s in share)
def test_random_clicks_give_a_tie_in_expectation():
"""Any click model on positions that ignores the documents gives expected outcome 0."""
for n in range(1, 5):
perms = list(itertools.permutations(range(n)))
for a in perms:
for b in perms:
dist = outcomes(a, b)
for pat in itertools.product((0, 1), repeat=n):
e = Fraction(0)
for q, shown, ta, tb in dist:
e += q * credit(ta, tb, {shown[i] for i in range(n) if pat[i]})
assert e == 0
def test_dominating_ranking_never_loses():
for n in range(1, 5):
docs = list(range(n))
for rels in itertools.product((0, 1), repeat=n):
rel = dict(zip(docs, rels))
for a in itertools.permutations(docs):
for b in itertools.permutations(docs):
if dominates(a, b, rel):
for eps in ((0, Fraction(1, 5)) if n <= 3 else (0,)):
assert expected_outcome(list(a), list(b), pbm(rel, eps)) >= 0
def test_insensitive_case():
"""Hofmann, Whiteson and de Rijke: only d3 is relevant, l2 ranks it higher, yet a tie."""
l1, l2 = ["d1", "d2", "d3"], ["d2", "d3", "d1"]
rel = {"d1": 0, "d2": 0, "d3": 1}
assert dominates(l2, l1, rel)
assert expected_outcome(l2, l1, pbm(rel, 0)) == 0
assert expected_outcome(l2, l1, pbm(rel, Fraction(1, 5))) == 0
def test_clear_winner():
a = list("abcd")
b = list("dcba")
rel = {"a": 1, "b": 1, "c": 0, "d": 0}
assert expected_outcome(a, b, pbm(rel, 0)) > 0
def test_random_against_oracle():
rng = random.Random(9)
for _ in range(1000):
universe = list(range(rng.randint(1, 8)))
a = rng.sample(universe, rng.randint(0, len(universe)))
b = rng.sample(universe, rng.randint(0, len(universe)))
bits = [rng.randint(0, 1) for _ in range(10)]
shown, ta, tb = interleave(a, b, coins_from(bits))
assert (shown, ta, tb) == oracle(a, b, bits)
assert ta | tb == set(shown) and not ta & tb
clicked = set(rng.sample(shown, rng.randint(0, len(shown))))
h = len(clicked & ta) - len(clicked & tb)
assert credit(ta, tb, clicked) == (h > 0) - (h < 0)
History
Radlinski, Kurup and Joachims introduced team-draft interleaving at CIKM 2008, next to balanced interleaving, after showing that the older method is biased when the two rankings are close. In their study on the arXiv.org search engine, both interleaving methods ordered rankings of known quality correctly, while none of eight absolute click metrics did so reliably. Hofmann, Whiteson and de Rijke (CIKM 2011) pointed out that team draft cannot detect some ranking differences, and proposed probabilistic interleaving. Chapelle, Joachims, Radlinski and Yue (2012) ran team draft and balanced interleaving at scale on commercial search engines and measured their agreement with offline judgments and with each other.
References
- Filip Radlinski, Madhu Kurup and Thorsten Joachims, How does clickthrough data reflect retrieval quality?, CIKM 2008, pp. 43-52, Section 5.2, Algorithm 2 and equations (4) and (5) (team-draft interleaving and its credit rule, and the remark that random clicks give equal preferences in expectation); the user study on arXiv.org
- Katja Hofmann, Shimon Whiteson and Maarten de Rijke, A probabilistic method for inferring preferences from clicks, CIKM 2011, pp. 249-258, Section 3.2 and Figure 2 (team draft fails to detect a ranking difference when the relevant document always lands at the same rank)
- Olivier Chapelle, Thorsten Joachims, Filip Radlinski and Yisong Yue, Large-scale validation and analysis of interleaved search evaluation, ACM Transactions on Information Systems 30(1), Article 6, 2012, the large-scale comparison of team-draft and balanced interleaving on commercial search engines and their agreement with offline judgments
Where this leads
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 8FBQBW Edit this page History Report an error Markdown