From group filtering to gradient geometry

This companion to Mathematics on Tap: Agents, Lean, and the Research Loop derives the filtering and gradient-norm calculations used in the RL example. The update-decomposition note supplies the broader on-policy and off-policy framework.

Group filtering: the mean does not settle the comparison

A useful starting point is the homogeneous-group filter in Minimal-RL. For a group of more than one response, the inspected coefficient rule retains each raw reward when the group’s rewards differ and sets the coefficients to zero when they all agree. We analyze this filtering choice with an original-group mean, IID on-policy samples, binary correctness, and explicitly signed rewards \(A=2R-1\). The calculation isolates the filter from the implementation’s token aggregation, other loss terms, and optimizer.

Fix a prompt and a positive differentiable policy on a finite response space. Let \(p\) be its success probability, \(s_\theta(y)=\nabla_\theta\log\pi_\theta(y)\) its score, and \(G=\nabla_\theta p\). Draw \(K>1\) responses independently from that policy. Define

\[U_{\mathrm{full}}=\frac1K\sum_{i=1}^{K}(2R_i-1)s_\theta(y_i), \qquad U_{\mathrm{mixed}}=\mathbf 1_{\{\text{both reward classes appear}\}}U_{\mathrm{full}}.\]

The original denominator \(K\) remains in both updates. Masking coefficients does not divide the update by the number of retained responses or groups. At the on-policy snapshot,

\[\mathbb E[s_\theta]=0, \qquad \mathbb E[Rs_\theta]=G, \qquad \mathbb E[(1-R)s_\theta]=-G.\]

A particular correct response survives if at least one of the other \(K-1\) responses is wrong; that event has probability \(1-p^{K-1}\). A wrong response survives with probability \(1-(1-p)^{K-1}\). Consequently,

\[\begin{aligned} \mathbb E[U_{\mathrm{full}}]&=2G,\\ \mathbb E[U_{\mathrm{mixed}}] &=\bigl[2-p^{K-1}-(1-p)^{K-1}\bigr]G. \end{aligned}\]

For \(K=2\), separating the two kinds of homogeneous groups gives four rules:

Coefficients retained Expected raw update
Every pair \(2G\)
Every pair except all-wrong pairs \((1+p)G\)
Every pair except all-correct pairs \((2-p)G\)
Mixed pairs only \(G\)

The all-wrong and all-correct cells contribute \((1-p)G\) and \(pG\), respectively. Removing both halves the expected update by the same factor on every prompt. But matching the expected updates by rescaling does not match their sampling fluctuations or their clipped updates.

The signed encoding matters. With rewards \(R\in\{0,1\}\) instead, the mixed-group expectation is \((1-p^{K-1})G\), against \(G\) without filtering. At \(K=2\), this changes the prompt weight by \(1-p\), rather than a constant factor.

An exact example with equal means

Take a Bernoulli-logit policy with \(p=1/2\), so its scalar score is \(s_\theta=R-p\). Every response contributes \((2R-1)(R-p)=1/2\). Thus \(U_{\mathrm{full}}=1/2\) for every pair. The mean-matched estimator \(V=2U_{\mathrm{mixed}}\) is 1 on mixed pairs and 0 on tied pairs, each with probability \(1/2\).

For a scalar or vector \(u\), define

\[\mathrm{clip}_C(u)= \begin{cases} \min(1,C/\Vert u\Vert)u,&u\ne0,\\ 0,&u=0. \end{cases}\]

At threshold \(C=1/4\), exact enumeration gives

\[\mathbb E[U_{\mathrm{full}}]=\mathbb E[V]=\tfrac12, \qquad \mathbb E[\mathrm{clip}_{1/4}(U_{\mathrm{full}})]=\tfrac14, \qquad \mathbb E[\mathrm{clip}_{1/4}(V)]=\tfrac18.\]

The means agree before clipping and differ afterward. This example includes zero updates, so it illustrates clipping’s nonlinearity; it is not an application of the everywhere-clipped bound below.

A score that includes gradient norm and noise

Let \(U\) be a random batch ascent update with a finite probability law in a finite-dimensional Euclidean space. Fix a reference reward objective \(J\) and its gradient \(G=\nabla_\theta J\) at the current parameters; \(G\) is held fixed while averaging over \(U\). Assume \(r_U=\sqrt{\mathbb E\Vert U\Vert^2}>0\). Define

\[\mathcal S(U;G) =\frac{\mathbb E\langle U,G\rangle} {\sqrt{\mathbb E\Vert U\Vert^2}}, \qquad \mathbb E\Vert U\Vert^2 =\Vert\mathbb E U\Vert^2 +\mathbb E\Vert U-\mathbb E U\Vert^2.\]

The numerator measures expected progress along \(G\). The denominator is the update’s root-mean-square norm, including sampling noise. The score is invariant under a positive constant rescaling of \(U\). It is not simply the cosine between \(\mathbb E U\) and \(G\).

There is a direct one-step connection to norm clipping. In addition to the assumptions above, let \(C>0\) and require \(\Vert U\Vert\ge C\) for every possible update under the finite law. Define the norm-spread quantity

\[\sigma_U=\sqrt{2\left(1-\frac{\mathbb E\Vert U\Vert}{r_U}\right)}.\]

Then

\[\boxed{ \left|\left\langle\mathbb E[\mathrm{clip}_C(U)],G\right\rangle -C\mathcal S(U;G)\right| \le C\Vert G\Vert\sigma_U. }\]

To prove it, write \(n=\Vert U\Vert\) and \(e=U/n\). The clipping premise implies \(\mathrm{clip}_C(U)=Ce\) and \(\Vert e\Vert=1\). The difference inside the absolute value is

\[C\mathbb E\!\left[\langle e,G\rangle(1-n/r_U)\right].\]

Cauchy–Schwarz bounds its absolute value by \(C\Vert G\Vert\sqrt{\mathbb E(1-n/r_U)^2}\). Expanding the square and using \(\mathbb E[n^2]=r_U^2\) gives

\[\mathbb E(1-n/r_U)^2 =2\left(1-\frac{\mathbb E[n]}{r_U}\right),\]

which proves the bound. The square root is real because \(\mathbb E[n]\le\sqrt{\mathbb E[n^2]}\).

If all update norms are equal, \(\sigma_U=0\), so \(C\mathcal S(U;G)\) is exactly the projection of the expected clipped update onto \(G\). More generally, suppose \(U\) and \(V\) both satisfy the clipping premise with the same threshold \(C\) and reference gradient \(G\). If

\[\mathcal S(U;G)-\mathcal S(V;G) >\Vert G\Vert(\sigma_U+\sigma_V),\]

their expected clipped projections have the same strict ordering. Observing that every batch in one run was clipped does not establish the premise for every possible batch.

Closing the research loop

The raw mean suggests a matched comparison. Clipping exposes a missing quantity: the distribution of batch gradients, including their norms. The revised model identifies which gradients and moments to measure before a run. Their score generates an ordering to compare with the eventual training results.

The bound concerns a single clipped update. Whether its diagnostic also ranks full Adam runs is a separate empirical question. The useful connection is concrete: a question about which samples to train on becomes a question about update geometry, and then a measurement that can guide the next experiment.