Core RL formulas: a self-contained derivation

This note gives the assumptions and derivation behind the RL example in Mathematics on Tap: Agents, Lean, and the Research Loop. It analyzes the expected raw updates of the coefficient rules below.

Setup

Fix a prompt, a positive group size \(K\), binary reward \(R(y)\in\{0,1\}\), a frozen sampling distribution \(\mu\), and a positive differentiable policy \(\pi_\theta\) on a finite response space. Let \(q=\Pr_\mu(R=1)\) and define the policy score

\[s_\theta(y)=\nabla_\theta\log\pi_\theta(y).\]

Draw \(y_1,\ldots,y_K\) independently from \(\mu\). A coefficient \(a_i\) can depend on the group’s reward labels and on its position, but not otherwise on the response contents. Independent random tie-breaking may be averaged into these coefficients. Rewards, samples, coefficients and the sampler are held fixed when differentiating. The update convention is

\[U=\frac1K\sum_{i=1}^{K}a_i s_\theta(y_i).\]

This describes gradient ascent on a weighted log-probability objective before clipping or the optimizer transforms the update. Sequence-level importance weights and arbitrary response-dependent coefficients need a different reduction.

One split

For \(0<q<1\), average each reward class’s coefficient over group positions and over the other sampled responses:

\[c_r=\frac1K\sum_{i=1}^{K}\mathbb E[a_i\mid R(y_i)=r], \qquad \omega(q)=c_1-c_0, \qquad \beta(q)=q c_1+(1-q)c_0.\]

Conditional on \(R(y_i)\), the remaining reward labels are independent of \(y_i\). The expected group update therefore has the same value as a single-response update with effective coefficient \(c(R)=c_0+\omega(q)R\). Centering that coefficient gives

\[c(R)=\omega(q)(R-q)+\beta(q).\]

Consequently,

\[\begin{aligned} \mathbb E[U] &=\omega(q)\mathbb E_\mu[(R-q)s_\theta] +\beta(q)\mathbb E_\mu[s_\theta]\\ &=\omega(q)\mathrm{Cov}_\mu(R,s_\theta) -\beta(q)\nabla_\theta\mathrm{KL}(\mu\Vert\pi_\theta). \end{aligned}\]

Here the vector covariance means \(\mathbb E_\mu[(R-q)s_\theta]\). The last step follows by differentiating \(\mathrm{KL}(\mu\Vert\pi_\theta)=\sum_y\mu(y)\log(\mu(y)/\pi_\theta(y))\) with \(\mu\) held fixed.

\(\omega\) controls the contrast between correct and incorrect responses. \(\beta\) controls a component that moves toward the sampler when positive, and away when negative. These are components of an expected raw update, not independent guarantees about training performance.

\(\beta=\mathbb E[K^{-1}\sum_i a_i]\) is a population mean, not a realized group’s mean. Replacing it by a random within-group mean does not permit pulling it outside an expectation. At \(q=0\) or \(q=1\), the covariance is zero and the update is the observed class’s mean coefficient times \(\mathbb E_\mu[s_\theta]\); conditioning on the absent class is unnecessary.

On policy: prompt weighting

At an exact snapshot where \(\mu=\pi_\theta\),

\[\mathbb E_{\pi_\theta}[s_\theta]=0, \qquad \mathrm{Cov}_{\pi_\theta}(R,s_\theta)=\nabla_\theta p, \qquad p=\Pr_{\pi_\theta}(R=1).\]

Thus the sampler component vanishes. For a fixed finite distribution of prompts \(x\) and the same coefficient rule across prompts,

\[\mathbb E[U] =\mathbb E_x[\omega(p_x)\nabla_\theta p_x] =\nabla_\theta\mathbb E_x[f(p_x)], \qquad f'=\omega.\]

Different coefficient rules can therefore weight prompt success probabilities differently even when they share the same binary reward. The nonlinear-success-objective perspective was developed in our Reinforce-Ada work.

“On policy” requires equality of the actual sampling law and the scored policy. Using an older snapshot, another temperature, or a different model generally breaks that equality. Off policy, the covariance is measured under \(\mu\) and need not equal the trained policy’s reward gradient.

Adding a constant to every coefficient

Let \(\lambda\) be fixed independently of the sampled responses and replace every \(a_i\) by \(a_i+\lambda\). Linearity gives

\[\Delta\mathbb E[U] =\lambda\mathbb E_\mu[s_\theta] =-\lambda\nabla_\theta\mathrm{KL}(\mu\Vert\pi_\theta).\]

This changes \(\beta\) to \(\beta+\lambda\) while leaving \(\omega\) unchanged. It vanishes at an exact on-policy snapshot. A baseline computed from the same sampled group can be correlated with the scores and does not satisfy this fixed-constant argument.

Two responses: GRPO and top-one RAFT

Consider the following explicitly normalized coefficient rules:

Rule Mixed correct/incorrect pair Tied pair \(\omega\) \(\beta\)
GRPO coefficient rule \(+1\) on correct, \(-1\) on incorrect Both zero 1 0
Top-one RAFT 2 on correct, 0 on incorrect Select one uniformly; give it 2 1 1

GRPO’s effective coefficient is \(R-q\); RAFT’s is \(R+1-q\). Their expected difference is therefore

\[\mathbb E[U_{\mathrm{RAFT}}-U_{\mathrm{GRPO}}] =\mathbb E_\mu[s_\theta] =-\nabla_\theta\mathrm{KL}(\mu\Vert\pi_\theta).\]

On policy, these two rules have the same expected raw update under these conventions. Their realized updates and sampling noise can still differ. Off policy, the mean difference remains. This is a comparison of specified coefficient rules, not an equivalence between complete implementations with clipping, regularization, advantage normalization constants, or different losses.

An intervention on all-wrong pairs

Suppose we add coefficient \(\lambda\) to both responses only when both rewards are zero. With two IID responses and \(0\leq q<1\),

\[\Delta\mathbb E[U] =\lambda(1-q)^2\mathbb E_\mu[s_\theta(y)\mid R(y)=0].\]

This is a way to isolate the component being changed. It does not determine whether that change helps training. In the exact on-policy case it becomes \(-\lambda(1-p)\nabla_\theta p\), so restricting a coefficient to all-wrong groups is not the same operation as adding a uniform baseline to every response.

The general scope remains local: finite-batch noise, clipping, Adam preconditioning and multiple training steps can change the effect. A mathematical decomposition identifies quantities to measure and interventions to compare; experimental prediction needs additional evidence.

These decompositions use standard conditioning, covariance and score identities.