Reinforcement Learning Introduction
Reinforcement Learning: Model-Free, Gradient-Free Methods
Introduction and Core Concepts
This section reviews foundational ideas from dynamic programming that set the stage for model-free methods. The Markov decision process (MDP) provides the mathematical framework: an agent moves through states s, takes actions a, receives rewards r, and transitions probabilistically to a next state s′ according to p(s′∣s,a).
The value function V(s) tells the expected cumulative future discounted reward from state s, assuming optimal actions thereafter. The quality function Q(s,a) is richer: it gives the value of being in state s and taking a specific action a, again assuming optimal behavior afterward. They are related by:
The optimal policy π∗(s) is simply the action that maximizes Q(s,a):
The Bellman optimality principle expresses these functions recursively:
While dynamic programming (value iteration, policy iteration) can solve for V and Q when the model (transition probabilities p and reward function r) is known, many real-world problems lack such a model. This motivates model-free reinforcement learning — learning purely through trial and error.
Key idea: Without a model of the environment, an agent must experience actual transitions and rewards to estimate value and quality functions.
Monte Carlo Learning
Monte Carlo learning is the simplest model-free approach. It is episodic: learning occurs only after completing an entire trajectory (e.g., a game of chess, a race to a finish line).
For each episode, the agent follows some policy (often the current best estimate) and accumulates the discounted cumulative reward Rk=∑t=kTγt−krt for each time step k. The value function update is:
where α is a learning rate. In the basic formulation, the total episode reward R is divided equally among all visited states (or each state gets its own Rk). The same idea applies to Q updates:
Characteristics of Monte Carlo learning:
No bias — if enough episodes are run, it converges to the true values.
High variance — because entire episode returns vary widely.
Inefficient — must wait until the episode ends to learn; all steps are weighted equally regardless of temporal proximity to reward.
Definition: Monte Carlo learning averages complete episode returns to estimate value functions. It is unbiased but requires many episodes and does not exploit temporal structure.
Temporal Difference (TD) Learning
Temporal difference (TD) learning is a breakthrough that combines ideas from dynamic programming and Monte Carlo methods. Instead of waiting for the end of an episode, TD updates are made after every time step using the Bellman equation as a target.
TD(0) — One-Step TD
The value function update rule for TD(0) is:
Here:
TD target: rk+γV(sk+1) — the observed reward plus the estimated value of the next state.
TD error: δk=rk+γV(sk+1)−Vold(sk) — the difference between the target and the current estimate.
The update nudges V(sk) toward the observed one-step return, which is a sample of the Bellman expectation. This makes TD(0) a bootstrapping method (it uses an estimate to update another estimate).
Biological connection: The TD error signal resembles dopamine release in the brain. When the actual reward differs from the expected reward, dopamine strengthens connections between neurons that fired just before the reward, reinforcing behaviors that lead to reward.
N-Step TD
One can expand the Bellman recursion to use n steps of actual rewards:
As n→∞ (to the end of the episode), this converges toward Monte Carlo learning. Small n gives faster updates but more bias; larger n reduces bias at the cost of higher variance.
TD(λ)
TD(λ) elegantly combines all n-step returns using an exponential weighting with decay parameter λ∈[0,1]:
where Rkλ is the λ-return, a weighted average of all n-step returns. This allows tuning the bias-variance trade-off continuously.
Key insight: Temporal difference learning exploits the temporal structure of rewards — events closer in time to a reward are weighted more heavily, mirroring biological learning mechanisms.
Q-Learning: Off-Policy TD on the Quality Function
Q-learning applies the temporal difference idea directly to the quality function Q(s,a). The update rule (the most common form, TD(0) for Q) is:
Key difference from earlier TD on V: The TD target uses maxa′Q(sk+1,a′) — it assumes the agent will act optimally in the future, regardless of which action was actually taken at time k to get reward rk.
This max operator makes Q-learning off-policy: the agent can learn about the optimal policy even while taking suboptimal or exploratory actions. The action ak used to get rk does not have to be the greedy action with respect to Q; it can be random, exploratory, or even taken from another agent's behavior.
Why off-policy matters:
Experience replay: Past experiences (even suboptimal ones) can be reused to improve Q, as long as the target uses maxa′Q.
Imitation learning: Watching an expert (or even a non-expert) and updating Q based on their actions, provided the future is evaluated optimally.
Faster convergence: Exploration can be aggressive (e.g., taking random actions) without corrupting the learning target.
Exploration Strategies
To balance exploration and exploitation, Q-learning often uses ϵ-greedy policy:
With probability 1−ϵ, choose the greedy action argmaxaQ(s,a).
With probability ϵ, choose a random action (uniformly).
Typically ϵ starts high (e.g., 0.5 or 1.0) and anneals toward zero over time, so the agent explores broadly early on and exploits its knowledge later.
Definition: Q-learning is an off-policy, model-free, gradient-free reinforcement learning algorithm that learns the optimal quality function Q∗(s,a) by using the Bellman optimality operator on sampled transitions.
SARSA: On-Policy TD on the Quality Function
SARSA (State-Action-Reward-State-Action) is almost identical to Q-learning but with a single, crucial difference. The update rule is:
Here, instead of using maxa′Q(sk+1,a′), we use Q(sk+1,ak+1) — the quality of the actual next action ak+1 that was taken (or will be taken) by the current policy. This makes SARSA on-policy: the target depends on the very policy being followed.
Implications:
If the policy is greedy (always takes argmaxaQ), SARSA and Q-learning become equivalent.
If the policy includes exploration (e.g., ϵ-greedy), SARSA accounts for the exploration's effect on future returns. Q-learning ignores it by assuming optimal future actions.
SARSA tends to be safer during training because it doesn't assume perfect future optimality — it learns a policy that works well given the exploration noise.
Extending SARSA to N-Step and TD(λ)
Like TD learning on V, SARSA can be extended to n-step returns and the TD(λ) framework. The TD target simply uses the observed rewards and the Q values of the actual actions taken over those n steps.
Comparison: Q-Learning vs. SARSA
Key Differences
Aspect | Q-Learning | SARSA |
|---|---|---|
Policy type | Off-policy | On-policy |
TD target | rk+γmaxa′Q(sk+1,a′) | rk+γQ(sk+1,ak+1) |
Assumption about future | Always optimal (max over actions) | Follows current policy (which may be suboptimal) |
Exploration handling | Can learn from any behavior; ignores exploration in target | Learns a policy that accounts for its own exploration |
Convergence | Converges to optimal Q∗ under standard conditions | Converges to the optimal policy for the given exploration schedule |
Typical behavior | Faster convergence, higher variance, riskier during training | Slower, safer, higher cumulative reward during learning |
Practical Trade-Offs
Q-learning is preferred when safe exploration is possible and speed of learning matters. It can leverage diverse experiences, including offline data or demonstrations.
SARSA is better when safety during training is critical (e.g., autonomous driving, robotic manipulation). It tends to avoid dangerous actions because the target penalizes exploratory blunders.
Key insight: The off-policy nature of Q-learning allows it to learn from any behavior (including past suboptimal experiences), while SARSA's on-policy nature makes it inherently conservative but often safer.
Summary and Outlook
Model-free, gradient-free reinforcement learning provides a powerful framework for learning optimal behavior when the environment dynamics are unknown. The progression from Monte Carlo (wait until the end) to TD (update every step) to Q-learning and SARSA (apply TD to the quality function) shows how each innovation improves sample efficiency and flexibility.
Key takeaways:
Monte Carlo: Unbiased but high variance and requires complete episodes.
TD learning: Bootstraps from current estimates, enabling online learning; characterized by the TD error signal.
Q-learning (off-policy TD on Q): Learns the optimal Q while exploring arbitrarily; enables experience replay and imitation.
SARSA (on-policy TD on Q): Learns a policy that is safe and consistent with the exploration strategy.
These methods are the foundation of modern deep reinforcement learning, where deep neural networks approximate the Q function. The combination of Q-learning with deep networks (DQN) led to breakthroughs in game playing and robotics. Future lectures will build on these gradient-free methods to incorporate function approximation and policy gradients.

Ask anything
We've read this whole study set, so questions about it get the best answers.