v9.1.1
Menu

Chapter 12: Reinforcement Learning

Week 23-24

Explore the paradigm of learning through interaction with an environment. Master the Markov Decision Process framework, value-based methods like Q-learning, policy gradient algorithms, and understand how deep learning enables RL to tackle complex problems from game playing to robotics.

Topics
3
Videos
3
Min read
27

Chapter Overview

Reinforcement Learning (RL) represents a fundamentally different paradigm from supervised and unsupervised learning. Instead of learning from labeled examples or finding patterns in data, RL agents learn by interacting with an environment and receiving feedback in the form of rewards.

The key challenge in RL is the credit assignment problem: when an agent receives a reward, it must determine which of its past actions contributed to that reward. This is complicated by delayed rewards—a good action might not produce a reward until many steps later.

RL has achieved remarkable successes: defeating world champions at Go and Chess, learning to play Atari games from raw pixels, controlling robots, optimizing data center cooling, and training the RLHF component of modern language models like ChatGPT.

The fundamental framework for RL is the Markov Decision Process (MDP), which formalizes the agent-environment interaction loop. Two main approaches exist for solving MDPs: value-based methods (learning which states/actions are valuable) and policy-based methods (directly learning which actions to take).

This chapter covers:

  • MDP Framework: The mathematical formalization of sequential decision-making with states, actions, rewards, and transitions
  • Q-Learning: A value-based algorithm that learns optimal action-values through temporal difference updates
  • Policy Gradients: Methods that directly parameterize and optimize the policy using gradient ascent
  • Deep RL: Combining neural networks with RL for high-dimensional state spaces and complex policies

Chapter Roadmap

Click any topic to jump in

Explainer video
1
Markov Decision Process

The mathematical framework for sequential decision-making — states, actions, rewards, transitions, and discount factors.

MDP ComponentsPolicyValue FunctionDiscount Factor γ
Solving MDPs with value and policy methods

Value-based and policy-based approaches

2
Q-Learning

Model-free value learning through temporal difference updates — epsilon-greedy exploration and the Bellman equation.

Q-FunctionBellman Updateε-Greedy ExplorationDQN (Deep Q-Network)
3
Policy Gradients

Directly optimizing the policy with REINFORCE — advantage estimation, actor-critic, and continuous action spaces.

Policy ParameterizationREINFORCE AlgorithmBaseline / AdvantageActor-Critic

Topics

3 topics in this chapter

The previous chapter, NLP Fundamentals, ended with models pretrained on fixed datasets: every training example arrived with its input and its target already written down. Many problems do not look like that. A robot vacuum, a game-playing program, or a system that sets prices must choose an action, see what happens, and choose again, and its choices change the data it sees next. Nobody labels the correct move. The only feedback is a reward, often delayed: a chess win arrives 40 moves after the decisive mistake. How do we even state such a problem precisely enough to solve it?

This chapter, Reinforcement Learning, studies agents that learn from interaction and reward. This topic builds the formal language every later method uses, the Markov Decision Process. It starts with the MDP components: states, actions, transition probabilities, rewards, and the discount factor. A policy is the agent's rule for choosing actions. A value function scores a state by the reward that policy expects to collect from it. The topic closes with the discount factor, which trades immediate reward against future reward.

Definition

