Chapter 3: Finite Markov Decision Process (MDP)

Chapter 3: Finite Markov Decision Process (MDP)

Last reviewedStable21 min read
Danger

This article was originally written in Vietnamese. The following is an English translation with improvements created with the assistance of GPT 5.6 Sol to make the content accessible to a broader audience.

1. The Agent-Environment Interaction

Before writing down any equations, let’s agree on the picture we have in mind. Reinforcement Learning is still Machine Learning, but the data does not arrive as a fixed dataset as in Supervised Learning: the agent gathers it by interacting with the world, and its actions change what it will encounter next. We therefore need to think in terms of a continuous loop of action and feedback.

The two main components of Reinforcement Learning (RL) are the Agent and the Environment.

  • The environment is the world that the agent lives in and interacts withThe Boundary Between Agent and Environment: This boundary is not physical (like the skin of a robot). Anything the Agent cannot change arbitrarily is considered part of the Environment (e.g., a robot’s battery level, as the Agent cannot command “Battery, become full” arbitrarily)..
  • At each step (or time step), the agent perceives itself to be in a state of the environment and, based on that state, decides what it needs to doAccording to [Achiam, 2018], there are two definitions we need to distinguish: state (a complete description of the world, like chess) and observation (an incomplete description, like poker). In this context, we assume descriptions are always complete, so o=so = s..

After performing an action, the agent receives a scalar reward signal (or simply reward) associated with the resulting transition. A reward need not measure the quality of the current state by itself; it may depend on the preceding state, action, and next state. Usually, it is a real number and indicates how good or bad the action or the current state is.

The agent’s goal is to maximize the cumulative reward it receives over time. We call this accumulated reward the Return.

MDP Image
Definition (What is an MDP?).

A Markov Decision Process (MDP) is a mathematical framework used to formalize sequential decision making problems. In this problem, at each time step, the agent must make a decision (action) based on the state. The agent’s action influences not just the immediate reward and next state, but also future rewards and states.

An MDP is appropriate when the chosen state representation contains the information needed to model future transitions and rewards. Within that model, interaction is summarized by three signals crossing the agent-environment boundary: the state on which a decision is based, the action selected by the agent, and the resulting reward.

The term “Markov” in MDP refers to the Markov property—the future depends only on the present state, not the historySutton and Barto’s also said that: “MDPs assume that regardless of the details of the agent (like torques, sensors, etc.) and regardless of the goal the agent is trying to achieve, any goal-directed problem can be reduced to three signals passing between the agent and its environment: a signal representing the choices made by the agent (action), a signal representing the basis on which choices are made (state), and a signal defining the agent’s goal (reward).”..

We can formalize this process as follows:

  • At each time step tt, the agent receives a representation of the environment’s state, StSS_t \in \mathcal{S}, where S\mathcal{S} is the set of all possible states.
  • Based on StS_t, the agent selects an Action AtA(St)A_t \in \mathcal{A}(S_t) (note that the set of available actions may differ per state).
  • One time step later (at t+1t+1), the agent receives a reward Rt+1RRR_{t+1} \in \mathcal{R} \subset \mathbb{R} and finds itself in a new state St+1SS_{t+1} \in \mathcal{S}.

This sequence of interactions forms a trajectory. Where do S0S_0 and A0A_0 come from? We sample S0S_0 from an initial-state distribution, and then the policy selects A0A_0. This distribution matters because it changes which parts of the MDP we are likely to visit—and therefore which behavior our overall performance measure emphasizes (read more here).

S0,A0,R1,S1,A1,R2,S2,A2,S_{0}, A_{0}, R_{1}, S_{1}, A_{1}, R_{2}, S_{2}, A_{2}, \dots
Definition (Dynamics of the MDP).

In a finite MDP, the sets S,A,R\mathcal{S}, \mathcal{A}, \mathcal{R} are all finite. Under the Markov and time-homogeneity assumptions (read more here), the distribution of StS_t and RtR_t depends only on the preceding state and action (St1,At1)(S_{t-1}, A_{t-1}), and the same kernel applies at every time step. We call this kernel the dynamics of the MDP:

p(s,rs,a)=Pr{St=s,Rt=rSt1=s,At1=a}p(s', r \mid s, a) = \text{Pr} \{ S_{t} = s', R_{t} =r \mid S_{t-1} = s, A_{t-1} = a \}

