Unit 13, lesson 82

Markov Decision Processes and Bellman Equations

How a Markov decision process specifies states, actions, transition probabilities and rewards, how a policy and the discounted return define a value function, and what the Bellman expectation and optimality equations say.

  • Video 82
  • Markov Decision Processes and Value Learning

A warehouse robot can work or recharge. Working pays more now, but it can drain the battery and make later steps less productive, so the better action depends on where it leads and not only on its immediate reward. A bandit, whose actions never change the situation, cannot describe this. This lesson specifies the problem as a Markov decision process, measures long-run reward by the discounted return, computes the value of a fixed policy from the Bellman expectation equation, and checks that policy against the Bellman optimality equation. The robot and all its numbers are constructed for illustration.

Key terms

Markov decision process

Definition A finite Markov decision process, or MDP, consists of a finite set S\mathcal{S} of states, a finite nonempty set A(s)\mathcal{A}(s) of actions for each state ss, transition probabilities and rewards. At each step t=0,1,2,…t = 0, 1, 2, \dots the agent observes the state StS_t and chooses an action AtA_t in A(St)\mathcal{A}(S_t), and then it receives a reward Rt+1R_{t+1} and moves to the next state St+1S_{t+1}. The transition probability

p(s′∣s,a)=P(St+1=s′∣St=s,At=a)\begin{aligned} &p(s' \mid s, a) \\[0.4em] &\quad = P(S_{t+1} = s' \mid \\[0.4em] &\qquad S_t = s, A_t = a) \end{aligned}

is the probability of moving to state s′s' when action aa is taken in state ss, the same at every step. For each pair (s,a)(s, a) these numbers are nonnegative and add to 11 over s′s'. On this page the reward of a step is a fixed number r(s,a)r(s, a) that depends only on the current state and action and is received on the transition. More general MDPs let the reward depend on the next state or be random, and then r(s,a)r(s, a) denotes the expected reward. The Markov property is the assumption that, given the current state and action, the next state and reward do not depend on anything earlier. For every history HtH_t of earlier states, actions and rewards that has positive probability,

P(St+1=s′∣St=s,At=a, Ht)=p(s′∣s,a),\begin{aligned} &P(S_{t+1} = s' \mid S_t = s, \\[0.4em] &\qquad A_t = a,\ H_t) \\[0.4em] &\quad = p(s' \mid s, a), \end{aligned}

and the same holds for the reward.

In plain words An MDP is the complete rulebook of a sequential decision problem. It says what the agent observes, what it can do, what each choice pays and how likely each next state is. Unlike in a bandit, an action changes the next state and with it the rewards available later. The Markov property says that the current state carries everything about the past that matters for what happens next. It is an assumption about the chosen description of the state, not a fact about the world, and a description that leaves out something relevant can break it. The reward also defines the goal. An agent that maximizes reward pursues exactly what the reward measures, which need not include costs, safety or effects on people that the reward leaves out.

Example A constructed warehouse robot has the states charged and low and the actions work and recharge. The table gives the reward and the next-state probabilities for every state and action.

StateActionRewardNext state chargedNext state low
chargedwork550.50.50.50.5
chargedrecharge001100
lowwork110011
lowrecharge−1-11100

The probabilities in each row add to 11. The reward −1-1 for recharging from low is a modeled cost, not a measured amount of money. If the chance of running low after work also rose with the number of consecutive work steps, for example through heat, the current state and action would no longer fix the next-state probabilities, and this two-state model would break the Markov property. Adding the missing information, such as a temperature level, to the state could restore it.

Policy

Definition A policy π\pi assigns to each state ss a probability distribution over the actions in A(s)\mathcal{A}(s). On this page a policy depends only on the current state, so at every step and whatever happened earlier,

P(At=a∣St=s)=π(a∣s).\begin{aligned} &P(A_t = a \mid S_t = s) \\[0.4em] &\quad = \pi(a \mid s). \end{aligned}

A deterministic policy puts probability 11 on one action in each state. Together with the MDP and the distribution of the starting state, a policy fixes the probability of every trajectory S0,A0,R1,S1,A1,R2,…S_0, A_0, R_1, S_1, A_1, R_2, \dots. For instance, the first two states and actions are s0,a0,s1,a1s_0, a_0, s_1, a_1 with probability

P(S0=s0) π(a0∣s0)×p(s1∣s0,a0)×π(a1∣s1).\begin{aligned} &P(S_0 = s_0)\, \pi(a_0 \mid s_0) \\[0.4em] &\quad \times p(s_1 \mid s_0, a_0) \\[0.4em] &\quad \times \pi(a_1 \mid s_1). \end{aligned}

