Policy Gradient Review 2026: From One Answer

By Xuhui Zhou

Version history: [v1] · [v2] (revised)

The math ladder from reward to return to Q to advantage.

FWIW, there are numerous tutorials, videos, and blog posts introducing policy gradient. However, I often find them a bit hard to understand and follow because they become abstract too quickly. As an LLM guy, I want to work through a derivation with the right amount of intuition and help people like me better understand this fundamental idea behind almost all modern post-training.

You do not need much background to follow along. I assume little to no prior knowledge of probability or statistics, and we will unpack each expectation and mathematical step as it appears.

The core policy-gradient move is simple: if an action leads to better future outcomes than expected, make that action more likely next time. The hard part is being precise about what "future outcomes" means. (If you want to know more about RL in general, see my Thinking in RL post.)

And here we are, the policy gradient therom could be seen as chain of equalities:

RGtQπ(st,at)Aπ(st,at).R \quad\Longrightarrow\quad G_t \quad\Longrightarrow\quad Q^\pi(s_t,a_t) \quad\Longrightarrow\quad A^\pi(s_t,a_t).

Each arrow is not a vibe. Each arrow is a legal replacement inside an expectation. We will work through them from the simplest case to the more general one, ending with why we use an advantage instead of a raw reward.

The one-answer case

Start with the smallest language-model RL problem. A prompt xx arrives. In this setting, xx is often a math problem or a question-answering problem that requires only one complete response to finish the interaction. The model samples that response yπθ(x)y \sim \pi_\theta(\cdot \mid x), and a judge gives it one terminal reward R(x,y)R(x,y). If the full response is treated as one action, the objective is:

J(θx)=Eyπθ(x)[R(x,y)].J(\theta \mid x) = \mathbb{E}_{y \sim \pi_\theta(\cdot \mid x)} \left[R(x,y)\right].

What does this objective mean? Imagine repeatedly sampling answers from the policy. Some answers appear more often because the model assigns them more probability. J(θx)J(\theta \mid x) is the average reward across those samples, or equivalently, a weighted average of every possible answer's reward, where the weight is πθ(yx)\pi_\theta(y \mid x). We maximize JJ so the policy places as much probability mass as possible on answers that receive high rewards, such as correct answers to the math or question-answering problem.

To make this weighted average explicit, write the expectation as a sum:

J(θx)=yπθ(yx)R(x,y).J(\theta \mid x) = \sum_y \pi_\theta(y \mid x)R(x,y).

Now differentiate it:

θJ(θx)=yθπθ(yx)R(x,y)=yπθ(yx)θlogπθ(yx)R(x,y)=Eyπθ(x)[R(x,y)θlogπθ(yx)].\begin{aligned} \nabla_\theta J(\theta \mid x) &= \sum_y \nabla_\theta \pi_\theta(y \mid x)R(x,y) \\ &= \sum_y \pi_\theta(y \mid x) \nabla_\theta \log \pi_\theta(y \mid x) R(x,y) \\ &= \mathbb{E}_{y \sim \pi_\theta(\cdot \mid x)} \left[ R(x,y)\nabla_\theta \log \pi_\theta(y \mid x) \right]. \end{aligned}

The tiny trick is:

θπθ(yx)=πθ(yx)θlogπθ(yx).\nabla_\theta \pi_\theta(y \mid x) = \pi_\theta(y \mid x) \nabla_\theta \log \pi_\theta(y \mid x).

Intuition: we are not differentiating the judge. We are differentiating how likely the model was to produce the sampled answer. High reward says "increase this answer's log-probability." Later, after we subtract a baseline, worse-than-expected reward says "decrease it."

This one-answer case is the whole story when T=1T=1. Now we make TT bigger.

The trajectory case

A real trajectory has many actions:

τ=(s1,a1,r1,s2,a2,r2,,sT,aT,rT).\tau=(s_1,a_1,r_1,s_2,a_2,r_2,\ldots,s_T,a_T,r_T).

For now, use the undiscounted return:

G1=k=1Trk.G_1 = \sum_{k=1}^{T} r_k.

The objective is:

J(θ)=Eτpθ[G1].J(\theta) = \mathbb{E}_{\tau \sim p_\theta} \left[G_1\right].

The trajectory probability factors as:

pθ(τ)=ρ(s1)t=1Tπθ(atst)P(st+1st,at).p_\theta(\tau) = \rho(s_1) \prod_{t=1}^{T} \pi_\theta(a_t \mid s_t) P(s_{t+1}\mid s_t,a_t).

