Skip to main content
Open In Colab

Computing the value functions given a policy

This section derives a recursion for the value functions, like the one for the return, and then replaces the expectations with sums over actions and next states. Start with the state-value function: vπ(s)=Eπ[Gt∣St=s]=Eπ[∑k=0∞γkRt+1+k∣St=s]=Eπ[Rt+1+γRt+2+γ2Rt+3+⋯∣St=s]=Eπ[Rt+1+γ(Rt+2+γRt+3+… )∣St=s]=Eπ[Rt+1+γGt+1∣St=s]=Eπ[Rt+1+γvπ(St+1)∣St=s].\begin{aligned} v_\pi(s) &= \mathbb{E}_\pi [G_t \mid S_t=s] = \mathbb{E}_\pi [\textstyle \sum_{k=0}^{\infty}\gamma^k R_{t+1+k} \mid S_t=s] \\ &= \mathbb{E}_\pi [ R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots \mid S_t=s ] \\ &= \mathbb{E}_\pi [ R_{t+1} + \gamma (R_{t+2} + \gamma R_{t+3} + \dots) \mid S_t=s ] \\ &= \mathbb{E}_\pi [ R_{t+1} + \gamma G_{t+1} \mid S_t=s ] \\ &= \mathbb{E}_\pi [ R_{t+1} + \gamma v_\pi(S_{t+1}) \mid S_t=s ]. \end{aligned} The expectations are over the trajectories that the policy π\pi and the transition model generate together. Only the last step needs an argument: it replaces Eπ[Gt+1∣St=s]\mathbb{E}_\pi[G_{t+1} \mid S_t = s] by Eπ[vπ(St+1)∣St=s]\mathbb{E}_\pi[v_\pi(S_{t+1}) \mid S_t = s]. The step uses the Markov property of both the environment and the policy. The next state and reward depend only on the current state and action, and the policy chooses each action from the current state alone. So, given St+1S_{t+1}, the rewards after t+1t+1 do not depend on the earlier states, actions and rewards. Fix one transition from ss: action aa, reward rr and next state s′s'. Conditioning the remaining return on this transition gives the same expectation as conditioning on the next state alone, and because the policy is stationary, that expectation is the value of s′s': Eπ[Gt+1∣St=s,At=a,Rt+1=r,St+1=s′]=Eπ[Gt+1∣St+1=s′]=vπ(s′).\begin{aligned} \mathbb{E}_\pi[G_{t+1} \mid S_t = s, A_t = a, R_{t+1} = r, S_{t+1} = s'] &= \mathbb{E}_\pi[G_{t+1} \mid S_{t+1} = s'] \\ &= v_\pi(s'). \end{aligned} Averaging over the transitions that can follow ss, each with probability π(a∣s) p(s′,r∣s,a)\pi(a \mid s)\, p(s', r \mid s, a), gives Eπ[Gt+1∣St=s]=∑aπ(a∣s)∑s′,rp(s′,r∣s,a) vπ(s′)=Eπ[vπ(St+1)∣St=s].\begin{aligned} \mathbb{E}_\pi[G_{t+1} \mid S_t = s] &= \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\, v_\pi(s') \\ &= \mathbb{E}_\pi[v_\pi(S_{t+1}) \mid S_t = s]. \end{aligned} This recursion is the Bellman expectation equation: vπ(s)=Eπ[Rt+1+γ vπ(St+1)∣St=s].v_\pi(s) = \mathbb{E}_\pi [ R_{t+1} + \gamma\, v_\pi(S_{t+1}) \mid S_t=s ].
Output from cell 3 The backup tree for vπ(s)v_\pi(s) with one step of lookahead. The policy picks an action aa with probability π(a∣s)\pi(a \mid s), and the environment then moves to a next state s′s' and emits a reward rr with probability p(s′,r∣s,a)p(s', r \mid s, a). The tree shows the recursion. The value at the root is computed from the values vπ(s′)v_\pi(s') at the leaves, and each leaf is itself the root of the same tree one step later, whose value comes from the states one step further on. Each edge below an action is one possible outcome of the transition, a next state s′s' with a reward rr, so the rr on an edge is a sampled reward value Rt+1=rR_{t+1} = r, not the reward function. The reward function is the average over those edges, r(s,a)=∑s′,rp(s′,r∣s,a) rr(s, a) = \sum_{s', r} p(s', r \mid s, a)\, r, and it is the reward term in the equations below. By linearity, the expectation splits into two parts, the expected immediate reward and the discounted expected value of the next state: vπ(s)=Eπ[Rt+1∣St=s]+γ Eπ[vπ(St+1)∣St=s].v_\pi(s) = \mathbb{E}_\pi [ R_{t+1} \mid S_t=s ] + \gamma\, \mathbb{E}_\pi [ v_\pi(S_{t+1}) \mid S_t=s ]. The first part is the reward function averaged over the policy, ∑aπ(a∣s) r(s,a)\sum_a \pi(a \mid s)\, r(s, a). The same argument, with the first action also fixed, gives the Bellman expectation equation for the action-value function: qπ(s,a)=Eπ[Rt+1+γ vπ(St+1)∣St=s,At=a].q_\pi(s,a) = \mathbb{E}_\pi [ R_{t+1} + \gamma\, v_\pi(S_{t+1}) \mid S_t=s, A_t=a ]. To compute the two value functions, look at a single time step. In state St=sS_t = s the agent can choose among several actions, and the policy gives the probability π(a∣s)\pi(a \mid s) of each. The value of taking action aa is qπ(s,a)q_\pi(s, a). In equation form, the state value is the policy-weighted average of the action values: vπ(s)=∑a∈Aπ(a∣s) qπ(s,a).v_\pi(s) = \sum_{a \in \mathcal A} \pi(a \mid s)\, q_\pi(s,a). Each action value is weighted by its probability π(a∣s)\pi(a \mid s), and the results are added. Computing a value from the values one step ahead in this way is called a backup. Like the state value, the action value splits into two expectations: qπ(s,a)=E[Rt+1∣St=s,At=a]+γ Eπ[vπ(St+1)∣St=s,At=a].q_\pi(s,a) = \mathbb{E} [ R_{t+1} \mid S_t=s, A_t= a ] + \gamma\, \mathbb{E}_\pi [ v_\pi(S_{t+1}) \mid S_t=s, A_t= a ]. The first expectation is the expected reward r(s,a)r(s, a). It has no subscript π\pi, because once the action is fixed the reward is distributed according to the environment’s dynamics p(s′,r∣s,a)p(s', r \mid s, a) alone, r(s,a)=∑rr∑s′p(s′,r∣s,a)r(s, a) = \sum_{r} r \sum_{s'} p(s', r \mid s, a), and the policy does not enter. For the second, after the agent takes action aa, the environment can move to one of several next states St+1=s′S_{t+1} = s' and emit a reward Rt+1R_{t+1}. In the 4x3 gridworld, for example, the agent moves in the intended direction with probability 0.8 and at right angles to it with probability 0.1 each, so one action can lead to several next states. Which next state follows depends on the transition model p(s′∣s,a)=Pr⁡[St+1=s′∣St=s,At=a]p(s' \mid s, a) = \Pr[S_{t+1}=s' \mid S_t=s, A_t=a], and the value of each next state is vπ(s′)v_\pi(s'). The second expectation is therefore the sum ∑s′∈Sp(s′∣s,a) vπ(s′)\sum_{s' \in \mathcal S} p(s' \mid s, a)\, v_\pi(s'), and the action value is qπ(s,a)=r(s,a)+γ∑s′∈Sp(s′∣s,a) vπ(s′).q_\pi(s,a) = r(s, a) + \gamma \sum_{s' \in \mathcal S} p(s' \mid s, a)\, v_\pi(s'). Substituting vπ(s′)=∑a′π(a′∣s′) qπ(s′,a′)v_\pi(s') = \sum_{a'} \pi(a' \mid s')\, q_\pi(s', a') gives the backup tree for qπ(s,a)q_\pi(s, a) below. The open circles are states and the solid dots are actions. From the state-action pair at the root, the environment moves to one of the next states s′s' with probability p(s′∣s,a)p(s' \mid s, a), and the policy then picks each action a′a' with probability π(a′∣s′)\pi(a' \mid s'). Output from cell 4 The backup tree for qπ(s,a)q_\pi(s, a) with one step of lookahead.
Bellman State-Action Value Expectation Equationqπ(s,a)=r(s,a)+γ∑s′∈Sp(s′∣s,a)∑a′∈Aπ(a′∣s′) qπ(s′,a′)q_\pi(s,a) = r(s, a) + \gamma \sum_{s' \in \mathcal S} p(s' \mid s, a) \sum_{a' \in \mathcal A} \pi(a' \mid s')\, q_\pi(s',a')In vector form, qπ\mathbf q_\pi stacks the values of all state-action pairs and r\mathbf r the expected rewards r(s,a)r(s, a), both with ∣S∣∣A∣|\mathcal S||\mathcal A| entries:qπ=r+γPΠ qπ.\mathbf q_\pi = \mathbf r + \gamma P \Pi\, \mathbf q_\pi.The two matrices split one step into the environment’s part and the policy’s part:P((s,a),s′)=p(s′∣s,a),∣S∣∣A∣×∣S∣,Π(s′,(s′,a′))=π(a′∣s′),∣S∣×∣S∣∣A∣.\begin{aligned} P\bigl((s, a), s'\bigr) &= p(s' \mid s, a), && |\mathcal S||\mathcal A| \times |\mathcal S|, \\ \Pi\bigl(s', (s', a')\bigr) &= \pi(a' \mid s'), && |\mathcal S| \times |\mathcal S||\mathcal A|. \end{aligned}PP moves from a state-action pair to the next states, and Π\Pi averages the action values of a state over the policy, so Π qπ=vπ\Pi\, \mathbf q_\pi = \mathbf v_\pi. The state-value form below follows from the same two matrices: rπ=Π r\mathbf r_\pi = \Pi\, \mathbf r and Pπ=ΠPP_\pi = \Pi P.
The backup tree for vπ(s)v_\pi(s) above has the same two levels in the other order: the policy’s actions first, then the environment’s next states. Expanding the state-value equation along that tree, with this expression for qπ(s,a)q_\pi(s, a), gives
Bellman State Value Expectation Equationvπ(s)=∑a∈Aπ(a∣s)(r(s,a)+γ∑s′∈Sp(s′∣s,a) vπ(s′)),vπ=rπ+γPπvπ.\begin{aligned} v_\pi(s) &= \sum_{a \in \mathcal A} \pi(a \mid s) \bigl( r(s, a) + \gamma \sum_{s' \in \mathcal S} p(s' \mid s, a)\, v_\pi(s') \bigr), \\ \mathbf v_\pi &= \mathbf r_\pi + \gamma P_\pi \mathbf v_\pi. \end{aligned}In the vector form on the second line, vπ\mathbf v_\pi stacks the values of all states, and rπ\mathbf r_\pi and the transition matrix PπP_\pi average the rewards and transitions over the policy:rπ(s)=∑a∈Aπ(a∣s) r(s,a),Pπ(s,s′)=∑a∈Aπ(a∣s) p(s′∣s,a).\begin{aligned} r_\pi(s) &= \sum_{a \in \mathcal A} \pi(a \mid s)\, r(s, a), \\ P_\pi(s, s') &= \sum_{a \in \mathcal A} \pi(a \mid s)\, p(s' \mid s, a). \end{aligned}
Policy Evaluation uses this equation to compute vπv_\pi for a given MDP and policy. Applied as an update, the equation is called the Bellman expectation backup, because it backs up the values of the leaf nodes s′s' to the root of the tree. This equation answers a prediction question: how good is a given policy? Bellman Optimality Backup turns it into the control question, which policy is best, by replacing the policy-weighted average over actions with a max. Key references: (Sutton & Barto, 2018)

References

  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link

Found an error on this page? Report an issue.