All courses

Bradley-Terry model for pairwise preferences

Model P(a preferred over b) = σ(s_a − s_b), where σ is the logistic sigmoid, and fit the scores s by maximum likelihood on comparison data, such as people choosing the better of two responses. Explain why only score differences are identifiable.

Builds on: Maximum likelihood as cross-entropy minimization, From base model to assistant

▶ Watch this lesson Start the whole course

What the animation says

Choices into scores

People see two answers and pick the better one, thousands of times. Each person judges only a few pairs, and many pairs are never compared at all. We want one number per answer that predicts how often it beats any other, even for pairs nobody compared. That is the Bradley-Terry model. Reward models are trained exactly this way.

Only gaps matter

Give every answer a hidden strength score. When A meets B, only the gap matters: A's score minus B's score. The sigmoid squashes that gap into a win chance. A gap of zero gives fifty percent. A big positive gap pushes toward a hundred percent; a big negative gap, toward zero. Now add ten to every score. What happens to A's chance of beating B?

Pin one score, fit the rest

It stays the same. Shifting all scores changes nothing, so the data can't fix the absolute level. So we pin one score, say A at zero. To fit, add up the log of each winner's predicted win chance, and choose the scores that maximize that sum. That is the same as minimizing cross-entropy against the observed choices. But scores are fixed only where comparisons link items to the pinned one. A is pinned. Tap the answers whose scores the data cannot fix.

Fit two answers by hand

A beats B three times out of four. Call the score gap delta. The best delta makes predicted wins match observed wins: four times the win chance equals three. So the win chance is three quarters. Undo the sigmoid: the gap is the log of win chance over lose chance. Here that is log of three, about one point one. Scores of one point one and zero fit exactly as well as five point one and four. Now fix A at five point one instead. What must B be?

Three items, numerically

Four. Fix one score, and the gap fixes the other. With three items we fit numerically. Eight comparisons, written as winner, loser. Each comparison pushes the winner up by its chance of having lost, and the loser down equally. Sum the pushes, step by point one, and re-pin item zero. It settles where observed wins equal predicted wins: zero, minus one point two five, minus two point one one. Danger: an item that never loses runs off to infinity. Tap the one pair whose removal does that to item zero.

Turning A-vs-B choices into scores

People are shown two responses and pick the better one, thousands of times. Each person sees only a few pairs, and many pairs are never compared at all. Can we give every response **one number** that predicts how often it would win against any other, including unseen pairs? And what do those numbers actually mean? Later, a model that scores responses is trained in exactly this way.

Hidden strengths, only gaps matter

Give each item a hidden strength score $s$. When $a$ meets $b$, the chance $a$ wins depends only on the **gap** $s_a - s_b$. A gap of 0 gives 50%. A large positive gap gives nearly 100%. The sigmoid squashes the gap into a probability. Because only gaps matter, adding 10 to every score changes no prediction. So the data can never fix the absolute level. We pin one score, for example $s_1=0$.

The Bradley-Terry likelihood

Write $a\succ b$ for 'a is preferred over b'. $$P(a\succ b)=\sigma(s_a-s_b)=\frac{e^{s_a}}{e^{s_a}+e^{s_b}}$$ The data are $n$ comparisons, where $w_i$ won and $l_i$ lost. Maximum likelihood picks the scores $s$ that maximize $$\ell(s)=\sum_{i=1}^{n}\log\sigma(s_{w_i}-s_{l_i})$$ This is the same as minimizing the average cross-entropy between observed outcomes and predicted win probabilities. Since $\ell(s+c)=\ell(s)$ for any constant $c$, at most the differences are identifiable. They are all pinned down only if the comparisons link every item, directly or through others, into one connected web.

Fit two responses by hand

$A$ is chosen over $B$ in 3 of 4 comparisons. Let $\Delta=s_A-s_B$. $\ell(\Delta)=3\log\sigma(\Delta)+\log(1-\sigma(\Delta))$ Using $\tfrac{d}{d\Delta}\log\sigma(\Delta)=1-\sigma(\Delta)$ and $\tfrac{d}{d\Delta}\log(1-\sigma(\Delta))=-\sigma(\Delta)$: $3(1-\sigma)-\sigma=0\Rightarrow\sigma(\Delta)=\tfrac34$ $\Delta=\ln\tfrac{0.75}{0.25}=\ln 3\approx1.10$ The scores $(s_A,s_B)=(1.10,\,0)$ and $(5.10,\,4)$ fit equally well.

Fitting three items numerically

The code repeatedly nudges each score in proportion to its slope of the log-likelihood. Each comparison pushes its winner up by the winner's predicted chance of having lost, and pushes the loser down by the same amount. The pushes are summed and then scaled by the step size 0.1. At the optimum, each item's observed wins equal its predicted wins. Pinning $s_0=0$ removes the free shift. If an item never lost, its score would run off to infinity.
import numpy as np
wins = [(0,1),(0,1),(0,1),(1,0),(1,2),(1,2),(2,1),(0,2)]  # (winner, loser)
sig = lambda x: 1/(1+np.exp(-x))
s = np.zeros(3)
for _ in range(5000):
    d = np.zeros(3)  # slope of log-likelihood wrt each score
    for w, l in wins:
        miss = 1 - sig(s[w] - s[l])  # d/ds_w of log sig(s_w - s_l)
        d[w] += miss; d[l] -= miss
    s += 0.1 * d
    s -= s[0]  # pin s_0 = 0: only differences are identifiable
print(s.round(2))  # about [0, -1.25, -2.11]: s_0 > s_1 > s_2

Remember

Bradley-Terry: $P(a\succ b)=\sigma(s_a-s_b)$, where $a\succ b$ means a is preferred. Fit the scores by maximizing $\sum\log\sigma(s_{\text{winner}}-s_{\text{loser}})$. Only score differences mean anything, because shifting every score by the same constant leaves every prediction unchanged.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.