Only the policy terms contain θ\theta. The initial-state distribution ρ\rho and environment transition PP do not. So:

θlogpθ(τ)=θ[logρ(s1)+t=1Tlogπθ(atst)+t=1TlogP(st+1st,at)]=t=1Tθlogπθ(atst).\begin{aligned} \nabla_\theta \log p_\theta(\tau) &= \nabla_\theta \left[ \log \rho(s_1) + \sum_{t=1}^{T}\log \pi_\theta(a_t\mid s_t) + \sum_{t=1}^{T}\log P(s_{t+1}\mid s_t,a_t) \right] \\ &= \sum_{t=1}^{T} \nabla_\theta \log \pi_\theta(a_t\mid s_t). \end{aligned}

Now use the same log-derivative trick as before:

θJ(θ)=θpθ(τ)G1dτ=pθ(τ)θlogpθ(τ)G1dτ=Eτpθ[G1t=1Tθlogπθ(atst)]=Eτpθ[t=1TG1θlogπθ(atst)].\begin{aligned} \nabla_\theta J(\theta) &= \nabla_\theta \int p_\theta(\tau)G_1\,d\tau \\ &= \int p_\theta(\tau) \nabla_\theta \log p_\theta(\tau) G_1\,d\tau \\ &= \mathbb{E}_{\tau \sim p_\theta} \left[ G_1 \sum_{t=1}^{T} \nabla_\theta \log \pi_\theta(a_t\mid s_t) \right] \\ &= \mathbb{E}_{\tau \sim p_\theta} \left[ \sum_{t=1}^{T} G_1 \nabla_\theta \log \pi_\theta(a_t\mid s_t) \right]. \end{aligned}

This is the first raw trajectory formula. It says: attach the total return G1G_1 to every action.

From total return to reward-to-go

But should an action receive credit for rewards that happened before the action was chosen?

Before touching the algebra, imagine a game. An agent arrives at a checkpoint with 10 coins already in its pocket. It must now choose left or right:

  • Left leads to 2 more coins, for 12 total.
  • Right leads to 5 more coins, for 15 total.

The 10 old coins change both totals, but they do not help us decide between left and right. Adding the same 10 to both choices preserves their difference: right is better by 3 either way. The current action cannot travel backward and collect, lose, or deserve credit for those 10 coins.

That is the intuition behind reward-to-go: when training the action chosen at time tt, keep rewards that can still depend on that action and drop rewards that were already settled.

For a language model trained with only one terminal reward, this particular simplification changes nothing numerically. Before every token, the accumulated reward is zero; the entire reward still lies in the future. Thus Gt=G1=R(x,y)G_t=G_1=R(x,y) for every token. We need the general argument because other environments can have intermediate rewards, costs, tool outcomes, or safety penalties.

Now define the reward-to-go from time tt:

Gt=k=tTrk.G_t = \sum_{k=t}^{T} r_k.

So:

G1=k=1t1rkpast before at+k=tTrkGt.G_1 = \underbrace{\sum_{k=1}^{t-1} r_k}_{\text{past before }a_t} + \underbrace{\sum_{k=t}^{T} r_k}_{G_t}.

For the gradient term at time tt:

E[G1θlogπθ(atst)]=E[Gtθlogπθ(atst)]\mathbb{E} \left[ G_1 \nabla_\theta \log \pi_\theta(a_t\mid s_t) \right] = \mathbb{E} \left[ G_t \nabla_\theta \log \pi_\theta(a_t\mid s_t) \right]

because the past part has zero expectation when multiplied by the score function.

Expectation is just an average over possible worlds

The expectation symbol can make a simple operation look mysterious. Imagine replaying the random experiment many times. Each replay produces one possible world ww and one value X(w)X(w). The expectation is the long-run average:

E[X]=wP(w)X(w).\mathbb{E}[X] = \sum_w P(w)X(w).

In reinforcement learning, one possible world is indeed one complete trajectory τpθ(τ)\tau\sim p_\theta(\tau). We never stop sampling complete trajectories.

The key is that one complete trajectory is generated in order. By time tt, the rollout has already produced a history HtH_t. From that history, its next random draw is

atπθ(st).a_t\sim\pi_\theta(\cdot\mid s_t).

So the action sample is not a new experiment that replaces the trajectory sample. It is one draw inside the trajectory we already sampled. Equivalently:

