The most fundamental question in reinforcement learning: How good is it to be in this state?
The state-value function answers this question precisely. Imagine you’re playing a board game and look at the current position: “Am I winning or losing?” That intuitive assessment is what formalizes—it compresses all future possibilities into one number.
The state-value function is the expected return—the discounted sum of all future rewards—when starting in state and following policy thereafter.
where is the discounted return from time .
Think of value as a “how close to treasure” measure. In a navigation problem:
- States near the goal have high values because rewards are within reach
- States far from the goal have lower values because rewards are distant (and discounted)
- States in dead ends or dangerous areas have very low values
The values form a gradient that essentially “points toward” the rewards. If you could see the value of every state, you’d see a landscape with peaks at rewarding states.
Values Depend on Policy
A crucial insight: value is always relative to a policy. The same state can have very different values under different policies.
Consider a simple 3-state problem:
[Start] --right--> [Middle] --right--> [Goal: +10]
| |
v v
wall [Pit: -10]Rewards arrive on transitions: +10 for entering Goal, for entering Pit, 0 otherwise. Goal and Pit are terminal, so their values are 0. Discount: .
Under a smart policy (always go right from Start, always go right from Middle):
- (the +10 arrives one step later, discounted once)
- (the +10 arrives on the very next transition)
- (terminal: no future rewards)
Under a terrible policy (always go down from Middle):
- (leads to the pit one step later)
- (goes straight to the pit)
- (terminal, unchanged)
Same states, drastically different values. The policy determines the value.
The Full Definition
The value is an expected return because two sources of randomness stand between you and the future: the policy may choose actions stochastically, and the environment may respond stochastically. Even from the same state, following the same policy, you can experience different trajectories—the value averages over all of them.
Expanding the definition with the return formula:
The expectation is taken over:
- The stochastic policy : Which actions we might take
- The stochastic environment : Where those actions might lead
- All future time steps: The infinite sum of discounted rewards
This makes a well-defined function for any policy in any MDP with bounded rewards and .
GridWorld Example
Let’s compute values for a concrete example. Consider this 4x4 GridWorld:
_____ _____ _____ _____
| | | | |
| S | . | . | . |
|_____|_____|_____|_____|
| | | | |
| . | X | . | . |
|_____|_____|_____|_____|
| | | | |
| . | . | . | . |
|_____|_____|_____|_____|
| | | | |
| . | . | . | G |
|_____|_____|_____|_____|
S = Start, G = Goal (+10), X = Wall, . = Empty (-1 per step)Reward setup (used for both tables below): every move costs , including moves that bump into a wall or the grid edge (the agent stays in place but still pays). Entering G additionally pays +10 on that transition, so the final step nets . G is terminal, so . Discount: .
Under a random policy (equal probability for each direction):
| Col 0 | Col 1 | Col 2 | Col 3 | |
|---|---|---|---|---|
| Row 0 | -8.9 | -8.6 | -7.8 | -7.2 |
| Row 1 | -8.6 | Wall | -6.4 | -5.5 |
| Row 2 | -7.8 | -6.4 | -4.3 | -0.9 |
| Row 3 | -7.2 | -5.5 | -0.9 | 0.0 |
Values are negative everywhere: the random policy wanders so long that the accumulated step penalties outweigh the (heavily discounted) +10 bonus, even one step from the goal.
Under an optimal policy (always move toward goal):
| Col 0 | Col 1 | Col 2 | Col 3 | |
|---|---|---|---|---|
| Row 0 | 1.22 | 2.47 | 3.85 | 5.39 |
| Row 1 | 2.47 | Wall | 5.39 | 7.10 |
| Row 2 | 3.85 | 5.39 | 7.10 | 9.00 |
| Row 3 | 5.39 | 7.10 | 9.00 | 0.00 |
Now values increase as we approach the goal: 9.00 one step away (the netted arrives immediately), then two steps away, and so on. The value gradient points directly toward the reward. The goal cell itself shows 0.00 because it is terminal; the +10 lives on the transitions into it.
How Discount Factor Affects Values
The discount factor dramatically changes the value landscape:
Think of as “how far-sighted” the agent is:
- : Completely myopic. Only immediate rewards matter.
- : Moderate foresight. Rewards 10 steps away worth about 35% of immediate.
- : Far-sighted. Rewards 100 steps away still worth about 37%.
- : No discounting; all future rewards count fully. Only safe for episodic tasks that are guaranteed to terminate.
Consider a state that’s 5 steps from a +100 reward (with no intermediate rewards). The value of that state is the +100 discounted five times—and the choice of changes it enormously:
Each number is . Higher means distant states still have substantial value. Lower means only nearby rewards matter.
Computing Values by Hand
For simple MDPs, we can compute values directly from the definition.
Consider a 3-state chain with deterministic policy that always goes right:
[A] --right--> [B] --right--> [C: terminal]
Rewards on transitions: A->B gives -1, B->C gives +10
Discount: γ = 0.9Work backward from the terminal state. C is terminal, so : there are no rewards after reaching C, and the +10 is earned on the transition into it. B is one step from C, so it collects the +10 immediately: . A pays the step cost, then gets B’s value discounted once: .
State C (terminal):
State B:
State A:
The values form a gradient: , increasing as we approach the goal.
Values as Predictions
Value functions are predictions. They predict the expected cumulative reward. Good predictions enable good decisions: if you know the value of every state you could end up in, you can evaluate any policy.
This predictive nature is why value functions are central to RL:
- Evaluation: Given a policy, compute its value function to see how good it is
- Improvement: Use values to find better policies
- Learning: Estimate values from experience when the MDP is unknown
The predictive interpretation becomes clear when we think about Monte Carlo estimation. If we run many episodes following policy and track the returns from state :
where is the return from the -th visit to state . As , this converges to the true value.
Implementation
Here’s how to represent and estimate value functions in Python:
import numpy as np
from collections import defaultdict
# Value function as a dictionary
V = defaultdict(float)
# Simple Monte Carlo value estimation
def estimate_values_mc(episodes, gamma=0.99):
"""
Estimate V(s) from a list of episodes using Monte Carlo.
Each episode is a list of (state, reward) tuples, where reward is
the one received on the transition out of that state.
"""
returns = defaultdict(list)
for episode in episodes:
# Calculate returns for each state visited
G = 0
# Work backward through the episode
for state, reward in reversed(episode):
G = reward + gamma * G
returns[state].append(G)
# Average the returns for each state
V = {}
for state, state_returns in returns.items():
V[state] = np.mean(state_returns)
return V
# Example usage (same chain MDP as above: A->B costs -1, B->C pays +10)
episodes = [
# Episode 1: A -> B -> C (goal)
[('A', -1), ('B', 10)],
# Episode 2: A -> B -> C (goal)
[('A', -1), ('B', 10)],
# Episode 3: A -> A -> B -> C (got stuck briefly)
[('A', -1), ('A', -1), ('B', 10)],
]
V = estimate_values_mc(episodes, gamma=0.9)
print("Estimated values:")
for state in sorted(V.keys()):
print(f" V({state}) = {V[state]:.2f}")Output:
Estimated values:
V(A) = 7.55
V(B) = 10.00Note that the estimate for state A (7.55) is below the true value of 8.0 we computed by hand, because episode 3 started poorly (stuck at A). The terminal state C never accrues a return; its value is 0 by convention.
Visualizing Value Functions
Values are often visualized as heatmaps, showing the “terrain” of the value landscape:
import numpy as np
import matplotlib.pyplot as plt
def visualize_values(V, grid_shape, title="Value Function"):
"""
Visualize a value function as a heatmap.
V: dict mapping (row, col) -> value
grid_shape: (rows, cols) tuple
"""
rows, cols = grid_shape
value_grid = np.zeros((rows, cols))
for (r, c), value in V.items():
value_grid[r, c] = value
plt.figure(figsize=(8, 6))
plt.imshow(value_grid, cmap='RdYlGn', interpolation='nearest')
plt.colorbar(label='Value')
# Add value labels to each cell
for r in range(rows):
for c in range(cols):
plt.text(c, r, f'{value_grid[r, c]:.1f}',
ha='center', va='center', fontsize=12)
plt.title(title)
plt.xlabel('Column')
plt.ylabel('Row')
plt.show()
# Example: 4x4 GridWorld values under optimal policy
# (-1 per step, +10 on entering the goal, gamma = 0.9; see table above)
V_optimal = {
(0, 0): 1.22, (0, 1): 2.47, (0, 2): 3.85, (0, 3): 5.39,
(1, 0): 2.47, (1, 1): 0.0, (1, 2): 5.39, (1, 3): 7.10, # (1,1) is wall
(2, 0): 3.85, (2, 1): 5.39, (2, 2): 7.10, (2, 3): 9.00,
(3, 0): 5.39, (3, 1): 7.10, (3, 2): 9.00, (3, 3): 0.0, # goal (terminal)
}
visualize_values(V_optimal, (4, 4), "GridWorld Values (Optimal Policy)")The resulting heatmap shows values increasing toward the goal in the bottom-right corner. The wall and the terminal goal cell both display zero.
Key Properties of Value Functions
Three facts make value functions mathematically well-behaved: they’re always finite (bounded rewards plus discounting keep the infinite sum in check), each policy has exactly one value function, and value functions let us rank policies against each other.
Property 1: Boundedness
For bounded rewards and :
This ensures values are always finite and well-defined.
Property 2: Uniqueness
For a given policy and MDP, there is exactly one value function . Different policies have different value functions, but each policy determines a unique one.
Property 3: Policy Ordering
We can compare policies via their value functions. Policy is better than if:
Summary
- State-value function measures expected return from state under policy
- Values depend on the policy: same state, different policies, different values
- Values form a gradient pointing toward rewards
- The discount factor controls how much future rewards matter
- Values can be estimated from experience using Monte Carlo methods
But state values alone don’t tell us what to do. To make decisions, we need to compare actions. That’s where action-value functions come in.