Algorithmadvanced
RankBoost
Also called Freund-Iyer-Schapire-Singer RankBoost.
Boosts weak rankers by reweighting the misordered document pairs at each round, minimizing an exponential pairwise loss.
- Solves
- Learning to rank
- Time
- O(T (P + d N log N)) totalO(P + d N log N) per_round
- Space
- O(P + N) extra
- Where
- N = the number of documents; P = the number of crucial pairs; T = the number of rounds; d = the number of features
- Model
- Word RAM
- Properties
- deterministicnot exactofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Yoav Freund, Raj Iyer, Robert Schapire, Yoram Singer, 2003
RankBoost learns a ranking function for learning to rank by boosting on pairs. It keeps a distribution over the crucial pairs, the pairs where should rank above . Each round picks a weak ranker that orders the heavy pairs well, gives it a weight , and multiplies the weight of each pair by , so pairs that misorders get heavier. The AdaBoost page does this for labeled examples; here the examples are pairs and a weak ranker is any real function, so the analysis bounds the fraction of misordered pairs, not a classification error. The learning-to-rank page lists RankBoost among the pairwise methods; this page gives the algorithm, the bound , the choice of , the threshold weak learner, and a traced run.
In plain words
We have pairs of results where we know which one should come first. We build the scoring rule from many very simple rules, each looking at one feature and asking whether it is above a cut. Every pair has a weight, and at the start all weights are equal. In each round we choose the simple rule that gets the most weight of pairs right, and we decide how much to trust it. Then we make the pairs it got wrong heavier and the pairs it got right lighter, so the next rule focuses on the hard pairs. The final score adds the simple rules with their trust values.
The problem
The input is a set of queries with documents and labels. The crucial pairs are the pairs of one query with label of greater than label of ; there are of them, with the initial distribution uniform. A weak ranker is a function ; the code uses the thresholds . The ranking loss of a scorer is the weighted fraction of crucial pairs that it does not order strictly right:
A tie counts as an error. RankBoost minimizes the exponential upper bound of this loss greedily, one weak ranker at a time.
The algorithm
For a weak ranker under the distribution , let .
RANKBOOST(pairs, T)- 1
D_1(p) ← 1 / P for each crucial pair p - 2
for t ← 1 to T: - 3
h_t ← the threshold ranker [x_f > θ] with the largest |r_t| under D_t - 4
α_t ← (1/2) ln((1 + r_t) / (1 - r_t)) - 5
Z_t ← Σ_p D_t(p) e^{α_t (h_t(x_0) - h_t(x_1))} - 6
D_{t+1}(p) ← D_t(p) e^{α_t (h_t(x_0) - h_t(x_1))} / Z_t - 7
return H = Σ_t α_t h_t
Line L4 is method 3 of Freund et al. (2003), Section 3.2, which minimizes an upper bound on and is the choice used in their experiments. Line L3 maximizes , which minimizes that bound. A negative gives a negative , which reverses the weak ranker.
Example
One query has four documents with features , , , and labels 0, 1, 2, 1. The crucial pairs say that documents 1, 2 and 3 are above document 0, and document 2 is above documents 1 and 3: five pairs, each with weight at the start.
| round | weak ranker | rloss after the round | |||||
|---|---|---|---|---|---|---|---|
| 1 | 0.6 | 0.6931 | 0.7000 | 0.8000 | 0.7000 | 0.4 | |
| 2 | 0.7143 | 0.8959 | 0.5773 | 0.6999 | 0.4041 | 0.0 | |
| 3 | 0.5959 | 0.6868 | 0.7039 | 0.8030 | 0.2845 | 0.0 |
In round 1, three thresholds tie at : , and ; the code takes the first in the order of feature and threshold. orders the three pairs above document 0 and ties on the two pairs above documents 1 and 3, so , and the two tied pairs are errors, . Their weights grow from to , and the other three fall to . Round 2 picks , which puts document 2 on top, with . After it, every pair is ordered right. At each round, is below and the ranking loss is below , as the proof requires.
Correctness
The proof in proofs/correctness.md shows:
- The bound. Unrolling line L6 gives . Since sums to 1 and , . This is the theorem of Freund et al. (2003), Section 3, and it holds after every round, for any choice of .
- The choice of . For with values in , convexity of gives , minimized by the of line L4 at the value . So .
- Exact minimum for 0-1 rankers. For , is minimized at , method 2 of the paper. It can give a smaller than line L4.
- The weak learner. Line L3 tries the midpoint between each two consecutive distinct values of each feature, which covers every distinct threshold ranker.
All of this is about the training pairs. How well orders the pairs of new queries is measured, not proved here.
Proof: The RankBoost ranking loss is at most the product of the normalizers, and each normalizer is at most the square root of 1 minus r squaredfull
The input is a list of crucial pairs , each saying that must rank above , and a distribution over them; the code uses the uniform . Round picks a weak ranker and a real , and sets
The final ranker is , and its ranking loss is , so a tie counts as an error. This is the RankBoost of Freund et al. (2003), Figure 1.
Theorem 1 (the bound). .
Proof. Unroll the update. By induction on ,
sums to 1, so . Each term is at least , because for and always.
The same argument holds for every prefix , so the bound holds after each round. It is the theorem of Freund et al. (2003), Section 3.
Lemma 2 (method 3). Let take values in and let , so . For every ,
and for the right side is smallest at , where it equals . So with this , .
Proof. Put . By convexity of , . Take the -weighted sum and use . The right side with and is smallest at , with the value .
Line L4 of the code uses this , and line L5 computes exactly. Since , Theorem 1 gives : if every round has , the ranking loss on the training pairs falls at least as fast as .
Lemma 3 (method 2 for rankers). For with values in , let for . Then , and when its minimum is at , with the value . Also .
Proof. The value of depends only on ; group the pairs by . The minimum of is at . For , .
Method 2 gives the exact minimum of for a ranker, and method 3 a bound on it. The code uses method 3, with from , as do the experiments of the paper. For the same ranker, method 2 can give a smaller than method 3, since always.
Lemma 4 (the weak learner). Line L3 returns a threshold ranker with the largest over all features and all real .
Proof. For a fixed , depends on only through the set of values of above . Below the smallest value or at or above the largest, is constant and . Between two consecutive distinct values, every gives the same , and the code tries the midpoint of each such interval. So it tries every distinct nonconstant threshold ranker, and keeps the one of largest .
Maximizing minimizes the bound of Lemma 2, which is the choice of Freund et al. (2003), Section 4. A negative gives a negative , which reverses the ranker.
Remark (clipping). If , the chosen ranker orders every pair of positive weight the right way (or every pair the wrong way), and would be infinite. The code clips to , which gives and . Theorem 1 does not depend on how is chosen, so it still holds.
What is not proved. Theorem 1 bounds the loss on the training pairs. The paper also studies generalization under assumptions on how the data are drawn; that analysis is not used here, and the quality on new queries is a matter of measurement.
The tests in impl/python/test_rankboost.py check the choice of each weak ranker against a brute force over all features and all observed values, the formula for , against a direct sum and against , , Theorem 1 after every round on random data, Lemma 3 by a grid search over , the clipping, the rule that ties are errors, and the worked example of the page.
Complexity
Write for each document, the weight of pairs where should be above minus the weight where it should be below. Then , so after computing in , all thresholds of one feature are scored in one pass over the documents sorted by that feature. With the sorted orders kept from the start, a round costs ; sorting anew costs . Lines L5 and L6 cost . The space is . The code in this repository computes directly for each candidate, which is per round and is meant only as a reference. Predicting a document costs .
Properties
- The bound needs no assumption. holds for any weak rankers and any .
- Fast training loss decay. If every round has , the training ranking loss is at most .
- Scale free features. Threshold rankers depend only on the order of feature values, so monotone transforms of a feature change nothing.
- Exponential weights. Pairs that stay misordered gain weight exponentially, so label noise can dominate later rounds, as in AdaBoost.
- Equal weight per pair. The loss knows nothing about positions in the list, like the Ranking SVM and RankNet.
Variants and improvements
- RankBoost.B. For bipartite feedback, relevant against irrelevant, Freund et al. (2003), Section 3.3, keep one weight per document instead of one per pair, which reduces the cost from to per round. The ranking loss is then the fraction of relevant-irrelevant pairs in the wrong order, which is one minus the area under the ROC curve when ties count as errors.
- Missing features. The weak rankers of Freund et al. (2003), Section 4, give a default value to documents for which a feature is missing.
- Method 2. For 0-1 rankers, choose by the exact minimum of and the weak ranker by the smallest such .
- Combining rankings. Each feature can be the score of another ranker, which makes RankBoost a learned form of rank aggregation, the setting the paper started from.
- Boosted trees on pairwise losses. LambdaMART also boosts, with regression trees and a position-aware gradient in place of exponential weights.
When to use it
Use RankBoost when the features are scores or votes from several sources, the labels give preferences rather than grades, and a simple, interpretable additive model of thresholds is wanted. Its training bound is clean and needs no tuning of a step size. When the metric weights the top of the list or the data are large, LambdaMART is the usual choice today, which is a measured judgment.
Pitfalls
- Ties are errors. The loss of line L7 counts as wrong. Counting ties as right, as some evaluations do, makes the bound look loose and hides weak rankers that do not separate pairs.
- A perfect weak ranker. If , line L4 divides by zero. Clip slightly below 1; the code uses , which gives .
- Pairs across queries. The crucial pairs of line L1 must come from one query, or the model learns to order queries.
- Normalizing. Line L6 must divide by . Without it, the weights shrink or grow exponentially and the of later rounds is not on the scale that line L4 assumes.
- Overfitting with many rounds. The training loss can fall to 0 quickly, as in the example after two rounds. Choose by validation.
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/rankboost/. The authors are listed in prov/FQ86DQ.jsonl.
"""RankBoost with threshold weak rankers (Freund, Iyer, Schapire, Singer 2003).
A crucial pair (x0, x1) says that x1 must rank above x0. The distribution D
over the pairs starts uniform. In each round the weak ranker is
h(x) = 1 if x[f] > theta else 0, chosen to maximize |r| with
r = sum over pairs of D(x0, x1) (h(x1) - h(x0)),
and its weight is alpha = (1/2) ln((1 + r) / (1 - r)). The update is
D'(x0, x1) = D(x0, x1) exp(alpha (h(x0) - h(x1))) / Z.
The final ranker is H(x) = sum of alpha_t h_t(x). The ranking loss counts a
pair as an error if H(x0) >= H(x1), so ties are errors. Theorem 1 of the
paper gives rloss(H) <= prod Z_t. If |r| = 1 the weight would be infinite;
the code clips r to 1 - 1e-10 in absolute value.
"""
import math
CLIP = 1.0 - 1e-10
def crucial_pairs(queries):
"""(x0, x1) for every pair of one query with y(x1) > y(x0)."""
out = []
for xs, y in queries:
for i in range(len(xs)):
for j in range(len(xs)):
if y[i] > y[j]:
out.append((xs[j], xs[i]))
return out
def weak(f, theta):
return lambda x: 1.0 if x[f] > theta else 0.0
def candidates(pts):
"""All (f, theta) with theta a midpoint of two consecutive distinct values of x[f]."""
out = []
for f in range(len(pts[0][0])):
vals = sorted(set(x[f] for p in pts for x in p))
out += [(f, (a + b) / 2.0) for a, b in zip(vals, vals[1:])]
return out
def r_value(h, pts, dist):
return sum(d * (h(x1) - h(x0)) for d, (x0, x1) in zip(dist, pts))
def best_weak_ranker(pts, dist):
"""The (f, theta, r) with the largest |r|; ties keep the first candidate."""
best = None
for f, theta in candidates(pts):
r = r_value(weak(f, theta), pts, dist)
if best is None or abs(r) > abs(best[2]) + 1e-15:
best = (f, theta, r)
return best
def alpha_of(r):
r = max(-CLIP, min(CLIP, r))
return 0.5 * math.log((1.0 + r) / (1.0 - r))
def fit(queries, rounds):
"""Returns the list of (alpha, f, theta) and the list of Z_t."""
pts = crucial_pairs(queries)
dist = [1.0 / len(pts)] * len(pts) # @line L1
model, zs = [], []
for _ in range(rounds): # @line L2
f, theta, r = best_weak_ranker(pts, dist) # @line L3
a = alpha_of(r) # @line L4
h = weak(f, theta)
new = [d * math.exp(a * (h(x0) - h(x1))) for d, (x0, x1) in zip(dist, pts)]
z = sum(new) # @line L5
dist = [v / z for v in new] # @line L6
model.append((a, f, theta))
zs.append(z)
return model, zs # @line L7
def score(model, x):
return sum(a * weak(f, theta)(x) for a, f, theta in model)
def ranking_loss(model, pts, dist=None):
"""sum of D(x0, x1) over the pairs with H(x0) >= H(x1); D is uniform by default."""
dist = [1.0 / len(pts)] * len(pts) if dist is None else dist
return sum(d for d, (x0, x1) in zip(dist, pts) if score(model, x0) >= score(model, x1))
def w_values(h, pts, dist):
"""W_b = sum of D over the pairs with h(x0) - h(x1) = b, for b = -1, 0, +1."""
w = {-1: 0.0, 0: 0.0, 1: 0.0}
for d, (x0, x1) in zip(dist, pts):
w[int(h(x0) - h(x1))] += d
return w
def method2_alpha(w):
"""The alpha that minimizes Z = W_-1 e^-alpha + W_0 + W_+1 e^alpha (Section 3.2)."""
return 0.5 * math.log(w[-1] / w[1])
def method2_z(w):
return w[0] + 2.0 * math.sqrt(w[-1] * w[1])
test_rankboost.py 127 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/rankboost/. The authors are listed in prov/FQ86DQ.jsonl.
import math
import random
from rankboost import (
alpha_of,
best_weak_ranker,
crucial_pairs,
fit,
method2_alpha,
method2_z,
r_value,
ranking_loss,
w_values,
weak,
)
EXAMPLE = [([[0.0, 1.0], [1.0, 0.0], [2.0, 2.0], [3.0, 1.0]], [0, 1, 2, 1])]
def random_queries(seed, n_queries=4, n_docs=6, d=3):
rng = random.Random(seed)
queries = []
for _ in range(n_queries):
xs = [[rng.randint(0, 9) / 9.0 for _ in range(d)] for _ in range(n_docs)]
y = [rng.randint(0, 2) for _ in range(n_docs)]
queries.append((xs, y))
return queries
def replay(queries, model):
"""The distributions D_1, ..., D_T recomputed from the fitted model."""
pts = crucial_pairs(queries)
dist = [1.0 / len(pts)] * len(pts)
out = []
for a, f, theta in model:
out.append(dist)
h = weak(f, theta)
new = [d * math.exp(a * (h(x0) - h(x1))) for d, (x0, x1) in zip(dist, pts)]
z = sum(new)
dist = [v / z for v in new]
return pts, out
def test_page_example():
pts = crucial_pairs(EXAMPLE)
assert len(pts) == 5
model, zs = fit(EXAMPLE, 2)
a1, f1, t1 = model[0]
assert (f1, t1) == (0, 0.5)
assert abs(a1 - math.log(2)) < 1e-12
assert abs(zs[0] - 0.7) < 1e-12
assert (model[1][1], model[1][2]) == (1, 1.5)
assert abs(ranking_loss(model[:1], pts) - 0.4) < 1e-12
assert ranking_loss(model, pts) == 0.0
def test_weak_ranker_is_brute_force_best():
for seed in range(5):
queries = random_queries(seed)
model, _ = fit(queries, 4)
pts, dists = replay(queries, model)
for (a, f, theta), dist in zip(model, dists):
r = r_value(weak(f, theta), pts, dist)
values = sorted(set(v for p in pts for x in p for v in x))
for ff in range(3):
for tt in values:
assert abs(r_value(weak(ff, tt), pts, dist)) <= abs(r) + 1e-12
assert abs(a - 0.5 * math.log((1 + r) / (1 - r))) < 1e-12
def test_z_matches_formula_and_square_root_bound():
for seed in range(5):
queries = random_queries(seed + 10)
model, zs = fit(queries, 5)
pts, dists = replay(queries, model)
for (a, f, theta), dist, z in zip(model, dists, zs):
h = weak(f, theta)
direct = sum(d * math.exp(a * (h(x0) - h(x1))) for d, (x0, x1) in zip(dist, pts))
assert abs(direct - z) < 1e-12
r = r_value(h, pts, dist)
assert z <= math.sqrt(1 - r * r) + 1e-12
w = w_values(h, pts, dist)
assert abs(r - (w[-1] - w[1])) < 1e-12
def test_ranking_loss_bounded_by_product_of_z():
for seed in range(5):
queries = random_queries(seed + 20)
pts = crucial_pairs(queries)
model, zs = fit(queries, 8)
prod = 1.0
for t in range(1, len(model) + 1):
prod *= zs[t - 1]
assert ranking_loss(model[:t], pts) <= prod + 1e-12
def test_method2_alpha_minimizes_z():
queries = random_queries(30)
pts = crucial_pairs(queries)
dist = [1.0 / len(pts)] * len(pts)
f, theta, _ = best_weak_ranker(pts, dist)
h = weak(f, theta)
w = w_values(h, pts, dist)
a = method2_alpha(w)
def z(alpha):
return w[-1] * math.exp(-alpha) + w[0] + w[1] * math.exp(alpha)
assert abs(z(a) - method2_z(w)) < 1e-12
grid = [i / 1000.0 for i in range(-3000, 3001)]
assert all(z(a) <= z(b) + 1e-12 for b in grid)
def test_clipping_of_perfect_weak_ranker():
# One weak ranker orders every pair, so r = 1 and alpha is clipped.
queries = [([[0.0], [1.0]], [0, 1])]
model, zs = fit(queries, 1)
assert math.isfinite(model[0][0]) and model[0][0] > 10
assert alpha_of(1.0) == alpha_of(2.0)
def test_ties_count_as_errors():
pts = crucial_pairs([([[0.0], [0.0]], [0, 1])])
assert ranking_loss([], pts) == 1.0
History
Freund, Iyer, Schapire and Singer presented RankBoost at ICML in 1998, in the setting of combining the preferences of several experts, such as several search engines, and published the full version in JMLR in 2003. It carried the AdaBoost analysis to pairs: the bound on the training ranking loss, the choices of , an efficient bipartite version, and threshold weak rankers. It was one of the first learning-to-rank methods and is a standard baseline beside the Ranking SVM and RankNet.
References
- Yoav Freund, Raj Iyer, Robert E. Schapire and Yoram Singer, An efficient boosting algorithm for combining preferences, Journal of Machine Learning Research 4, 2003, pp. 933-969; Figure 1 (the algorithm), Section 3 and Theorem 1 (the ranking loss is at most the product of the Z_t), Section 3.2 (the choices of alpha), Section 3.3 and Figure 2 (RankBoost.B for bipartite feedback), Section 4 (threshold weak rankers)
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 FQ86DQ Edit this page History Report an error Markdown