In plain words A policy is the agent’s decision rule. It decides which actions appear in the data and, through the transitions, which states are visited. Data collected under one policy describe that policy’s trajectories, consecutive steps depend on each other, and actions the policy never takes leave no outcomes at all. A random split of logged steps into training and test rows cannot reveal what another action would have done, or which states a different policy would reach.

Example The policy π1\pi_1 works when charged and recharges when low. Starting charged, one possible trajectory works and drops to low, which has probability 0.50.5, then recharges and returns to charged with probability 11, then works again. Its first three rewards are 55, −1-1 and 55. A stochastic policy might instead recharge from low with probability 0.80.8 and work with probability 0.20.2. A policy that always works never produces a recharge step, so its data say nothing about the reward or the transitions of recharging.

Discounted return

Definition For a discount factor γ\gamma, the return from step tt is the next reward plus γ\gamma times the reward after it, plus γ2\gamma^2 times the one after that, and so on,

Gt=∑k=0∞γkRt+k+1.G_t = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}.

A continuing task, which never ends, assumes bounded rewards, ∣Rt∣≤Rmax⁡\lvert R_t \rvert \le R_{\max}, and 0≤γ<10 \le \gamma < 1. Then the series converges and ∣Gt∣≤Rmax⁡/(1−γ)\lvert G_t \rvert \le R_{\max}/(1 - \gamma). In an episodic task, which ends when it reaches a terminal state at step TT, the sum stops with the reward RTR_T and all later rewards count as 00. There γ=1\gamma = 1 is also allowed when every episode ends within a bounded number of steps. In both cases the return satisfies, at every step tt of a continuing task and every step before the end of an episode,

Gt=Rt+1+γGt+1,G_t = R_{t+1} + \gamma G_{t+1},

with GT=0G_T = 0 at the end of an episode.

In plain words The return adds up all future rewards, each weighted by how far ahead it comes. With γ=0\gamma = 0 only the next reward counts, and with γ\gamma close to 11 distant rewards count almost as much as near ones, so the choice of γ\gamma is part of the objective. The return is a random variable. One run of the process gives one realized value, and another run from the same state can give a different one. Neither a familiar value of γ\gamma nor a large total reward shows that the objective captures every real consequence.

Example The robot task never ends, so it uses γ=0.5\gamma = 0.5. Its rewards lie between −1-1 and 55, so every return lies between −2-2 and 1010. Along the trajectory of the policy example, with rewards 55, −1-1 and 55, the first three terms of the return are 55, 0.5×(−1)=−0.50.5 \times (-1) = -0.5 and 0.25×5=1.250.25 \times 5 = 1.25, and

5−0.5+1.25=5.75.5 - 0.5 + 1.25 = 5.75 .

This is a partial sum along one realized path. The full return adds γ3G3=0.125 G3\gamma^3 G_3 = 0.125\, G_3, which depends on how the path continues, so it lies between 5.55.5 and 77.

Value function

Definition For a policy π\pi in an MDP with bounded rewards and 0≤γ<10 \le \gamma < 1, or in an episodic task as above, the state-value function of π\pi is

Vπ(s)=Eπ[Gt∣St=s],V^{\pi}(s) = E_{\pi}[G_t \mid S_t = s],

the expected return when the process is in state ss and the agent follows π\pi from then on. The action-value function

Qπ(s,a)=Eπ[Gt∣St=s,At=a]\begin{aligned} &Q^{\pi}(s, a) = E_{\pi}[G_t \mid \\[0.4em] &\qquad S_t = s, A_t = a] \end{aligned}

is the expected return when the agent takes action aa in state ss and follows π\pi afterward. Neither depends on tt, because the MDP and the policy are the same at every step. The two are linked by Vπ(s)=∑aπ(a∣s) Qπ(s,a)V^{\pi}(s) = \sum_a \pi(a \mid s)\, Q^{\pi}(s, a), and a terminal state has value 00.

In plain words A value is an average over all the paths a policy can produce from a state, weighted by their probabilities, and not the return of one path. Values belong to a policy, so changing the policy changes them. Three kinds of numbers need to be kept apart. A reward is paid on one step, a value is an expected discounted total, and an action probability is part of the policy.

Example The policy that always recharges keeps a charged robot charged with reward 00 on every step, so its value at charged is 00. From low it pays −1-1 once and then 00 forever, so its value at low is −1-1. The policy that always works keeps a low robot low with reward 11 on every step, so with γ=0.5\gamma = 0.5 its value at low is

