RLS RL, Read as Statistics

Chapter 10 · Part 3: Learning policies

Preferences, reward models, and the KL-regularised policy

RLHF read as statistics, with a Bradley-Terry reward model fitted by logistic regression, a KL-regularised optimum read as a Bayesian posterior, DPO as maximum likelihood, and over-optimisation as overfitting.

Chapter 8 assumed the reward was given. For many tasks, among them most of what we want a language model to do, nobody can write the reward down, but people can say which of two outputs they prefer. This chapter takes that setting as a statistics problem: estimate a reward from comparisons, then use it to improve a policy without letting the policy exploit the estimate's errors.

What is the setting?

For readers who have not met RLHF (reinforcement learning from human feedback), here is the vocabulary. A language model is a policy πθ(y∣x)\pi_\theta(y \mid x): given a prompt xx, it produces a response yy (a sequence of tokens) by sampling. The "action" is the whole response, so the problem is a one-step decision problem with an enormous action set. We drop xx from the notation and write π(y)\pi(y). A reference policy πref\pi_{\text{ref}} is the model before the procedure starts, typically one already fine-tuned on demonstrations.

The pipeline of Christiano et al. (2017) for control tasks and of Ouyang et al. (2022) (InstructGPT) for language is:

  1. Collect pairs of responses and a human label saying which is better.
  2. Fit a reward model rϕ(y)r_\phi(y) to those labels.
  3. Optimise the policy to maximise reward, with a penalty that keeps it close to πref\pi_{\text{ref}}.

Steps 2 and 3 are the two halves of this chapter.

How is a reward fitted from comparisons?

Humans give an ordinal answer, not a number. The Bradley–Terry model (Bradley and Terry, 1952) says each response has a latent score r(y)r(y) and the probability that ywy_w is preferred to yly_l is

P(yw≻yl)=σ(r(yw)−r(yl)),σ(u)=11+e−u.\mathbb{P}(y_w \succ y_l) = \sigma\big(r(y_w) - r(y_l)\big), \qquad \sigma(u) = \frac{1}{1 + e^{-u}} .

Given nn independent comparisons (wi,li)(w_i, l_i) with winner listed first, the log-likelihood is

ℓ(r)=∑i=1nlog⁡σ(r(wi)−r(li)).\ell(r) = \sum_{i=1}^{n} \log \sigma\big(r(w_i) - r(l_i)\big).

Two statistical points matter.

Identifiability. Every row xix_i sums to zero, so adding a constant cc to every r(y)r(y) leaves every probability unchanged. Rewards are identified only up to a constant, and the Hessian is singular along the all-ones vector. Only differences are estimable, which is also why only differences matter downstream: a constant shift cancels in the KL-regularised optimum below. We fix the constant by centring.

Existence. If the comparison graph is disconnected, scores in different components are not comparable. If one item wins every comparison it appears in, the likelihood increases without bound in its score: complete separation, exactly as in logistic regression. With few comparisons per item this is common, so practical fits add a penalty, which is a Gaussian prior on the rewards (MAP estimation). Our code uses a ridge penalty λ=0.1\lambda = 0.1.

A worked example by hand

Three items and five observed comparisons, winner first: 2≻12 \succ 1, 2≻12 \succ 1, 1≻21 \succ 2, 3≻23 \succ 2, 3≻13 \succ 1. Take the candidate reward r=(0,ln⁡2,ln⁡4)r = (0, \ln 2, \ln 4). The Bradley–Terry probabilities are

σ(ln⁡2)=23,σ(ln⁡4)=45.\sigma(\ln 2) = \frac{2}{3}, \qquad \sigma(\ln 4) = \frac{4}{5}.

So P(2≻1)=23\mathbb{P}(2 \succ 1) = \tfrac23, P(1≻2)=13\mathbb{P}(1 \succ 2) = \tfrac13, P(3≻2)=σ(ln⁡4−ln⁡2)=23\mathbb{P}(3 \succ 2) = \sigma(\ln 4 - \ln 2) = \tfrac23 and P(3≻1)=45\mathbb{P}(3 \succ 1) = \tfrac45. The likelihood of the data is

(23)2⋅13⋅23⋅45=32405≈0.0790.\left(\tfrac23\right)^2 \cdot \tfrac13 \cdot \tfrac23 \cdot \tfrac45 = \tfrac{32}{405} \approx 0.0790 .

