5.1.4Reinforcement Learning Foundations

State-value and action-value functions

2,592 words12 min readdifficulty · medium

Overview

The state-value function and action-value function are the core tools for quantifying "how good" a state or action is under a given policy. They convert the abstract goal of "maximize future rewards" into concrete numbers we can compute and optimize.

Figure — State-value and action-value functions

[!intuition] Why do we need value functions?

An agent in state ss wants to know: "Should I stay or should I go?" But the answer depends on:

  • Immediate reward from actions available now
  • Future rewards from states we'll visit later
  • The policy we're following (which actions we tend to take)

Value functions compress the infinite future into a single number: the expected cumulative reward from this point forward. This lets us compare states and actions numerically.

The key insight: Instead of simulating thousands of possible futures every time, we learn a function V(s)V(s) or Q(s,a)Q(s,a) that memorizes "how good is this situation?"


[!definition] State-value function Vπ(s)V^\pi(s)

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

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

where 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} + \ldots = \sum_{k=0}^\infty \gamma^k R_{t+k+1} is the discounted return.

Breaking it down:

  • Eπ\mathbb{E}_\pi: expectation over trajectories sampled by following policy π\pi
  • GtG_t: sum of future rewards, discounted by γ[0,1]\gamma \in [0,1]
  • St=sS_t = s: we condition on starting in state ss

What this tells us: Vπ(s)V^\pi(s) answers "If I'm in state ss and keep following my current policy π\pi, what's my expected total reward?"


[!definition] Action-value function Qπ(s,a)Q^\pi(s,a)

The action-value function (or Q-function) Qπ(s,a)Q^\pi(s,a) is the expected return when starting in state ss, taking action aa, and then following policy π\pi:

Qπ(s,a)=Eπ[GtSt=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi\left[ G_t \mid S_t = s, A_t = a \right]

The difference from Vπ(s)V^\pi(s):

  • Vπ(s)V^\pi(s): "How good is this state under policy π\pi?"
  • Qπ(s,a)Q^\pi(s,a): "How good is taking action aa in state ss, then following π\pi?"

QQ gives us action-level granularity. We can compare different actions in the same state.


[!formula] Relationship between VV and QQ

Deriving VV from QQ

If we know Qπ(s,a)Q^\pi(s,a) for all actions, we can compute Vπ(s)V^\pi(s) by averaging over the policy's action distribution:

Vπ(s)=aAπ(as)Qπ(s,a)V^\pi(s) = \sum_{a \in \mathcal{A}} \pi(a|s) \, Q^\pi(s,a)

Why this works:

  1. Vπ(s)V^\pi(s) is the expected return when following π\pi from state ss
  2. Policy π\pi gives us a probability distribution over actions: π(as)\pi(a|s)
  3. For each action aa, the return is Qπ(s,a)Q^\pi(s,a)
  4. Expected value over actions: weight each QQ by its probability

Intuition: VV is the weighted average of all action-values, where weights are the policy's action probabilities.


Deriving QQ from VV

We can express QQ in terms of VV using the one-step dynamics:

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

where p(s,rs,a)p(s', r | s, a) is the environment's transition probability.

Derivation from first principles:

Start with the definition: Qπ(s,a)=Eπ[GtSt=s,At=a]Q^\pi(s,a) = \mathbb{E}_\pi[G_t | S_t = s, A_t = a]

Expand the return GtG_t: 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]

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

The immediate reward depends only on (s,a,,r)(s,a,',r) from the environment: E[Rt+1St=s,At=a]=s,rp(s,rs,a)r\mathbb{E}[R_{t+1} | S_t = s, A_t = a] = \sum_{s',r} p(s',r|s,a) \cdot r

The future return Eπ[Gt+1St=s,At=a]\mathbb{E}_\pi[G_{t+1} | S_t = s, A_t = a] depends on where we land. Condition on ss': Eπ[Gt+1St=s,At=a]=s,rp(s,rs,a)Eπ[Gt+1St+1=s]\mathbb{E}_\pi[G_{t+1} | S_t = s, A_t = a] = \sum_{s',r} p(s',r|s,a) \cdot \mathbb{E}_\pi[G_{t+1} | S_{t+1} = s']

But Eπ[Gt+1St+1=s]=Vπ(s)\mathbb{E}_\pi[G_{t+1} | S_{t+1} = s'] = V^\pi(s') by definition!

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

Why this step? We're using the law of iterated expectation and the Markov property: the future return from ss' depends only on ss', not on how we got there.


[!formula] Bellman expectation equations

These are recursive consistency conditions that value functions must satisfy.

Bellman 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) \left[ r + \gamma V^\pi(s') \right]

Derivation:

  1. Start with Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s) Q^\pi(s,a) (averaging over actions)
  2. Substitute Qπ(s,a)=s,rp(s,rs,a)[r+γVπ(s)]Q^\pi(s,a) = \sum_{s',r} p(s',r|s,a)[r + \gamma V^\pi(s')]
  3. Result: Vπ(s)V^\pi(s) expressed in terms of Vπ(s)V^\pi(s') for successor states

Intuition: The value of state ss equals the immediate reward plus the discounted value of wherever you land next, averaged over your policy's action choices.

Bellman 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) \left[ r + \gamma \sum_{a'} \pi(a'|s') Q^\pi(s',a') \right]

Derivation:

  1. Start with Qπ(s,a)=s,rp(s,rs,a)[r+γVπ(s)]Q^\pi(s,a) = \sum_{s',r} p(s',r|s,a)[r + \gamma V^\pi(s')]
  2. Substitute Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s') = \sum_{a'} \pi(a'|s') Q^\pi(s',a')
  3. Result: Qπ(s,a)Q^\pi(s,a) in terms of Qπ(s,a)Q^\pi(s',a')

Why recursive? Both equations express value functions in terms of themselves at successor states. This is the bootstrapping principle: we can solve for value functions iteratively.


[!example] Example 1: Gridworld state values

Consider a 3×33 \times 3 gridworld. Agent starts anywhere, goal is top-right corner (0,2)(0,2). Actions: up, down, left, right. Deterministic. Reward: 1-1 per step, +10+10 at goal. γ=0.9\gamma = 0.9. Policy π\pi: move right if possible, else move up.

Let's compute Vπ(s)V^\pi(s) for state s=(1,1)s = (1,1) (center).

Step-by-step:

  1. Identify policy action: From (1,1)(1,1), π\pi says go right with probability 1.

  2. Next state: (1,2)(1,2) (one step from goal)

  3. Immediate reward: r=1r = -1

  4. Bellman equation: Vπ(1,1)=1.0[1+0.9Vπ(1,2)]V^\pi(1,1) = 1.0 \cdot [-1 + 0.9 \cdot V^\pi(1,2)]

  5. Compute Vπ(1,2)V^\pi(1,2): From (1,2)(1,2), policy goes up to (0,2)(0,2) (goal). Vπ(1,2)=1.0[1+0.9Vπ(0,2)]V^\pi(1,2) = 1.0 \cdot [-1 + 0.9 \cdot V^\pi(0,2)]

  6. At goal: Vπ(0,2)=0V^\pi(0,2) = 0 (terminal state, no future rewards)

  7. Back-substitute: Vπ(1,2)=1+0.90=1V^\pi(1,2) = -1 + 0.9 \cdot 0 = -1 Vπ(1,1)=1+0.9(1)=10.9=1.9V^\pi(1,1) = -1 + 0.9 \cdot (-1) = -1 - 0.9 = -1.9

Why this step? Each application of the Bellman equation propagates value backward from the goal. The negative values reflect the cost of reaching the goal (each step incurs 1-1).


[!example] Example 2: Action values in a two-action state

State ss, two actions: a1a_1 (safe), a2a_2 (risky). Stochastic outcomes.

a1a_1 (safe):

  • 100% chance: next state ss', reward r=1r=1

a2a_2 (risky):

  • 70% chance: next state ss'', reward r=5r=5
  • 30% chance: next state ss''', reward r=3r=-3

Assume Vπ(s)=10V^\pi(s') = 10, Vπ(s)=15V^\pi(s'') = 15, Vπ(s)=2V^\pi(s'') = 2. γ=1.0\gamma = 1.0 (undiscounted).

Compute Qπ(s,a1)Q^\pi(s, a_1):

Qπ(s,a1)=.0[1+1.010]=11Q^\pi(s, a_1) = .0 \cdot [1 + 1.0 \cdot 10] = 11

Why? Deterministic transition, so the sum colapses to a single term.

Compute Qπ(s,a2)Q^\pi(s, a_2):

Qπ(s,a2)=0.7[5+1.015]+0.3[3+1.02]Q^\pi(s, a_2) = 0.7 \cdot [5 + 1.0 \cdot 15] + 0.3 \cdot [-3 + 1.0 \cdot 2] =0.720+0.3(1)=140.3=13.7= 0.7 \cdot 20 + 0.3 \cdot (-1) = 14 - 0.3 = 13.7

Why this step? We weight each outcome by its probability. The risky action has higher expected value despite the negative outcome possibility.

Comparison: Qπ(s,a2)=13.7>Qπ(s,a1)=11Q^\pi(s, a_2) = 13.7 > Q^\pi(s, a_1) = 11. If we're trying to improve the policy, we'd prefer a2a_2 in state ss.


[!example] Example 3: Computing VV from QQ with a stochastic policy

State ss, actions{a_1, a_2, a_3}$. Known Q-values:

  • Qπ(s,a1)=5Q^\pi(s, a_1) = 5
  • Qπ(s,a2)=8Q^\pi(s, a_2) = 8
  • Qπ(s,a3)=3Q^\pi(s, a_3) = 3

Policy π\pi:

  • π(a1s)=0.5\pi(a_1|s) = 0.5
  • π(a2s)=0.3\pi(a_2|s) = 0.3
  • π(a3s)=0.2\pi(a_3|s) = 0.2

Compute Vπ(s)V^\pi(s):

Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s) Q^\pi(s,a) =0.55+0.38+0.23= 0.5 \cdot 5 + 0.3 \cdot 8 + 0.2 \cdot 3 =2.5+2.4+0.6=5.5= 2.5 + 2.4 + 0.6 = 5.5

Why this step? The policy is stochastic, so we take a weighted average. a2a_2 has the highest Q-value but only 30% probability, so it contributes 0.3×8=2.40.3 \times 8 = 2.4 to the overall value.

Insight: Even though a2a_2 is best, Vπ(s)=5.5V^\pi(s) = 5.5 is less than Qπ(s,a2)=8Q^\pi(s, a_2) = 8 because the policy sometimes choses suboptimal actions.


[!mistake] Common mistake: Confusing VV and QQ in policy improvement

Wrong reasoning: "I'll compute Vπ(s)V^\pi(s) for all states, then pick the action that leads to the state with highest VV."

Why it feels right: We want to go to valuable states, so picking the action that takes us to the highest-value next state seems logical.

Why it's wrong:

  1. Vπ(s)V^\pi(s) already averages over the policy's action distribution. It doesn't tell you which action is best.
  2. An action might lead to a high-value state but have a low immediate reward, making it worse overall.
  3. The transition might be stochastic—you can't just "choose" the next state.

The fix: Use Qπ(s,a)Q^\pi(s,a) for action selection! Compute: Qπ(s,a)=s,rp(s,rs,a)[r+γVπ(s)]Q^\pi(s,a) = \sum_{s',r} p(s',r|s,a)[r + \gamma V^\pi(s')] for each action, then pick argmaxaQπ(s,a)\arg\max_a Q^\pi(s,a).

Why this works: QQ already accounts for:

  • Immediate reward rr
  • Stochastic transitions via the sum over ss'
  • Future value via Vπ(s)V^\pi(s')

Steel-man the mistake: The confusion arises because in deterministic environments with zero immediate reward, VV and QQ collapse to similar meanings. But in general, they're distinct!


[!mistake] Common mistake: Ignoring the policy in value function notation

Wrong statement: "The value function V(s)=10V(s) = 10."

Why it's wrong: Value functions are policy-dependent! Different policies lead to different values for the same state.

The fix: Always write Vπ(s)V^\pi(s) or specify which policy you mean. For the optimal policy π\pi^*, write V(s)V^*(s).

Example:

  • Random policy πrandom\pi_{\text{random}}: Vπrandom(s)=2V^{\pi_{\text{random}}}(s) = 2
  • Optimal policy π\pi^*: V(s)=10V^*(s) = 10

Same state, different values!

Why this matters: In policy evaluation, we compute VπV^\pi for a fixed π\pi. In policy optimization, we search for the π\pi that maximizes VπV^\pi.


[!recall]- Explain to a 12-year-old

Imagine you're playing a video game. You're at certain level (state). You want to know: "How good is my situation right now?"

V(s)V(s) is like a score for your current level. It tells you, "If you keep playing the way you normally play, here's how many total points you'll probably get from now until the game ends." It's a single number that sums up your future.

Q(s,a)Q(s,a) is like a score for each possible move. It says, "If you press the jump button right now, here's how many points you'll get." Then, "If you press the shoot button, here's how many points you'll get." It helps you compare different moves.

The key difference: VV tells you how good your position is if you keep doing what you usually do. QQ tells you how good each specific choice is, so you can pick the best one!

Why we need both: Sometimes you want to know "Am I in a good spot overall?" (use VV). Other times you want to know "Which button should I press?" (use QQ).


[!mnemonic] Remember V vs Q

V = "Value of a Vacation spot"

  • You're in Hawaii (s)(s). How good is being here overall? That's V(s)V(s).
  • Depends on your plans (policy): if you like beaches, Hawaii is great. If you hate sun, not so much!

Q = "Quality of a Quest"

  • You're in Hawaii and deciding: swim, hike, or surf? Each choice has a quality score: Q(s,swim)Q(s, \text{swim}), Q(s,hike)Q(s, \text{hike}), Q(s,surf)Q(s, \text{surf}).
  • Pick the highest QQ for the best experience.

V is where you are. Q is what you do.


Connections

  • 5.1.03-Returns-and-episodes: Value functions compute the expected return GtG_t
  • 5.1.05-Optimal-policiesand-optimal-value-functions: Optimal VV^* and QQ^* are value functions for the best policy
  • 5.2.01-Dynamic-programming-policy-evaluation: Iterative algorithms solve Bellman equations for VπV^\pi
  • 5.2.02-Policy-iteration: Uses VπV^\pi and QπQ^\pi to improve policies
  • 5.3.01-Monte-Carlo-prediction: Estimates VπV^\pi from sampled episodes
  • 5.4.01-Temporal-difference-learning: Updates VV and QQ using bootstrapping from Bellman equations

#flashcards/ai-ml

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 thereafter: 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 when taking action aa in state ss, 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]
How do you compute Vπ(s)V^\pi(s) from Qπ(s,a)Q^\pi(s,a)?
Vπ(s)=aπ(as)Qπ(s,a)V^\pi(s) = \sum_a \pi(a|s) Q^\pi(s,a) — weighted average over actions according to the policy
How do you compute Qπ(s,a)Q^\pi(s,a) from Vπ(s)V^\pi(s')?
Qπ(s,a)=s,rp(s,rs,a)[r+γVπ(s)]Q^\pi(s,a) = \sum_{s',r} p(s',r|s,a)[r + \gamma V^\pi(s')] — immediate reward plus discounted value of next states
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')] — recursive relation expressing value in terms 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')]
Why are value functions policy-dependent?
Because different policies take different actions, leading to different trajectories and different expected returns from the same state
What does "bootstrapping" mean in the context of Bellman equations?
Using estimates of value functions (like V(s)V(s')) to update estimates of value functions (like V(s)V(s)) — the value estimate depends on other value estimates
If Qπ(s,a1)=10Q^\pi(s,a_1) = 10 and Qπ(s,a2)=5Q^\pi(s,a_2) = 5, which action should you prefer under policy improvement?
Action a1a_1, because it has higher expected return (higher Q-value)
Why can't you just pick the action that leads to the highest Vπ(s)V^\pi(s') for policy improvement?
Because Vπ(s)V^\pi(s') doesn't account for immediate rewards or stochastic transitions — you need Qπ(s,a)Q^\pi(s,a) which includes both

Concept Map

quantified by

includes

includes

expectation defines

expectation defines

conditions

conditions

weighted by pi a given s

one-step dynamics p

gives

enables

compresses

Goal maximize future rewards

Value functions

State-value V pi s

Action-value Q pi s a

Discounted return G_t

Policy pi

Action-level granularity

Compare actions in a state

Infinite future into one number

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, reinforcement learning mein agent ko yeh samajhna hota hai ki koi state ya action kitna "acha" hai. Value functions yeh kaam karte hain — woh future rewards ko ek single number mein summarize kar dete hain.

State-value function Vπ(s)V^\pi(s) bolta hai: "Agar main is state ss mein hoon aur apni current policy π\pi follow karta rahoon, toh total kitna reward milegा?" Yeh state ki overall "goodness" measure karta hai. Action-value function Qπ(s,a)Q^\pi(s,a) thoda aur specific hai — yeh bolta hai: "Agar main state ss mein action aa loon, phir policy follow karoon, toh total kitna reward milegا?" Matlab, har action ke liye alag score.

Dono ke bech ka difference samajhna zaroori hai. VV already policy ke actions ko average kar leta hai, isliye woh bata hai state overall kitna valuable hai. Lekin agar tumhe action choose karna hai, toh QQ chahiye, kyunki woh explicitly har action ko compare karta hai. Bellman equations yeh guarantee dete hain ki value functions recursive tareke se consistent rahein — matlab current state ka value next states ke values se connected hota hai. Yeh bootstrapping principle RL algorithms ka backbone hai, jaise dynamic programming aur TD learning.Agar yeh concepts clear ho jayein, toh baki RL topics jaise policy improvement aur Q-learning automatically samajh ayenge!

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections