Search

Search papers and lessons

Value Function

Level 3 · 14 min read

A value function scores each state, or each state-action pair, by the expected total reward an agent gets from there if it keeps following its policy.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Reinforcement learning
  • Stochastic processes
  • Discrete math
  • Sign in to see your progress

TL;DR

A value function answers one question: how good is this situation? For a state, it gives the expected total discounted reward an agent collects from there onward, if it keeps following its current policy. For a state and action pair, it gives the same expectation given that the agent takes that action first. Once you have good values, choosing well is mostly a matter of comparing them.

Values are judgements about the future, not the present. A state can have a small immediate reward and still a high value, because it leads to good states later.

Where you'll see it: as the critic in actor-critic methods, as the baseline that reduces variance in policy-gradient training, and as the table that a Q-learning agent fills in while it plays.

Intuition

A score for every position

Think of a chess position. Material on the board is a partial picture, but a strong player has a feeling for whether the position is winning. That feeling is a value: a number for the position that summarises the games likely to follow. Two positions with identical material can have very different values, because one has a dangerous attack coming and the other does not.

A value function does the same job for an agent. It assigns every state a number that reflects its long-term prospects under a given way of behaving. The number is not a fact about the state alone. It depends on the policy, since a careless player and a careful player face different futures from the same board. That dependence is why the notation always carries the policy.

Why bother storing it

An agent that knows only rewards must look ahead by simulating futures, which is expensive and often impossible. An agent that stores a value for each state can decide with one comparison. Standing in state ss, it asks which neighbouring state or action has the highest value, and goes there. The value function compresses an unbounded future into one number per state, which is why almost every practical RL method learns one.

The math

Setting

As in the Markov decision process lesson, the environment has states S\mathcal{S}, actions A\mathcal{A}, transition probabilities P(s′∣s,a)P(s' \mid s, a), and rewards RR, where R(s,a)R(s, a) denotes the expected immediate reward for taking action aa in state ss. A policy π(a∣s)\pi(a \mid s) gives the probability of each action in each state. The discount factor γ\gamma lies between 0 and 1. The return from time tt is

Gt=∑k=0∞γkRt+k+1.G_t = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}.

Everything below is an expectation of GtG_t under the policy and the environment.

State-value function

The state-value function under policy π\pi is

vπ(s)=Eπ[Gt∣St=s].v_\pi(s) = \mathbb{E}_\pi\left[ G_t \mid S_t = s \right].

Read it as: start in ss, follow π\pi forever, average the discounted rewards over all the random paths. The subscript π\pi marks the policy, and Eπ\mathbb{E}_\pi is the expectation over paths generated by it.

Action-value function

The action-value function, or Q-function, fixes the first action:

qπ(s,a)=Eπ[Gt∣St=s,At=a].q_\pi(s, a) = \mathbb{E}_\pi\left[ G_t \mid S_t = s, A_t = a \right].

The two are linked by averaging over the policy's choice:

vπ(s)=∑aπ(a∣s) qπ(s,a).v_\pi(s) = \sum_{a} \pi(a \mid s)\, q_\pi(s, a).

So qq says what each action is worth, and vv is the average over actions weighted by how often the policy picks them.

The recursive form (Bellman expectation equation)

The return satisfies Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}. Taking expectations gives a relation between the value of a state and the values of its successors:

vπ(s)=∑aπ(a∣s)[R(s,a)+γ∑s′P(s′∣s,a) vπ(s′)].v_\pi(s) = \sum_{a} \pi(a \mid s) \Big[ R(s, a) + \gamma \sum_{s'} P(s' \mid s, a)\, v_\pi(s') \Big].

This is the Bellman expectation equation. It says the value of ss is the average, over actions and next states, of the immediate reward plus the discounted value of where you land. It is a system of linear equations in the unknowns vπ(s)v_\pi(s), one per state. For a finite problem you can solve it directly, or iterate it from any starting guess until the numbers stop changing. The Bellman equation lesson covers the optimal version, where the sum over actions becomes a maximum.

Optimal values

The optimal value function is the best value any policy can achieve:

v∗(s)=max⁡πvπ(s),q∗(s,a)=max⁡πqπ(s,a).v^*(s) = \max_{\pi} v_\pi(s), \qquad q^*(s, a) = \max_{\pi} q_\pi(s, a).

An optimal policy picks, in each state, an action that maximises q∗q^*. Knowing q∗q^* therefore gives a complete plan with no further search.

00.20.40.60.805101520discount factor γvalue
  • value of reward 1 forever
A state that pays 1 every step has value 1/(1 − γ). With γ = 0.5 it is worth 2; with γ = 0.9 it is worth 10. As γ approaches 1, the value grows without bound.

Worked examples

Example
A state that pays forever

An agent sits in a state that pays reward 1 on every step, forever. Use γ=0.9\gamma = 0.9.

  1. Write the value as a sum: v=1+0.9+0.92+0.93+⋯v = 1 + 0.9 + 0.9^2 + 0.9^3 + \cdots.
  2. This is a geometric series with first term 1 and ratio 0.9, so the sum is 11−0.9=10\dfrac{1}{1 - 0.9} = 10.
  3. Check with partial sums: after 3 terms the total is 1+0.9+0.81=2.711 + 0.9 + 0.81 = 2.71; after 20 terms it is about 8.78. It creeps toward 10.
  4. Interpretation: a stream of 1s is worth ten times its immediate payoff, because each future step is discounted but there are infinitely many of them.
Example
Computing values for a random outcome

From state SS the reward is 0, and then with probability 0.5 the agent moves to state GG and with probability 0.5 to state BB. Both GG and BB are terminal, meaning the episode ends there. From GG the reward is 2 and then the episode ends. From BB the reward is 0 and then the episode ends. The policy is fixed, there is one action, and γ=0.9\gamma = 0.9.

  1. Each terminal state pays its reward once and then nothing more, so v(G)=2v(G) = 2 and v(B)=0v(B) = 0.
  2. Apply the Bellman expectation equation at SS: v(S)=0+0.9×(0.5×v(G)+0.5×v(B))v(S) = 0 + 0.9 \times (0.5 \times v(G) + 0.5 \times v(B)).
  3. Substitute: v(S)=0.9×(0.5×2+0.5×0)=0.9×1=0.9v(S) = 0.9 \times (0.5 \times 2 + 0.5 \times 0) = 0.9 \times 1 = 0.9.
  4. The value of SS is 0.9 even though the immediate reward is 0. The state is worth something because it leads to a coin flip with a good outcome half the time.
Example
Choosing between two actions with values

A taxi driver at a station must decide whether to take a fare now or wait. Take a fare: the reward is 5 now, and the driver ends downtown, where the value is v(downtown)=10v(\text{downtown}) = 10. Wait: the reward is 0, and the driver ends at the airport, where the value is v(airport)=12v(\text{airport}) = 12. Use γ=0.9\gamma = 0.9.

  1. Action values: q(take)=5+0.9×10=14q(\text{take}) = 5 + 0.9 \times 10 = 14.
  2. q(wait)=0+0.9×12=10.8q(\text{wait}) = 0 + 0.9 \times 12 = 10.8.
  3. Compare: 14>10.814 \gt 10.8, so taking the fare has the higher action value.
  4. Note that the airport state is more valuable than downtown, yet taking the fare still wins because its reward is immediate and certain. Action values combine both effects, which a state value alone cannot separate.

Tradeoffs and failure modes

Value functions are the backbone of many RL methods, but they come with costs. A table-based value function needs one entry per state, which is infeasible for images or long text. A neural network can generalise across states, but then the values it learns are approximate, and errors can spread from state to state. Values also depend on the policy, so when the policy changes, old estimates can become stale.

SituationWhat goes wrongBetter choice
Few states, known transitionsNothing, a table is exact and fastDynamic programming on the Bellman equation
Many states, unknown transitionsA table cannot be filled inFunction approximation with a neural network
The policy changes oftenOld value estimates describe a policy you no longer followOn-policy methods that re-estimate, or off-policy methods that learn the optimal values directly
You need the best action onlyLearning a full vv wastes effortLearn qq instead, so action choice needs no model of transitions
The estimates are noisyPolicy-gradient updates have high varianceSubtract a baseline such as the state value (see the [[advantage-function

A subtle trap is confusing vv with qq. The state value cannot tell you which action to take without knowing the transitions. If the agent has no model of the environment, it needs qq.

History

The idea comes from Richard Bellman's work on dynamic programming in the 1950s, which framed sequential decisions in terms of a function over states that satisfies a recursive equation. Bellman's book on the subject appeared in 1957. Ronald Howard's policy iteration in 1960 gave a practical method for finding optimal policies by alternately evaluating a policy's value and improving the policy.

In reinforcement learning, Richard Sutton's work in the late 1980s on temporal-difference learning showed how to estimate values from experience without a model of the environment. Chris Watkins's 1989 thesis introduced Q-learning, which learns action values directly. Sutton and Barto's textbook, first published in 1998, made these ideas the standard vocabulary. Later, the rise of deep networks made it possible to approximate value functions over images and other large inputs, which is what made the approach practical for games and robotics.

Check yourself

QuestionRecall: what is the difference between a state-value function and an action-value function?Show answer

The state-value function vπ(s)v_\pi(s) gives the expected return from state ss when following policy π\pi. The action-value function qπ(s,a)q_\pi(s, a) gives the expected return when the agent takes action aa in ss first and then follows π\pi. The state value is the policy-weighted average of the action values, vπ(s)=∑aπ(a∣s)qπ(s,a)v_\pi(s) = \sum_a \pi(a \mid s) q_\pi(s, a).

QuestionComputation: a state pays reward 2 on every step forever, with γ=0.8\gamma = 0.8. What is its value?Show answer

The value is 21−0.8=20.2=10\dfrac{2}{1 - 0.8} = \dfrac{2}{0.2} = 10. Each reward is 2, so the constant part contributes 2×11−γ2 \times \dfrac{1}{1 - \gamma}, the sum of the geometric series 1+γ+γ2+⋯1 + \gamma + \gamma^2 + \cdots times the reward.

QuestionWhy: why does a value function let an agent choose actions without simulating the whole future?Show answer

Because the value already summarises the expected return of every future from that state. The agent only needs to compare the values of the options available to it, which costs one lookup or one comparison, rather than rolling out every possible sequence of moves. The price is that the value must have been learned from experience or computed from a model beforehand.

QuestionApplied: a recommendation system stores a value for each user state and a policy that changes every day. Why might yesterday's values mislead it, and what could help?Show answer

The values were computed for the policy that was in use when they were estimated. If the policy changes, the expected returns change too, since the future actions are different. Yesterday's values therefore describe a policy the system no longer follows. Helpful responses include re-estimating values with recent data, weighting recent experience more heavily, or using off-policy learning, which estimates values for the target policy from data collected under another policy. Each has trade-offs: re-estimating costs compute, and off-policy estimates can have high variance when the two policies differ a lot.

Sources