For comparison, the model with all rewards equal assigns every comparison probability 12\tfrac12 and gives (12)5=0.03125(\tfrac12)^5 = 0.03125. The likelihood ratio is about 2.52.5 in favour of the candidate. (It is a candidate, not the MLE: the MLE would raise the score of item 3, which has won both of its comparisons, as far as the prior or penalty allows. That is complete separation for item 3 at work.)

What is the KL-regularised policy objective?

Given a reward rr (estimated or known), the naive goal is max⁡πEy∼π[r(y)]\max_\pi \mathbb{E}_{y \sim \pi}[r(y)]. Its solution is a point mass on the best response. If rr is estimated, that is precisely the response about which the estimate is least trustworthy. The standard fix penalises distance from πref\pi_{\text{ref}}:

J(π)=Ey∼π[r(y)]−β KL(π ∥ πref),KL(π ∥ πref)=∑yπ(y)log⁡π(y)πref(y),J(\pi) = \mathbb{E}_{y \sim \pi}[r(y)] - \beta\, \mathrm{KL}(\pi \,\|\, \pi_{\text{ref}}), \qquad \mathrm{KL}(\pi \,\|\, \pi_{\text{ref}}) = \sum_y \pi(y) \log \frac{\pi(y)}{\pi_{\text{ref}}(y)},

with β>0\beta > 0. This is the objective used in InstructGPT, with the penalty applied per token.

Claim. The unique maximiser is

π∗(y)=πref(y) er(y)/βZ,Z=∑yπref(y) er(y)/β,\pi^*(y) = \frac{\pi_{\text{ref}}(y)\, e^{r(y)/\beta}}{Z}, \qquad Z = \sum_y \pi_{\text{ref}}(y)\, e^{r(y)/\beta},

and the maximum value is βlog⁡Z\beta \log Z.

Derivation (Gibbs variational principle). From the definition of π∗\pi^*, log⁡π∗(y)=log⁡πref(y)+r(y)/β−log⁡Z\log \pi^*(y) = \log \pi_{\text{ref}}(y) + r(y)/\beta - \log Z, which rearranges to

r(y)=βlog⁡π∗(y)πref(y)+βlog⁡Z.r(y) = \beta \log \frac{\pi^*(y)}{\pi_{\text{ref}}(y)} + \beta \log Z .

Substitute into JJ:

J(π)=∑yπ(y)[βlog⁡π∗(y)πref(y)+βlog⁡Z−βlog⁡π(y)πref(y)]=βlog⁡Z−β∑yπ(y)log⁡π(y)π∗(y)  =  βlog⁡Z−β KL(π ∥ π∗).\begin{aligned} J(\pi) &= \sum_y \pi(y)\Big[\beta \log \tfrac{\pi^*(y)}{\pi_{\text{ref}}(y)} + \beta \log Z - \beta \log \tfrac{\pi(y)}{\pi_{\text{ref}}(y)}\Big] \\ &= \beta \log Z - \beta \sum_y \pi(y) \log \frac{\pi(y)}{\pi^*(y)} \;=\; \beta \log Z - \beta\, \mathrm{KL}(\pi \,\|\, \pi^*). \end{aligned}

