Q-Learning
Value-Based Methods, Bellman Equation, and Temporal-Difference Learning
In the first chapter we formalized the reinforcement learning problem as a Markov Decision Process and introduced the notions of policy, value function, and Bellman equation. The central question left open was: how does an agent actually learn a good policy from experience? This chapter answers that question for the simple tabular setting. We develop value-based methods — algorithms that learn a value function and derive a policy from it — culminating in the Q-Learning algorithm Watkins & Dayan, 1992, the most important stepping stone toward the deep RL methods we will study in subsequent chapters.
We begin with the two types of value functions (state-value and action-value), revisit the Bellman equation in more depth, and compare two model-free strategies for estimating values: Monte Carlo and Temporal Difference learning. We then present Q-Learning itself — an off-policy, TD-based algorithm that converges to the optimal action-value function — and walk through a worked example.
Slides for this chapter (open full screen).
Value-Based Methods¶
Recall from the previous chapter that there are two broad families of approaches to finding a good policy:
Policy-based methods learn the policy directly, by parameterizing it and optimizing with gradient ascent.
Value-based methods learn a value function that estimates how good each state or state-action pair is, and then derive a policy from it (e.g., by acting greedily).
In this chapter we focus on value-based methods. The core idea is simple: if we can accurately estimate the value of every action in every state, the optimal policy is just the one that always picks the best action. The challenge, of course, is learning those values from experience.
The State-Value Function¶
The state-value function measures the expected cumulative discounted reward an agent receives when starting from state and following policy thereafter Sutton & Barto, 2018:
Here denotes the return — the total discounted reward collected from time step onward, , where is the reward received at step and is the discount factor that down-weights future rewards.
Intuitively, answers the question: “How good is it to be in state if I follow policy from here on?”
The Action-Value Function¶
The action-value function is closely related but conditions on both the current state and the action taken Sutton & Barto, 2018:
This answers: “How good is it to take action in state , and then follow policy ?”
Figure 2:State-value assigns a single number to each state, while action-value assigns a number to each state-action pair.
Why Q Is More Useful for Control¶
With alone, the agent cannot decide which action to take without a model of the environment — it would need to evaluate for every possible successor state reached by each action, which requires knowledge of the transition function .
With , the agent simply selects:
No model needed. This makes the action-value function the natural foundation for model-free control, which is the dominant paradigm in deep RL. If we can learn the optimal action-value function , we immediately obtain the optimal policy.
The Bellman Equation¶
The Bellman equation is the mathematical backbone of nearly every RL algorithm. It expresses a recursive relationship: the value of a state equals the immediate reward plus the discounted value of the successor state Sutton & Barto, 2018.
Bellman Equation for ¶
Expanding the expectation:
Figure 3:Bellman backup diagram. The value of state depends on the immediate rewards and the values of successor states , weighted by the policy and transition probabilities .
Bellman Equation for ¶
Similarly, the action-value function satisfies:
Bellman Optimality Equations¶
The optimal value functions, denoted and , satisfy:
The key difference from the policy-dependent versions is that the operator replaces the policy-weighted average. If we could solve these equations directly, we would obtain the optimal policy . In practice, we cannot solve them analytically for large problems — but we can estimate from experience using algorithms like Q-Learning.
Monte Carlo vs Temporal Difference Learning¶
Both Monte Carlo (MC) and Temporal Difference (TD) methods estimate value functions from experience, without requiring a model. They differ fundamentally in when and how they update their estimates.
Monte Carlo Methods¶
Monte Carlo methods wait until the end of an episode to compute the actual return — the sum of discounted rewards from time step to the terminal state Sutton & Barto, 2018, Ch. 5:
The value estimate is then updated toward this observed return:
where is the learning rate. Since is a sample of the true expected return, MC estimates are unbiased. However, because each trajectory can vary widely, the estimates have high variance. MC also requires complete episodes, limiting it to episodic tasks.
Temporal Difference Learning¶
Temporal Difference learning, introduced by Sutton Sutton, 1988, updates the value estimate at every time step using a bootstrapped target:
The quantity is called the TD error. Instead of waiting for the full return, TD uses the immediate reward plus the current estimate of the next state’s value. This bootstrapping introduces bias (because is itself an estimate), but it reduces variance and allows learning to happen online, step by step, even in continuing (non-episodic) tasks.
Figure 4:Monte Carlo computes the actual return over the full trajectory. Temporal Difference bootstraps from a single transition.
Comparison¶
| Monte Carlo | Temporal Difference | |
|---|---|---|
| Updates | After full episode | After every step |
| Target | Actual return | Estimated |
| Bias | Unbiased | Biased (bootstrap) |
| Variance | High | Lower |
| Data efficiency | Lower | Higher |
| Episode required | Yes | No |
| Bootstrapping | No | Yes |
As Sutton and Barto put it, TD learning “combines the sampling of Monte Carlo with the bootstrapping of dynamic programming” Sutton & Barto, 2018. Q-Learning, which we turn to next, is a TD method.
Q-Learning¶
Q-Learning is an off-policy, value-based algorithm that uses temporal difference updates to learn the optimal action-value function directly Watkins & Dayan, 1992. It was introduced by Watkins in his 1989 PhD thesis and is one of the most fundamental algorithms in reinforcement learning.
The Q-Table¶
In the tabular setting, Q-Learning maintains a table with one entry for every state-action pair. Each entry stores the agent’s current estimate of how good it is to take action in state . The table is initialized to zeros (or small random values) and updated through interaction with the environment.
Figure 5:A Q-table with states as rows and actions as columns. The highlighted cells indicate the greedy action (highest Q-value) for each state. The policy simply picks the action with the maximum Q-value.
Epsilon-Greedy Exploration¶
To balance exploration and exploitation, Q-Learning uses an -greedy strategy for action selection:
With probability : exploit — select
With probability : explore — select a random action
The exploration rate typically starts at 1.0 (fully random) and is gradually decayed over training, so that the agent explores broadly at first and increasingly exploits its learned Q-values as they become more accurate. Common decay strategies include linear decay and exponential decay.
The Q-Learning Update Rule¶
After taking action in state , observing reward and next state , the Q-value is updated:
Breaking this down:
is the learning rate, controlling how much the estimate shifts toward the new information.
is the TD target — the immediate reward plus the discounted value of the best action in the next state.
is the TD error — the difference between the target and the current estimate.
The critical feature is the operator: the update always uses the value of the best action at the next state, regardless of which action the -greedy policy actually selected. This is what makes Q-Learning off-policy.
Figure 6:The Q-Learning algorithm loop. The agent observes a state, selects an action via -greedy, executes it, observes the reward and next state, and updates its Q-table.
The Complete Algorithm¶
The full Q-Learning algorithm is:
Initialize for all ,
For each episode:
Initialize state
For each step of the episode:
Choose action from using -greedy on
Take action , observe reward and next state
Update:
Under standard conditions — all state-action pairs are visited infinitely often and the learning rate satisfies the Robbins-Monro conditions (, ) — Q-Learning converges to Watkins & Dayan, 1992.
Off-Policy vs On-Policy: Q-Learning vs SARSA¶
Q-Learning is off-policy: the policy used for selecting actions during training (the behavior policy, -greedy) differs from the policy being optimized (the target policy, greedy). The update uses — the value of the best possible action, not the action actually taken.
SARSA (State-Action-Reward-State-Action) is the on-policy counterpart Rummery & Niranjan, 1994. Its update rule is:
The difference is subtle but significant: SARSA uses — the Q-value of the action that was actually taken — rather than the maximum. This means SARSA’s updates reflect the exploration noise from the -greedy policy. In practice, SARSA tends to learn a more conservative policy that accounts for the agent’s own exploratory behavior, while Q-Learning learns the optimal policy assuming perfect exploitation.
A classic illustration is the cliff-walking environment from Sutton and Barto Sutton & Barto, 2018, Example 6.6: SARSA learns a safe path far from the cliff (because it accounts for occasional random steps toward the edge), while Q-Learning learns the optimal shortest path right along the cliff edge.
The off-policy nature of Q-Learning is also what makes it a natural foundation for experience replay and Deep Q-Networks (DQN) Mnih et al., 2015, which we will cover in the next chapter.
Summary¶
This chapter introduced the foundations of value-based reinforcement learning:
Value functions ( and ) measure expected cumulative reward. The action-value function is particularly useful because it enables model-free control.
The Bellman equation provides a recursive decomposition of values that underlies nearly all RL algorithms.
Monte Carlo methods estimate values from complete episodes (unbiased, high variance), while Temporal Difference methods bootstrap from single steps (biased, lower variance, more data-efficient).
Q-Learning combines off-policy control with TD updates to converge to the optimal . Its on-policy counterpart is SARSA.
In the next chapter, we move from tabular Q-Learning to Deep Q-Networks (DQN) — using neural networks to approximate in high-dimensional state spaces, which opened the door to learning directly from raw pixels in Atari games Mnih et al., 2015.
- Watkins, C. J. C. H., & Dayan, P. (1992). Q-Learning. Machine Learning, 8(3–4), 279–292. 10.1007/BF00992698
- Sutton, R. S., & Barto, A. G. (2018). Reinforcement Learning: An Introduction (2nd ed.). MIT Press. http://incompleteideas.net/book/the-book-2nd.html
- Sutton, R. S. (1988). Learning to Predict by the Methods of Temporal Differences. Machine Learning, 3(1), 9–44. 10.1007/BF00115009
- Rummery, G. A., & Niranjan, M. (1994). On-Line Q-Learning Using Connectionist Systems (Techreport CUED/F-INFENG/TR 166). Cambridge University Engineering Department.
- Mnih, V., Kavukcuoglu, K., Silver, D., Rusu, A. A., Veness, J., Bellemare, M. G., Graves, A., Riedmiller, M., Fidjeland, A. K., Ostrovski, G., Petersen, S., Beattie, C., Sadik, A., Antonoglou, I., King, H., Kumaran, D., Wierstra, D., Legg, S., & Hassabis, D. (2015). Human-Level Control through Deep Reinforcement Learning. Nature, 518(7540), 529–533. 10.1038/nature14236