Reinforcement learning studies sequential decision making under uncertainty. Unlike supervised learning, where data is static, an RL agent's actions actively influence future states and rewards, creating a closed feedback loop.
Purpose of this lecture#
The core question in reinforcement learning is how to express goal-directed behavior with a single mathematical objective.
Reinforcement learning formalizes this by maximizing expected cumulative reward. Translating this objective into practical algorithms introduces three core problems:
-
Reward specification. Specifying reward functions precisely is difficult. A misspecified reward leads an agent to optimize the literal reward signal while violating the intended goal.
-
Uncertainty over time. Environments are stochastic and often partially observable. The agent must evaluate actions whose delayed consequences unfold over time.
-
Computational tractability. Maximizing an infinite sum of future rewards directly is intractable. Computing solutions requires recursive structure.
This lecture addresses these problems with the MDP formalism, value functions, and the Bellman equations. These concepts provide the mathematical encoding of sequential decision making. Later algorithms, including tabular methods, deep RL, RLHF, and GRPO, build directly on this theory.
This lecture derives the Bellman equations from first principles, examines where the Markov assumption fails in practice, and presents the engineering techniques used to address partial observability.
Sequential decision making#
In reinforcement learning, an agent interacts with an environment over discrete time steps. This interaction forms a closed feedback loop:
- At time step , the agent observes a state
- It selects an action
- The environment transitions to a new state
- The agent receives a scalar reward
The agent's objective is not to maximize immediate reward, but to act so as to maximize cumulative reward over time.
This temporal coupling distinguishes reinforcement learning from:
- Supervised learning, where data is fixed and independent of the learner's actions
- Standard optimization, where decisions are made once rather than sequentially
The notation for the reward received after taking action follows the Sutton and Barto convention: reward is indexed by the time step at which the agent receives it, rather than when the action occurred. This notation is standard across RL literature.
Formalizing "maximize cumulative reward over time" requires two components: an environment model (an MDP) and an accumulation criterion (the return). We begin with the reward hypothesis, which formalizes the scalar objective.
The reward hypothesis#
A foundational premise in reinforcement learning is the Reward Hypothesis:
All goals and purposes can be well thought of as the maximization of the expected value of the cumulative sum of a received scalar signal (reward).
This hypothesis asserts that complex behaviors, such as playing games or robotic locomotion, can be framed as maximizing expected cumulative reward. It is a working hypothesis rather than a mathematical theorem, and much of modern reinforcement learning explores its practical limits.
A concrete starting point#
Before examining where the hypothesis succeeds and fails, consider the simplest case where it clearly works.
Gridworld navigation. An agent moves through a grid to reach a goal cell in the lower-right corner. The reward function assigns upon reaching the goal and at every other step. This is unambiguous and easy to specify. An agent maximizing expected cumulative reward will learn to navigate efficiently to the goal. The reward hypothesis applies cleanly.
Atari Pong. Reward is when the agent scores a point and when the opponent scores. This objective is also unambiguous. Mnih et al. (2013) demonstrated that DQN learned competitive policies directly from pixel inputs and score deltas, providing early empirical support for the hypothesis.
These cases share a key property: the reward function captures the intended goal. The difficulty arises when the written reward diverges from the desired behavior.
Why reward design matters#
The hypothesis does not prescribe how to design reward functions. Reward specification remains a central challenge in RL. The same principles apply directly to RLHF, preference optimization (GRPO, DPO, PPO), and autonomous agents. Three common failure modes arise:
- Sparse rewards: signal arrives only at task completion, making learning slow
- Dense rewards: accelerate learning but risk reward hacking
- Reward misspecification: the gap between the written reward and the actual goal
The tension between what is easy to specify and what is safe to optimize is a recurring theme across RL.
With the reward hypothesis establishing the scalar objective, we next formalize the environment: the mathematical framework defining how actions yield transitions and rewards.
Markov decision processes (MDPs)#
The canonical mathematical model for reinforcement learning is the Markov Decision Process (MDP).
An MDP is defined by the tuple:
where:
- is the state space,
- is the action space,
- is the transition probability kernel,
- is the expected reward function,
- is the discount factor.
The standard RL interaction cycle. At each step the agent selects an action from its policy, the environment returns a reward and the next state, and the agent updates its policy and value estimates. Every algorithm in this course is a different answer to "how should that update be computed?"
The Markov property#
The defining assumption of an MDP is the Markov property:
The future is conditionally independent of the past given the current state and action. The current state contains all information relevant to predicting what happens next.
Markovianity is engineered, not given#
In practice, raw sensor observations are rarely Markovian. This distinction is fundamental to reinforcement learning system design.
Consider a robot arm that measures joint angles but not joint velocities. The subsequent state depends directly on momentum, which is absent from position measurements alone. Two robots with identical joint angles but different velocities transition to different subsequent states, making the transition dynamics non-Markovian in the observed state. Similarly, a language model observing only the latest token cannot distinguish between different meanings of polysemous words. In both cases, the current observation fails to capture the true underlying state.
Systems where the agent receives observations generated by an underlying hidden state are modeled as Partially Observable Markov Decision Processes (POMDPs):
POMDPs represent the general case; MDPs represent the special case where . Most physical systems and real-world problems are POMDPs.
Three standard engineering techniques address partial observability:
- State augmentation: incorporate physical derivatives, such as velocities or acceleration signals, directly into the state vector.
- Observation stacking: concatenate a temporal window of recent observations, as in Atari frame stacking.
- Recurrent representations: maintain a recurrent state vector via an LSTM or transformer architecture to track belief states over time.
Language models as MDPs#
To ground this abstraction, consider RL applied to large language models, as in RLHF:
| MDP component | LLM interpretation |
|---|---|
| State | Prompt and all generated tokens in the context window |
| Action | Next token selected from vocabulary |
| Transition | Appending selected token to context |
| Reward | Scalar score from a reward model |
| Discount | Relative weighting across token positions |
Under this formulation, text generation is a sequential decision process, and preference alignment algorithms operate as reinforcement learning methods in this MDP. Because the state includes the prompt and all generated tokens within the context window, the MDP formulation satisfies the Markov property for the sequence.
With the environment formalized as an MDP, the agent's behavior must be specified. A policy defines how the agent maps states to actions, and its properties determine whether the agent can learn effectively.
Policies#
A policy specifies the agent's behavior as a mapping from states to actions:
Policies may be:
- Deterministic: , selecting a single action per state. Common in deployment and in control-theoretic settings.
- Stochastic: sampling actions from a probability distribution. Essential for exploration during training, and fundamental to policy gradient methods.
In LLMs, the policy is the language model itself: a stochastic policy over the vocabulary at each token position, with temperature controlling the degree of stochasticity.
Given a policy, we evaluate its quality across trajectories using the return.
Returns and discounting#
The agent's objective is formalized via the return, the discounted cumulative reward from time step onward:
This formulation reflects three distinct mathematical interpretations.
Three interpretations of the discount factor #
1. Convergence guarantee. For continuing tasks, the infinite sum diverges if rewards are bounded away from zero. When , the return is bounded by . This condition is a mathematical requirement for infinite horizons.
2. Time preference. The discount factor balances immediate rewards against delayed rewards. As , future rewards receive comparable weight to immediate ones. As , the agent prioritizes immediate reward. The effective planning horizon is approximately steps because .
3. Survival probability. The factor can represent the probability that an interaction continues at each step. With termination probability , the expected horizon matches geometric discounting. This perspective unifies episodic and continuing formulations under a single framework.
A warning on and policy gradient bias#
In policy gradient methods, using introduces an objective discrepancy. The discounted policy gradient does not match the gradient of the undiscounted return. This discrepancy matters in continuing tasks where the ultimate objective is undiscounted cumulative reward. We return to this derivation in later lectures.
Episodic vs continuing tasks#
- Episodic tasks terminate at a terminal state (games, robotic manipulation episodes, single LLM responses). The return is a finite sum.
- Continuing tasks run indefinitely (control systems, long-running agents). The return requires discounting for finiteness.
For episodic tasks, terminal states are often treated as absorbing states with zero reward and self-loops: once reached, the agent stays there and receives zero reward forever. This unifies the episodic and continuing formalisms and is the standard implementation in most RL libraries.
The return is a random variable that depends on transition dynamics, policy stochasticity, and trajectory length. We evaluate policies by computing the expected return, which defines the value function.
Value functions#
Value functions quantify expected return from states and actions under policy . They are not directly observable; algorithms estimate them from experience or derive them from an environment model.
State-value function#
This is the expected cumulative discounted reward, starting from state and following policy thereafter.
Action-value function (Q-function)#
This conditions on both state and action: the expected return from taking action in state , then following .
The relationship between them:
Value functions turn sequential decision problems into expected return estimation. The optimal value functions and determine optimal policies directly: .
Because infinite horizon returns cannot be evaluated by exhaustive summation, reinforcement learning relies on recursive representations. The Bellman equations express the value of each state in terms of expected successor state values.
Bellman equations#
The Bellman equations express value functions recursively, relating the value of the current state to the values of successor states. They define the computational foundation of reinforcement learning.
Bellman expectation equation#
This formulation constitutes a system of linear equations in . Given policy , transition model , and reward function , solving for requires solving a linear algebra problem. This process is called policy evaluation.
Bellman optimality equation#
This formulation is a nonlinear fixed-point equation in . Because the operator is nonlinear, cannot be solved by matrix inversion. It possesses a unique solution that iterative methods reach geometrically.
The contraction mapping property#
Define the Bellman optimality operator :
is a -contraction in the norm:
By the Banach fixed-point theorem, repeated application of converges geometrically to the unique fixed point at rate per iteration. This process constitutes value iteration. The contraction property explains why is an algorithmic necessity: setting removes the contraction guarantee.
Connection to the LQR Riccati equation#
This subsection provides optional background connecting dynamic programming to classical control. Readers familiar with robotics control will recognize the Bellman optimality equation in the following continuous-state form.
Consider linear dynamics with quadratic cost, known as the Linear Quadratic Regulator (LQR). Let represent the state-transition matrix, the input matrix, the state-cost matrix, and the action-cost matrix. We use subscript to distinguish cost matrices from reward functions. The discrete-time Algebraic Riccati Equation (DARE) is:
This equation is the Bellman optimality equation specialized to linear quadratic systems, where matrix parameterizes the quadratic value function. LQR solves optimal control in closed form under these linear-quadratic assumptions.
Worked example: Bellman backup on a two-state MDP#
Setup. Consider the following MDP:
- States:
- Actions:
- Transitions: deterministic. Action stay keeps the agent in the current state, and action switch moves the agent to the other state.
- Rewards: , (reward depends only on the state).
- Discount: .
Policy (always stay). The Bellman expectation equations are:
Solving the linear system:
Interpretation. Starting in , the agent collects at every step. The geometric series confirms the analytical result. Starting in , the agent receives zero reward indefinitely.
Policy (always switch). The agent alternates between and , receiving rewards . The Bellman equations couple both states:
Substituting the second equation into the first yields , which gives .
Conclusion. Policy strictly dominates policy because . The optimal policy stays in to maximize cumulative return, matching the choice selected by the Bellman optimality operator.
Browser lab: iterative policy evaluation#
Implement the policy evaluation above using the iterative (not direct) method, and verify convergence to the analytical solution.
import numpy as np
# MDP definition
gamma = 0.9
# States: 0 = s1, 1 = s2. Actions: 0 = stay, 1 = switch.
# P[s, a, s'] = P(s' | s, a)
P = np.array([
[[1.0, 0.0], [0.0, 1.0]], # from s1: stay->s1, switch->s2
[[0.0, 1.0], [1.0, 0.0]], # from s2: stay->s2, switch->s1
])
R = np.array([1.0, 0.0]) # R(s1) = 1, R(s2) = 0
# Policy pi1: always stay (action 0 in both states)
pi = np.array([0, 0])
# Iterative policy evaluation
# Initial guess of zeros. The contraction property guarantees convergence
# regardless of the starting estimate.
V = np.zeros(2)
for i in range(1000):
# Bellman expectation backup: R(s) + γ Σ_{s'} P(s'|s,π(s)) V(s')
# np.dot computes the expected future value over successor states.
V_new = np.array([
R[s] + gamma * np.dot(P[s, pi[s]], V)
for s in range(2)
])
# Convergence check using the infinity norm (max absolute difference).
# A small value means we've reached the fixed point of the Bellman operator.
if np.max(np.abs(V_new - V)) < 1e-8:
print(f"Converged after {i+1} iterations")
break
# Synchronous (Jacobi-style) update: all states updated using the old V.
V = V_new
print(f"V(s1) = {V[0]:.4f}") # Expected: 10.0
print(f"V(s2) = {V[1]:.4f}") # Expected: 0.0
# Always-stay: V(s1) = 1/(1-γ) = 10 under perpetual reward 1.
print("Notice: under π=always stay, s2 is never left and has R=0 → V(s2)=0.")
Try it: Change to pi = np.array([1, 1]) (always switch) and re-run. The value
converges to . Policy evaluation computes values for a fixed policy;
subsequent weeks explore policy optimization.
Taxonomy of reinforcement learning problems#
Classifying reinforcement learning problems guides algorithm selection and clarifies necessary assumptions.
Bandits vs full MDPs#
- Multi-armed bandits: no state transitions occur; actions yield immediate rewards. The agent balances exploration of uncertain actions against exploitation of high-value actions.
- Full MDPs: actions influence subsequent states and long-term returns. Local bandit reasoning applies within each state, but value functions account for delayed transitions.
Bandits appear throughout reinforcement learning applications, including recommendation systems, online experimentation, and pairwise preference models in RLHF.
Online vs offline reinforcement learning#
- Online RL: the agent collects experience while learning. Transitions originate from current or recent policies. Common algorithms, such as Q-learning, PPO, and SAC, operate online.
- Offline RL: the agent learns from a fixed dataset collected by a behavior policy, without active environment interaction. This paradigm is essential in domains such as robotics and healthcare where exploratory actions carry high cost or risk.
The primary technical challenge in offline reinforcement learning is distributional shift. The learned policy can select state-action pairs rare or missing in the dataset. Without environment interaction, erroneous value estimates remain uncorrected, yielding inflated Q-values. Supervised learning avoids this issue because classifiers do not generate sequential trajectories in low-density data regions.
Model-free vs model-based RL#
- Model-free RL: learns value functions or policies directly from experience without constructing an explicit transition model .
- Model-based RL: constructs or queries an explicit transition model to plan actions, improving sample efficiency at the risk of model bias.
These paradigms form a continuum. Dyna algorithms combine environment transitions with rollouts simulated from a learned model. Dreamer and MuZero plan inside learned latent-state representations rather than physical observation spaces.
Relationship to supervised learning and control theory#
Supervised learning#
Supervised learning optimizes a fixed loss on static datasets where outputs do not alter subsequent data. Reinforcement learning violates this assumption. The policy selects actions that determine subsequent states and collected transitions, forming a non-stationary feedback loop that requires managing exploration.
Control theory#
Classical control theory addresses sequential decision making under distinct assumptions:
- Known dynamics: classical control assumes analytical access to transition dynamics , whereas reinforcement learning generally estimates them from data or operates model-free.
- Stability guarantees: control theory emphasizes Lyapunov stability and robustness criteria that general reinforcement learning rarely guarantees.
- Function approximation: classical control focuses on low-dimensional analytical representations, whereas reinforcement learning scales to high-dimensional state spaces using neural network approximations.
These formulations intersect directly. Linear Quadratic Regulation (LQR) solves optimal control in closed form for linear dynamics and quadratic costs. Reinforcement learning generalizes this formulation to unknown dynamics and non-linear function approximators.
Connections to modern methods#
The MDP formalism provides the mathematical foundation for modern reinforcement learning architectures. The following examples illustrate how the formal components map to current methods.
DreamerV3: Bellman backups in latent space#
DreamerV3 (Hafner et al., 2023) learns visual control tasks across domains, including robotics and video games, directly from pixel observations. Its architecture instantiates the MDP tuple:
| MDP component | DreamerV3 implementation |
|---|---|
| State | Latent state vector from a recurrent state-space model (RSSM) |
| Transition | Learned latent dynamics model |
| Reward | Learned reward predictor from latent states |
| Value | Neural network updated via Bellman backups over simulated latent rollouts |
| POMDP belief | RSSM maintains a sequence belief state over raw observations |
Once the world model is trained, the agent applies Bellman updates to imagined trajectories without querying the environment. Value iteration operates directly in latent space.
TD-MPC: value-weighted planning#
TD-MPC (Hansen et al., 2022) combines model predictive control with value learning:
- A learned latent world model provides short-horizon rollouts for local transition dynamics .
- A learned action-value function , updated via Bellman optimality backups, estimates long-term returns beyond the rollout window.
- Trajectory optimization evaluates action sequences by combining simulated short-term rewards with terminal Q-value estimates.
The Bellman Q-function summarizes delayed returns, allowing the planner to truncate rollouts while retaining long-horizon evaluation.
Common pitfalls in RL problem formulation#
Recognizing common formulation errors prevents systematic failures in implementation:
-
Non-Markovian state representation. Using raw observations without establishing state sufficiency causes value estimates to oscillate. Verify whether identical observations from different histories transition to different outcomes. Restore Markovianity using state augmentation or observation stacking.
-
Reward hacking. Optimizing a proxy reward that diverges from true intent produces unintended behaviors that maximize numerical reward. Before training, evaluate potential degenerate policies. For instance, rewarding joint velocities alone causes a walking robot to fall forward.
-
Setting in continuing tasks. Infinite-horizon sums diverge and Bellman operators lose their contraction property, producing numerical overflow. Set , or formulate the task as episodic with absorbing terminal states.
-
Confusing episodic and continuing formulations. Fixed-duration manipulation tasks are episodic, whereas continuous process control is continuing. Select the formulation based on whether natural terminal states exist. Continuing formulations require discounting to preserve finite returns.
-
Ignoring distributional shift in offline RL. Training on static datasets without penalizing out-of-distribution actions causes overoptimistic value estimates. Unlike supervised learning, bootstrapped value estimation compounds extrapolation errors on unsupported state-action pairs.
Key takeaways#
The concepts introduced here form the foundation for every algorithm in the course. The reward hypothesis formalizes goal-directed behavior as scalar reward maximization. Markov decision processes formalize the sequential interaction between agent and environment. Policies specify actions across states, and value functions evaluate expected long-term return.
The Bellman equations express value functions recursively, yielding a linear system for policy evaluation and a nonlinear fixed-point equation for optimality. The contraction mapping property ensures that iterative dynamic programming converges to the unique optimal value function. The discount factor bounds returns in infinite-horizon tasks and defines the effective planning horizon. Finally, the connection to the Riccati equation demonstrates that reinforcement learning extends classical optimal control to systems with unknown dynamics.
Conceptual questions#
-
A robot observes only joint positions, not velocities. Explain why this observation model violates the Markov property. Propose two state engineering strategies to restore approximate Markovianity, and specify what each strategy assumes about the system.
-
Suppose you run value iteration with on a continuing task with bounded positive rewards. Explain why returns diverge, and describe why the Bellman optimality operator loses its contraction property. What minimal modification restores convergence?
-
A language model trained with RLHF achieves high preference scores but generates persuasive yet factually incorrect responses. Analyze this outcome using the reward hypothesis and reward misspecification. How should the training objective be adjusted to mitigate this behavior?
-
Compare the Bellman expectation equation for policy evaluation with the linear system . Identify , , and , and prove that is invertible for any .
-
An offline reinforcement learning policy is trained on robot manipulation demonstrations. The policy succeeds on configurations close to the training demonstrations but fails on novel configurations. Contrast this failure mode with compounding imitation error in behavioral cloning.
Knowledge Check#
Check the MDP building blocks from this week.
In the standard discounted return, future rewards are scaled by powers of ___, the discount factor in (0, 1].
The Bellman optimality equation for V* uses which operator over actions?
A finite MDP is fully specified by which core ingredients?
Looking ahead#
The next lecture examines multi-armed bandits, the simplest non-trivial reinforcement learning problem. Bandits isolate the trade-off between exploration and exploitation without state transitions. Regret analysis and bandit algorithms provide tools that generalize to full MDPs and to preference feedback mechanisms in later lectures.
Further reading#
- Bellman, R. (1957). Dynamic Programming. Princeton University Press. Foundational text introducing the Bellman equation and the principle of optimality.
- Sutton & Barto (2018). Reinforcement Learning: An Introduction (2nd ed.), Chapter 3: Finite Markov Decision Processes. Standard textbook reference.
- Puterman, M. L. (1994). Markov Decision Processes: Discrete Stochastic Dynamic Programming. Wiley. Comprehensive mathematical treatment of dynamic programming convergence and contraction proofs.
- Silver, D. (2015). UCL Course on RL, Lecture 2: Markov Decision Processes. Lecture slides and video covering tabular MDP examples.
- Bertsekas, D. P. (2019). Reinforcement Learning and Optimal Control. Athena Scientific. Bridges classical optimal control and modern reinforcement learning.
Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.