A Markov Decision Process is a tuple (S,A,P,R,γ)(S, A, P, R, \gamma): a set of states SS, a set of actions AA, transition probabilities P(s′∣s,a)P(s' \mid s, a), a reward function R(s,a)R(s, a), and a discount factor γ∈[0,1)\gamma \in [0, 1). The Markov property requires that the next state and reward depend only on the current state and action, not on earlier history.

In this topic

  1. 1MDP Components
  2. 2Policy
  3. 3Value Function
  4. 4Discount Factor γ
1 of 4
MDP Components

An MDP has five parts. The state set SS describes every situation the agent can be in; the action set AA lists its choices. The transition model P(s′∣s,a)P(s' \mid s, a) gives the probability of landing in state s′s' after taking action aa in state ss, so it captures randomness such as a wheel slipping. The reward R(s,a)R(s, a) is a number the environment returns after each step, and it is the only signal of what we want. The discount γ\gamma weights future rewards. The Markov property says the current state carries everything relevant, so a state that omits needed history, such as velocity, breaks the model.

Mathematical Intuition

An MDP is the tuple (S,A,P,R,γ)(S, A, P, R, \gamma) where P(s′∣s,a)P(s'|s,a) satisfies the Markov property: P(st+1∣st,at,st−1,…)=P(st+1∣st,at)P(s_{t+1}|s_t, a_t, s_{t-1}, \ldots) = P(s_{t+1}|s_t, a_t). The state space can be finite (∣S∣=n|S| = n) or continuous (S⊆RdS \subseteq \mathbb{R}^d). For finite MDPs, the transition model is a set of ∣A∣|A| stochastic matrices Pa∈R∣S∣×∣S∣P_a \in \mathbb{R}^{|S| \times |S|}, each row summing to 1. The total number of parameters to fully specify the MDP is ∣S∣⋅∣A∣⋅(∣S∣+1)|S| \cdot |A| \cdot (|S| + 1).

Example:

Robot vacuum cleaner: identify the MDP components. States? Actions? What determines transitions?

2 of 4
Policy

π(a∣s)=P(At=a∣St=s)\pi(a|s) = P(A_t = a | S_t = s)

A policy π(a∣s)\pi(a \mid s) is the agent's behavior: for each state, a probability distribution over actions. A deterministic policy puts all its probability on one action per state. The goal of reinforcement learning is an optimal policy, written π∗\pi^*, that maximizes expected discounted return from every state at once. For finite MDPs a deterministic optimal policy always exists, but stochastic policies are still useful during learning, because they explore, and in partially observed or adversarial settings. Note that 'optimal' means maximizing the expected value; the plain MDP objective is risk-neutral, so a high-variance gamble with the best average is preferred.

Mathematical Intuition

A policy π:S→Δ(A)\pi: S \to \Delta(A) maps states to action distributions. The optimal policy π∗\pi^* satisfies Vπ∗(s)≥Vπ(s)V^{\pi^*}(s) \geq V^{\pi}(s) for all ss and all π\pi. For finite MDPs, an optimal deterministic policy always exists — randomization is unnecessary. The policy improvement theorem guarantees that making a policy greedy with respect to its own value function produces a policy that is at least as good: Vπ′(s)≥Vπ(s)V^{\pi'}(s) \geq V^{\pi}(s) where π′(s)=arg⁡max⁡aQπ(s,a)\pi'(s) = \arg\max_a Q^{\pi}(s,a).

Example:

Gridworld: state A (near cliff), actions {safe_path, risky_shortcut}. Optimal policy if safe=+5, risky=+10 with 50% fall (-100)?

3 of 4
Value Function

Vπ(s)=E[∑t=0∞γtRt∣S0=s,π]V^\pi(s) = E[\sum_{t=0}^{\infty} \gamma^t R_t | S_0 = s, \pi]

The state-value function Vπ(s)V^\pi(s) is the expected discounted return when the agent starts in state ss and follows policy π\pi forever after: the sum of γtRt\gamma^t R_t over all future steps tt, averaged over the randomness in both actions and transitions. It turns a long sequence of future rewards into one number per state, which lets the agent compare situations. Values satisfy the Bellman equation: a state's value equals the expected immediate reward plus γ\gamma times the expected value of the next state. That recursion is what dynamic programming, temporal-difference learning, and Q-learning exploit. Values depend on the policy: a bad policy makes a good state look poor.

Mathematical Intuition

The Bellman equation Vπ(s)=∑aπ(a∣s)∑s′P(s′∣s,a)[R(s,a,s′)+γVπ(s′)]V^{\pi}(s) = \sum_a \pi(a|s) \sum_{s'} P(s'|s,a) [R(s,a,s') + \gamma V^{\pi}(s')] is a system of ∣S∣|S| linear equations in ∣S∣|S| unknowns, solvable in O(∣S∣3)O(|S|^3) by matrix inversion: V=(I−γPπ)−1rπV = (I - \gamma P_{\pi})^{-1} r_{\pi}. The Bellman optimality equation replaces the weighted sum over actions with a max: V∗(s)=max⁡a∑s′P(s′∣s,a)[R+γV∗(s′)]V^*(s) = \max_a \sum_{s'} P(s'|s,a)[R + \gamma V^*(s')], which is nonlinear and requires iterative methods like value iteration.

Example:

γ=0.9. From state A, policy gives rewards: +10 (t=0), +10 (t=1), +10 (t=2), then 0 forever. V(A)?

4 of 4
Discount Factor γ

The discount factor γ\gamma, between 0 and 1, multiplies a reward received tt steps in the future by γt\gamma^t. It makes infinite sums finite, since returns are bounded by the maximum reward divided by 1−γ1 - \gamma, and it encodes a preference for sooner rewards. A rough effective horizon is 1/(1−γ)1/(1 - \gamma) steps: about 10 for 0.9 and 100 for 0.99. With γ=0\gamma = 0 the agent is myopic and maximizes only the next reward; as γ\gamma approaches 1 it becomes far-sighted but learning gets harder, because values become larger and noisier. The discount is part of the problem definition, though it is often tuned like a hyperparameter.

Mathematical Intuition

The discount factor γ∈[0,1)\gamma \in [0, 1) ensures the infinite sum ∑t=0∞γtRt\sum_{t=0}^{\infty} \gamma^t R_t converges: ∣V(s)∣≤Rmax⁡/(1−γ)|V(s)| \leq R_{\max} / (1 - \gamma). Economically, γ\gamma represents the agent's time preference — the present value of receiving reward RR at time tt is γtR\gamma^t R. The effective planning horizon is approximately 1/(1−γ)1/(1-\gamma) steps: with γ=0.99\gamma = 0.99, the agent considers the next ~100 steps; with γ=0.9\gamma = 0.9, only ~10 steps matter.

Example:

Two paths: A gives +1 now, B gives +10 in 5 steps. Which is better for γ=0.5 vs γ=0.99?

Theory Exercise

Problem:

An agent in a 4-state MDP receives +1 reward every step until reaching a terminal state. Why must the discount factor γ be strictly less than 1 in a continuing (non-terminating) task, and what happens to the value function as γ → 1?

Hints:
  • Write out the value function as an infinite geometric series
  • Recall the convergence condition for a geometric series
  • Think about the effective planning horizon 1/(1-γ)

Coding Exercise

Problem:

Implement iterative policy evaluation for a tiny 4-state chain MDP under a uniform-random policy, then report the converged state values for a given discount factor.

Hints:
  • Loop the Bellman expectation backup V(s) ← Σ_a π(a|s) Σ_s' P(s'|s,a)[R + γV(s')] until values stop changing
  • Represent transitions as a dict keyed by (state, action) → (next_state, reward)
  • Stop when the largest value change (delta) falls below a small threshold