Simple Algorithm
Reinforcement Learning · Bootstrapping · 22 min

Distributional RL

Learning the whole distribution of returns, not just its average.

Two policies can have the same expected return while one pays a steady salary and the other buys lottery tickets. Distributional RL learns the whole probability distribution of the return, through a Bellman equation for distributions. This lesson derives that equation and the equations its mean and variance obey, works one categorical backup by hand on a lottery, builds the two standard representations, C51's atoms and QR-DQN's quantiles, and uses the learned distributions to measure risk.

What you’ll learn

  • State the distributional Bellman equation as an equality in distribution, and derive the Bellman equations obeyed by the mean and the variance of the return.
  • Compute a categorical projection by hand, and explain C51's cross-entropy loss and why the projection preserves the mean inside its support.
  • Derive why the minimizer of the quantile loss is a quantile, and find QR-DQN's fixed points on a two-outcome return.
  • Compute CVaR from a learned distribution, and explain how coarse representations bias it.

Returns are random, and they obey a Bellman equation

Even for a fixed policy the return is a random variable: rewards are random, transitions are random, and the policy may randomize. Write Zπ(s,a)Z^\pi(s,a) for the return from (s,a)(s,a) under π\pi; its probability distribution is the return distribution, and its mean is Qπ(s,a)=E[Zπ(s,a)]Q^\pi(s,a)=\E[Z^\pi(s,a)]. A running example: from s0s_0 a lottery pays +10+10 with probability 0.1 and 0 otherwise, then state s1s_1 pays +1+1, then the episode ends. With γ=0.9\gamma=0.9 the return from s0s_0 is 0.90.9 with probability 0.9 and 10.910.9 with probability 0.1. Its mean, 1.9, is a return that never happens.

The return satisfies a Bellman equation of its own, the distributional Bellman equation:

Zπ(s,a) =D R+γ Zπ(S′,A′),R,S′∼P(⋅∣s,a),A′∼π(⋅∣S′)Z^\pi(s,a)\ \overset{D}{=}\ R+\gamma\,Z^\pi(S',A'),\qquad R,S'\sim P(\cdot\mid s,a),\quad A'\sim\pi(\cdot\mid S')
=D\overset{D}{=} means equal in distribution: the two sides have the same law. On the right, the next return Zπ(S′,A′)Z^\pi(S',A') is drawn independently of RR given (S′,A′)(S',A'). The distributional Bellman operator does three things to the next state's distribution: shrink it by γ\gamma, shift it by the reward, and mix over the possible (R,S′,A′)(R,S',A').
DerivationThe mean and the variance obey Bellman equations too
  1. E[Zπ(s,a)∣R,S′,A′]=R+γQπ(S′,A′),Var⁡[Zπ(s,a)∣R,S′,A′]=γ2Var⁡Zπ(S′,A′)\E\big[Z^\pi(s,a)\mid R,S',A'\big]=R+\gamma Q^\pi(S',A'),\qquad\Var\big[Z^\pi(s,a)\mid R,S',A'\big]=\gamma^2\Var Z^\pi(S',A')

    Condition on what happens in the first step. Given (R,S′,A′)(R,S',A'), the return is RR plus γ\gamma times an independent draw of Zπ(S′,A′)Z^\pi(S',A'), so

  2. Qπ(s,a)=E[R+γQπ(S′,A′)]Q^\pi(s,a)=\E\big[R+\gamma Q^\pi(S',A')\big]

    The tower property gives the ordinary Bellman equation for the mean:

  3. Var⁡Zπ(s,a)=Var⁡(R+γQπ(S′,A′))+γ2 E[Var⁡Zπ(S′,A′)]\Var Z^\pi(s,a)=\Var\big(R+\gamma Q^\pi(S',A')\big)+\gamma^2\,\E\big[\Var Z^\pi(S',A')\big]

    The law of total variance (variance = mean of the conditional variance + variance of the conditional mean) gives a Bellman equation for the variance, with "reward" Var⁡(R+γQπ(S′,A′))\Var(R+\gamma Q^\pi(S',A')) and discount γ2\gamma^2 (Sobel, 1982):

Predict before you look

Use the variance equation on the lottery at s0s_0: the return from s1s_1 is always 1 (variance 0, mean 1). What is the standard deviation of the return from s0s_0?

Show the answer

3

The second term vanishes, and R+γQπ(s1)=R+0.9R+\gamma Q^\pi(s_1)=R+0.9 has the variance of RR: 102×0.1×0.9=910^2\times0.1\times0.9=9, so the standard deviation is 3. Directly: E[G2]=0.9⋅0.81+0.1⋅118.81=12.61\E[G^2]=0.9\cdot0.81+0.1\cdot118.81=12.61 and 12.61−1.92=912.61-1.9^2=9.

Why learn the whole distribution? First, risk: anyone who cares about more than the average, an investor or a robot near a cliff, needs the spread and the tails (step 4, and Risk-Sensitive RL). Second, a richer training signal: predicting many properties of the return gives a deep network more to learn its features from. Third, it worked: on 57 Atari games, the first deep distributional agent, C51 (Bellemare, Dabney & Munos, 2017), raised DQN's median human-normalized score from 79% to 178%, although it still chose actions by the mean. Why it helps a mean-maximizing agent is still debated (Distributional Bellman Operators). Neuroscience has taken note too: there is evidence that dopamine neurons encode a distribution of reward predictions rather than a single one (TD Learning in the Brain).

Categorical returns: C51’s atoms and projection

C51 represents each Z(s,a)Z(s,a) as a categorical distribution on KK fixed atoms z0<z1<⋯<zK−1z_0<z_1<\dots<z_{K-1}, evenly spaced on [Vmin⁡,Vmax⁡][V_{\min},V_{\max}] with gap Δz=(Vmax⁡−Vmin⁡)/(K−1)\Delta z=(V_{\max}-V_{\min})/(K-1). The network outputs KK logits per action, a softmax turns them into probabilities pi(s,a)p_i(s,a), and the value is Q(s,a)=∑ipi(s,a)ziQ(s,a)=\sum_ip_i(s,a)z_i. On Atari, with rewards clipped to ±1\pm1, C51 used K=51K=51 atoms on [−10,10][-10,10], hence its name. For a sampled transition (s,a,r,s′)(s,a,r,s') with a⋆=arg max⁡a′Q(s′,a′)a^\star=\argmax_{a'}Q(s',a'), each atom zjz_j of Z(s′,a⋆)Z(s',a^\star) moves to r+γzjr+\gamma z_j; these points fall between atoms, so the categorical projection Φ\Phi splits each one's probability between its two nearest atoms, in proportion to closeness:

(Φ T^Z)i=∑j=0K−1[1−∣[ r+γzj ]Vmin⁡Vmax⁡−zi∣Δz]01 pj(s′,a⋆)\big(\Phi\,\hat{\mathcal T}Z\big)_i=\sum_{j=0}^{K-1}\Big[1-\frac{\big|[\,r+\gamma z_j\,]_{V_{\min}}^{V_{\max}}-z_i\big|}{\Delta z}\Big]_0^1\,p_j(s',a^\star)
[x]ab[x]_{a}^{b} clips xx to [a,b][a,b], so mass shifted beyond the support piles onto the end atoms; the outer clip to [0,1][0,1] means each shifted atom feeds only its two neighbors.
DerivationOne backup by hand: the lottery with 51 atoms on [0, 12]
  1. Φ δ1=56 δ0.96+16 δ1.20\Phi\,\delta_1=\tfrac56\,\delta_{0.96}+\tfrac16\,\delta_{1.20}

    Here Δz=12/50=0.24\Delta z=12/50=0.24. From s1s_1 the return is exactly 1, which lies between the atoms 0.96 and 1.20, so the learned Z(s1)Z(s_1) is the projection of the point mass at 1, with weights (1.20−1)/0.24(1.20-1)/0.24 and (1−0.96)/0.24(1-0.96)/0.24:

  2. 0.9×0.96=0.864,0.9×1.20=1.080.9\times0.96=0.864,\qquad0.9\times1.20=1.08

    Back up s0s_0 with the draw r=0r=0. Scaling by γ=0.9\gamma=0.9 moves the two atoms; shifting by 0 leaves them:

  3. 56(0.4 δ0.72+0.6 δ0.96)+16(0.5 δ0.96+0.5 δ1.20)=13 δ0.72+712 δ0.96+112 δ1.20\tfrac56\big(0.4\,\delta_{0.72}+0.6\,\delta_{0.96}\big)+\tfrac16\big(0.5\,\delta_{0.96}+0.5\,\delta_{1.20}\big)=\tfrac13\,\delta_{0.72}+\tfrac7{12}\,\delta_{0.96}+\tfrac1{12}\,\delta_{1.20}

    Project: 0.864 lies between 0.72 and 0.96 (weights 0.4 and 0.6), and 1.08 halfway between 0.96 and 1.20:

  4. 13(0.72)+712(0.96)+112(1.20)=0.9\tfrac13(0.72)+\tfrac7{12}(0.96)+\tfrac1{12}(1.20)=0.9

    Its mean is 0.24+0.56+0.10=0.90.24+0.56+0.10=0.9, exactly this branch's return: inside the support, every split is a linear interpolation, so the projection preserves the mean. It does not preserve the shape: a point mass at 0.9 has become three atoms.

Predict before you look

At the fixed point, Z(s0)Z(s_0) mixes this r=0r=0 branch (probability 0.9) with the jackpot branch r=10r=10 (probability 0.1), whose atoms land near 10.9. How much total probability does the learned Z(s0)Z(s_0) put on the atom at 0.72?

Show the answer

0.3

0.9×13=0.30.9\times\tfrac13=0.3; the jackpot branch adds nothing that low. The true return is never below 0.9, yet the learned distribution puts 30% of its mass at 0.72. This smearing is harmless for the mean and costly for the tails, as step 4 shows.

C51 trains by cross-entropy between the projected target and its prediction, L=−∑i(ΦT^Z)ilog⁡pi(s,a)\loss=-\sum_i(\Phi\hat{\mathcal T}Z)_i\log p_i(s,a), with the target computed by a target network as in DQN. With softmax logits ℓi\ell_i, the gradient is simply ∂L/∂ℓi=pi(s,a)−(ΦT^Z)i\partial\loss/\partial\ell_i=p_i(s,a)-(\Phi\hat{\mathcal T}Z)_i: prediction minus target. Actions are still chosen ε\varepsilon-greedily by the mean.

Quantile returns: QR-DQN

QR-DQN (Dabney, Rowland, Bellemare & Munos, 2018) flips the parameterization: KK atoms, each with probability 1/K1/K, whose locations θ1(s,a),…,θK(s,a)\theta_1(s,a),\dots,\theta_K(s,a) are learned. There is no support to choose in advance and no projection step: the targets for (s,a)(s,a) are just the KK points r+γθj(s′,a⋆)r+\gamma\theta_j(s',a^\star). Where should θi\theta_i go? To the quantile of the return at level τ^i=2i−12K\hat\tau_i=\frac{2i-1}{2K}, the midpoint of the ii-th slice of probability (the Fixed Point lessons prove this is the best equally weighted KK-atom approximation in the Wasserstein-1 distance). A loss whose minimizer is a quantile does the job.

DerivationWhy quantile regression finds quantiles
  1. ρτ(u)=u (τ−1[u<0]),u=Z−θ\rho_\tau(u)=u\,\big(\tau-\mathbf 1[u<0]\big),\qquad u=Z-\theta

    The quantile regression loss (Koenker & Bassett, 1978) charges a residual uu asymmetrically: τu\tau u if the target is above the estimate, (1−τ)∣u∣(1-\tau)\abs u if below.

  2. ddθE[ρτ(Z−θ)]=−τPr⁡(Z>θ)+(1−τ)Pr⁡(Z<θ)=F(θ)−τ\frac{d}{d\theta}\E\big[\rho_\tau(Z-\theta)\big]=-\tau\Pr(Z>\theta)+(1-\tau)\Pr(Z<\theta)=F(\theta)-\tau

    For a target ZZ with a continuous CDF FF, differentiate the expected loss in θ\theta: raising θ\theta costs τ\tau for every target above it and saves 1−τ1-\tau for every target below it.

  3. θ⋆=F−1(τ),θ←θ+α(τ−1[Z<θ])\theta^\star=F^{-1}(\tau),\qquad\theta\leftarrow\theta+\alpha\big(\tau-\mathbf 1[Z<\theta]\big)

    The expected loss is convex in θ\theta and its derivative vanishes where F(θ)=τF(\theta)=\tau: the minimizer is the τ\tau-quantile. The single-sample gradient gives the update, an unbiased estimate of the expected gradient, which the Wasserstein distance itself does not offer.

QR-DQN minimizes 1K∑i∑jρτ^iκ(r+γθj(s′,a⋆)−θi(s,a))\frac1K\sum_{i}\sum_j\rho^\kappa_{\hat\tau_i}\big(r+\gamma\theta_j(s',a^\star)-\theta_i(s,a)\big) with the quantile Huber loss ρτκ(u)=∣τ−1[u<0]∣ Lκ(u)\rho^\kappa_\tau(u)=\abs{\tau-\mathbf 1[u<0]}\,L_\kappa(u), where LκL_\kappa is the Huber loss with threshold κ=1\kappa=1: quadratic near zero, which smooths the kink at zero that the authors suspected could limit performance with networks. On 57 Atari games it reached a median human-normalized score of 211%, against C51's 178%. On the lottery the fixed points are easy to find: a quantile at a level below 0.9 settles at 0.9, one above 0.9 at 10.9. With K=21K=21, the levels 3942\frac{39}{42} and 4142\frac{41}{42} exceed 0.9, so 19 quantiles sit at 0.9 and 2 at 10.9, and the representation's mean is (19⋅0.9+2⋅10.9)/21≈1.852(19\cdot0.9+2\cdot10.9)/21\approx1.852, not 1.9: unlike the categorical projection, the quantile representation does not preserve the mean.

Predict before you look

Use only K=3K=3 quantiles on the lottery, at levels 16,12,56\tfrac16,\tfrac12,\tfrac56. What mean does the learned representation report at its fixed point?

Show the answer

0.9

All three levels are below 0.9, so all three quantiles settle at 0.9 and the mean reads 0.9. The jackpot, which contributes 1.0 of the true mean 1.9, is invisible: a 10% event is resolved only when some level τ^i\hat\tau_i exceeds 0.9.

Using the distribution: risk

A learned distribution supports decisions that the mean cannot. The most common risk measure in RL is the conditional value at risk, the mean of the worst α\alpha fraction of outcomes:

CVaR⁡α(G)=1α∫0αFG−1(u) du,\operatorname{CVaR}_\alpha(G)=\frac1\alpha\int_0^\alpha F_G^{-1}(u)\,du,

which for a continuous distribution is E[G∣G≤FG−1(α)]\E[G\mid G\le F_G^{-1}(\alpha)]; CVaR⁡1\operatorname{CVaR}_1 is the mean. For the lottery, every α≤0.9\alpha\le0.9 gives 0.9: the worst 90% of outcomes are all 0.9. From a categorical distribution, add up the lowest atoms' probabilities until they reach α\alpha, taking a fraction of the last one; from KK quantiles, average the lowest αK\alpha K of them.

Think first What is CVaR⁡0.1\operatorname{CVaR}_{0.1} of the categorical fixed point computed in step 2, where the atom at 0.72 carries probability 0.3? And with 11 atoms on [0,12][0,12]?

With 51 atoms the lowest atom, 0.72, already carries 0.3 > 0.1 of the mass, so CVaR⁡0.1=0.72\operatorname{CVaR}_{0.1}=0.72, below the true 0.9: the projection's smearing biases the tail low. With 11 atoms (Δz=1.2\Delta z=1.2) the point mass at 0.9 is split between the atoms at 0 and 1.2, and 22.5% of the mass ends up on the atom at 0, so the learned CVaR⁡0.1\operatorname{CVaR}_{0.1} is 0. With 101 atoms it is 0.84. The mean stays exactly 1.9 in all three cases (for 11 atoms or more).

An agent can use such numbers to act: choose actions by CVaR⁡α\operatorname{CVaR}_\alpha of Z(s,a)Z(s,a) instead of its mean, to avoid catastrophes. Doing this greedily at every step is, however, not the same as maximizing the CVaR of the whole return, because CVaR is not time-consistent; Risk-Sensitive RL works out what each approach really optimizes.

Key takeaways

  • The return is a random variable whose law obeys Zπ(s,a)=DR+γZπ(S′,A′)Z^\pi(s,a)\overset{D}{=}R+\gamma Z^\pi(S',A'): shrink by γ\gamma, shift by the reward, mix over transitions. Its mean and its variance obey Bellman equations with discounts γ\gamma and γ2\gamma^2.
  • C51 learns probabilities on fixed atoms; the categorical projection splits each shifted atom between its neighbors, which preserves the mean inside the support but smears point masses, and training minimizes cross-entropy to the projected target.
  • QR-DQN learns the locations of equally likely atoms at the quantile midpoints by quantile regression, whose minimizer is the τ\tau-quantile; it needs no support or projection, but its mean is not exactly the true mean (1.852 against 1.9 on the lottery with 21 quantiles).
  • CVaR averages the worst α\alpha of outcomes; coarse representations bias it badly (0.72 instead of 0.9 with 51 atoms, 0 with 11) while barely moving the mean, and acting on it step by step is not the same as optimizing the CVaR of the return.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Which statistic of the return obeys a Bellman equation whose discount is γ2\gamma^2?

  1. The mean
  2. The variance
  3. The median
  4. The probability that the return is positive
Show the answer

B. The variance

By the law of total variance, Var⁡Zπ(s,a)=Var⁡(R+γQπ(S′,A′))+γ2E[Var⁡Zπ(S′,A′)]\Var Z^\pi(s,a)=\Var(R+\gamma Q^\pi(S',A'))+\gamma^2\E[\Var Z^\pi(S',A')]: scaling a random variable by γ\gamma scales its variance by γ2\gamma^2. The mean obeys the ordinary equation with discount γ\gamma.

Question 2

The categorical projection is applied to a distribution lying entirely inside [Vmin⁡,Vmax⁡][V_{\min},V_{\max}]. What happens to the mean?

  1. It is unchanged, because each point is split between its two neighboring atoms by linear interpolation
  2. It moves to the nearest atom
  3. It drops by half an atom gap
  4. It can change by up to Δz\Delta z in either direction
Show the answer

A. It is unchanged, because each point is split between its two neighboring atoms by linear interpolation

A point yy between ziz_i and zi+1z_{i+1} goes to ziz_i with weight (zi+1−y)/Δz(z_{i+1}-y)/\Delta z and to zi+1z_{i+1} with weight (y−zi)/Δz(y-z_i)/\Delta z, and the weighted average of the two atoms is yy again. By linearity the mean of any distribution inside the support is preserved. Mass outside the support is clipped to the end atoms, which does change the mean.

Question 3

For a target ZZ with continuous CDF FF, which θ\theta minimizes E[ρτ(Z−θ)]\E[\rho_\tau(Z-\theta)] with ρτ(u)=u(τ−1[u<0])\rho_\tau(u)=u(\tau-\mathbf 1[u<0])?

  1. The mean of ZZ
  2. The median of ZZ, whatever τ\tau is
  3. The mode of ZZ
  4. The τ\tau-quantile, F−1(τ)F^{-1}(\tau)
Show the answer

D. The τ\tau-quantile, F−1(τ)F^{-1}(\tau)

The derivative of the expected loss is F(θ)−τF(\theta)-\tau, which vanishes at F(θ)=τF(\theta)=\tau, and the expected loss is convex. Each QR-DQN atom uses its own level τ^i\hat\tau_i, so together they learn KK quantiles.

Question 4

The lottery returns 0.9 with probability 0.9 and 10.9 with probability 0.1. What is CVaR⁡0.95\operatorname{CVaR}_{0.95}?

  1. 0.9
  2. About 1.43
  3. 1.9
  4. 10.9
Show the answer

B. About 1.43

The worst 95% consist of all the 0.9 outcomes (probability 0.9) and 0.05 of the 10.9 outcomes: (0.9⋅0.9+0.05⋅10.9)/0.95=1.355/0.95≈1.426(0.9\cdot0.9+0.05\cdot10.9)/0.95=1.355/0.95\approx1.426.

Go deeper

Further reading

  • M. G. Bellemare, W. Dabney & R. Munos, "A distributional perspective on reinforcement learning", ICML (2017).
  • W. Dabney, M. Rowland, M. G. Bellemare & R. Munos, "Distributional reinforcement learning with quantile regression", AAAI (2018).
  • M. G. Bellemare, W. Dabney & M. Rowland, Distributional Reinforcement Learning (MIT Press, 2023).
  • M. J. Sobel, "The variance of discounted Markov decision processes", Journal of Applied Probability 19(4), 794–802 (1982).