Equations · Bellman, “Dynamic Programming” (1957); this expectation form for a fixed policy is the standard one in reinforcement learning

The Bellman equation, explained term by term

The value of a state equals the expected immediate reward plus the discounted value of wherever you land next. It turns a sum over an entire future into a one-step relation between neighbouring states, which is what most RL algorithms solve or approximate.

vπ(s)=∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γ vπ(s′)]v_\pi(s) = \sum_{a} \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\left[r + \gamma\, v_\pi(s')\right]

Open in the decoder →

Term by term

  1. vπ(s)v_\pi(s)

    The value of state ss under policy π\pi: the expected return when starting in ss and following π\pi. Many papers write Vπ(s)V^\pi(s).

  2. ∑aπ(a∣s)\sum_{a} \pi(a \mid s)

    Average over the actions the policy might take in ss, weighted by their probabilities. π\pi is the policy, not 3.14.

  3. ∑s′,rp(s′,r∣s,a)\sum_{s', r} p(s', r \mid s, a)

    Average over what the environment does next: the probability of landing in state s′s' with reward rr after taking aa in ss. The prime marks "the next state".

  4. r+γ vπ(s′)r + \gamma\, v_\pi(s')

    The reward now plus the next state's value, discounted by γ∈[0,1)\gamma \in [0, 1). A γ\gamma of 0.9 means a reward one step later is worth 90% of the same reward now.

A small worked example

In state ss the policy picks each of two actions with probability 0.50.5, and γ=0.9\gamma = 0.9. Action 1 gives reward 1 and leads to a state with value 10; action 2 gives 0 and leads to a state with value 4. Then vπ(s)=0.5(1+9)+0.5(0+3.6)=6.8v_\pi(s) = 0.5(1 + 9) + 0.5(0 + 3.6) = 6.8. The optimal version replaces the average over actions with a max: v∗(s)=10v_*(s) = 10.

In code

import numpy as np

def evaluate_policy(P, R, pi, gamma=0.9, iters=500):
    # P[s, a, s2]: transition probs; R[s, a]: expected reward; pi[s, a]: policy
    v = np.zeros(P.shape[0])
    for _ in range(iters):           # apply the Bellman equation until it settles
        q = R + gamma * P @ v        # (S, A)
        v = (pi * q).sum(axis=1)
    return v

Every symbol in it

Vπ(s)=Eπ[Gt∣st=s]V^\pi(s) = \mathbb{E}_\pi\left[G_t \mid s_t = s\right]Value function Vxi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)st,at,rt,st+1s_t, a_t, r_t, s_{t+1}State s and action a∑i=1Nxi\sum_{i=1}^{N} x_iSummationπθ(a∣s)\pi_\theta(a \mid s)Pip(y∣x)p(y \mid x)Given (conditional bar)p(x),  q(z)p(\mathbf{x}), \; q(\mathbf{z})Probability p(x)f′(x)f'(x)Primert=R(st,at)r_t = R(s_t, a_t)Reward rγ\gammaGamma

Other equations