Deriving policy gradient theorem with log-derivative trick
The derivation starts from the expected return objective and uses the log-derivative trick to move the gradient inside the expectation over trajectories, revealing the gradient as an expectation of the return weighted by the gradient of the log policy. This shows the gradient depends on adjusting action probabilities rather than differentiating the reward, enabling optimization despite non-differentiable rewards by shifting the action probability density.
A convenient way to write the return that a stochastic policy induces is as an expectation over complete trajectories
[
\tau = (s_0,a_0,s_1,a_1,\dots),\qquad J(\theta);=;\mathbb{E}{\tau\sim p\theta} \bigg[\sum_{t=0}^\infty \gamma^t r(s_t,a_t)\bigg],
]
with
[
p_\theta(\tau);=;p(s_0),\prod_{t\geq 0} \pi_\theta(a_t\mid s_t),p(s_{t+1}\mid s_t,a_t).
]
The only factors that depend on (\theta) are the policy terms. Differentiating (J) naïvely produces a sum over (\tau) of (\nabla_\theta p_\theta(\tau)), which is hard to manipulate directly. The log–derivative or “likelihood–ratio” identity
[
\nabla_\theta p_\theta(x) ;=; p_\theta(x),\nabla_\theta \log p_\theta(x)
]
allows you to pull the derivative inside the expectation. Applying it to (J) gives
[
\begin{aligned}
\nabla_\theta J(\theta)
&= \sum_\tau R(\tau),\nabla_\theta p_\theta(\tau)
;=; \sum_\tau R(\tau),p_\theta(\tau),\nabla_\theta \log p_\theta(\tau)\
&= \mathbb{E}{\tau\sim p\theta}\Big[,R(\tau),\nabla_\theta \sum_{t}\log \pi_\theta(a_t\mid s_t),\Big]
;=; \mathbb{E}{\tau\sim p\theta}\Big[\sum_{t}R(\tau),\nabla_\theta \log \pi_\theta(a_t\mid s_t)\Big].
\end{aligned}
]
If you now redistribute the return (R(\tau)) over time, e.g. by writing it as the return from time (t) onward (G_t=\sum_{k\geq t}\gamma^{k-t}r(s_k,a_k)), you obtain the REINFORCE form
[
\nabla_\theta J(\theta) ;=; \mathbb{E}{\tau\sim p\theta}\Big[\sum_{t} G_t,\nabla_\theta \log \pi_\theta(a_t\mid s_t)\Big].
]
Using the discounted state visitation distribution (d^{\pi_\theta}) and the action–value function (Q^{\pi_\theta}(s,a)=\mathbb{E}[G_t\mid s_t!=!s,a_t!=!a]), this can be written compactly as the policy gradient theorem,
[
\nabla_\theta J(\theta) ;=; \mathbb{E}{s\sim d^{\pi\theta},,a\sim \pi_\theta}\big[,\nabla_\theta \log \pi_\theta(a\mid s),Q^{\pi_\theta}(s,a),\big].
]
Two key points are visible in this derivation:
-
The gradient appears as an expectation of the score function (\nabla_\theta\log\pi_\theta) weighted by returns; there is no derivative of (r(s,a)) or of the environment dynamics.
-
The way you improve (J) is by increasing the probability mass of action choices that tend to lead to high return. A nondifferentiable or even discontinuous reward signal can still serve as the weight (R(\tau)): you do not back‑propagate through it, you adjust (\theta) so that the policy’s density shifts towards trajectories with larger observed reward.
This likelihood–ratio construction is what makes policy gradient methods applicable in environments with non‑smooth dynamics or rewards: the only differentiation is through the log of the policy’s probability density, not through the reward function itself.