Markov Decision Processes & Bellman Equations

962 words
5 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Markov Decision Processes & Bellman Equations ## ๐ŸŽฏ Learning Objectives - Formalize RL problems as MDPs - Understand the Markov property and its implications - Derive Bellman expectation and optimality equations - Implement value and action-value function estimation ## ๐Ÿ“‹ Prerequisites - Probability (conditional p...

Markov Decision Processes & Bellman Equations

๐ŸŽฏ Learning Objectives

  • Formalize RL problems as MDPs
  • Understand the Markov property and its implications
  • Derive Bellman expectation and optimality equations
  • Implement value and action-value function estimation

๐Ÿ“‹ Prerequisites

  • Probability (conditional probability, expectation)
  • Dynamic programming concepts

1. ๐Ÿ“– Core Content

1.1 The Agent-Environment Interface

An MDP is defined by the tuple (S,A,P,R,ฮณ)(S, A, P, R, \gamma):
  • SS: Set of states
  • AA: Set of actions
  • P(sโ€ฒโˆฃs,a)P(s'|s,a): Transition probability to state s' after action a in state s
  • R(s,a,sโ€ฒ)R(s,a,s'): Reward received after transitioning
  • ฮณโˆˆ[0,1]\gamma \in [0,1]: Discount factor (Diagram) Markov Property: The future is independent of the past given the present:
P(St+1โˆฃSt,At)=P(St+1โˆฃS1,A1,...,St,At)P(S_{t+1} | S_t, A_t) = P(S_{t+1} | S_1, A_1, ..., S_t, A_t)

1.2 Returns and Discounting

The return GtG_t is the discounted cumulative reward:
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}
Why discount?
  1. Mathematical convenience: Infinite sums converge
  2. Uncertainty: Future rewards are uncertain
  3. Human preference: We prefer immediate rewards
  4. Avoids infinite loops (in continuing tasks)

1.3 Value Functions

State-value function Vฯ€(s)V^\pi(s): Expected return starting from state s following policy ฯ€\pi
Vฯ€(s)=Eฯ€[GtโˆฃSt=s]V^\pi(s) = \mathbb{E}_\pi[G_t | S_t = s]
Action-value function Qฯ€(s,a)Q^\pi(s,a): Expected return starting from state s, taking action a, then following policy ฯ€\pi
Qฯ€(s,a)=Eฯ€[GtโˆฃSt=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi[G_t | S_t = s, A_t = a]

1.4 Bellman Expectation Equation

For a given policy ฯ€\pi:
Vฯ€(s)=โˆ‘aฯ€(aโˆฃs)โˆ‘sโ€ฒ,rP(sโ€ฒ,rโˆฃs,a)[r+ฮณVฯ€(sโ€ฒ)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s',r} P(s',r|s,a)[r + \gamma V^\pi(s')] Qฯ€(s,a)=โˆ‘sโ€ฒ,rP(sโ€ฒ,rโˆฃs,a)[r+ฮณโˆ‘aโ€ฒฯ€(aโ€ฒโˆฃsโ€ฒ)Qฯ€(sโ€ฒ,aโ€ฒ)]Q^\pi(s,a) = \sum_{s',r} P(s',r|s,a)[r + \gamma \sum_{a'} \pi(a'|s') Q^\pi(s',a')]

1.5 Bellman Optimality Equation

The optimal value function Vโˆ—V^* gives the maximum return attainable:
Vโˆ—(s)=maxโกaโˆ‘sโ€ฒ,rP(sโ€ฒ,rโˆฃs,a)[r+ฮณVโˆ—(sโ€ฒ)]V^*(s) = \max_a \sum_{s',r} P(s',r|s,a)[r + \gamma V^*(s')] Qโˆ—(s,a)=โˆ‘sโ€ฒ,rP(sโ€ฒ,rโˆฃs,a)[r+ฮณmaxโกaโ€ฒQโˆ—(sโ€ฒ,aโ€ฒ)]Q^*(s,a) = \sum_{s',r} P(s',r|s,a)[r + \gamma \max_{a'} Q^*(s',a')]
The optimal policy ฯ€โˆ—\pi^* can be derived greedily from Qโˆ—Q^*:
ฯ€โˆ—(s)=argโกmaxโกaQโˆ—(s,a)\pi^*(s) = \arg\max_a Q^*(s,a)

1.6 Worked Example: Grid World

Consider a 2ร—2 grid with 4 states S = {A, B, C, D}:
pseudo
A โ†’ B
โ†“   โ†“
C โ†’ D
Actions: {Right, Down}, ฮณ=0.9
  • From A: Right โ†’ B (R=0), Down โ†’ C (R=0)
  • From B: Right โ†’ D (R=0), Down โ†’ D (R=+1)
  • From C: Right โ†’ D (R=0), Down โ†’ D (R=+1)
  • From D: Terminal (no further reward) Let's compute Vโˆ—V^* using Bellman optimality: Vโˆ—(D)=0V^*(D) = 0 (terminal) Vโˆ—(B)=maxโก{0.9ร—Vโˆ—(D),โˆ’1+0.9ร—Vโˆ—(D)}=maxโก{0,โˆ’1}=0V^*(B) = \max\{0.9 \times V^*(D), -1 + 0.9 \times V^*(D)\} = \max\{0, -1\} = 0 (Right is optimal) Vโˆ—(C)=maxโก{0.9ร—Vโˆ—(D),โˆ’1+0.9ร—Vโˆ—(D)}=maxโก{0,โˆ’1}=0V^*(C) = \max\{0.9 \times V^*(D), -1 + 0.9 \times V^*(D)\} = \max\{0, -1\} = 0 (Right is optimal) Vโˆ—(A)=maxโก{0.9ร—Vโˆ—(B),0.9ร—Vโˆ—(C)}=maxโก{0,0}=0V^*(A) = \max\{0.9 \times V^*(B), 0.9 \times V^*(C)\} = \max\{0, 0\} = 0 (tie, either action)

๐Ÿ“ Practice Questions

Q1
<strong>Q1</strong>: In a 3-state MDP, transitioning from state 1 gives reward +5 and goes to state 2. State 2 gives reward +10 and goes to state 3 (terminal). ฮณ=0.9. Compute V(1) and V(2).
V(3) = 0 (terminal)
V(2) = 10 + 0.9 ร— V(3) = 10 + 0 = 10
V(1) = 5 + 0.9 ร— V(2) = 5 + 0.9 ร— 10 = 5 + 9 = 14
So V*(1) = 14 (assuming only one action per state). The agent should expect total discounted return of 14 starting from state 1. Q2
<strong>Q2
<strong>Q2</strong>: If ฮณ=0.5 instead of 0.9, how do the values change? What does this tell us?
V(2) = 10 + 0.5 ร— 0 = 10 (same โ€” value of future is already 0) V(1) = 5 + 0.5 ร— 10 = 5 + 5 = 10
With lower ฮณ: V(1) drops from 14 to 10. This shows that lower discount factors make the agent value immediate rewards more. With ฮณ=0.5, the reward from state 1 (5) contributes more to V(1) than the delayed reward from state 2 (10 ร— 0.5 = 5).
ฮณ close to 1: far-sighted agent ฮณ close to 0: short-sighted agent Q3
<strong>Q3
<strong>Q3
<strong>Q3
<strong>Q3</strong>: Why is the Bellman equation a "self-consistency" condition?
The Bellman equation expresses a recursive relationship:
  • V(s) = immediate reward + discounted V(next state)
  • But V(next state) depends on V(state after that), and so on
This is self-consistency because the value of a state must equal its own definition. It's not a computational formula (you can't compute V(s) from V(s') without knowing V(s') first) โ€” it's a condition that the true value function must satisfy.
The Bellman equation becomes useful when turned into an update rule: Vk+1(s)=maxโกaโˆ‘P[r+ฮณVk(sโ€ฒ)]V_{k+1}(s) = \max_a \sum P[r + \gamma V_k(s')]
This iterative approach converges to the true V*. Q4
<strong>Q4
<strong>Q4
<strong>Q4
<strong>Q4
<strong>Q4</strong>: Derive the relationship between V^ฯ€ and Q^ฯ€.
By definition: Vฯ€(s)=Eฯ€[GtโˆฃSt=s]V^\pi(s) = \mathbb{E}_\pi[G_t | S_t = s] Qฯ€(s,a)=Eฯ€[GtโˆฃSt=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi[G_t | S_t = s, A_t = a]
The relationship: Vฯ€(s)=โˆ‘aฯ€(aโˆฃs)Qฯ€(s,a)V^\pi(s) = \sum_a \pi(a|s) Q^\pi(s,a)
The value of a state under policy ฯ€ is the weighted average of action-values, weighted by the probability of taking each action under ฯ€.
Conversely: Qฯ€(s,a)=โˆ‘sโ€ฒ,rP(sโ€ฒ,rโˆฃs,a)[r+ฮณVฯ€(sโ€ฒ)]Q^\pi(s,a) = \sum_{s',r} P(s',r|s,a)[r + \gamma V^\pi(s')]
The value of taking action a in state s is the immediate reward plus the discounted value of the next state.
</details> * * * ## ๐Ÿ”— Cross-References - **Next**: [Dynamic Programming](/notes/04-degree-electives-bsda5007-reinforcement-learning-week03-03-dynamic-programming) - **Previous**: [Multi-Armed Bandits](/notes/04-degree-electives-bsda5007-reinforcement-learning-week01-01-multi-armed-bandits) - **Video**: BSDA5007 Week 2 transcripts [Join Discord](https://discord.gg/gE2m4Qrdqv) [Previous**Multi-Armed Bandits**](/notes/04-degree-electives-bsda5007-reinforcement-learning-week01-01-multi-armed-bandits)[Next**Dynamic Programming**](/notes/04-degree-electives-bsda5007-reinforcement-learning-week03-03-dynamic-programming)
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.