5.1.3Reinforcement Learning Foundations

Policies and value functions

3,034 words14 min readdifficulty · medium1 backlinks

What Is a Policy?

Why stochastic? In some environments (partially observable, or games with mixed strategies), randomizing actions is optimal. Also helps with exploration during learning.

How does it work?

  1. Agent observes state sts_t
  2. Policy outputs action: atπ(st)a_t \sim \pi(\cdot|s_t) (sample from the distribution)
  3. Environment responds with reward rt+1r_{t+1} and next state st+1s_{t+1}
  4. Repeat

State-Value Function: V(s)

Derivation from first principles:

Start with the definition of "expected future return" when you're in state ss at time tt.

The return (cumulative reward) is: 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 discount?

  • Rewards now are more certain than rewards later (uncertainty)
  • Mathematically ensures convergence when rewards are bounded
  • Models impatience (economic interpretation)

The value of state ss under policy π\pi is the expected return: Vπ(s)=Eπ[GtSt=s]V^\pi(s) = \mathbb{E}_\pi[G_t | S_t = s]

Why expectation? The policy might be stochastic, and the environment dynamics P(ss,a)P(s'|s,a) are often stochastic, so we average over all possible futures.

Bellman Equation for VπV^\pi:

We can decompose the return recursively: Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}

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

Now split the expectation. First, we sample action from policy, then next state from dynamics: Vπ(s)=aπ(as)sP(ss,a)[R(s,a,s)+γVπ(s)]V^\pi(s) = \sum_a \pi(a|s) \sum_{s'} P(s'|s,a) \left[R(s,a,s') + \gamma V^\pi(s')\right]

Why this step? We marginalize over all possible actions (weighted by policy) and all possible next states (weighted by transition probability).

Compact form: Vπ(s)=aπ(as)s,rp(s,rs,a)[r+γVπ(s)]\boxed{V^\pi(s) = \sum_a \pi(a|s) \sum_{s',r} p(s',r|s,a)\left[r + \gamma V^\pi(s')\right]}

This is the Bellman expectation equation for VπV^\pi.


Action-Value Function: Q(s,a)

Why Q vs V?

  • Vπ(s)V^\pi(s) tells you "how good is state ss"
  • Qπ(s,a)Q^\pi(s,a) tells you "how good is taking action aa in state ss"
  • Q is more granular—you can improve the policy by comparing Q-values for different actions.

Relationship between Q and V:

Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s) Q^\pi(s,a)

Why? The value of a state is the expected Q-value over actions sampled from the policy.

Bellman Equation for QπQ^\pi:

Derive from first principles. The return after taking action aa in state ss: Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}

So: Qπ(s,a)=Eπ[Rt+1+γGt+1St=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi[R_{t+1} + \gamma G_{t+1} | S_t=s, A_t=a]

After taking aa, you transition to ss', then follow policy π\pi from there: Qπ(s,a)=s,rp(s,rs,a)[r+γaπ(as)Qπ(s,a)]Q^\pi(s,a) = \sum_{s',r} p(s',r|s,a) \left[r + \gamma \sum_{a'} \pi(a'|s') Q^\pi(s',a')\right]

Why?

  1. Immediate reward rr comes from the transition (s,a)s(s,a) \to s'
  2. Future value aπ(as)Qπ(s,a)\sum_{a'} \pi(a'|s') Q^\pi(s',a') is just Vπ(s)V^\pi(s')

Compact form: Qπ(s,a)=s,rp(s,rs,a)[r+γVπ(s)]\boxed{Q^\pi(s,a) = \sum_{s',r} p(s',r|s,a)\left[r + \gamma V^\pi(s')\right]}

Or equivalently: Qπ(s,a)=s,rp(s,rs,a)[r+γaπ(as)Qπ(s,a)]\boxed{Q^\pi(s,a) = \sum_{s',r} p(s',r|s,a)\left[r + \gamma \sum_{a'} \pi(a'|s') Q^\pi(s',a')\right]}


Optimal Policy and Optimal Value Functions

Key insight: For any MDP, there exists at least one optimal policy π\pi^* (may not be unique). Once you know Q(s,a)Q^*(s,a), the optimal policy is: π(s)=argmaxaQ(s,a)\pi^*(s) = \arg\max_a Q^*(s,a)

Why? If you always pick the action with the highest Q-value, you're being greedy with respect to the optimal Q-function, which is the optimal policy.

Bellman Optimality Equation for VV^*:

V(s)=maxas,rp(s,rs,a)[r+γV(s)]V^*(s) = \max_a \sum_{s',r} p(s',r|s,a)\left[r + \gamma V^*(s')\right]

Derivation:

  • Optimal value = best possible expected return
  • "Best possible" means choosing the best action at every step
  • So: V(s)=maxaQ(s,a)V^*(s) = \max_a Q^*(s,a)
  • Substitute the Bellman equation for QQ^*

Bellman Optimality Equation for QQ^*:

Q(s,a)=s,rp(s,rs,a)[r+γmaxaQ(s,a)]Q^*(s,a) = \sum_{s',r} p(s',r|s,a)\left[r + \gamma \max_{a'} Q^*(s',a')\right]

Why the max? After taking action aa and reaching ss', you act optimally from ss' onward, which means taking the action that maximizes Q(s,a)Q^*(s',a').


Common Mistakes


Active Recall

Recall Explain to a12-year-old

Imagine you're playing a video game. You're trying to get the highest score possible.

A policy is your game plan—like "when I see an enemy, I should jump" or "when I find a coin, I pick it up." It's the rules you follow to decide what to do in each situation.

A value function is like a score predictor. It tells you, "If I'm at this spot in the game, following my game plan, how many points will I get from now until the game ends?"

There are two types:

  1. V(s): "How good is this spot?" (Just based on where you are.)
  2. Q(s,a): "How good is doing this specific action at this spot?" (Like, "If I jump right now, how many points will I get?")

The coolest part? You can use Q to improve your game plan: just always do the action with the highest Q-value!


Connections 5.1.01-Introduction-to-RL — Policies and value functions formalize the RL problem

  • 5.1.02-Markov-Decision-Processes — MDPs provide the mathematical framework for defining policies and computing values
  • 5.1.04-Bellman-Equations — Bellman equations are the recursive relationships for V and Q
  • 5.2.01-Policy-Iteration — Uses value functions to iteratively improve policies
  • 5.2.02-Value-Iteration — Directly computes optimal value function, then extracts optimal policy
  • 5.3.01-Q-Learning — Model-free algorithm that learns Q* directly from experience
  • 5.3.03-PolicyGradient-Methods — Directly optimizes parameterized policies πθ\pi_\theta

#flashcards/ai-ml

What is a policy in RL? :: A mapping from states to actions (or distributions over actions). It defines the agent's behavior. Deterministic: a=π(s)a = \pi(s); Stochastic: π(as)\pi(a|s).

What is the state-value function Vπ(s)V^\pi(s)?
The expected cumulative discounted reward starting from state ss, following policy π\pi: Vπ(s)=Eπ[k=0γkRt+k+1St=s]V^\pi(s) = \mathbb{E}_\pi[\sum_{k=0}^\infty \gamma^k R_{t+k+1} | S_t=s].
What is the action-value function Qπ(s,a)Q^\pi(s,a)?
The expected cumulative discounted reward starting from state ss, taking action aa, then following policy π\pi: Qπ(s,a)=Eπ[k=0γkRt+k+1St=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi[\sum_{k=0}^\infty \gamma^k R_{t+k+1} | S_t=s, A_t=a].
How are V and Q related?
Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s) Q^\pi(s,a). The value of a state is the expected Q-value over actions sampled from the policy.
What is the Bellman expectation equation for VπV^\pi?
Vπ(s)=aπ(as)s,rp(s,rs,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')]. It recursively relates the value of a state to the values of successor states.
What is the Bellman expectation equation for QπQ^\pi?
Qπ(s,a)=s,rp(s,rs,a)[r+γaπ(as)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')]. It decomposes Q into immediate reward plus discounted future Q-values.
What is an optimal policy π\pi^*?
A policy that maximizes the value function for all states: Vπ(s)Vπ(s)V^{\pi^*}(s) \geq V^\pi(s) for all ss and all policies π\pi.
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). Choose the action with the highest optimal Q-value in each state.
What is the Bellman optimality equation for VV^*?
V(s)=maxas,rp(s,rs,a)[r+γV(s)]V^*(s) = \max_a \sum_{s',r} p(s',r|s,a)[r + \gamma V^*(s')]. The optimal value is the maximum over actions of the expected immediate reward plus discounted successor value.
What is the Bellman optimality equation for QQ^*?
Q(s,a)=s,rp(s,rs,a)[r+γmaxaQ(s,a)]Q^*(s,a) = \sum_{s',r} p(s',r|s,a)[r + \gamma \max_{a'} Q^*(s',a')]. The optimal Q-value is the expected immediate reward plus the discounted maximum Q-value in the next state.
Why use a discount factor γ<1\gamma < 1?
Ensures convergence of infinite sums (geometric series), models uncertainty about the future, and represents time preference. Without it, infinite-horizon problems with positive rewards have infinite value.
What's the difference between Vπ(s)V^\pi(s) and V(s)V^*(s)?
Vπ(s)V^\pi(s) is the value under a specific policy π\pi (average over actions by π\pi). V(s)V^*(s) is the optimal value (maximum over all policies, or equivalently, max over actions).

Concept Map

selects action in

env responds

feeds into

deterministic or stochastic

expected value gives

discounted by

recursive split

defines

critic evaluates

acts, V improves it

ensures

stochastic aids

Policy pi

Action a_t

Reward and next state

Return G_t

Policy types

State-Value V of s

Discount factor gamma

Bellman Equation

Policy improvement

Convergence and impatience

Exploration

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalो dost, is note ka core idea bahut simple hai. Do cheezein samajhni hain — policy aur value function. Policy matlab tumhaari strategy — kaunse situation (state) mein kya action lena hai. Jaise chess khelte waqt "opponent meri queen pe attack kare toh main use hata dunga" — yeh tumhara plan hai. Value function alag cheez hai — yeh ek critic ki tarah hai jo batata hai ki "yeh position kitni acchi hai long run mein". Matlab abhi ke reward ke saath saath future ke saare rewards ka andaaza. Policy tumhe act karne mein help karti hai, aur value function us policy ko improve karne mein help karta hai — dono milke agent ko smart banate hain.

Ab value function ke andar ek important concept hai — discount factor gamma. Isse hum future rewards ko thoda kam weight dete hain, kyunki aaj ka reward zyada certain hota hai bajaye future ke reward ke. Aur sabse pyaari baat hai Bellman equation — yeh bolta hai ki kisi state ki value = abhi milne wala reward + gamma times next state ki value. Yani problem ko recursive tod diya, ek badi cheez ko chhoti cheezon mein. Yahi trick puri RL ki backbone hai. Aur jab policy ya environment stochastic (random) ho, tab hum expectation (average) lete hain saare possible futures ka — isliye formulas mein summation aur probabilities aati hain.

Yeh matter kyun karta hai? Kyunki jitne bhi real-world RL systems hain — game-playing AI, self-driving cars, stock trading bots, robots — sab isi foundation pe khade hain. Agent ko decide karna hota hai ki har situation mein kya karna hai (policy) aur kaunsa decision long-term mein faayda dega (value function). Agar tum yeh Bellman recursion aur expectation ka funda ache se pakad loge, toh aage ke advanced topics jaise Q-learning aur policy gradients tumhe bilkul easy lagenge. Isliye is chhoti si example ko haath se solve karna zaroor — practice se hi intuition set hoti hai!

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections