5.1.2Reinforcement Learning Foundations

Markov Decision Processes (MDP)

2,927 words13 min readdifficulty · medium6 backlinks

What is an MDP?

Why these components?

  • States: Where can the agent be? (positions, configurations, observations)
  • Actions: What choices does the agent have? (moves, controls, decisions)
  • Transitions: What happens when I act? (physics, rules, uncertainty)
  • Rewards: What's good/bad? (goals encoded as numbers)
  • Discount: Future rewards are worth less (impatience, uncertainty, mathematical convenience)

Deriving the Value Function from First Principles

The Goal: Find a policy π:SA\pi: S \to A (a strategy mapping states to actions) that maximizes total reward.

Step 1: Total Return

Starting from state s0s_0, if we follow policy π\pi, we get a sequence: s0a0s1a1s2a2s_0 \xrightarrow{a_0} s_1 \xrightarrow{a_1} s_2 \xrightarrow{a_2} \cdots

The return GtG_t from time tt is the sum of all future rewards: Gt=Rt+1+Rt+2+Rt+3+G_t = R_{t+1} + R_{t+2} + R_{t+3} + \cdots

Problem: This sum can be infinite and doesn't distinguish near vs. far rewards.

Step 2: Discounted Return

Why discount? Three reasons:

  1. Uncertainty: Future is less certain, so worth less
  2. Impatience: Prefer reward now over later (economic time-preference)
  3. Mathematical convergence: Ensures finite sums for infinite horizons

Discounted return: 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} + \cdots = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}

Why this works: If γ<1|\gamma| < 1 and rewards are bounded by RmaxR_{\max}, then: Gtk=0γkRmax=Rmax1γG_t \leq \sum_{k=0}^{\infty} \gamma^k R_{\max} = \frac{R_{\max}}{1-\gamma} The geometric series converges!

Step 3: State-Value Function

The state-value function Vπ(s)V^\pi(s) is the expected return starting from state ss and following policy π\pi:

Vπ(s)=Eπ[GtSt=s]V^\pi(s) = \mathbb{E}_\pi[G_t | S_t = s]

Why expectation? Transitions are stochastic (P(ss,a)P(s'|s,a)), so we average over all possible futures.

Expanding: Vπ(s)=Eπ[Rt+1+γGt+1St=s]V^\pi(s) = \mathbb{E}_\pi[R_{t+1} + \gamma G_{t+1} | S_t = s]

Key insight: Gt+1G_{t+1} is itself a return from St+1S_{t+1}, so E[Gt+1St+1=s]=Vπ(s)\mathbb{E}[G_{t+1}|S_{t+1}=s'] = V^\pi(s').

Step 4: Bellman Equation Derivation

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}) | S_t = s]

Under policy π\pi, we take action a=π(s)a = \pi(s) deterministically (or sample from π(as)\pi(a|s) for stochastic policies). For deterministic π\pi:

Vπ(s)=sP(ss,π(s))[R(s,π(s),s)+γVπ(s)]V^\pi(s) = \sum_{s'} P(s'|s,\pi(s)) \left[ R(s,\pi(s),s') + \gamma V^\pi(s') \right]

