Skip to content
MLiS Hub
On this page

Rare Trajectory Sampling with Reinforcement Learning

Reward Function

The KL derivation established the form of the per-step reward directly: it is the unique reward for which maximising the RL objective is equivalent to minimising the KL divergence against the Doob distribution. This section unpacks what each component of that reward encodes and why both are necessary.

The Return and the Objective

Before specifying the rewards, it is useful to state precisely what is being optimised. In RL, the agent does not optimise individual rewards in isolation; it optimises the return: the total accumulated reward over a trajectory. The return-to-go from timestep tt is,

χ(ω)t=∑t′=tT−1γt′−tr(st′,at′,st′+1), \chi(\omega)_t = \sum_{t'=t}^{T-1} \gamma^{t'-t} \, r(s_{t'}, a_{t'}, s_{t'+1}),(1)

where γ∈(0,1]\gamma \in (0, 1] is a discount factor that down-weights rewards received further in the future (we use γ=1\gamma = 1). The quantity χ(ω)0\chi(\omega)_0 is simply the total reward along the trajectory, the full return G(ω)G(\omega) introduced in RL Mehtods. The overall training objective is the expectation of this return over trajectories sampled from the current policy,

ℒ︀(θ)=𝔼ω∼πθ[χ(ω)0]. \mathcal{L}(\theta) = \mathbb{E}_{\omega \sim \pi_\theta}\left[\chi(\omega)_0\right].(2)

The return-to-go χ(ω)t\chi(\omega)_t reappears in the policy gradient updates of Policy Gradient: it is the signal that tells each state’s parameters whether the choices made from that point onward were good ones.

The Reward Components

The full per-step reward is,

r(s,a,s′)=W(s′,s,a)−ln⁡πθ(a|s)P0(a|s), r(s, a, s') = W(s', s, a) - \ln \frac{\pi_\theta(a|s)}{P_0(a|s)},(3)

where P0(a|s)=12P_0(a|s) = \frac{1}{2} is the baseline uniform random walk policy. The two terms have distinct roles. WW encodes the excursion condition, directing the agent towards trajectories that return to the origin. The log-ratio −ln⁡πθ(a|s)/P0(a|s)-\ln \pi_\theta(a|s)/P_0(a|s) penalises the policy whenever it deviates from the reference dynamics: if the policy assigns probability close to 1 to one action and close to 0 to the other, ln⁡(πθ/P0)\ln(\pi_\theta / P_0) becomes large and positive and the reward is heavily penalised.

To see why the log-ratio term is essential, consider removing it. A purely terminal reward creates a well-known failure mode: collapse to determinism. In standard RL problems, the optimal policy for a deterministic environment is a deterministic policy, that is normally correct. Here the target is not just a policy that achieves the excursion condition, but the Doob distribution over all excursion trajectories. Once the policy finds any returning trajectory, the gradient signal concentrates entirely on those paths. The policy is pushed to reproduce them with higher and higher probability until it becomes nearly deterministic, the entropy of the trajectory distribution drops to near zero, the gradient signal vanishes, and training stalls. The log-ratio prevents this by keeping the full distribution over trajectories in play throughout training.

The Terminal Reward

The terminal penalty W(xT)W(x_T) encodes the excursion condition. It should be maximised at xT=0x_T = 0 and decrease as the final position moves away from the origin.

Two natural choices are a negative parabola,

W(xT)=−xT2, W(x_T) = -x_T^2,(4)

and a negative absolute value,

W(xT)=−|xT|, W(x_T) = -|x_T|,(5)

both of which are maximised at xT=0x_T = 0 and penalise the walker proportionally to how far it ends from the origin. The parabola penalises large deviations more aggressively; the absolute value treats all deviations uniformly in distance. In either case, a trajectory that returns exactly to the origin receives the maximum reward of zero, and all other trajectories receive a negative penalty.

The only additional reward structure at intermediate timesteps is a large negative penalty applied if the walker’s position crosses below x=0x = 0. This acts as a soft barrier, strongly discouraging paths that dip into negative territory and keeping the walk in the physically meaningful region of the state space.

Importantly, none of the rewards in this problem are stochastic. This matters for training stability: stochastic rewards introduce noise into the gradient estimates that is difficult to separate from the variance already present in trajectory sampling.


← Back to the main article: Crafting a reward function