Skip to content
MLiS Hub
On this page

Rare Trajectory Sampling with Reinforcement Learning

Exact Solution

The method used here is backwards induction: a single pass over the grid that solves for the optimal policy parameters analytically, without any sampling.

Solving for the Optimal Parameters

Recall that the value function at state ss is,

Vπθ(s)=∑a∈𝒜︀πθ(a|s)Qπθ(s,a), V_{\pi_\theta}(s) = \sum_{a \in \mathcal{A}} \pi_\theta(a|s) \, Q_{\pi_\theta}(s, a),(1)

where Qπθ(s,a)Q_{\pi_\theta}(s, a) is the expected return from taking action aa in state ss and following πθ\pi_\theta thereafter. To simplify the expressions that follow, we introduce the shorthand

β(s,a)=W(s′,s,a)+γVπθ(s′), \beta(s, a) = W(s', s, a) + \gamma V_{\pi_\theta}(s'),(2)

which absorbs the immediate transition reward and the discounted future value into a single quantity. With this notation, Qπθ(s,a)=β(s,a)Q_{\pi_\theta}(s, a) = \beta(s, a) and the value function becomes Vπθ(s)=∑aπθ(a|s)β(s,a)V_{\pi_\theta}(s) = \sum_a \pi_\theta(a|s)\,\beta(s,a). The optimal policy is the one that maximises Vπθ(s)V_{\pi_\theta}(s) at every state. Rather than following gradients towards this maximum, we can find it directly by setting dV/dθs=0dV/d\theta_s = 0 and solving.

Working through the derivative (which involves the sigmoid identity dσ(aθs)/dθs=aσ(aθs)(1−σ(aθs))d\sigma(a\theta_s)/d\theta_s = a\sigma(a\theta_s)(1 - \sigma(a\theta_s)) and the entropy contribution from the reward), the condition dV/dθs=0dV/d\theta_s = 0 simplifies to a remarkably clean result:

θs=β(s,↑)−β(s,↓), \theta_s = \beta(s, \uparrow) - \beta(s, \downarrow),(3)

which, expanding β\beta, is,

θs=W(s↑′,s,↑)+γVπθ(s↑′)−W(s↓′,s,↓)−γVπθ(s↓′). \theta_s = W(s'_\uparrow, s, \uparrow) + \gamma V_{\pi_\theta}(s'_\uparrow) \;-\; W(s'_\downarrow, s, \downarrow) - \gamma V_{\pi_\theta}(s'_\downarrow).(4)

The interpretation is intuitive: the optimal parameter at each state is simply the difference in expected future reward between stepping up and stepping down. A large positive θs\theta_s means going up is strongly preferred; θs=0\theta_s = 0 means the two directions are equally valuable. The entropy regularisation in the reward ensures this solution is always finite, the policy never degenerates to a hard deterministic rule.

Why we can Iterate Backwards

What makes backwards induction work is that the excursion problem has a defined Terminal boundary condition: at the terminal time TT, the value of every state is fixed by the problem definition (the boundary reward), with no future steps to consider.

Starting from the boundary, the values there are known. One step back from the boundary, Equation 4 can be evaluated exactly, the next-state values are already in hand. Those states then have known values, and so on, peeling the grid back towards the starting point one column at a time.

 t = T        t = T-1         t = T-2       ...        t = 0
 
[known]  →  [compute θ]  →  [compute θ]  →  ...  →  [compute θ]

The two special conditions that make this work are:

  1. Finite horizon: the walk has a fixed terminal time TT, so the backwards sweep always terminates.
  2. Known boundary values: V(s)V(s) at t=Tt = T is determined entirely by the boundary reward WW, with no policy-dependent terms remaining.

Neither condition holds in general RL problems, which is precisely why exact backwards induction cannot replace the policy gradient methods discussed earlier. We return to this limitation in the conclusion.

Computing the Value Function Stably

To run the backwards sweep, we need to evaluate V(s)V(s) at each state as we go. Substituting the optimal policy back into Equation 1 and writing Z=eβ↑+eβ↓Z = e^{\beta_\uparrow} + e^{\beta_\downarrow}, the optimal value function reduces to,

Vs=log⁡(eβ↑+eβ↓)−log⁡2, V_s = \log\!\left(e^{\beta_\uparrow} + e^{\beta_\downarrow}\right) - \log 2,(5)

where the β(s,a)\beta(s,a) terms cancel exactly in the sum (a consequence of the softmax structure of the optimal policy). This log-sum-exp form is compact and exact, but directly computing the exponentials is numerically dangerous.

When either β\beta is large, eβe^\beta overflows to inf in floating-point arithmetic, even though the final answer is perfectly finite. For example, β↑=1000\beta_\uparrow = 1000 and β↓=999\beta_\downarrow = 999 should give a value around 999.8999.8, but naively evaluating e1000+e999e^{1000} + e^{999} immediately overflows.

The Log-Sum-Exp Trick

The fix is to factor out the larger of the two exponentials before taking the logarithm. Letting m=max⁡(β↑,β↓)m = \max(\beta_\uparrow, \beta_\downarrow),

log⁡(eβ↑+eβ↓)=m+log⁡(eβ↑−m+eβ↓−m), \log\!\left(e^{\beta_\uparrow} + e^{\beta_\downarrow}\right) = m + \log\!\left(e^{\beta_\uparrow - m} + e^{\beta_\downarrow - m}\right),(6)

so the stable expression for the value function is,

Vs=m+log⁡(eβ↑−m+eβ↓−m)−log⁡2. V_s = m + \log\!\left(e^{\beta_\uparrow - m} + e^{\beta_\downarrow - m}\right) - \log 2.(7)

Since m=max⁡(β↑,β↓)m = \max(\beta_\uparrow, \beta_\downarrow), both shifted exponents β↑−m\beta_\uparrow - m and β↓−m\beta_\downarrow - m are non-positive, so both exponentials lie safely in (0,1](0, 1]. The larger equals e0=1e^0 = 1 exactly, and overflow is impossible by construction.

Numerical example

Let β↑=1000\beta_\uparrow = 1000 and β↓=999\beta_\downarrow = 999. Direct evaluation overflows. The stable form gives m=1000m = 1000, and

Vs=1000+log⁡(e0+e−1)−log⁡2=1000+log⁡(1+e−1)−log⁡2≈999.807, V_s = 1000 + \log(e^0 + e^{-1}) - \log 2 = 1000 + \log(1 + e^{-1}) - \log 2 \approx 999.807,

a finite result computed entirely from numbers in (0,1](0, 1].

What the Exact Solution Gives Us

The backwards induction solution provides the ground truth optimal policy for the excursion problem, a fixed reference against which both the tabular and neural network approaches can be evaluated. In the KL divergence section that follows, we use this exact solution as the target distribution when measuring how closely the learned policies have converged. Crucially, the exact solution requires no sampling and no iterative training: it is a single backwards sweep over the grid, 𝒪︀(|S|)\mathcal{O}(|S|) in the number of states.

This is powerful, but its applicability is narrow. It requires finite horizon, known boundary conditions, and a state space small enough to sweep explicitly. Additionally, there should be no cycles in the state space - we can apply this method as the state contains time which only flows in one direction. For the generalisation goals of this project, and for real physical systems where the Doob hh-transform cannot be computed directly, the RL methods remain essential. We use this exact solution to benchmark our RL approach on this simple toy problem.


← Back to the main article: Finding an exact solution