Breaking it down:

  1. We're in state ss, policy says take action π(s)\pi(s)
  2. Environment randomly picks next state ss' with probability P(ss,π(s))P(s'|s,\pi(s))
  3. We get immediate reward R(s,π(s),s)R(s,\pi(s),s')
  4. Then we get discounted future value γVπ(s)\gamma V^\pi(s') from the new state

Step 5: Action-Value Function (Q-function)

Sometimes we want to know: "How good is taking action aa in state ss (and then following π\pi afterward)?"

Action-value function: Qπ(s,a)=Eπ[GtSt=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi[G_t | S_t = s, A_t = a]

Derivation: Qπ(s,a)=sP(ss,a)[R(s,a,s)+γVπ(s)]Q^\pi(s,a) = \sum_{s'} P(s'|s,a) \left[ R(s,a,s') + \gamma V^\pi(s') \right]

Relationship to VV: Vπ(s)=Qπ(s,π(s))V^\pi(s) = Q^\pi(s, \pi(s))

Optimal Policy and Bellman Optimality

Goal: Find the optimal policy π\pi^* that maximizes value for all states: π=argmaxπVπ(s),sS\pi^* = \arg\max_\pi V^\pi(s), \quad \forall s \in S

Define optimal value functions: V(s)=maxπVπ(s)V^*(s) = \max_\pi V^\pi(s) Q(s,a)=maxπQπ(s,a)Q^*(s,a) = \max_\pi Q^\pi(s,a)

Deriving Bellman Optimality Equation

If V(s)V^*(s) is the best possible value, then: V(s)=maxaAQ(s,a)V^*(s) = \max_{a \in A} Q^*(s,a)

Why? The optimal policy must pick the action with highest Q-value.

Substituting the Q-function expansion: V(s)=maxaAsP(ss,a)[R(s,a,s)+γV(s)]V^*(s) = \max_{a \in A} \sum_{s'} P(s'|s,a) \left[ R(s,a,s') + \gamma V^*(s') \right]

For Q-function: Q(s,a)=sP(ss,a)[R(s,a,s)+γmaxaQ(s,a)]Q^*(s,a) = \sum_{s'} P(s'|s,a) \left[ R(s,a,s') + \gamma \max_{a'} Q^*(s',a') \right]

Optimal policy extraction: Once we have VV^* or QQ^*: π(s)=argmaxaQ(s,a)\pi^*(s) = \arg\max_a Q^*(s,a)

Worked Examples

Common Mistakes and Fixes

Memory Aids

Recall Feynman Explain-to-a-12-Year-Old

Imagine you're playing a board game. At each turn, you're on some square (that's the state). You pick a move (that's your action). The game has some randomness—maybe you roll a dice to see where you land (that's the transition probability). When you land, you get points or lose points (that's the reward).

Now, the Markov property says: only your current square matters for deciding your next move and where you'll land. You don't need to remember every square you visited before—just look at where you are now.

Your policy is your strategy: "When I'm on square X, I'll choose move Y." The value of a square is: "If I start here and play smartly, how many points will I get in total?" The discount factor is like saying "I care about points I'll get soon more than points far in the future."

The Bellman equation is just math for: "The value of my current square = points I get right now + (discounted) value of where I'll land next."

Finding the best strategy means: for every square, pick the move that leads to the highest total points. That's the optimal policy!

Connections


#flashcards/ai-ml

What is the Markov Property in an MDP? :: The probability of the next state depends only on the current state and action, not on the history: P(st+1st,at,st1,...)=P(st+1st,at)P(s_{t+1}|s_t,a_t,s_{t-1},...) = P(s_{t+1}|s_t,a_t)

What are the five components of an MDP?
States (S), Actions (A), transition Probability (P), Rewards (R), and discount factor (gamma)
What is the discount factor γ\gamma and why is it needed?
A value in [0,1) that makes future rewards worth less than immediate rewards. Needed for mathematical convergence (ensures finite sums), models uncertainty about the future, and controls howarsighted the agent is.
What is the state-value function Vπ(s)V^\pi(s)?
The expected return (cumulative discounted reward) starting from state ss and following policy π\pi: Vπ(s)=Eπ[GtSt=s]V^\pi(s) = \mathbb{E}_\pi[G_t | S_t = s]
What is the action-value function Qπ(s,a)Q^\pi(s,a)?
The expected return starting from state ss, taking action aa, then following policy π\pi: Qπ(s,a)=Eπ[GtSt=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi[G_t | S_t=s, A_t=a]
State the Bellman Expectation Equation for VπV^\pi
Vπ(s)=sP(ss,π(s))[R(s,π(s),s)+γVπ(s)]V^\pi(s) = \sum_{s'} P(s'|s,\pi(s)) [R(s,\pi(s),s') + \gamma V^\pi(s')] — value equals immediate reward plus discounted future value
State the Bellman Optimality Equation for VV^*
V(s)=maxasP(ss,a)[R(s,a,s)+γV(s)]V^*(s) = \max_a \sum_{s'} P(s'|s,a)[R(s,a,s') + \gamma V^*(s')] — optimal value equals the maximum over actions of expected reward + discounted optimal future
How do you extract the optimal policy from Q(s,a)Q^*(s,a)?
π(s)=argmaxaQ(s,a)\pi^*(s) = \arg\max_a Q^*(s,a) — pick the action with the highest Q-value in each state
What is the relationship between Vπ(s)V^\pi(s) and Qπ(s,a)Q^\pi(s,a)?
Vπ(s)=Qπ(s,π(s))V^\pi(s) = Q^\pi(s, \pi(s)) — the value of a state equals the Q-value of the action the policy takes in that state
What is a policy in an MDP?
A mapping from states to actions π:SA\pi: S \to A (deterministic) or a probability distribution π(as)\pi(a|s) (stochastic) that defines the agent's behavior
If γ=0.9\gamma = 0.9 and you're in a state 3 steps from a terminal reward of 100, what's the undiscounted contribution?
γ3×100=0.93×100=0.729×100=72.9\gamma^3 \times 100 = 0.9^3 \times 100 = 0.729 \times 100 = 72.9
Why can't we use γ=1\gamma = 1 in continuing tasks?
Because the infinite sum of rewards would diverge (infinite value), making the problem mathematically intractable unless all rewards are zero
What's the effective planning horizon for γ=0.95\gamma = 0.95?
Approximately 1/(1γ)=1/0.05=201/(1-\gamma) = 1/0.05 = 20 steps — that's how far into the future the agent effectively considers

True or False: The Markov property means the policy cannot use historical information :: False. The policy can use history if encoded in the state. The Markov property applies to environment dynamics, not policy design.

What's the difference between model-based and model-free RL in the context of MDPs?
Model-based knows P(ss,a)P(s'|s,a) and R(s,a,s)R(s,a,s') (the MDP model) and can use planning. Model-free doesn't know these and must learn from experience.

Concept Map

defined as tuple

includes

includes

includes

includes

includes

relies on

mapped by policy

generates

ensures convergence of

expected value gives

recursive form

Markov Decision Process

S A P R gamma

States S

Actions A

Transition P(s prime given s, a)

Reward R

Discount gamma

Markov Property Memoryless

Policy pi

Discounted Return G_t

State-Value V pi

Bellman Equation

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo, is concept ko simple tareeke se samajhte hain. MDP ka core idea yeh hai ki jab bhi tum koi decision lete ho—jaise video game khelte waqt—tumhare liye sirf abhi ki current situation matter karti hai, poori purani history nahi. Iss "memoryless" property ko hi Markov property kehte hain. Iska matlab: agar tum jaante ho abhi kahan ho (current state), toh next best move decide karne ke liye tumhe yeh yaad rakhne ki zarurat nahi ki tum yahan tak kaise pahunche. Yeh property complex problems ko manageable bana deti hai, kyunki hume infinite history track nahi karni padti—bas "yahan best action kya hai?" pe focus karo.

Ab MDP ek tuple hai (S,A,P,R,γ)(S, A, P, R, \gamma) jismein states (kahan ho sakte ho), actions (kya choices hain), transition probability (action lene par kahan pahunchoge, thoda uncertainty ke saath), reward (kya achha ya bura hai, numbers mein) aur discount factor γ\gamma (future rewards ki value aaj se kam) hote hain. Discount factor bahut important hai—yeh isliye lagate hain kyunki future uncertain hai, hum abhi ke reward ko zyada prefer karte hain, aur mathematically yeh infinite sum ko converge kara deta hai. Iske baad hum Value Function banate hain jo batati hai ki kisi state se start karke, ek policy follow karte hue, average mein kitna total reward milega.

Yeh matter kyun karta hai? Kyunki self-driving cars, game-playing AI (jaise AlphaGo), robotics, aur recommendation systems—yeh sab MDP framework pe hi chalte hain. Bellman equation jo humne derive ki—Vπ(s)=V^\pi(s) = immediate reward ++ discounted future value—yeh ek recursive relationship hai jo poore Reinforcement Learning ki neev hai. Ek baar tumne yeh intuition pakad li ki "aaj ki value = abhi ka reward + kal ki value ka discounted version," toh aage ke saare RL algorithms (Q-learning, policy gradients, sab) tumhe naturally samajh aane lagenge. Isliye yeh foundation rock-solid banana zaruri hai!

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections