\documentclass[11pt]{article} \usepackage{common} \begin{document} \notehead{0102}{Policy gradient from first principles, REINFORCE, baselines}{Policy-optimisation lineage, batch M1. Companion notebook: \texttt{notebooks/0102-policy-gradient-baselines.ipynb}.} \begin{problem} \question{How can we estimate $\nabla_\theta J$ from samples and scalar verifier rewards alone, and keep the variance low enough to train?} We want to climb $J(\theta)=\E_{o\sim\pi_\theta}[R(o)]$. Two things stand in the way: the sampling distribution itself depends on $\theta$ (so we cannot push the gradient inside the expectation), and $R$ is a verifier verdict on a discrete string, which has no derivative with respect to anything. We need an estimator of $\nabla_\theta J$ that uses only samples and scalar rewards, and then a way to make that estimator usable (its variance, un-controlled, is what makes naive RL on long responses fail). \end{problem} \section{Notation} \begin{center}\small\begin{tabular}{@{}ll@{}}\toprule symbol & meaning\\\midrule $J(\theta)=\sum_o\pi_\theta(o)R(o)$ & expected reward (prompt $q$ suppressed; a finite sum over responses)\\ $s(o)=\nabla_\theta\log\pi_\theta(o)=\sum_t s_t$ & sequence score; $s_t=\nabla_\theta\log\pi_\theta(o_t\mid s_t)$ is the token score\\ $G_t=\sum_{t'\ge t}r_{t'}$ & reward-to-go (for an outcome reward $G_t=R$ for all $t$)\\ $b,\ b_t(o_{0$ (true for a softmax, whose support is all of $\mathcal O_L$), so $\nabla\pi_\theta=\pi_\theta\nabla\log\pi_\theta$ holds everywhere. Then \begin{align} \nabla_\theta J&=\sum_o R(o)\,\nabla_\theta\pi_\theta(o)=\sum_o\pi_\theta(o)\,R(o)\,\nabla_\theta\log\pi_\theta(o)=\E_{o\sim\pi_\theta}\big[R(o)\,s(o)\big]\label{eq:ld}\\ &=\E_{o\sim\pi_\theta}\Big[R(o)\sum_{t=1}^{|o|}\nabla_\theta\log\pi_\theta(o_t\mid s_t)\Big]\quad\text{(using 0101, Eq.~(2)).}\nonumber \end{align} The right side is an expectation under the \emph{current} policy of a quantity we can compute from samples: this is REINFORCE (Williams 1992, as used in arXiv:2510.26788 Eq.~2). No derivative of $R$ appears. \ver{three computations of $\nabla J$ agree: autograd of $\sum_o\pi R$ vs.\ Eq.~\eqref{eq:ld}: $1.4\times10^{-17}$; vs.\ central finite differences: $3.4\times10^{-11}$.} \subsection{The score has zero mean} \begin{lemma}\label{lem:zero}For every state $s_t$: $\sum_a\pi_\theta(a\mid s_t)\nabla_\theta\log\pi_\theta(a\mid s_t)=0$, and hence $\E_{o\sim\pi_\theta}[s(o)]=0$.\end{lemma} \emph{Proof.} $\sum_a\pi\nabla\log\pi=\sum_a\nabla\pi(a)=\nabla\sum_a\pi(a)=\nabla 1=0$. The sequence statement is the same computation on $\sum_o\pi(o)=1$. $\blacksquare$ \ver{$\E_\pi[s]$ has max component $5.6\times10^{-17}$.} \subsection{Baselines: which ones are free} \paragraph{Constant (or prompt-only) baseline.} For $b$ independent of the sampled $o$: $\E[(R-b)s]=\E[Rs]-b\,\E[s]=\nabla J$ by Lemma~\ref{lem:zero}. Any $b$, even a wrong one, leaves the expectation unchanged. \paragraph{State-dependent baseline.} Let $b_t=b_t(s_t)$ depend on the prefix only. Condition on $s_t$ (tower rule; the event $\{|o|\ge t\}$ that token $t$ exists is decided by $o_{0$: \[V(b)\E R\iff\Cov(R,\|s\|^2)>0$. \ver{Eq.~\eqref{eq:var} vs.\ direct variance: $\le8.9\times10^{-16}$; on the toy $b^\star=0.302$, $\E R=0.166$, $V(0)=0.690$, $V(\E R)=0.516$, $V(b^\star)=0.472$; here the successes carry the larger score norm ($\E[\|s\|^2\mid R{=}1]=4.36$ vs.\ $\E[\|s\|^2\mid R{=}0]=2.00$), which is why $b^\star>\E R$; the interval statement holds at 50 interior and 4 exterior points.} \subsection{Group baselines (preview of 0105)} Draw $G$ i.i.d.\ responses $o_1,\dots,o_G$ for one prompt with rewards $R_i$. \begin{itemize} \item \emph{Leave-one-out (RLOO):} $b_i=\frac1{G-1}\sum_{j\ne i}R_j$ is independent of $o_i$, so $\E[(R_i-b_i)s_i]=\nabla J$. \item \emph{Mean including self:} $\bar R=\frac1G\sum_jR_j$. Then $\E[(R_i-\bar R)s_i]=(1-\tfrac1G)\E[R_is_i]-\tfrac1G\sum_{j\ne i}\E[R_j]\E[s_i]=(1-\tfrac1G)\nabla J$ (independence and Lemma~\ref{lem:zero}). The estimator is unbiased \emph{up to the known scale} $(G-1)/G$, which a learning rate absorbs. Indeed $\frac{G}{G-1}(R_i-\bar R)=R_i-\frac1{G-1}\sum_{j\ne i}R_j$ (arXiv:2503.20783 App.~A). \end{itemize} \ver{exact enumeration of all $121^2$ ($G{=}2$) and $121^3$ ($G{=}3$) sample tuples: LOO error $\le2.1\times10^{-17}$; mean-including-self equals $\frac{G-1}{G}\nabla J$ to $\le1.0\times10^{-17}$.} \subsection{Implementation: naive vs.\ efficient} Naive: a Python loop calling \texttt{backward} on $(R_i-b)\log\pi(o_i)$ once per sample. Efficient: because $\hat g=\nabla_\theta[\sg(A)\,\log\pi_\theta]$ where $\sg$ is stop-gradient, one backward through the \emph{surrogate} $\frac1n\sum_i\sg(A_i)\log\pi_\theta(o_i)$ gives the whole estimate. (The surrogate is not $J$; its value is meaningless, only its gradient is.) The stop-gradient on the advantage is essential; later notes extend this idea to the ratio. \ver{loop vs.\ single backward: $8.6\times10^{-16}$; Monte Carlo estimate from $4\times10^5$ samples within $2.0$ standard errors (max over components) of the exact gradient.} \section{Boxed result} \begin{result} \[\boxed{\begin{aligned}\nabla_\theta J&=\E_{o\sim\pi_\theta}\Big[\sum_{t=1}^{|o|}\big(G_t-b_t(s_t)\big)\,\nabla_\theta\log\pi_\theta(o_t\mid s_t)\Big]\quad\text{for any }b_t=b_t(s_t),\\ V(b)&=A_0-2bA_1+b^2A_2-\|\nabla J\|^2,\qquad b^\star=\frac{\E[R\|s\|^2]}{\E[\|s\|^2]},\qquad V(b)