KL≥0\mathrm{KL} \ge 0 with equality only at π=π∗\pi = \pi^* (Gibbs' inequality), so JJ is maximised uniquely at π∗\pi^* with value βlog⁡Z\beta\log Z. □\square

Two consequences. Adding a constant to rr multiplies numerator and ZZ by the same factor, so π∗\pi^* is unchanged: consistent with rewards being identified only up to a constant. And β\beta is a temperature: as β→∞\beta \to \infty, π∗→πref\pi^* \to \pi_{\text{ref}}; as β→0\beta \to 0, π∗\pi^* concentrates on arg⁡max⁡r\arg\max r (within the support of πref\pi_{\text{ref}}).

Worked example, continued

Take the reward estimate r^=(0,ln⁡2,ln⁡4)\hat r = (0, \ln 2, \ln 4) from above and πref=(0.5, 0.3, 0.2)\pi_{\text{ref}} = (0.5,\ 0.3,\ 0.2). For β=1\beta = 1, er^/β=(1,2,4)e^{\hat r/\beta} = (1, 2, 4), so the unnormalised weights are (0.5, 0.6, 0.8)(0.5,\ 0.6,\ 0.8), with Z=1.9Z = 1.9 and

π∗=(0.263, 0.316, 0.421).\pi^* = (0.263,\ 0.316,\ 0.421).

For β=12\beta = \tfrac12, er^/β=(1,4,16)e^{\hat r/\beta} = (1, 4, 16), the weights are (0.5, 1.2, 3.2)(0.5,\ 1.2,\ 3.2), Z=4.9Z = 4.9 and

π∗=(0.102, 0.245, 0.653).\pi^* = (0.102,\ 0.245,\ 0.653).

The smaller temperature moves more mass onto the high-reward item, at a price in KL: KL(π∗ ∥ πref)\mathrm{KL}(\pi^* \,\|\, \pi_{\text{ref}}) is 0.1610.161 for β=1\beta = 1 and 0.5610.561 for β=12\beta = \tfrac12. The objective values are βlog⁡Z=0.642\beta \log Z = 0.642 and 0.7950.795, which one can confirm equal Eπ∗[r^]−βKL\mathbb{E}_{\pi^*}[\hat r] - \beta \mathrm{KL}: for β=1\beta = 1, 0.803−0.161=0.6420.803 - 0.161 = 0.642.

The Bayesian reading

π∗(y)∝πref(y)⋅er(y)/β\pi^*(y) \propto \pi_{\text{ref}}(y) \cdot e^{r(y)/\beta} has the form prior times likelihood. Treat πref\pi_{\text{ref}} as a prior over responses and er(y)/βe^{r(y)/\beta} as the likelihood of an "observation" that the response is good. Then π∗\pi^* is the posterior. Korbak, Perez and Buckley (2022) argue exactly this: the KL-regularised objective is variational inference towards that posterior, since the derivation above shows J(π)=βlog⁡Z−β KL(π ∥ π∗)J(\pi) = \beta\log Z - \beta\,\mathrm{KL}(\pi \,\|\, \pi^*), so maximising JJ minimises the reverse KL to π∗\pi^*.

What is DPO?

The two-stage pipeline fits r^\hat r by maximum likelihood, then optimises a policy against it by RL, with samples, a value baseline and so on. Direct preference optimisation (Rafailov et al., 2023) observes that the optimum above can be inverted. For any reward rr, the policy π\pi that is optimal for it satisfies

r(y)=βlog⁡π(y)πref(y)+βlog⁡Z.r(y) = \beta \log \frac{\pi(y)}{\pi_{\text{ref}}(y)} + \beta \log Z .

Substitute this into the Bradley–Terry probability. The term βlog⁡Z\beta \log Z does not depend on yy and cancels in the difference:

P(yw≻yl)=σ ⁣(βlog⁡π(yw)πref(yw)−βlog⁡π(yl)πref(yl)).\mathbb{P}(y_w \succ y_l) = \sigma\!\left(\beta \log \frac{\pi(y_w)}{\pi_{\text{ref}}(y_w)} - \beta \log \frac{\pi(y_l)}{\pi_{\text{ref}}(y_l)}\right).

The preference probability is now a function of the policy π\pi itself. Maximum likelihood on the observed comparisons over π=πθ\pi = \pi_\theta is minimisation of the DPO loss

LDPO(θ)=−1n∑i=1nlog⁡σ ⁣(βlog⁡πθ(wi)πref(wi)−βlog⁡πθ(li)πref(li)).\mathcal{L}_{\text{DPO}}(\theta) = -\frac{1}{n}\sum_{i=1}^n \log \sigma\!\left(\beta \log \frac{\pi_\theta(w_i)}{\pi_{\text{ref}}(w_i)} - \beta \log \frac{\pi_\theta(l_i)}{\pi_{\text{ref}}(l_i)}\right).

Its gradient is −β 1n∑iσ(−mi) (∇log⁡πθ(wi)−∇log⁡πθ(li))-\beta\,\frac1n\sum_i \sigma(-m_i)\,\big(\nabla\log\pi_\theta(w_i) - \nabla\log\pi_\theta(l_i)\big), where mim_i is the argument of the sigmoid: log-likelihood of the winner goes up, of the loser down, weighted by how badly the current implicit reward r^(y)=βlog⁡(πθ/πref)\hat r(y) = \beta\log(\pi_\theta/\pi_{\text{ref}}) mis-ranks the pair.

DPO skips the sampling-based RL step, but it does not escape the statistical problem: it uses a fixed batch of comparisons, and its implicit reward is as noisy as an explicit one. If the loss is driven far enough, it too finds policies that exploit the particular sample.

Why does pushing harder make things worse?

Let r^=r+ε\hat r = r + \varepsilon, where ε\varepsilon is the reward model's estimation error. The policy πβ∗\pi^*_\beta places most mass where r^\hat r is high, so

Eπβ∗[r^]−Eπβ∗[r]=Eπβ∗[ε],\mathbb{E}_{\pi^*_\beta}[\hat r] - \mathbb{E}_{\pi^*_\beta}[r] = \mathbb{E}_{\pi^*_\beta}[\varepsilon] ,

and the policy is selected to make this positive: it prefers responses where the error happens to be large and positive. As β→0\beta \to 0 the policy tends to the maximiser of r^\hat r, and E[max⁡yr^(y)]≥max⁡yE[r^(y)]\mathbb{E}[\max_y \hat r(y)] \ge \max_y \mathbb{E}[\hat r(y)].

Gao, Schulman and Hilton (2023) measured this with a fixed "gold" reward model standing in for humans and proxy reward models trained on its labels. They found that, as the policy moves away from the initial one (measured by KL), the gold reward first rises, then peaks and falls while the proxy keeps rising, and that the relationship depends on reward-model size and data in smooth, fitted functional forms. Our toy below reproduces the widening gap; it only partly reproduces the peak.

What is GRPO's group-normalised advantage?

Chapter 8 showed that subtracting a baseline from the return reduces the variance of a policy-gradient estimate without bias. PPO-style RLHF learns a value function as the baseline, which is costly for large models. Group relative policy optimisation (Shao et al., 2024, introduced in DeepSeekMath) removes it. For each prompt it samples a group of GG responses y1,…,yGy_1, \dots, y_G from the current policy, scores them with rewards r1,…,rGr_1, \dots, r_G and uses the advantage

A^i=ri−rˉsr,rˉ=1G∑jrj,sr2=sample variance of r1,…,rG,\hat A_i = \frac{r_i - \bar r}{s_r}, \qquad \bar r = \frac1G\sum_j r_j, \quad s_r^2 = \text{sample variance of } r_1,\dots,r_G ,

for every token of response ii, inside a clipped, PPO-like objective together with a KL penalty towards πref\pi_{\text{ref}} added directly to the loss.

Experiment

The companion file ch10_preferences.py runs the whole experiment in about ten seconds. The response space has K=20K = 20 items with true rewards drawn from N(0,1)\mathcal{N}(0, 1) and centred, and a reference policy that is a softmax of random logits. Comparisons are between two uniformly chosen distinct items, with outcomes drawn from the Bradley–Terry model. The reward is fitted by Newton's method with ridge penalty 0.10.1. Part 1 reports error against the number of comparisons nn; part 2 builds πβ∗\pi^*_\beta from the estimated reward and reports, averaged over 200 repetitions, its KL from πref\pi_{\text{ref}}, its expected proxy reward Eπ[r^]\mathbb{E}_{\pi}[\hat r] and its expected true reward Eπ[r]\mathbb{E}_{\pi}[r], along with the gap between them and the fraction of repetitions in which the true reward is lower than at β=1\beta = 1. Part 3 fits DPO by gradient descent on the policy logits starting from πref\pi_{\text{ref}}, with 400 steps and no explicit penalty, on the same n=200n=200 comparisons.

Output
1. Bradley-Terry fit: estimation error vs number of comparisons (100 repetitions)
     n       RMSE corr(r,rhat) argmax correct
    20      1.223        0.456           0.13
    50      1.220        0.591           0.22
   100      0.830        0.756           0.35
   200      0.543        0.865           0.40
   500      0.323        0.939           0.60
  2000      0.162        0.984           0.76
(true reward sd across items: 0.851; best item has reward 1.487)

2. KL-regularised policy under the estimated reward (mean over 200 repetitions)
   true reward of the best item: 1.487; of the reference policy: 0.055
   n = 50 comparisons
     beta       KL    proxy     true      gap   P(<beta=1)
      inf    0.000    0.077    0.055    0.022         0.99
     3.00    0.120    0.809    0.274    0.535         0.97
     1.00    0.806    1.889    0.578    1.311         0.00
     0.30    2.319    2.791    0.822    1.969         0.23
     0.10    2.847    2.897    0.861    2.036         0.26
     0.03    3.029    2.909    0.871    2.039         0.27
     0.01    3.081    2.910    0.876    2.034         0.27
   n = 500 comparisons
     beta       KL    proxy     true      gap   P(<beta=1)
      inf    0.000    0.057    0.055    0.003         1.00
     3.00    0.043    0.319    0.270    0.049         1.00
     1.00    0.330    0.759    0.625    0.134         0.00
     0.30    1.648    1.463    1.166    0.297         0.00
     0.10    2.719    1.672    1.321    0.350         0.00
     0.03    3.147    1.699    1.353    0.346         0.01
     0.01    3.278    1.702    1.360    0.342         0.01

3. DPO vs two-stage (BT fit, then tilt), same preferences, n = 200, 100 repetitions
  beta | two-stage KL / true        | DPO KL / true              |  mean TV
  1.00 |        0.425        0.623 |        0.545        0.663 |    0.038
  0.30 |        1.904        1.087 |        1.999        1.099 |    0.029
  0.10 |        2.785        1.196 |        2.809        1.196 |    0.016

Reading part 1. The error falls as the data grow, and from n=500n = 500 to n=2000n = 2000 it halves (0.323 to 0.162) as the 1/n1/\sqrt{n} rate predicts. At n≤50n \le 50 the estimate is worse than useless: an RMSE of 1.22 exceeds the standard deviation of the true rewards (0.851), so predicting that all items are equal would do better. With 20 items and 20 to 50 comparisons, each item appears about two to five times, which is the regime of large MLE variance. Even at 2,000 comparisons the true best item is the estimated best in only 76% of runs, because the estimate still carries error comparable to the gaps between the top few items.

Reading part 2. With n=50n = 50 comparisons, pushing β\beta from ∞\infty down to 0.010.01 raises the proxy reward from 0.080.08 to 2.912.91, while the true reward rises only from 0.060.06 to 0.880.88, and the gap between them grows from 0.020.02 to about 2.02.0. The proxy tells you the policy is far better than it is, by a factor of more than three. The true reward saturates from about β=0.3\beta = 0.3 (0.822, then 0.861, 0.871, 0.876) while the KL cost keeps growing (2.3 to 3.1 nats). The last column shows the pathology at the level of a single experiment: in about a quarter of the repetitions at n=50n = 50 a smaller β\beta gives a lower true reward than β=1\beta = 1 does. With n=500n = 500 the same picture is milder: the gap is 0.340.34 and the true reward is almost never lower than at β=1\beta = 1.

This does not reproduce a decline in mean true reward, which Gao et al. observe with real reward models. In our toy the errors are the benign ones of a correctly specified model fitted to independent labels. Real reward models are misspecified, and their errors are correlated with features that optimisation can find. What the toy does show is the mechanism, selection on noise, and its two dependencies: the gap shrinks with more preference data and grows as β→0\beta \to 0.

Reading part 3. The two-stage policy and the DPO policy are close: the total variation distance between them is 0.040.04 at β=1\beta=1 and 0.0160.016 at β=0.1\beta=0.1, and both achieve nearly the same true reward (0.6230.623 against 0.6630.663 at β=1\beta = 1; 1.1961.196 for both at β=0.1\beta=0.1). DPO's KL from the reference is slightly higher at each β\beta. That is what the regularisation story predicts: the ridge penalty in the two-stage fit shrinks the rewards a little, while DPO's early stopping shrinks them differently. Neither is "right"; they are two ways of choosing how much to trust the data. Because the policy here is a free table, the likelihoods coincide, which is why the agreement is so close; with a parametric policy class they need not.

Exercises

  1. (Pen and paper.) Using the worked example, compute the log-likelihood gradient with respect to r3r_3 at r=(0,ln⁡2,ln⁡4)r = (0, \ln 2, \ln 4). Is the likelihood increased by raising r3r_3? What does this say about the MLE for these data?
  2. (Pen and paper.) Show that, for fixed rr, βlog⁡Z→max⁡yr(y)\beta\log Z \to \max_y r(y) as β→0\beta \to 0 (assume πref\pi_{\text{ref}} has full support) and βlog⁡Z→Eπref[r]\beta \log Z \to \mathbb{E}_{\pi_{\text{ref}}}[r] as β→∞\beta \to \infty.
  3. (Pen and paper.) For two responses with a single comparison yw≻yly_w \succ y_l and no penalty, show the Bradley–Terry likelihood has no maximiser, and say which penalty (a ridge λ2∥r∥2\tfrac{\lambda}{2}\lVert r\rVert^2 on the scores) gives a finite one. What is the maximiser of log⁡σ(d)−λ2⋅d22\log\sigma(d) - \tfrac{\lambda}{2}\cdot\tfrac{d^2}{2} in the score difference d=rw−rld = r_w - r_l when rr is centred, for λ=1\lambda=1? (Hint: set the derivative to zero and solve numerically; dd is between 00 and 11.)
  4. (Change the code.) Set LAM = 0.01 and predict how part 1 changes at n=20n = 20 and at n=2000n = 2000. Run it.
  5. (Change the code.) In experiment_overopt, use n=200n=200 and look at the last column. Predict whether the fraction of repetitions where a small β\beta is worse than β=1\beta=1 is above or below the n=50n=50 and n=500n=500 values, then check.

Sources