Since pp is a probability distribution, the sum of probabilities for all possible pairs (s,r)(s', r) must equal 1:

sSrRp(s,rs,a)=1,sS,aA(s)\sum_{s' \in \mathcal{S}} \sum_{r \in \mathcal{R}} p(s', r \mid s, a) = 1, \quad \forall s \in \mathcal{S}, a \in \mathcal{A}(s)

In a finite MDP, the agent’s world (the environment) follows rules. These rules are probabilistic, and these rules are exactly the dynamics of the environment.

The next key point of MDPs is the Markov Property: The probability of StS_t and RtR_t depends only on St1S_{t-1} and At1A_{t-1}, not on the longer history. The current state encapsulates all information necessary to make decisions for the future.

From the dynamics pp, we can compute other important information such as:

  • State-transition probability: p(ss,a)=rRp(s,rs,a)p(s' \mid s, a) = \sum_{r \in \mathcal{R}} p(s', r \mid s, a)
  • Expected reward: The reward we expect to receive when taking action aa in state ss. r(s,a)=E[RtSt1=s,At1=a]=rRrp(rs,a)=rRr[sSp(s,rs,a)]\begin{aligned} r(s, a) &= \mathbb{E}[R_{t} \mid S_{t-1} = s, A_{t-1} = a] \\ &= \sum_{r \in \mathcal{R}} r p(r \mid s, a) \\ &= \sum_{r \in \mathcal{R}} r \left[ \sum_{s' \in \mathcal{S}} p(s', r \mid s, a) \right] \end{aligned}

2. Goals, Rewards, and Returns

Sutton and Barto’s reward hypothesis proposes that a goal can be represented as maximizing the expected cumulative value of a scalar reward signal [Sutton et al., 2018]. This is a modeling hypothesis, not a theorem that every real objective admits a faithful scalar representation. Once adopted, the reward specifies what behavior the objective favors, while the policy and learning procedure determine how to pursue it.

So, what is the Agent’s true goal? It is not to get the highest reward immediately, but to maximize the total accumulated reward in the long run. We call this accumulated sum the Return (GtG_t). To be precise, GtG_t is the sum of rewards the agent accumulates in the future (i.e., from time step t+1t+1 until the end). Why future sums instead of past sums? Recall the agent’s purpose is to maximize the reward it accumulates—like constantly looking ahead and choosing the future path that yields the most reward.

Depending on the task type, the calculation of GtG_t differs:

  1. Episodic Tasks: Interaction breaks down into subsequences called episodes. Each episode ends in a terminal state at time TT.

    Gt=Rt+1+Rt+2++RTG_t = R_{t+1} + R_{t+2} + \dots + R_T
  2. Continuing Tasks: Interaction goes on forever (T=T = \infty). For the discounted formulation here, we assume rewards are uniformly bounded and choose 0γ<10 \leq \gamma < 1.

    Gt=Rt+1+γRt+2+γ2Rt+3+...=k=0γkRt+k+1G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + ... = \sum_{k=0}^\infty \gamma^k R_{t+k+1}

Discounting serves two distinct purposes:

  • Well-defined infinite-horizon returns: if rewards are uniformly bounded and γ<1\gamma < 1, the return converges absolutely because it is bounded by a geometric series.
  • Time preference: γ\gamma controls how strongly delayed rewards affect the objective. If γ=0\gamma = 0, the agent is myopic, caring only about the immediate reward.

For a continuing task with γ=1\gamma = 1, the discounted-return argument no longer guarantees convergence. Such problems require additional structure or a different objective, such as average reward, and are outside this note’s scope.

We can observe a recursive relationship:

Gt=Rt+1+γRt+2+γ2Rt+3+...=Rt+1+γ(Rt+2+γRt+3+...)=Rt+1+γGt+1\begin{aligned} G_t &= R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + ... \\ &= R_{t+1} + \gamma (R_{t+2} + \gamma R_{t+3} + ...) \\ &= R_{t+1} + \gamma G_{t+1} \end{aligned}

The formula above is a critically important recursive property. It means: The return of the current sequence equals the immediate reward plus the discounted return of the next sequence.

To define a single return formula for both cases, we use:

Gt=k=t+1Tγk(t+1)RkG_{t} = \sum_{k=t+1}^T \gamma^{k - (t + 1)} R_{k}

In continuing cases, T=T = \infty and γ<1\gamma < 1. In episodic cases, γ\gamma may equal 11 when the episode terminates with a finite return.

3. Policies and Value Functions

How does an agent know if a state is “good”? A state is only good if it leads to a high return. But the return depends on how the agent behaves in the future. Therefore, the agent needs to know how to select and evaluate its actions.

We use Value Functions to describe the goodness of a state (or a state-action pair). The goodness of a state ss is the expected return from that state under a particular policy. Meanwhile, the Policy is the agent’s brain—it tells the agent how to select actions from the current state.

We will keep our policies stationary and Markov: π(as)\pi(a\mid s) depends on the current state, but not explicitly on the time step or the earlier history. This choice is what lets us write one value vπ(s)v_\pi(s) for a state without carrying the whole trajectory into the notation.

Definition (Policy).A Policy π\pi is a mapping from states to probabilities of selecting each possible action. If an agent follows policy π\pi at time tt, then π(as)\pi(a \mid s) is the probability that At=aA_t=a given St=sS_t=s. Since π(as)\pi(a \mid s) is a probability distribution, aA(s)π(as)=1\sum_{a \in \mathcal{A}(s)} \pi(a \mid s) = 1.

There are two main types of Value Functions. We write both as infinite sums; for episodic tasks, the zero-reward absorbing-state convention in Appendix C makes every term after termination vanish.

  1. State-value function vπ(s)v_{\pi}(s): The expected return when starting in ss and following policy π\pi thereafter.

    vπ(s)=Eπ[GtSt=s]=Eπ[k=0γkRt+k+1St=s]v_{\pi}(s) = \mathbb{E}_{\pi}[G_{t} \mid S_{t} = s] = \mathbb{E}_{\pi}\left[ \sum_{k=0}^{\infty} \gamma^{k} R_{t+k+1} \mid S_{t} = s \right]
  2. Action-value function qπ(s,a)q_{\pi}(s, a): The expected return starting from ss, taking action aa, and then following policy π\pi.

    qπ(s,a)=Eπ[GtSt=s,At=a]=Eπ[k=0γkRt+k+1St=s,At=a]q_{\pi}(s, a) = \mathbb{E}_{\pi}[G_{t} \mid S_{t} = s, A_{t} = a] = \mathbb{E}_{\pi} \left[ \sum_{k=0}^{\infty} \gamma^{k} R_{t+k+1} \mid S_t = s, A_t = a \right]

The Q-Learning algorithm uses Q(s,a)Q(s,a) for its estimate of the optimal action-value function [Watkins et al., 1992].

A key concept here is following policy π\pi. At any state ss, different policies may assign different action probabilities. The value function is the exact expected return defined by a particular policy and MDP; a learning algorithm may only approximate that value from data.

4. The Bellman Equation

Because GtG_t is recursive, the Value Function must also be recursive. This relationship allows us to compute Value Functions by “backtracking” (or backing up) information from the next state to the current state. We can write vπv_{\pi} recursively as:

vπ(s)=Eπ[GtSt=s]=Eπ[Rt+1+γGt+1St=s]=Eπ[Rt+1St=s]+γEπ[Gt+1St=s]=aA(s)π(as)s,rp(s,rs,a)[r+γvπ(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[R_{t+1} + \gamma G_{t+1} \mid S_{t} = s] \\ &= \mathbb{E}_\pi[R_{t+1} \mid S_{t} = s] + \gamma\mathbb{E}_\pi[G_{t+1} \mid S_{t} = s] \\ &= \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a) [r + \gamma v_{\pi}(s')] \\ &= \mathbb{E}[R_{t+1} + \gamma v_{\pi}(S_{t+1}) \mid S_t = s] \end{aligned}

The equation above is the Bellman equation for the state-value function (see Appendix A).

  • The Bellman equation states that the value of the start state must equal the (discounted) value of the expected next state, plus the reward expected along the way. This is the Consistency Condition.
  • It shows that the value of a state is the weighted average of the immediate rewards the agent expects, plus the discounted value of the states the agent enters next.

Similarly, we have the Bellman equation for the action-value function qπ(s,a)q_{\pi}(s, a):

qπ(s,a)=Eπ[GtSt=s,At=a]=s,rp(s,rs,a)[r+γaA(s)π(as)qπ(s,a)]\begin{aligned} q_{\pi}(s, a) &= \mathbb{E}_\pi[G_{t} \mid S_{t} = s, A_{t} = a] \\ &= \sum_{s', r}p(s', r \mid s, a)\left[ r + \gamma \sum_{a' \in \mathcal{A}(s')} \pi(a' \mid s')q_{\pi}(s', a') \right] \end{aligned}

The derivation is given in Appendix B.

Why do we need q(s,a)q(s, a) if we already have v(s)v(s)?

If we know only v(s)v(s), selecting an action by one-step lookahead requires the environment dynamics. If we already know q(s,a)q(s,a), action selection can compare actions directly without consulting an explicit dynamics model at decision time. Obtaining an accurate qq-function still requires either a model or sufficient experience. This distinction motivates model-free value-based methods such as Q-learning, which estimate action values from sampled transitions.

Backup Diagram
A state-value backup diagram: policy probabilities weight actions, and environment probabilities weight successor outcomes.

Another important concept is the backup diagram, which visualizes the Bellman equation using the notation of Sutton and Barto [Sutton et al., 2018]. We can interpret it as a tree where leaves are future states and internal nodes aggregate the outcomes below them.

We call this operation a backup. Unlike a simulated transition, which moves forward from ss to a sampled ss', a backup updates information at ss using estimates or returns associated with later states. Dynamic programming and temporal-difference value methods explicitly use Bellman-style backups. Monte Carlo methods instead use sampled complete returns, while policy-gradient methods need not explicitly approximate a Bellman equation.

  • Root (open circle, representing state): State ss.
  • First branch: Policy π(as)\pi(a \mid s).
  • First node (solid circle, representing state-action pair): Pair (s,a)(s, a).
  • Second branch: Environment dynamics p(s,rs,a)p(s', r \mid s, a).
  • Leaves: Next state ss', whose value vπ(s)v_{\pi}(s') contributes to the backup at the current state ss.

5. Bellman Optimality Equation

We have defined value functions for a specific policy π\pi. But we don’t just want to evaluate a policy; we want to find the best policy (the Optimal Policy).

Definition (Optimal Policy).

A policy π\pi is defined to be better than or equal to policy π\pi' if its expected return is greater than or equal to that of π\pi' for all states sSs \in \mathcal{S}. In other words:

ππ    vπ(s)vπ(s), sS\pi \geq \pi' \iff v_\pi(s) \geq v_{\pi'}(s), \ \forall s \in \mathcal{S}

For a finite discounted MDP, there exists at least one stationary deterministic policy whose value is maximal in every state. Any such policy is an optimal policy, denoted by π\pi_{\ast} [Sutton et al., 2018]. Undiscounted continuing problems require additional conditions and are not covered by this statement.

All optimal policies share the same state-value function, called the optimal state-value function:

v(s)=maxπvπ(s),sSv_{\ast}(s) = \max_{\pi} v_{\pi}(s), \qquad \forall s \in \mathcal{S}

Similarly, optimal policies share the same optimal action-value function:

q(s,a)=maxπqπ(s,a),sS, aA(s)q_{\ast}(s, a) = \max_{\pi} q_{\pi}(s, a), \qquad \forall s \in \mathcal{S},\ a \in \mathcal{A}(s)

Just like the value function for a specific policy π\pi, the value function for the optimal policy π\pi^{\ast} can be written in a Bellman form, called the Bellman Optimality Equation.

The relationship between optimal action-value and optimal state-value is:

v(s)=maxaA(s)q(s,a)v_{\ast}(s) = \max_{a \in \mathcal{A}(s)} q_{\ast}(s, a)

Bellman Optimality Equation for state-value function:

v(s)=maxaA(s)q(s,a)=maxaA(s)E[Rt+1+γv(St+1)St=s,At=a]=maxaA(s)s,rp(s,rs,a)[r+γv(s)]\begin{aligned} v_{\ast}(s) &= \max_{a \in \mathcal{A}(s)} q_{\ast}(s, a) \\ &= \max_{a \in \mathcal{A}(s)} \mathbb{E}[R_{t+1} + \gamma v_{\ast}(S_{t+1}) \mid S_{t} = s, A_{t} = a] \\ &= \max_{a \in \mathcal{A}(s)} \sum_{s', r} p(s', r \mid s, a) [r + \gamma v_{\ast}(s')] \end{aligned}

Bellman Optimality Equation for action-value function:

q(s,a)=E[Rt+1+γv(St+1)St=s,At=a]=E[Rt+1+γmaxaA(St+1)q(St+1,a)St=s,At=a]=s,rp(s,rs,a)[r+γmaxaA(s)q(s,a)]\begin{aligned} q_{\ast}(s, a) &= \mathbb{E}[R_{t+1} + \gamma v_{\ast}(S_{t+1}) \mid S_{t} = s, A_{t} = a] \\ &= \mathbb{E}[R_{t+1} + \gamma \max_{a' \in \mathcal{A}(S_{t+1})} q_{\ast}(S_{t+1}, a') \mid S_{t} = s, A_{t} = a] \\ &= \sum_{s', r} p(s', r \mid s, a) [r + \gamma \max_{a' \in \mathcal{A}(s')} q_{\ast}(s', a')] \end{aligned}
Backup Diagram 2
Backup Diagram for the Bellman Optimality Equation. The arc between branches represents taking the max instead of the weighted average.

The arc between branches represents maximizing over actions instead of averaging them under a fixed policyWhy is the Bellman optimality equation harder to solve? For a fixed policy, the Bellman expectation equation is a linear system; see Appendix D. Maximizing over actions makes the optimality operator nonlinear, so the same direct linear solve does not apply. Standard alternatives include value iteration, policy iteration, and a linear-programming formulation. Q-learning estimates qq_\ast from samples when the dynamics are unavailable. See Appendix E..

Two boundary checks make the equations easier to interpret:

  • If γ=0\gamma = 0, then vπ(s)v_\pi(s) is only the expected immediate reward under π\pi.
  • For an absorbing terminal state with zero subsequent reward, both its state value and every permitted terminal action value are zero.

6. References

  1. [Achiam, 2018]
    Spinning Up in Deep Reinforcement Learning[HTML]
    Achiam, Joshua, 2018.
  2. [Sutton et al., 2018]
    Reinforcement Learning: An Introduction[HTML]
    Sutton, Richard S. and Barto, Andrew G., 2018. The MIT Press.
  3. [Watkins et al., 1992]
    Q-learning[DOI]
    Watkins, Christopher J. C. H. and Dayan, Peter, 1992. Machine Learning, vol. 8, pp. 279--292
  4. [Alekh Agarwal et al., 2021]
    Reinforcement Learning: Theory and Algorithms[HTML]
    Alekh Agarwal, Nan Jiang Sham, M. Kakade and Wen Sun, 2021.
  5. [Vazquez-Reina]
    Deriving Bellman Equation in Reinforcement Learning[HTML]
    Amelio Vazquez-Reina (https://stats.stackexchange.com/users/2798/amelio-vazquez-reina). Cross Validated.
  6. [krishnab]
    How to setup the Bellman Equation as a linear system of equation[HTML]
    krishnab (https://cs.stackexchange.com/users/17922/krishnab). Computer Science Stack Exchange.

Appendix

A. Proof of Bellman Equation for State-Value Function

We have:

vπ(s)=E[GtSt=s]=E[Rt+1+γGt+1St=s]=E[Rt+1St=s]+γE[Gt+1St=s]\begin{aligned} v_{\pi}(s) &= \mathbb{E}[G_{t} \mid S_{t} = s] \\ &= \mathbb{E}[R_{t+1} + \gamma G_{t+1} \mid S_{t} = s] \\ &= \mathbb{E}[R_{t+1} \mid S_{t} = s] + \gamma\mathbb{E}[G_{t+1} \mid S_{t} = s] \end{aligned}

There are two expectations we need to resolve: the expectation of the immediate reward and the expectation of the next return Gt+1G_{t+1}.

Expectation of the next reward:

We have:

E[Rt+1St=s]=rrp(rs)\mathbb{E}[R_{t+1} \mid S_t = s] = \sum_{r} r p(r \mid s)

We need to “decompose” the probability p(rs)p(r \mid s) into two parts: the policy π(as)\pi(a \mid s) and the dynamics p(r,ss,a)p(r, s' \mid s, a). Applying the sum rule and product rule, we get:

p(rs)=aA(s)p(r,as)=aA(s)p(ra,s)p(as)=aA(s)π(as)[sp(r,sa,s)]\begin{aligned} p(r \mid s) &= \sum_{a \in \mathcal{A}(s)} p(r, a \mid s) \\ &= \sum_{a \in \mathcal{A}(s)} p(r \mid a, s)p(a \mid s) \\ &= \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \left[ \sum_{s'} p(r, s' \mid a, s) \right] \end{aligned}

Substituting this back into the expectation:

E[Rt+1St=s]=rrp(rs)=rr[aA(s)π(as){sp(r,sa,s)}]\begin{aligned} \mathbb{E}[R_{t+1} \mid S_t = s] &= \sum_{r} r p(r \mid s) \\ &= \sum_{r} r \left[ \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \left\{ \sum_{s'} p(r, s' \mid a, s) \right\} \right] \end{aligned}

Once ss and aa are fixed, π(as)\pi(a \mid s) does not vary with rr or ss'. We can therefore move it outside those sums and rearrange the finite sums:

E[Rt+1St=s]=aA(s)π(as)[sS,rRp(r,sa,s)r]\mathbb{E}[R_{t+1} \mid S_{t} = s] = \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \left[ \sum_{s' \in \mathcal{S}, r \in \mathcal{R}} p(r, s' \mid a, s) r \right]

Expectation of the return at the next time step:

Applying the law of total expectation, E[X]=yE[XY=y]Pr{Y=y}\mathbb{E}[X] = \sum_{y}\mathbb{E}[X \mid Y = y] \text{Pr} \{ Y = y \}, we get:

E[Gt+1St=s]=aA(s)E[Gt+1St=s,At=a]Pr{At=aSt=s}=aA(s)E[Gt+1St=s,At=a]π(as)\begin{aligned} \mathbb{E}[G_{t+1} \mid S_{t} = s] &= \sum_{a \in \mathcal{A}(s)} \mathbb{E}[G_{t+1} \mid S_{t} = s, A_{t} = a] \text{Pr} \{A_{t} = a \mid S_{t} = s \} \\ &= \sum_{a \in \mathcal{A}(s)} \mathbb{E}[G_{t+1} \mid S_{t} = s, A_{t} = a]\pi(a \mid s) \end{aligned}

Further expanding the conditional expectation:

E[Gt+1St=s,At=a]=r,sp(r,ss,a)E[Gt+1St+1=s,St=s,Rt+1=r,At=a]\mathbb{E}[G_{t+1} \mid S_{t} = s, A_{t} = a] = \sum_{r, s'} p(r, s' \mid s, a) \mathbb{E}[G_{t+1} \mid S_{t+1} = s', S_{t} = s, R_{t+1} = r, A_{t}= a]

Here we also use the fact that π\pi is a stationary Markov policy: after we condition on St+1=sS_{t+1}=s', future actions depend on ss' rather than on the earlier state, action, or reward. Together with the Markov property of the dynamics, this lets us remove StS_t, AtA_t, and Rt+1R_{t+1} from the inner conditional expectationConditional Independence: If P(AB,C)=P(AC)P(A \mid B, C) = P(A \mid C), we say AA and BB are conditionally independent given CC.:

E[Gt+1St=s,At=a]=s,rp(r,ss,a)E[Gt+1St+1=s]=s,rp(r,ss,a)vπ(s)\begin{aligned} \mathbb{E}[G_{t+1} \mid S_{t} =s, A_{t} = a] &= \sum_{s', r} p(r, s' \mid s, a) \mathbb{E}[G_{t+1} \mid S_{t+1} = s'] \\ &= \sum_{s', r}p(r, s' \mid s, a) v_{\pi}(s') \end{aligned}

Combining everything, we arrive at:

vπ(s)=aA(s)π(as)[s,rp(s,rs,a){γvπ(s)+r}]v_{\pi}(s) = \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \left[ \sum_{s', r} p(s', r \mid s, a) \{ \gamma v_{\pi}(s') + r \} \right]

B. Proof of Bellman Equation for Action-Value Function

From the proof in Appendix A, we see:

E[vπ(St+1)St=s,At=a]=s,rp(s,rs,a)vπ(s)=E[Gt+1St=s,At=a]\mathbb{E}[v_{\pi}(S_{t+1}) \mid S_{t} = s, A_{t} = a] = \sum_{s',r} p(s', r \mid s, a) v_{\pi}(s') = \mathbb{E}[G_{t+1} \mid S_{t} = s, A_{t} = a]

Also:

s,rp(s,rs,a){γvπ(s)+r}=E[γvπ(St+1)+Rt+1St=s,At=a]=E[Rt+1St=s,At=a]+γE[vπ(St+1)St=s,At=a]=E[Rt+1St=s,At=a]+γE[Gt+1St=s,At=a]=qπ(s,a)\begin{aligned} \sum_{s', r} p(s', r \mid s, a) \{ \gamma v_{\pi}(s') + r \} &= \mathbb{E}[\gamma v_{\pi}(S_{t+1}) + R_{t+1} \mid S_{t} = s, A_{t} = a] \\ &= \mathbb{E}[R_{t+1} \mid S_{t} = s, A_{t} = a] + \gamma \mathbb{E}[v_{\pi}(S_{t+1}) \mid S_{t} = s, A_{t} = a] \\ &= \mathbb{E}[R_{t+1} \mid S_{t} = s, A_{t} = a] + \gamma \mathbb{E}[G_{t+1} \mid S_{t} = s, A_{t} = a] \\ &= q_{\pi}(s, a) \end{aligned}

To satisfy the recursive nature of the Bellman equation, we need to express qπq_{\pi} in terms of another qπq_{\pi}. First:

vπ(s)=aA(s)π(as)[s,rp(s,rs,a){γvπ(s)+r}]=aA(s)π(as)qπ(s,a)\begin{aligned} v_{\pi}(s) &= \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \left[ \sum_{s', r} p(s', r \mid s, a) \{ \gamma v_{\pi}(s') + r \} \right] \\ &= \sum_{a \in \mathcal{A}(s)} \pi(a \mid s)q_{\pi}(s, a) \end{aligned}

The state name is only a dummy variable, so the same identity at the successor state is

vπ(s)=aA(s)π(as)qπ(s,a).v_{\pi}(s') = \sum_{a' \in \mathcal{A}(s')} \pi(a' \mid s') q_{\pi}(s', a').

Substituting this identity into qπ(s,a)q_{\pi}(s, a) gives the final result:

qπ(s,a)=s,rp(s,rs,a)[r+γaA(s)π(as)qπ(s,a)]q_{\pi}(s, a) = \sum_{s', r}p(s', r \mid s, a)\left[ r + \gamma \sum_{a' \in \mathcal{A}(s')} \pi(a' \mid s')q_{\pi}(s', a') \right]
Remark.

We can write the state-value function vπ(s)v_{\pi}(s) as a sum over action-value functions qπ(s,a)q_{\pi}(s, a):

vπ(s)=aA(s)π(as)qπ(s,a)=E[qπ(St,At)St=s]\begin{aligned} v_{\pi}(s) &= \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) q_{\pi}(s, a) \\ &= \mathbb{E}[q_{\pi}(S_t, A_t) \mid S_t = s] \end{aligned}

C. Unified Notation for Episodic and Continuing Tasks

In an episodic task, the interaction stops when we reach a terminal state. But our continuing-task equations are easier to reuse if every state has a next transition. We can bridge the two views by extending the process after termination in a harmless way.

Let S\mathcal{S} contain the non-terminal states and let S+=S{s}\mathcal{S}^+ = \mathcal{S} \cup \{s_\dagger\} include an absorbing terminal state ss_\dagger. We give this state one dummy action, A(s)={a}\mathcal{A}(s_\dagger)=\{a_\dagger\}, and define:

p(s,rs,a)={1if s=s and r=0,0otherwise.p(s', r \mid s_\dagger, a_\dagger) = \begin{cases} 1 & \text{if $s' = s_\dagger$ and $r = 0$}, \\ 0 & \text{otherwise}. \end{cases}

What does this convention buy us?

  • Once the process reaches ss_\dagger, it stays there.
  • Every later reward is zero, so extending the trajectory does not change the episodic return.
  • The terminal value is therefore vπ(s)=0v_\pi(s_\dagger)=0.

The dynamics now remain normalized even at the terminal state:

sS+rRp(s,rs,a)=1,sS+, aA(s).\sum_{s' \in \mathcal{S}^{+}} \sum_{r \in \mathcal{R}} p(s', r \mid s, a) = 1, \qquad \forall s \in \mathcal{S}^{+},\ a \in \mathcal{A}(s).

This is a notational extension, not a claim that the real environment continues after the episode ends.

D. Linearity of the Bellman Equation

For each state ss, we have a value vπ(s)v_{\pi}(s). If we assume nn states, we can write nn equations, turning the task of finding vπv_{\pi} into solving a system of equations:

{vπ(s1)=aA(s1)π(as1)s,rp(s,rs1,a)[r+γvπ(s)]vπ(s2)=aA(s2)π(as2)s,rp(s,rs2,a)[r+γvπ(s)]vπ(sn)=aA(sn)π(asn)s,rp(s,rsn,a)[r+γvπ(s)]\begin{cases} v_{\pi}(s_{1}) &= \sum_{a \in \mathcal{A}(s_1)} \pi(a \mid s_1) \sum_{s', r} p(s', r \mid s_1, a)[r + \gamma v_{\pi}(s')] \\ v_{\pi}(s_{2}) &= \sum_{a \in \mathcal{A}(s_2)} \pi(a \mid s_2) \sum_{s', r} p(s', r \mid s_2, a)[r + \gamma v_{\pi}(s')] \\ &\dots \\ v_{\pi}(s_{n}) &= \sum_{a \in \mathcal{A}(s_n)} \pi(a \mid s_n) \sum_{s', r} p(s', r \mid s_n, a)[r + \gamma v_{\pi}(s')] \\ \end{cases}

Writing this system in matrix form shows that the Bellman equation for a specific policy is linear. First, define:

  • State-transition matrix Pπ(ss)P^{\pi}(s' \mid s): If the agent is in state ss, what is the probability of transitioning to state ss', averaged over the actions of policy π\pi? Pπ(ss)=aA(s)π(as)rp(s,rs,a)P^{\pi}(s' \mid s) = \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \sum_{r} p(s', r \mid s, a)
  • Expected reward vector Rπ(s)R^{\pi}(s): If the agent is in state ss, what is the expected reward, averaged over the actions of policy π\pi? Rπ(s)=aA(s)π(as)s,rp(s,rs,a)rR^{\pi}(s) = \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)r

Thus, the Bellman equation becomes:

vπ(s)=Rπ(s)+γsSPπ(ss)vπ(s)v_{\pi}(s) = R^{\pi}(s) + \gamma \sum_{s' \in \mathcal{S}} P^{\pi}(s' \mid s) v_{\pi}(s')

Writing this as a system:

[vπ(s1)vπ(s2)vπ(sn)]vπ=[Rπ(s1)Rπ(s2)Rπ(sn)]rπ+γ[Pπ(s1s1)Pπ(s2s1)Pπ(sns1)Pπ(s1s2)Pπ(s2s2)Pπ(sns2)Pπ(s1sn)Pπ(s2sn)Pπ(snsn)]Pπ[vπ(s1)vπ(s2)vπ(sn)]vπ\underbrace{\begin{bmatrix} v_\pi(s_1) \\ v_\pi(s_2) \\ \vdots \\ v_\pi(s_n) \end{bmatrix}}_{\mathbf{v}_\pi} = \underbrace{\begin{bmatrix} R^\pi(s_1) \\ R^\pi(s_2) \\ \vdots \\ R^\pi(s_n) \end{bmatrix}}_{\mathbf{r}_{\pi}} + \gamma \underbrace{\begin{bmatrix} P^\pi(s_1|s_1) & P^\pi(s_2|s_1) & \dots & P^\pi(s_n|s_1) \\ P^\pi(s_1|s_2) & P^\pi(s_2|s_2) & \dots & P^\pi(s_n|s_2) \\ \vdots & \vdots & \ddots & \vdots \\ P^\pi(s_1|s_n) & P^\pi(s_2|s_n) & \dots & P^\pi(s_n|s_n) \end{bmatrix}}_{\mathbf{P}_\pi} \underbrace{\begin{bmatrix} v_\pi(s_1) \\ v_\pi(s_2) \\ \vdots \\ v_\pi(s_n) \end{bmatrix}}_{\mathbf{v}_\pi}

Finally, the matrix form is:

vπ=rπ+γPπvπ\mathbf{v}_\pi = \mathbf{r}_\pi + \gamma \mathbf{P}_\pi \mathbf{v}_\pi

If 0γ<10 \leq \gamma < 1, then Pπ\mathbf{P}_\pi is a stochastic matrix with spectral radius at most 11. Therefore, IγPπ\mathbf{I}-\gamma\mathbf{P}_\pi is invertible, and we can solve:

vπ=(IγPπ)1rπ\mathbf{v}_{\pi} = (\mathbf{I} - \gamma \mathbf{P}_{\pi})^{-1} \mathbf{r}_{\pi}

Notice how the condition γ<1\gamma<1 matters here. If we include our absorbing state and set γ=1\gamma=1, the full transition matrix has eigenvalue 11, so IPπ\mathbf{I}-\mathbf{P}_\pi is singular. For an episodic problem, we instead impose vπ(s)=0v_\pi(s_\dagger)=0 and solve over the transient non-terminal states when the policy reaches termination with probability 11.

Even when the inverse exists, we normally solve the linear system rather than explicitly forming the inverse. A direct dense solve costs roughly O(n3)\mathcal{O}(n^3), although sparse or iterative solvers may do much better. More fundamentally, large state spaces make the system expensive, and model-free settings do not give us Pπ\mathbf{P}_\pi or rπ\mathbf{r}_\pi in the first place.

E. How to Find the Optimal Policy

Suppose we already know vv_\ast. At each state, let a(s)a_\ast(s) be any action attaining the maximum in the Bellman optimality equation:

v(s)=maxaA(s)s,rp(s,rs,a)[r+γv(s)]a(s)argmaxaA(s)s,rp(s,rs,a)[r+γv(s)]=argmaxaA(s)q(s,a).\begin{aligned} v^{\ast}(s) &= \max_{a \in \mathcal{A}(s)} \sum_{s', r} p(s', r \mid s, a) [r + \gamma v_{\ast}(s')] \\ a_\ast(s) &\in \arg \max_{a \in \mathcal{A}(s)} \sum_{s', r} p(s', r \mid s, a) [r + \gamma v_{\ast}(s')] \\ &= \arg \max_{a \in \mathcal{A}(s)} q_{\ast}(s, a). \end{aligned}

We use \in because several actions may tie. Setting π(a(s)s)=1\pi_\ast(a_\ast(s)\mid s)=1 gives a deterministic greedy policy, and in the finite discounted setting that policy is optimal.

There are two equivalent ways to make this choice: one uses vv_{\ast} and the dynamics, while the other uses qq_{\ast} directly.

Based on optimal state-value function:

  • To find π\pi^{\ast} knowing vv_{\ast}, we must know the environment dynamics p(s,rs,a)p(s', r \mid s,a). We must also perform a one-step (look-ahead) search, summing over all possible next states ss' and rewards rr.
  • This selection appears greedy because we only look at the immediate reward rr and the value of the next state v(s)v_{\ast}(s'). However, this yields the optimal result for the long term. This works because v(s)v_{\ast}(s') already accounts for the optimal reward sequence in the future.

Based on optimal action-value function: If qq_\ast is already known, the agent does not need a one-step search; it can pick an action with the highest q(s,a)q_{\ast}(s, a). The decision itself does not require an explicit dynamics model, although learning or computing qq_\ast still requires information obtained from a model or experience.

/help