←Back to 03 / Published Articles
03 / Resources/Sequential Decision Making · Reinforcement Learning
Interactive Diagnostic Essay · Topic 15

Reinforcement Learning

Every model in this archive learned from a labelled answer key. This one gets nothing but consequences — a reward when it does well, a cost when it does not, and the job of working out which of its own actions put it there.

01 / No Answer Key

Learning From Consequences

Everything so far in this archive has been supervised: you hand the model rows of features with the correct answer attached, and it learns to reproduce that mapping. The signal is immediate and specific — “for this input, the answer was that”.

Reinforcement learning removes the answer key. An agent takes an action, the world changes, and it receives a single number — a reward — that may arrive long after the decision that caused it. It is never told which action was wrong. It has to infer that from outcomes.

The credit assignment problem

Win a game in 40 moves and which of those 40 moves deserves the credit? The reward is unambiguous; attributing it across a long sequence of decisions is the hard part, and it is what separates RL from everything else in this archive.

That changes the data too. In supervised learning the dataset is fixed before training begins. Here the agent's own choices determine what data it sees next — explore badly and you train on a distorted view of the world.

WHERE THE SIGNAL COMES FROM
Supervised

“Here is the input; here is the correct output.” Dense, immediate, independent per example.

Reinforcement

“You scored 0.7.” Sparse, delayed, and correlated with every choice you just made.

02 / The Vocabulary

The Problem Structure

Almost every RL problem is formalised the same way — as a Markov decision process. Five ingredients, and then one rule for choosing what to do.

States · S

Everything the agent can observe about where it currently is. On a board, the position of every piece.

Actions · A

The moves available in a state. Often shared across states, but not always.

Rewards · R

The scalar feedback after each transition. This is the whole specification of what you actually want.

Transitions · P

How the world moves when you act — possibly stochastic, which is why the same action need not give the same result twice.

Discount · γ

How much a reward now is worth versus the same reward later. In [0,1)[0, 1).

Policy · π

The thing being learned: a rule mapping states to actions. The goal is the policy that maximises total discounted reward.

The objective — what “good” meansGt=Rt+1+γRt+2+γ2Rt+3+⋯=∑k=0∞γkRt+k+1G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots = \sum_{k=0}^{\infty} \gamma^{k} R_{t+k+1}

Not the next reward — the discounted sum of everything that follows. That single change is what makes an agent willing to accept a small cost now for a large payoff later.

03 / The Simplest Possible Problem

The Bandit: Explore or Exploit?

Strip away states and sequences and one hard question remains. You face several slot machines, each with an unknown payout rate. Every pull is either experience or money — never both.

Pull the machine that looks best so far and you collect the reward you already understand (exploitation). Pull a different one and you learn something that might change your mind (exploration). Spend all your turns exploiting and you may never discover the better machine. Spend them all exploring and you never collect.