Pθ(at=aHt=h)=πθ(as(h)).P_\theta(a_t=a\mid H_t=h) = \pi_\theta(a\mid s(h)).

The picture below shows this without the probability bookkeeping. Start with complete rollouts, pause them at one shared prefix, and inspect which action each rollout takes next.

This is what conditioning does. For a quantity such as Zt=θlogπθ(atst)Z_t=\nabla_\theta\log\pi_\theta(a_t\mid s_t), which is determined once the history and current action are known, the same trajectory average can be written in two stages:

Eτpθ[Zt]=EHt[Eatπθ(st)[ZtHt]].\mathbb{E}_{\tau\sim p_\theta}[Z_t] = \mathbb{E}_{H_t} \left[ \mathbb{E}_{a_t\sim\pi_\theta(\cdot\mid s_t)} \left[Z_t\mid H_t\right] \right].

Read this from right to left: fix the prefix, average over the action that comes next, and then average over all prefixes. That produces the same average as sampling complete trajectories directly. This is the tower property.

In actual Monte Carlo training, we do not need to resample the action separately. One sampled trajectory already gives one sampled HtH_t and one sampled ata_t. Across many trajectories, those observed actions estimate the inner action average.

The past reward is not one global constant across trajectories: one rollout may arrive with 2 coins and another with 10. But after fixing one exact HtH_t, its past is fixed. This is why conditioning lets us ask whether that shared past can influence the action sampled next.

Most expectation manipulations in this post use four rules:

Split sums. Expectation is linear, so E[X+Y]=E[X]+E[Y]\mathbb{E}[X+Y]=\mathbb{E}[X]+\mathbb{E}[Y].

Pull out what is fixed. If cc does not vary over the worlds currently being averaged, then E[cX]=cE[X]\mathbb{E}[cX]=c\mathbb{E}[X]. Always ask: fixed with respect to which randomness?

Condition to freeze information. A quantity may vary globally but become fixed inside a conditional expectation. If YY is completely determined by HH, then E[YXH]=YE[XH]\mathbb{E}[YX\mid H]=Y\mathbb{E}[X\mid H].

Average the groups back together. Use the tower property. If the inner average is zero in every group, the outer average is an average of zeros and is also zero.

Freeze the prefix, then average its next actions

Define the history immediately before sampling the current action:

Ht=(s1,a1,r1,,st1,at1,rt1,st).H_t=(s_1,a_1,r_1,\ldots,s_{t-1},a_{t-1},r_{t-1},s_t).

In an autoregressive language model, this is the prompt plus the previously generated tokens. It contains sts_t, but not ata_t or anything that happens after ata_t.

Also name the two quantities we care about:

Pastt=k=1t1rk,Zt=θlogπθ(atst).\text{Past}_t=\sum_{k=1}^{t-1}r_k, \qquad Z_t=\nabla_\theta\log\pi_\theta(a_t\mid s_t).

Pastt\text{Past}_t varies across full trajectories, so we cannot pull it out of the outer expectation. But once we condition on one exact HtH_t, the past is already written and becomes fixed. Only the current action is resampled.

Now follow the picture.

1. Group by history using the tower property:

E[PasttZt]=EHt[E[PasttZtHt]].\mathbb{E}[\text{Past}_tZ_t] = \mathbb{E}_{H_t} \left[ \mathbb{E}[\text{Past}_tZ_t\mid H_t] \right].

2. Inside one history bucket, pull out the fixed past:

E[PasttZtHt]=PasttE[ZtHt].\mathbb{E}[\text{Past}_tZ_t\mid H_t] = \text{Past}_t\,\mathbb{E}[Z_t\mid H_t].

3. Average the score over the possible current actions:

This is the algebraic version of the last visual panel. The sum over aa does not mean that the training procedure stopped sampling trajectories. It lists the possible next-action branches inside one fixed history. A single rollout takes one branch; many rollouts estimate their weighted average.

In words:

conditional mean score=next actions(action probability)(score if sampled).\text{conditional mean score} = \sum_{\text{next actions}} (\text{action probability})(\text{score if sampled}). E[ZtHt]=aπθ(ast)θlogπθ(ast)=aθπθ(ast)=θaπθ(ast)=θ1=0.\begin{aligned} \mathbb{E}[Z_t\mid H_t] &= \sum_a \pi_\theta(a\mid s_t) \nabla_\theta\log\pi_\theta(a\mid s_t) \\ &= \sum_a \nabla_\theta\pi_\theta(a\mid s_t) \\ &= \nabla_\theta\sum_a\pi_\theta(a\mid s_t) \\ &= \nabla_\theta 1 \\ &=0. \end{aligned}