1+0.5+0.25+⋯=11−0.5=2.\begin{aligned} &1 + 0.5 + 0.25 + \cdots \\[0.4em] &\quad = \frac{1}{1 - 0.5} = 2 . \end{aligned}

For the always-recharge policy, the action value of working at low is 1+0.5×(−1)=0.51 + 0.5 \times (-1) = 0.5, since the robot stays low and then follows that policy. It is larger than the policy’s own value −1-1 at low, so a better first action exists there.

Bellman expectation equation

Definition Let π\pi be a policy in a finite MDP with 0≤γ<10 \le \gamma < 1. For every state ss and action aa,

Qπ(s,a)=r(s,a)+γ∑s′p(s′∣s,a)×Vπ(s′),\begin{aligned} &Q^{\pi}(s, a) = r(s, a) \\[0.4em] &\quad + \gamma \sum_{s'} p(s' \mid s, a) \\[0.4em] &\qquad \times V^{\pi}(s'), \end{aligned}

and Vπ(s)=∑aπ(a∣s) Qπ(s,a)V^{\pi}(s) = \sum_a \pi(a \mid s)\, Q^{\pi}(s, a). Substituting the first equation into the second gives one linear equation for each state, the Bellman expectation equation for VπV^{\pi}. It follows from Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} and the Markov property, which make the expected return after a move to s′s' equal to Vπ(s′)V^{\pi}(s'). The value function VπV^{\pi} is the unique solution of this system. It can also be approached by synchronous updates. Starting from any values V0V_0, each sweep computes Vk+1(s)V_{k+1}(s) for every state from the right sides of these equations, with the previous values Vk(s′)V_k(s') in place of Vπ(s′)V^{\pi}(s'). The sweeps converge to VπV^{\pi}, because each one multiplies the largest error over the states by a factor of at most γ\gamma.

In plain words The value of a state is the expected immediate reward plus the discounted expected value of wherever the policy leads next. The policy is fixed, so the equation averages over its actions with their probabilities and over the next states with their transition probabilities, and nothing is maximized. Solving it requires the transition probabilities and rewards. An exact solution for an inaccurate model describes the model and not the real system, and when only sampled transitions are available the values must be learned from experience, as in temporal-difference learning.

Example Take the policy π1\pi_1, which works when charged and recharges when low, with γ=0.5\gamma = 0.5, and write VCV_C and VLV_L for its values at charged and low. The equations are

VC=5+12(12VC+12VL),VL=−1+12VC.\begin{aligned} V_C &= 5 + \tfrac12 \bigl(\tfrac12 V_C + \tfrac12 V_L\bigr), \\[0.4em] V_L &= -1 + \tfrac12 V_C . \end{aligned}

The first averages the two possible next states, and the second has a certain next state. Substituting the second into the first gives VC=4.75+38VCV_C = 4.75 + \tfrac38 V_C, so 58VC=4.75\tfrac58 V_C = 4.75. Hence VC=7.6V_C = 7.6 and VL=−1+3.8=2.8V_L = -1 + 3.8 = 2.8. Starting instead from the values (0,0)(0, 0) at (charged, low), the synchronous updates give (5,−1)(5, -1) and then (6,1.5)(6, 1.5), each computed from the previous pair only. Their largest errors 7.67.6, 3.83.8 and 1.61.6 shrink by at least half at every sweep.

Bellman optimality equation

Definition In a finite MDP with 0≤γ<10 \le \gamma < 1, the optimal value function V∗(s)=max⁡πVπ(s)V^*(s) = \max_{\pi} V^{\pi}(s) is the largest value any policy achieves from state ss. With