ε-greedy — the simplest answerπ(a)={arg⁡max⁡aQ(a)w.p. 1−εuniform randomw.p. ε\pi(a) = \begin{cases} \arg\max_a Q(a) & \text{w.p. } 1 - \varepsilon \\ \text{uniform random} & \text{w.p. } \varepsilon \end{cases}

Usually take the best-known action; occasionally, at random, take something else. Crude, and remarkably hard to beat on many problems.

The mechanism that makes this work is the running average. Each machine's estimated value updates a little toward each new observation, so early noise gets averaged out rather than believed:

Qn+1(a)=Qn(a)+1n[Rn−Qn(a)]Q_{n+1}(a) = Q_n(a) + \frac{1}{n}\left[R_n - Q_n(a)\right]

Estimate, plus a fraction of the surprise — the gap between what you expected and what you got. That prediction error shape reappears in every RL algorithm, right up to the modern deep ones.

The tension, made concrete
Pure greedy
Locks onto whatever looked good in the first few pulls. Often a permanently wrong answer.
Pure random
Learns each machine's true rate perfectly, and cashes in on none of it.
ε-greedy
Buys information at a controlled, decaying price. Neither extreme, and better than both.
Why this matters later

Every RL algorithm inherits this problem. The bandit is where you meet it without the distraction of long sequences and delayed reward.

04 / From Numbers To Places

The Grid World

Now give the agent a body and a map. It starts top-left, wants the goal bottom-right, and must avoid the pit. Shading shows how good each cell is; the dot re-walks the learned route on a loop. Drag the discount factor to see what patience does to the map.

DISCOUNT FACTOR γ0.90
impatientfar-sighted
START VALUE:0.344
STEPS TO GOAL:6
long horizon · distant reward propagates back
Watch the map. At low γ only cells beside the goal look valuable — the shading barely reaches the start. As γ rises, that value propagates backwards one cell at a time until the whole grid knows how to reach the goal.
05 / The Equation Behind The Shading

Value Iteration

Those numbers in the grid were not learned by trial and error. They were computed directly, by sweeping the same update over and over until it stops changing:

Bellman optimality equationV(s)←max⁡a[R(s,a)+γ∑s′P(s′∣s,a) V(s′)]V(s) \leftarrow \max_{a} \Big[ R(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V(s') \Big]

Read it in words: the value of a state is the best immediate reward available, plus the discounted value of wherever that action lands you. The definition is recursive — value is expressed in terms of value — and that is exactly why iteration works. Start every cell at zero and sweep.

After one sweep, only cells adjacent to the goal have picked up its reward. After two, their neighbours have. Value propagates backward through the grid one ring at a time, which is precisely what the γ slider above lets you watch — a low discount stops the wave early; a high one carries it all the way to the start.

Then the policy is trivialπ∗(s)=arg⁡max⁡a[R(s,a)+γ∑s′P(s′∣s,a) V(s′)]\pi^*(s) = \arg\max_a \Big[ R(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V(s') \Big]

Once you know how good every state is, the best move is just the one leading to the best next state. That is the greedy walk the dot is running.

Two ways to get there
Model-based
You know the transition probabilities, so you can iterate the equation directly — what this page does. Needs a model of the world.
Model-free
You do not know the transitions, so you estimate values from experience instead — Q-learning and its relatives. Needs only the ability to act and observe.
The same surprise

Q-learning nudges its estimate by the gap between what it expected and what it got — the identical prediction-error move as the bandit's running average — only now the target is reward plus the value of the next state.

06 / Where This Leads

From A Grid To Everything Else

The 4×4 grid is where RL is tractable and honest: you can compute the true values and see the agent get it right. Real problems drop both guarantees. State spaces become enormous or continuous, and the transition model is unavailable.

The classical answer is to stop storing a value per state and instead approximate the value function with something that generalises — which is where this archive closes its loop. Topic 07 built a function approximator that learns from a gradient; swap the supervised loss for the Bellman error and you have deep reinforcement learning.

That combination is powerful and notoriously unstable. The targets move as the network updates, the data is heavily correlated because consecutive states resemble each other, and the exploration/exploitation tension from the bandit never goes away. Most of the engineering in modern RL exists to manage exactly those three problems.

Reward design deserves its own warning. The agent optimises precisely the number you wrote down — not the outcome you intended. Specify it carelessly and it will find the technically-correct, practically-wrong behaviour with unfailing enthusiasm.

Takeaways
  • • No answer key — only consequences, often delayed far from the decision that caused them.
  • • An MDP is states, actions, rewards, transitions and a discount; the policy is what you learn.
  • • Exploration buys information; exploitation spends it. You need both, balanced.
  • • Every algorithm here learns from a prediction error — the same idea as the bandit's running average.
  • • You get exactly the reward you specify, which is rarely exactly the behaviour you wanted.

Adapted from MLU-Explain (Amazon, CC BY-SA 4.0).