Why should this be zero? The action probabilities must always sum to 1. Increasing some probabilities forces others down; the policy cannot create or destroy total probability mass. The score of each individual action is generally not zero, but its probability-weighted average is.

4. Recombine the history buckets:

E[PasttZt]=EHt[Pastt0]=0.\mathbb{E}[\text{Past}_tZ_t] = \mathbb{E}_{H_t}[\text{Past}_t\cdot 0] =0.

Finally, use linearity to split total return into past and future:

E[G1Zt]=E[(Pastt+Gt)Zt]=E[PasttZt]0+E[GtZt]=E[GtZt].\begin{aligned} \mathbb{E}[G_1Z_t] &= \mathbb{E}[(\text{Past}_t+G_t)Z_t] \\ &= \underbrace{\mathbb{E}[\text{Past}_tZ_t]}_{0} + \mathbb{E}[G_tZ_t] \\ &= \mathbb{E}[G_tZ_t]. \end{aligned}

That is why G1G_1 may be replaced by GtG_t in the tt-th gradient term. We did not assume that past reward was globally constant. We grouped trajectories until it became locally constant, proved that its contribution was zero in every group, and then averaged the groups back together.

Applying that equality to every timestep gives:

θJ(θ)=E[t=1TGtθlogπθ(atst)].\begin{aligned} \nabla_\theta J(\theta) &= \mathbb{E}\left[ \sum_{t=1}^{T} G_t \nabla_\theta \log \pi_\theta(a_t\mid s_t) \right]. \end{aligned}

If using discounting, define:

Gt=k=tTγktrk.G_t = \sum_{k=t}^{T} \gamma^{k-t}r_k.

For the objective J=E[G1]J=\mathbb{E}[G_1], the exact derivation gives a factor γt1Gt\gamma^{t-1}G_t in front of the tt-th score term. Many presentations either include that factor or absorb it into how timesteps are weighted. The important idea is unchanged: past rewards vanish; future rewards stay.

From sampled return to Q

Reward-to-go GtG_t is still a sampled future. If you take the same state sts_t and same action ata_t, many different futures can happen.

The action-value function averages those futures:

Qπ(st,at)=Eπ[Gtst,at].Q^\pi(s_t,a_t) = \mathbb{E}_\pi \left[ G_t \mid s_t,a_t \right].

We have finally arrived at the Q-function. It is powerful because it turns all those uncertain futures into one number for each state-action pair. At a fixed state ss, Qπ(s,a)Q^\pi(s,a) asks: if I take action aa now and follow policy π\pi afterward, what return should I expect? Once we know these action values, we can compare the available actions and choose the one with the largest expected return:

agreedy(s)=argmaxaQπ(s,a).a_{\text{greedy}}(s) = \arg\max_a Q^\pi(s,a).

This is the central action-selection idea behind value-based methods such as Q-learning.

For our policy-gradient derivation, we are not switching algorithms. We are asking one precise question: can the noisy sampled future GtG_t be replaced by its conditional average Qπ(st,at)Q^\pi(s_t,a_t) without changing the expected gradient?

To answer it, let:

Zt=θlogπθ(atst).Z_t = \nabla_\theta \log \pi_\theta(a_t\mid s_t).

The key is the tower property of expectation:

E[ZtGt]=E[E[ZtGtst,at]]=E[ZtE[Gtst,at]]=E[ZtQπ(st,at)].\begin{aligned} \mathbb{E} \left[ Z_tG_t \right] &= \mathbb{E} \left[ \mathbb{E} \left[ Z_tG_t \mid s_t,a_t \right] \right] \\ &= \mathbb{E} \left[ Z_t \mathbb{E} \left[ G_t \mid s_t,a_t \right] \right] \\ &= \mathbb{E} \left[ Z_t Q^\pi(s_t,a_t) \right]. \end{aligned}

The second line is legal because ZtZ_t is fully determined once (st,at)(s_t,a_t) is known. It can be pulled outside the inner expectation.

So:

θJ(θ)=E[t=1TQπ(st,at)θlogπθ(atst)].\begin{aligned} \nabla_\theta J(\theta) &= \mathbb{E}\left[ \sum_{t=1}^{T} Q^\pi(s_t,a_t) \nabla_\theta \log \pi_\theta(a_t\mid s_t) \right]. \end{aligned}

