skip to content

Why can acting greedily with respect to a policy's value function never make it worse?

level: seniorimportance: should knowfreq 34%

answer

  1. compare q_pi(s,a) against v_pi(s)
  2. swap one action, keep the rest
  3. the swap is available again next step
  4. unroll until the tail vanishes
  5. no change means already optimal

basics

~20 s

In every state the greedy action is worth at least as much as the current policy's action, and that advantage applies again at the next step. Unrolling it shows the greedy policy's return dominates everywhere — the policy improvement theorem.

solid answer

~50 s

The policy improvement theorem says: if a new policy `pi'` satisfies `q_pi(s, pi'(s)) >= v_pi(s)` in every state, then `v_pi'(s) >= v_pi(s)` in every state. The greedy policy meets that premise by construction, since `max_a q_pi(s,a)` is at least `q_pi(s, pi(s))`, which is `v_pi(s)`. The proof is an unrolling argument: taking the greedy action once and then following `pi` is no worse than following `pi` throughout; apply the same substitution at the second step, the third, and so on, and in the limit you are following `pi'` forever with a value still no smaller. The important corollary is the stopping condition — if greedy selection changes nothing, then `v_pi` already equals the maximising backup of itself, which is the optimality condition, so `pi` is optimal. This is what makes policy iteration monotone rather than a hill-climb that can slide backwards.

go deeper

for a junior

Recall that making a policy greedy with respect to its own values can only help, and that this is why the evaluate-improve loop always moves forward.

for a middle

State the premise and conclusion precisely in terms of q_pi and v_pi, and explain that the maximum over actions is at least the current action's value, so the premise is automatic.

for a senior

Reproduce the unrolling argument and name the caveat: the guarantee assumes accurate evaluation, and with approximation you get a bounded loss rather than monotone improvement.

for a principal

Own the consequence for design: this theorem is why interleaving partial evaluation with partial improvement is safe, and why approximation error in evaluation is the thing to budget for when the discount factor is near one.

## The statement Let `v_pi(s)` be the expected discounted return from state `s` when following policy `pi` forever, and let `q_pi(s,a)` be the expected return from taking action `a` once in `s` and following `pi` thereafter. Note the definitional link: `v_pi(s) = q_pi(s, pi(s))` for a deterministic policy. **Policy improvement theorem.** If `pi'` is any policy such that ``` q_pi(s, pi'(s)) >= v_pi(s) for every state s ``` then ``` v_pi'(s) >= v_pi(s) for every state s ``` and if the first inequality is strict anywhere, the second is strict in at least one state. ## Why the greedy policy satisfies the premise Define `pi'(s) = argmax_a q_pi(s,a)`. Then ``` q_pi(s, pi'(s)) = max_a q_pi(s,a) >= q_pi(s, pi(s)) = v_pi(s) ``` The maximum over a set is at least any member of the set, and the current policy's action is a member. So the premise holds automatically — no assumption, no condition on the MDP beyond the values being the true values of `pi`. ## The unrolling argument This is the part interviewers want to hear, and it is genuinely simple. Start with `v_pi(s) <= q_pi(s, pi'(s))`. Read the right-hand side in words: *take `pi'`'s action now, then follow `pi` forever*. Expand it one step: ``` v_pi(s) <= E[ r + gamma * v_pi(s') ] with the first action from pi' ``` Now apply the same inequality to `v_pi(s')` inside the expectation, replacing it with *take `pi'`'s action there, then follow `pi`*: ``` v_pi(s) <= E[ r + gamma * ( r' + gamma * v_pi(s'') ) ] first two actions from pi' ``` Each substitution pushes the switch-back point one step further into the future while keeping the inequality pointing the same way. Repeat. Because rewards are bounded and `gamma < 1`, the tail term `gamma^n * v_pi(...)` vanishes, and what remains is the expected discounted return of following `pi'` from the start: ``` v_pi(s) <= v_pi'(s) ``` The intuition to say out loud: a single-step improvement that is available *from every state* is available again at the next step, so you never have to switch back. ## The corollary that terminates policy iteration Suppose greedy improvement returns the same policy: `pi' = pi`. Then for every state ``` v_pi(s) = max_a sum_s' P(s'|s,a) [ r + gamma * v_pi(s') ] ``` That is exactly the condition satisfied by the optimal value function, and it has a unique solution, so `v_pi = v*` and `pi` is optimal. This is why "the improvement pass changed nothing" is a *proof* of optimality and not merely a heuristic stopping rule. ## Practical consequences and caveats **Partial improvement is fine.** The premise is stated per state, so you may change the action in one state and leave the rest alone; the untouched states satisfy the premise with equality. This licenses asynchronous and prioritised improvement, updating whichever states look most promising. **The theorem needs the values to be the values of `pi`.** The whole argument leans on `v_pi(s) = q_pi(s, pi(s))`. If your evaluation was truncated or approximated, that identity holds only approximately, and monotone improvement is no longer guaranteed — with function approximation, policies can oscillate between candidates rather than settle. What survives is a bound, not monotonicity: if your value estimates are within `epsilon` in max-norm of the optimal values, the policy greedy with respect to them loses at most about `2 * gamma * epsilon / (1 - gamma)` relative to optimal. Note how that degrades as the discount factor approaches one. **Ties are harmless.** If several actions tie for the maximum, any tie-breaking rule preserves the theorem; picking a consistent rule just prevents the algorithm cycling between equally good policies and failing to detect its own stopping condition. **Stochastic improvements count too.** Nothing requires `pi'` to be deterministic. Any policy that raises the probability of higher-`q` actions satisfies the premise, which is the doorway to soft, gradual improvement schemes rather than hard greedy switches. ## The common misunderstanding Candidates often assume greedy improvement is a local hill-climb that could get stuck or move backwards, by analogy with greedy search in optimisation. It is not: because the improvement is evaluated against the *long-run* value of the current policy rather than the immediate reward, one greedy step already accounts for the entire discounted future. The greediness is only in the choice of the first action; everything after it is priced in by `v_pi`.

  • What if you only change the action in a single state?
    The theorem still applies. Its premise is a per-state inequality, and every state you left untouched satisfies it with equality. So a single-state greedy change cannot lower any state's value. This is what justifies asynchronous or prioritised improvement, where you update the states that look most promising instead of sweeping all of them.
  • Does the guarantee survive approximate policy evaluation?
    Not as strict monotonicity. The argument needs the values to be the true values of the current policy; with truncated or approximated evaluation, improvement steps can be non-monotone and policies can oscillate. What remains is a performance bound: values within `epsilon` in max-norm of the optimal ones yield a greedy policy losing at most about `2 * gamma * epsilon / (1 - gamma)`.
  • Why does the theorem imply policy iteration's stopping rule proves optimality?
    If greedy improvement returns the same policy, then `v_pi` equals the maximising backup applied to itself. That fixed-point condition has a unique solution, the optimal value function, so `v_pi = v*` and the policy is optimal. The unchanged policy is therefore a proof of optimality, not merely a sign that progress has stalled.

If on every road of your commute there is a turn that gets you home no later than your usual turn, then taking those turns every day cannot make the commute longer — you can keep taking them, one junction after another.

saying these in an interview costs you the question

  • Thinks greedy improvement can get stuck in a worse policy
  • Believes the theorem requires the value function to already be optimal
  • Treats greedy as maximising immediate reward only
  • Assumes monotone improvement still holds with approximate values
  • Confuses improving the policy with improving the value estimates

context