Policies, value functions and optimality
How policies map states to actions, how state- and action-value functions measure long-term performance under a policy, and how Bellman equations define and produce optimal policies and values.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Policies, value functions and optimality are the core language of reinforcement learning (RL). An engineer uses them to describe what an agent should do (policy), to evaluate how good a behaviour is (value functions), and to compute or prove which policy is best (optimality/Bellman equations). These concepts appear when designing controllers, simulators, and RL algorithms (policy iteration, value iteration, Q-learning, actor–critic).
Key ideas
Start with basic assumptions.
- Markov decision process (MDP) assumption: the environment is a Markov decision process (MDP) — future depends only on current state and action, not on earlier history. Define an MDP by (S, A, P, R, γ). State and action sets S and A may be finite or infinite.
Define the core terms (bold when first defined):
- Policy (π): a mapping from states to a probability distribution over actions. If π(a|s) = 1 for some a, the policy is deterministic; otherwise stochastic. Stationary policy means π does not depend on time.
- Return (G_t): total discounted reward from time t: G_t = R_{t+1} + γ R_{t+2} + γ^2 R_{t+3} + …. This assumes a discount factor γ with 0 ≤ γ < 1 for infinite-horizon continuing tasks; γ = 1 is allowed only for episodic tasks with bounded returns.
- State-value function (V^π(s)): expected return starting from state s and following policy π: V^π(s) = E_π[G_t | S_t = s]. Units: reward units.
- Action-value function (Q^π(s,a)): expected return starting from state s, taking action a once, then following π: Q^π(s,a) = E_π[G_t | S_t = s, A_t = a].
- Optimal value functions (V, Q): V(s) = max_π V^π(s); Q(s,a) = max_π Q^π(s,a). A policy π* that achieves these maxima for every state is an optimal policy.
Bellman equations (central theorems):
- Bellman expectation equation for V^π (exact equality):
- Statement: For any policy π and for each s ∈ S, V^π(s) = Σ_a π(a|s) Σ_{s',r} p(s',r | s,a) [r + γ V^π(s')].
- Assumptions: MDP, well-defined finite expectation (e.g., γ < 1 or episodic), sums over next-state and reward distribution.
- Derivation (standard exam derivation):
- Start from definition: V^π(s) = E_π[G_t | S_t = s] = E_π[R_{t+1} + γ G_{t+1} | S_t = s].
- Use the law of iterated expectations (tower property) and linearity: = E_π[R_{t+1} + γ V^π(S_{t+1}) | S_t = s].
- Expand expectation over action chosen by π and environment dynamics p(s',r|s,a): yields the stated sum.
- Bellman expectation equation for Q^π:
- Statement: Q^π(s,a) = Σ_{s',r} p(s',r | s,a) [r + γ Σ_{a'} π(a'|s') Q^π(s',a')].
- Derivation: from definition Q^π(s,a) = E[R_{t+1} + γ G_{t+1} | S_t=s, A_t=a] and substitute V^π(S_{t+1}) = Σ_{a'} π(a'|s') Q^π(s',a').
- Bellman optimality equations (nonlinear fixed-point):
- Statement: For each s, V*(s) = max_a Σ_{s',r} p(s',r | s,a) [r + γ V*(s')]. For Q*: Q*(s,a) = Σ_{s',r} p(s',r | s,a) [r + γ max_{a'} Q*(s',a')].
- These are the necessary and sufficient conditions that a value function be optimal.
- Assumptions: same as above; when 0 ≤ γ < 1, uniqueness of V* follows from contraction; the optimality operator T* is also a γ-contraction in sup-norm (see next).
Contraction and uniqueness (short theorem statement):
- The Bellman expectation operator (for fixed π) is a γ-contraction in sup-norm when 0 ≤ γ < 1. Therefore the Bellman equation V = T^π V has a unique fixed point V^π and iterative policy evaluation (repeatedly applying T^π) converges to it.
- Proof idea (exam style): show ||T^π V1 − T^π V2||∞ ≤ γ ||V1 − V2||∞.
Policy improvement theorem (exact statement and proof sketch):
- Statement: Given a policy π and a new policy π' defined greedily with respect to Q^π (π'(s) ∈ argmax_a Q^π(s,a)), then V^{π'}(s) ≥ V^π(s) for all s; if strict inequality at any state then π' is strictly better.
- Proof sketch: For any s, V^{π'}(s) = E_{π'}[R_{t+1} + γ V^{π'}(S_{t+1}) | S_t=s] ≥ E_{π'}[R_{t+1} + γ V^π(S_{t+1}) | S_t=s] = max_a Q^π(s,a) ≥ V^π(s). Use induction or contraction to complete.
What to draw (diagram for exams):
- For a small MDP draw circles for states, label them (s1, s2, ...), draw arrows for actions labelled a1,a2 with transition probabilities p(s'|s,a) on arrows, and write reward r(s,a,s') next to each arrow. Also show a small box with π(a|s) probabilities for each state.
Formulas
G_t = R_{t+1} + γ R_{t+2} + γ^2 R_{t+3} + … — G_t is return (reward units), γ is discount factor (unitless), applies to episodic or infinite-horizon with convergent sum.
V^π(s) = E_π[G_t | S_t = s] — V^π(s) in reward units; expectation under policy π.
Q^π(s,a) = E_π[G_t | S_t = s, A_t = a] — Q^π(s,a) in reward units. Also, V^π(s) = Σ_a π(a|s) Q^π(s,a).
V^π(s) = Σ_a π(a|s) Σ_{s',r} p(s',r|s,a) [ r + γ V^π(s') ] — Bellman expectation for V^π; here p(s',r|s,a) = Pr{S_{t+1}=s', R_{t+1}=r | S_t=s, A_t=a}.
Q^π(s,a) = Σ_{s',r} p(s',r|s,a) [ r + γ Σ_{a'} π(a'|s') Q^π(s',a') ] — Bellman expectation for Q^π.
V*(s) = max_a Σ_{s',r} p(s',r|s,a) [ r + γ V*(s') ] — Bellman optimality for V*.
Q*(s,a) = Σ_{s',r} p(s',r|s,a) [ r + γ max_{a'} Q*(s',a') ] — Bellman optimality for Q*.
π_*(s) ∈ argmax_a Q*(s,a) — optimal policy picks action(s) maximizing Q*; π_*(s) may be non-unique. In finite discounted MDPs (|S|, |A| finite, 0 ≤ γ < 1), there exists at least one optimal deterministic stationary policy.
||T^π V1 − T^π V2||_∞ ≤ γ ||V1 − V2||_∞ — contraction property for 0 ≤ γ < 1; norm is sup-norm (units: reward units).
Worked examples
Example 1 — compute V^π for a simple one-step MDP (numerical, exam-style): MDP: states S = {s1, s2}. s2 is terminal with V(s2)=0. From s1 two actions a1 and a2 both lead deterministically to s2. Rewards: r(s1,a1,s2) = 5, r(s1,a2,s2) = 10. Policy π at s1: π(a1|s1)=0.7, π(a2|s1)=0.3. γ = 0.9. Question: Compute V^π(s1). Solution steps:
- Use Bellman expectation equation for V^π(s1): V^π(s1) = Σ_a π(a|s1) Σ_{s',r} p(s',r|s1,a) [r + γ V^π(s')].
- Transitions are deterministic to s2 and V^π(s2)=0, so inner sum reduces to r. V^π(s1) = π(a1|s1)·5 + π(a2|s1)·10 + γ·0.
- Substitute probabilities and compute: V^π(s1) = 0.7·5 + 0.3·10 = 3.5 + 3.0 = 6.5. Answer: V^π(s1) = 6.5 reward units.
Example 2 — find the optimal policy using Bellman optimality (theory + numeric): MDP: single non-terminal state s1 and terminal s2. Two actions at s1:
- a1: immediate reward 1, next state s1 (stay).
- a2: immediate reward 5, next state s2 (terminal with future zero). Discount γ = 0.9. Compute V*(s1) and the optimal action. Solution steps:
- Use Bellman optimality: V*(s1) = max{Q*(s1,a1), Q*(s1,a2)}.
- Compute Q*(s1,a1). Since a1 returns to s1: Q*(s1,a1) = 1 + γ V*(s1). If we choose a1 in the optimum, we then have V*(s1) = Q*(s1,a1) = 1 + 0.9 V*(s1). Solve: V*(s1) − 0.9 V*(s1) = 1 → 0.1 V*(s1) = 1 → V*(s1) = 10. Then Q*(s1,a1) = 1 + 0.9·10 = 10.
- Compute Q*(s1,a2): taking a2 yields immediate 5 and then terminal: Q*(s1,a2) = 5 + 0 = 5.
- Compare: max is 10 from a1, so V*(s1) = 10 and optimal action is a1. Answer: V*(s1) = 10 reward units and optimal policy π*(s1) = a1 (always).
Example 3 — short model-answer style (proof): Show Bellman expectation equation for V^π. Model answer (exam full-marks): Start from definition V^π(s)=E_π[G_t|S_t=s] = E_π[R_{t+1} + γ G_{t+1} | S_t=s] = E_π[R_{t+1} + γ V^π(S_{t+1}) | S_t=s]. Now expand the expectation over π(a|s) and p(s',r|s,a): V^π(s)=Σ_a π(a|s) Σ_{s',r} p(s',r|s,a) [r + γ V^π(s')]. Thus proven. (State assumptions about finiteness or γ<1 if infinite horizon.)
Common mistakes
- Using Bellman optimality equation as linear when it is nonlinear (max operator) — some students try to solve it by simple linear algebra.
- Not distinguishing expectation over π and exact next-state: mix π(a|s) with p(s'|s,a) incorrectly.
- Forgetting that optimal policy may be stochastic if multiple actions tie for max.
For your exam
Typical question types and what full-marks answers include:
- Definitions: give precise definitions of policy, return, V^π, Q^π, optimal policy. Include units and assumptions (MDP and discounting).
- Short proofs/derivations: derive Bellman expectation equation for V^π and Q^π step-by-step from definitions; show contraction property (sup-norm inequality) with steps.
- Long question: compute V^π numerically for a small MDP (show formula, substitute numbers, show intermediate multiplications, final value with units).
- Conceptual: explain policy improvement theorem and sketch its proof (show inequality chain).
- Diagram question: draw an MDP graph with states, actions, transition probabilities and label rewards and π(a|s). Full-marks answers always state assumptions (finite S/A or γ < 1 for convergence), show each algebra step, and give final numeric answer with units.
Quick check
- Define Q^π(s,a) in one line.
- Write the Bellman optimality equation for V* (one line).
- If from state s a deterministic action yields immediate reward 3 and then stays in s, and γ=0.5, what is value of infinite repetition of that action? (Give numeric answer.) Answers: 1) Q^π(s,a) = E_π[G_t | S_t = s, A_t = a]. 2) V*(s) = max_a Σ_{s',r} p(s',r|s,a) [ r + γ V*(s') ]. 3) Solve V = 3 + 0.5 V → 0.5 V = 3 → V = 6.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?