Intuition: GtG_t is one roll of the future dice. Qπ(st,at)Q^\pi(s_t,a_t) is the average of many such rolls. They have the same expectation in the gradient, but QQ is less noisy if you can estimate it well.

From Q to advantage

The Q-function gives an action an absolute score: how much return do we expect if we take this action? But an absolute score is missing context. The same QQ-value might be impressive in a difficult state and disappointing in an easy one.

Advantage turns that absolute score into a relative gain:

Aπ(s,a)=Qπ(s,a)expected return after choosing aVπ(s)what the policy usually gets from s.A^\pi(s,a) = \underbrace{Q^\pi(s,a)}_{\text{expected return after choosing }a} - \underbrace{V^\pi(s)}_{\text{what the policy usually gets from }s}.

A positive advantage says that this action gives us more return than we would normally expect at this state. A negative advantage says that choosing it gives up expected return. The magnitude tells us how large that gain or loss is. This is the information we want for a policy update: not merely whether an action is good, but whether it is better than what the policy usually does at this state.

To see why subtracting Vπ(s)V^\pi(s) gives us this cleaner signal without changing the expected policy gradient, we need to track two expectation scopes. The notation often hides the switch between them.

We begin at the outer scope:

θJ(θ)=Eτpθ[t=1TQπ(st,at)Zt],Zt=θlogπθ(atst).\nabla_\theta J(\theta) = \mathbb{E}_{\tau\sim p_\theta} \left[ \sum_{t=1}^{T} Q^\pi(s_t,a_t)Z_t \right], \qquad Z_t=\nabla_\theta\log\pi_\theta(a_t\mid s_t).

This expectation still samples complete trajectories. A full trajectory determines which states are visited, which actions are sampled there, and which futures occur.

Temporarily zoom into one fixed state

The value function is the policy's average action-value at state ss:

Vπ(s)=Eaπ(s)[Qπ(s,a)].V^\pi(s) = \mathbb{E}_{a\sim\pi(\cdot\mid s)} \left[ Q^\pi(s,a) \right].

This action-only expectation is a conditional slice of the trajectory distribution, not a new training sampler. We pause full trajectories that reached the same state ss and ask which action they take next.

There are two nested averages hiding inside VV:

Vπ(s)=E[Gtst=s]=Eatπ(s)[E[Gtst=s,at]Qπ(s,at)].\begin{aligned} V^\pi(s) &= \mathbb{E}[G_t\mid s_t=s] \\ &= \mathbb{E}_{a_t\sim\pi(\cdot\mid s)} \left[ \underbrace{ \mathbb{E}[G_t\mid s_t=s,a_t] }_{Q^\pi(s,a_t)} \right]. \end{aligned}

Q(s,a)Q(s,a) first averages the possible futures after a fixed state-action pair. Then V(s)V(s) averages those QQ values over the actions the policy might choose at the fixed state.

First, watch the cancellation in one state

Suppose a policy reaches one state ss and can go left or right. Let

πθ(lefts)=p=0.4,πθ(rights)=1p=0.6,\pi_\theta(\text{left}\mid s)=p=0.4, \qquad \pi_\theta(\text{right}\mid s)=1-p=0.6,

and suppose Q(s,left)=12Q(s,\text{left})=12 while Q(s,right)=8Q(s,\text{right})=8. The value of the state is the policy-weighted average:

V(s)=0.4(12)+0.6(8)=9.6.V(s)=0.4(12)+0.6(8)=9.6.

Let θ\theta be the logit that controls the probability of going left. The two possible score terms point in opposite directions:

Z(left)=θlogp=1p=0.6,Z(right)=θlog(1p)=p=0.4.Z(\text{left})=\frac{\partial}{\partial\theta}\log p=1-p=0.6, \qquad Z(\text{right})=\frac{\partial}{\partial\theta}\log(1-p)=-p=-0.4.

Now keep only the baseline part of the expected gradient. It contributes

Eaπ(s)[V(s)Z(s,a)]=0.4(9.6)(0.6)+0.6(9.6)(0.4)=2.3042.304=0.\begin{aligned} \mathbb{E}_{a\sim\pi(\cdot\mid s)}[V(s)Z(s,a)] &=0.4(9.6)(0.6)+0.6(9.6)(-0.4) \\ &=2.304-2.304 \\ &=0. \end{aligned}

The baseline says "add the same 9.69.6 no matter which action happened." But increasing the left logit moves probability mass from right to left: the left branch pushes up exactly as much as the right branch pushes down after weighting by how often each branch occurs.

Subtracting the baseline therefore changes the numbers attached to individual samples, but not their expected gradient:

E[QZ]=0.4(12)(0.6)+0.6(8)(0.4)=0.96,A(left)=129.6=2.4,A(right)=89.6=1.6,E[AZ]=0.4(2.4)(0.6)+0.6(1.6)(0.4)=0.96.\begin{aligned} \mathbb{E}[QZ] &=0.4(12)(0.6)+0.6(8)(-0.4)=0.96, \\ A(\text{left})&=12-9.6=2.4, \\ A(\text{right})&=8-9.6=-1.6, \\ \mathbb{E}[AZ] &=0.4(2.4)(0.6)+0.6(-1.6)(-0.4)=0.96. \end{aligned}

The same fact as a general proof

The numbers above are not a coincidence. Because Vπ(s)V^\pi(s) is fixed while we average the possible actions at that state, for any fixed ss:

Eaπ(s)[Vπ(s)θlogπθ(as)]=Vπ(s)aπθ(as)θlogπθ(as)=Vπ(s)aθπθ(as)=Vπ(s)θ1=0.\begin{aligned} \mathbb{E}_{a\sim\pi(\cdot\mid s)} \left[ V^\pi(s) \nabla_\theta \log \pi_\theta(a\mid s) \right] &= V^\pi(s) \sum_a \pi_\theta(a\mid s) \nabla_\theta \log \pi_\theta(a\mid s) \\ &= V^\pi(s) \sum_a \nabla_\theta \pi_\theta(a\mid s) \\ &= V^\pi(s) \nabla_\theta 1 \\ &=0. \end{aligned}

Writing Z(s,a)=θlogπθ(as)Z(s,a)=\nabla_\theta\log\pi_\theta(a\mid s), this proves a local statement inside one state bucket:

Eaπ(s)[Qπ(s,a)Z(s,a)]=Eaπ(s)[(Qπ(s,a)Vπ(s))Z(s,a)]=Eaπ(s)[Aπ(s,a)Z(s,a)].\begin{aligned} \mathbb{E}_{a\sim\pi(\cdot\mid s)} \left[ Q^\pi(s,a) Z(s,a) \right] &= \mathbb{E}_{a\sim\pi(\cdot\mid s)} \left[ \bigl(Q^\pi(s,a)-V^\pi(s)\bigr) Z(s,a) \right] \\ &= \mathbb{E}_{a\sim\pi(\cdot\mid s)} \left[ A^\pi(s,a) Z(s,a) \right]. \end{aligned}

From one state bucket back to full trajectories

The previous subsection proved a local fact: after we fix one state ss and average over the actions available there, subtracting Vπ(s)V^\pi(s) contributes zero to the expected gradient. But the policy-gradient objective does not stay inside one fixed state. It averages complete trajectories, and different trajectories visit different states.

This subsection lifts that local result back to the full objective. If every state bucket contributes zero, then weighting those buckets by how often the policy visits them still gives a weighted average of zeros. That is what lets a proof about one fixed state justify the baseline subtraction across the entire trajectory distribution.

The notation can create a false picture here. It may look as if we first sampled a trajectory, threw it away, sampled a state, and then sampled an action. That does not happen.

Fix one timestep tt. A complete trajectory is a row containing many columns:

τ=(pastbefore t, st, at, futureafter t).\tau = (\underbrace{\text{past}}_{\text{before }t},\ s_t,\ a_t,\ \underbrace{\text{future}}_{\text{after }t}).

Sampling one row automatically samples every column in that row. If we later ignore every column except (st,at)(s_t,a_t), we have not drawn anything new. We have only projected each full-trajectory sample down to two of its coordinates.

The six-row toy table makes the operation literal. Averaging a function F(st,at)F(s_t,a_t) row by row gives

8+8+2+4+6+66=5.67.\frac{8+8+2+4+6+6}{6}=5.67.

Putting the same rows into state buckets and then action buckets gives

36[23(8)+13(2)]+36[13(4)+23(6)]=5.67.\frac{3}{6} \left[ \frac{2}{3}(8)+\frac{1}{3}(2) \right] + \frac{3}{6} \left[ \frac{1}{3}(4)+\frac{2}{3}(6) \right] =5.67.

The numbers did not change. Only the parentheses changed.

The exact probability proof