Q∗(s,a)=r(s,a)+γ∑s′p(s′∣s,a)×V∗(s′),\begin{aligned} &Q^*(s, a) = r(s, a) \\[0.4em] &\quad + \gamma \sum_{s'} p(s' \mid s, a) \\[0.4em] &\qquad \times V^*(s'), \end{aligned}

the Bellman optimality equation states that for every state ss

V∗(s)=max⁡aQ∗(s,a),V^*(s) = \max_{a} Q^*(s, a),

and V∗V^* is the unique solution of these equations. A policy is optimal, with Vπ=V∗V^{\pi} = V^* in every state, exactly when in every state it gives all its probability to actions that attain this maximum, so some deterministic policy is optimal. Written for action values alone, the equation reads

Q∗(s,a)=r(s,a)+γ∑s′p(s′∣s,a)×max⁡a′Q∗(s′,a′).\begin{aligned} &Q^*(s, a) = r(s, a) \\[0.4em] &\quad + \gamma \sum_{s'} p(s' \mid s, a) \\[0.4em] &\qquad \times \max_{a'} Q^*(s', a'). \end{aligned}

In plain words The expectation equation averages over the actions of a fixed policy, while the optimality equation takes the best action in each state. An action with a worse immediate reward can still be best if it leads to better states. The maximum runs over the agent’s actions only. The next state is still averaged with its transition probabilities, because the environment decides where the process goes. Optimal means optimal for the stated rewards and discount factor, and a different reward definition can lead to a different optimal policy.

Example At the values VC=7.6V_C = 7.6 and VL=2.8V_L = 2.8 of π1\pi_1, the four action values are as follows, where 5.2=0.5×7.6+0.5×2.85.2 = 0.5 \times 7.6 + 0.5 \times 2.8 is the expected next value after working while charged.

StateActionAction value
chargedwork5+0.5×5.2=7.65 + 0.5 \times 5.2 = 7.6
chargedrecharge0+0.5×7.6=3.80 + 0.5 \times 7.6 = 3.8
lowwork1+0.5×2.8=2.41 + 0.5 \times 2.8 = 2.4
lowrecharge−1+0.5×7.6=2.8-1 + 0.5 \times 7.6 = 2.8

In each state the largest action value equals the value of π1\pi_1, so these values satisfy the optimality equation, the optimal values are 7.67.6 and 2.82.8, and π1\pi_1 is optimal. At low, recharging pays −1-1 now, less than the 11 of working, yet it is the better action. The other three deterministic policies agree. Working in both states gives the values 22/3≈7.3322/3 \approx 7.33 at charged and 22 at low, recharging at charged and working at low gives 00 and 22, and recharging in both states gives 00 and −1-1, so none beats π1\pi_1 in either state.

Check yourself

  1. An episode gives the rewards 22, 00 and 33 and then reaches a terminal state. Find its return from the start with γ=0.8\gamma = 0.8. Then explain why the continuing robot task cannot use γ=1\gamma = 1 with the policy π1\pi_1.
Show the answer

The episode ends after the third reward, so the sum has three terms,

G0=2+0.8×0+0.82×3=3.92.\begin{aligned} G_0 &= 2 + 0.8 \times 0 \\[0.4em] &\quad + 0.8^2 \times 3 \\[0.4em] &= 3.92 . \end{aligned}

Under π1\pi_1 the robot earns 55 at every step in charged and −1-1 at every step in low, and every step in low is followed by a step in charged. With γ=1\gamma = 1 the rewards along every path therefore add up to infinity, and the values are not finite numbers. The Bellman expectation equations VC=5+12VC+12VLV_C = 5 + \tfrac12 V_C + \tfrac12 V_L and VL=−1+VCV_L = -1 + V_C then have no solution at all, since substituting the second into the first gives 0=4.50 = 4.5. A task that never ends needs 0≤γ<10 \le \gamma < 1.

  1. In some state the policy’s action pays reward 11 and leads to a state with value 44 with probability 34\tfrac34 and to a state with value 1010 with probability 14\tfrac14. With γ=0.5\gamma = 0.5, compute the Bellman expectation backup for this state. A classmate uses the larger next value 1010, because the Bellman optimality equation takes a maximum. What is wrong?
Show the answer

The expected next value is 0.75×4+0.25×10=5.50.75 \times 4 + 0.25 \times 10 = 5.5, so the backup is

1+0.5×5.5=3.75.1 + 0.5 \times 5.5 = 3.75 .

The classmate’s value 1+0.5×10=61 + 0.5 \times 10 = 6 is wrong in both equations. The environment, not the agent, decides which next state occurs, so both the expectation and the optimality equation average over next states with their transition probabilities. The maximum in the optimality equation runs over the actions available in the current state.

  1. A company logs a year of steps of the robot under the policy that always works, splits the logged steps at random into training and test rows, and wants to estimate the value of π1\pi_1. What can these data show, and what would be needed?
Show the answer

The policy π1\pi_1 recharges whenever the robot is low, but the logs contain no recharge steps, so they hold no information about the reward or the next state after recharging. No split of the rows can create those outcomes. The logs also reflect the states that always working produces. Working while low keeps the robot low, so after the first few steps nearly every logged step is in low, a distribution that π1\pi_1 would not produce. Consecutive steps of one trajectory are also dependent, so a random split places closely related steps in both parts.

The data can estimate what working does, for example the value of always working, which is 22 at low. To evaluate π1\pi_1 the data must include recharge actions taken in the low state, for example from a policy that sometimes explores, or the transitions must come from a model whose assumptions are trusted. The best evidence is to run π1\pi_1 itself and measure its returns.

How this lesson connects