Do one Bellman expectation backup for a single state
Why it matters Every value-based method, from policy evaluation to the critic in actor-critic, repeats this exact backup.
Policy evaluation improves a value table one state at a time. Each update asks: if I follow my policy from here, what reward do I get now plus what is the discounted value of where I land?
Write bellman_backup(V, P, R, pi, s, gamma).
- V: length S array of current state values.
- P: (S, A, S) array, P[s, a, s2] is the chance of moving to s2 after action a in state s.
- R: (S, A) array of immediate rewards.
- pi: (S, A) array of action probabilities of the policy.
- Return the new value of state s as a float: sum over a of pi[s, a] * (R[s, a] + gamma * sum over s2 of P[s, a, s2] * V[s2]).
def bellman_backup(V, P, R, pi, s, gamma)Example
V [0, 10], P[0, 0] = [1, 0], P[0, 1] = [0, 1], R[0] = [1, 0], pi[0] = [0.5, 0.5], gamma 0.9 gives 0.5 * (1 + 0) + 0.5 * (0 + 9) = 5.0.
Understand the idea
A value V(s) says how good it is to stand in state s if you keep following your policy. The Bellman equation says that value equals what you expect to collect right now plus the discounted value of the next state, averaged over what the policy does and where the world sends you.
Tiny example: from state 0 the policy flips a coin between action 0 (reward 1, stay in state 0 which is worth 0) and action 1 (reward 0, move to state 1 which is worth 10). With gamma 0.9: action 0 gives 1 + 0.9 * 0 = 1, action 1 gives 0 + 0.9 * 10 = 9, the average is 5.
Rule: V(s) = sum_a pi(a|s) * ( R(s, a) + gamma * sum_s2 P(s2|s, a) * V(s2) ).
Repeating this backup over all states until nothing changes is policy evaluation, the first half of policy iteration and the job of every critic network.
Shapes and data flow
P[s]: (A, S) @ V: (S,) -> (A,) next values; R[s]: (A,); pi[s] @ q -> scalar