For one fixed tt, start from the full-trajectory expectation:

Eτpθ[F(st,at)]=τpθ(τ)F(st(τ),at(τ)).\mathbb{E}_{\tau\sim p_\theta}[F(s_t,a_t)] = \sum_\tau p_\theta(\tau)F(s_t(\tau),a_t(\tau)).

Now partition the set of complete trajectories by the state and action appearing in their tt-th columns:

τpθ(τ)F(st(τ),at(τ))=sa[τ:st=s,at=apθ(τ)]F(s,a)=saPr(st=s,at=a)F(s,a).\begin{aligned} \sum_\tau p_\theta(\tau)F(s_t(\tau),a_t(\tau)) &= \sum_s\sum_a \left[ \sum_{\tau:\,s_t=s,\,a_t=a}p_\theta(\tau) \right]F(s,a) \\ &= \sum_s\sum_a \Pr(s_t=s,a_t=a)F(s,a). \end{aligned}

The probability in brackets is the total probability mass of all full trajectories whose row has that state-action pair. By the ordinary product rule of probability,

Pr(st=s,at=a)=Pr(st=s)dtπ(s)Pr(at=ast=s)πθ(as).\Pr(s_t=s,a_t=a) = \underbrace{\Pr(s_t=s)}_{d_t^\pi(s)} \underbrace{\Pr(a_t=a\mid s_t=s)}_{\pi_\theta(a\mid s)}.

The second factor is πθ(as)\pi_\theta(a\mid s) because the policy is exactly the mechanism that chooses the current action after seeing the current state. For an autoregressive language model, sts_t is the entire prompt-plus-token prefix, so it contains everything the next-token policy conditions on.

Substitute that product into the previous sum:

Eτpθ[F(st,at)]=sdtπ(s)aπθ(as)F(s,a)=Estdtπ[Eatπθ(st)[F(st,at)st]].\begin{aligned} \mathbb{E}_{\tau\sim p_\theta}[F(s_t,a_t)] &= \sum_s d_t^\pi(s) \sum_a \pi_\theta(a\mid s)F(s,a) \\ &= \mathbb{E}_{s_t\sim d_t^\pi} \left[ \mathbb{E}_{a_t\sim\pi_\theta(\cdot\mid s_t)} [F(s_t,a_t)\mid s_t] \right]. \end{aligned}

So the right-hand side is not a recipe for collecting different data. It is the same full-trajectory expectation after grouping its probability mass first by state and then by action. This is the tower property of expectation.

Finally choose

F(st,at)=Vπ(st)ZtF(s_t,a_t)=V^\pi(s_t)Z_t

and use linearity to sum over timesteps. Inside each fixed-state bucket, Vπ(st)V^\pi(s_t) is constant and E[Ztst]=0\mathbb{E}[Z_t\mid s_t]=0. Therefore:

Eτpθ[t=1TVπ(st)Zt]=t=1TEstdtπ[Eatπ(st)[Vπ(st)Ztst]]=t=1TEstdtπ[Vπ(st)0]=0.\begin{aligned} \mathbb{E}_{\tau\sim p_\theta} \left[ \sum_{t=1}^{T}V^\pi(s_t)Z_t \right] &= \sum_{t=1}^{T} \mathbb{E}_{s_t\sim d_t^\pi} \left[ \mathbb{E}_{a_t\sim\pi(\cdot\mid s_t)} \left[ V^\pi(s_t)Z_t\mid s_t \right] \right] \\ &= \sum_{t=1}^{T} \mathbb{E}_{s_t\sim d_t^\pi} \left[ V^\pi(s_t)\cdot 0 \right] \\ &=0. \end{aligned}

Every state bucket contributes zero. The outer trajectory expectation merely weights those buckets by how often full trajectories reach them, so it is a weighted average of zeros.

Therefore, back at the outer scope:

Eτpθ[tQπ(st,at)Zt]=Eτpθ[t(Qπ(st,at)Vπ(st))Zt]=Eτpθ[tAπ(st,at)Zt].\begin{aligned} \mathbb{E}_{\tau\sim p_\theta} \left[ \sum_t Q^\pi(s_t,a_t)Z_t \right] &= \mathbb{E}_{\tau\sim p_\theta} \left[ \sum_t \bigl(Q^\pi(s_t,a_t)-V^\pi(s_t)\bigr)Z_t \right] \\ &= \mathbb{E}_{\tau\sim p_\theta} \left[ \sum_t A^\pi(s_t,a_t)Z_t \right]. \end{aligned}

So the policy-gradient theorem becomes:

θJ(θ)=Eτpθ[t=1TAπ(st,at)θlogπθ(atst)].\begin{aligned} \nabla_\theta J(\theta) &= \mathbb{E}_{\tau\sim p_\theta}\left[ \sum_{t=1}^{T} A^\pi(s_t,a_t) \nabla_\theta \log \pi_\theta(a_t\mid s_t) \right]. \end{aligned}

In actual training there is no separate action-only rollout phase:

  1. Sample one complete trajectory τ\tau.
  2. For every visited (st,at)(s_t,a_t), estimate Q(st,at)Q(s_t,a_t) and V(st)V(s_t).
  3. Form At=Q(st,at)V(st)A_t=Q(s_t,a_t)-V(s_t) and multiply it by that action's log-probability gradient.

The action expectation appears in the proof and in the definition of VV. The training sample is still the full trajectory.

Intuition: QQ asks, "How good is this action?" Advantage asks, "How good is this action compared with what the policy usually does at this exact state?" That comparison is what gives the clean push-up or push-down signal.

Back to LLMs with terminal reward

For an autoregressive language model, the trajectory is a sequence of token actions:

y=(y1,,yT),πθ(yx)=t=1Tπθ(ytx,y<t).y=(y_1,\ldots,y_T), \quad \pi_\theta(y\mid x) = \prod_{t=1}^{T} \pi_\theta(y_t\mid x,y_{\lt t}).

If there is only one reward at the end, set:

r1==rT1=0,rT=R(x,y).r_1=\cdots=r_{T-1}=0, \quad r_T=R(x,y).

Then, without discounting:

Gt=R(x,y)for every token t.G_t = R(x,y) \quad \text{for every token }t.

This is the exact reason terminal-reward LLM RL broadcasts one final score across all sampled tokens:

θJ(θ)R(x,y)t=1Tθlogπθ(ytx,y<t).\nabla_\theta J(\theta) \approx R(x,y) \sum_{t=1}^{T} \nabla_\theta \log \pi_\theta(y_t\mid x,y_{\lt t}).

If we train a critic, use rollouts, or use a group baseline, we are trying to replace that raw terminal RR with a better estimate of advantage:

θJ(θ)t=1TA^tθlogπθ(ytx,y<t).\nabla_\theta J(\theta) \approx \sum_{t=1}^{T} \widehat{A}_t \nabla_\theta \log \pi_\theta(y_t\mid x,y_{\lt t}).

The policy-gradient machinery did not change. Only the multiplier attached to each token became more informative:

The whole derivation in one line

The mathematically careful story is:

θJ(θ)=E[tG1θlogπθ(atst)]=E[tGtθlogπθ(atst)]=E[tQπ(st,at)θlogπθ(atst)]=E[tAπ(st,at)θlogπθ(atst)].\begin{aligned} \nabla_\theta J(\theta) &= \mathbb{E} \left[ \sum_t G_1 \nabla_\theta\log\pi_\theta(a_t\mid s_t) \right] \\ &= \mathbb{E} \left[ \sum_t G_t \nabla_\theta\log\pi_\theta(a_t\mid s_t) \right] \\ &= \mathbb{E} \left[ \sum_t Q^\pi(s_t,a_t) \nabla_\theta\log\pi_\theta(a_t\mid s_t) \right] \\ &= \mathbb{E} \left[ \sum_t A^\pi(s_t,a_t) \nabla_\theta\log\pi_\theta(a_t\mid s_t) \right]. \end{aligned}

Each line is the same gradient in expectation:

  • G1GtG_1 \to G_t: remove past rewards; they are action-independent baselines.
  • GtQπG_t \to Q^\pi: average over possible futures using conditional expectation.
  • QπAπQ^\pi \to A^\pi: subtract the state value baseline; it has zero expected score-function term.

That is the math behind policy gradient: do not backprop through the reward. Backprop through the log-probability of the sampled action, weighted by how much better its future was than expected.

Citation

Please cite this work as:

Xuhui Zhou, “Policy Gradient Review 2026: From One Answer”, 2026.

Or use the BibTeX citation:

@misc{zhou2026policygradient,
  author = {Xuhui Zhou},
  title = {Policy Gradient Review 2026: From One Answer},
  year = {2026},
  howpublished = {\url{https://xuhuiz.com/blog/policy-gradient-from-one-action}},
}