Reinforcement Learning — States, Returns and the Bellman Equation
Everything until now started with a dataset. Supervised learning had examples and answers; unsupervised learning had examples; recommenders had a matrix of ratings. Reinforcement learning has none of them. There is an agent, a world it can act in, and a number that occasionally arrives saying how things are going. Nobody ever says which action was correct.
That last part is the whole difficulty. A rover that drives left for three steps and then finds a +100 crater was not told "left" three times — it was told nothing, twice, and then told 100. Working out which of those decisions deserves the credit is what this subject is about.
In one line. The value of an action is the return it eventually leads to — the sum of future rewards, each discounted by $\gamma$ per step into the future. Write that value as $Q(s,a)$, and it satisfies a recursion, $Q(s,a) = R(s) + \gamma \max_{a'} Q(s',a')$: one step of reward now, plus the discounted value of behaving optimally from wherever you land. The best policy is then just $\pi(s) = \arg\max_a Q(s,a)$.
What makes it a different problem
Three things are new, and each breaks an assumption used everywhere else in these notes:
- No labels, only rewards. The feedback is a scalar that says how well things went, not what should have been done. You cannot compute an error per action, because there is no target action.
- The feedback is late. A reward may arrive many steps after the decision that earned it. This is the credit assignment problem, and the return in §3 is the machinery for it.
- The data depends on the policy. An agent that never drives left never sees what is on the left. The dataset is not given; it is generated by the thing being trained, which is why exploration becomes a topic in its own right (the next note, §7).
The Mars rover
States, actions, rewards
The example is a rover on a six-cell strip of ground:
| State | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Description | left crater terminal | flat | flat | start | flat | right crater terminal |
| Reward $R(s)$ | +100 | 0 | 0 | 0 | 0 | +40 |
At every state the rover picks one of two actions: left ($\leftarrow$) or right ($\rightarrow$). Each action moves it one cell.
One step, written down
Every interaction is recorded as the same four-part tuple, and this is the only data reinforcement learning ever gets:
That example says: from state 4 the rover went left, received nothing, and is now in state 3.
The return
Definition
The return $G$ is the total future reward, with each step discounted by a factor $\gamma$ per unit of delay:
Discounting is not only impatience. It also keeps the sum finite when an episode might never end, and it encodes a real preference: a reward now is worth more than the same reward after a hundred risky steps.
What the discount factor does
Start at state 4 and drive left. The +100 arrives three steps later, so the return is $\gamma^3 \times 100$:
$$ \gamma = 0.9: \quad G = 0.9^3 \times 100 = 72.9 \qquad\qquad \gamma = 0.5: \quad G = 0.5^3 \times 100 = 12.5 . $$
Same path, same reward, and a factor of nearly six between them.
Returns from every state
With $\gamma = 0.5$ fixed, the return depends on where you start and which way you commit to going:
| State | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Reward $R(s)$ | 100 | 0 | 0 | 0 | 0 | 40 |
| Return, always $\leftarrow$ | 100 | 50 | 25 | 12.5 | 6.25 | 40 |
| Return, always $\rightarrow$ | 100 | 2.5 | 5 | 10 | 20 | 40 |
| Return, mixed ($\leftarrow$ then $\rightarrow$) | 100 | 50 | 25 | 12.5 | 20 | 40 |
The mixed row is the interesting one: the best behaviour is not the same everywhere. From state 5 it is right; from state 4, one cell away, it is left. Which is exactly why the thing being learned has to be a function of the state.
Policies
A policy $\pi$ is that function — it maps each state to the action to take there:
For the rover, the policy that the return table above recommends is $\pi(2) = \pi(3) = \pi(4) = {\leftarrow}$ and $\pi(5) = {\rightarrow}$. Drawn on the strip it is just an arrow in each cell — a complete description of the agent's behaviour, with no memory of how it got there.
The Markov decision process
The loop
Everything above is an instance of one formal object, the Markov decision process. Each step: the agent is in state $s_t$, its policy picks $a_t = \pi(s_t)$, and the environment answers with a reward $R_t$ and a next state $s_{t+1}$.
The word Markov is the assumption that makes this tractable: the future depends only on the present state and action, not on the path taken to reach it. Choosing a state representation that actually has this property is a design decision, and the next note shows what it looks like when the state is a vector of positions and velocities.
Three domains
| Domain | States | Actions | $\gamma$ | Rewards |
|---|---|---|---|---|
| Mars rover | 6 grid positions | move $\leftarrow$ or $\rightarrow$ | 0.5 | terminal 100 and 40, else 0 |
| Helicopter | position and attitude | control stick | 0.99 | $+1$ flying well, $-1000$ crash |
| Chess | pieces on the board | any legal move | 0.995 | $+1$ win, $0$ draw, $-1$ loss |
That $\gamma$ column is worth one picture, because the difference between $0.5$ and $0.995$ is not a matter of degree — it is the difference between an agent that can see two steps and one that can see two hundred:
The state-action value function
What $Q$ means
Here is the central definition, and it is worth reading twice because of the odd conditional in it:
The "take $a$ once, then be optimal" construction is what makes $Q$ useful. It lets you compare actions from the same state on equal terms, and the comparison immediately gives the policy.
Working it out on the chain
Because all intermediate rewards are zero, $V^\ast(s)$ is just the best discounted terminal reward reachable from $s$:
$$ V^\ast(1) = 100, \quad V^\ast(2) = 0.5 \times 100 = 50, \quad V^\ast(3) = 0.5^2 \times 100 = 25, $$ $$ V^\ast(4) = 0.5^3 \times 100 = 12.5, \quad V^\ast(5) = 0.5 \times 40 = 20, \quad V^\ast(6) = 40 . $$
And each $Q^\ast$ is one step of that: move, then collect the value of where you land.
$$ Q^\ast(2, \rightarrow) = 0 + \gamma\, V^\ast(3) = 0.5 \times 25 = 12.5, \qquad Q^\ast(2, \leftarrow) = 0 + \gamma\, V^\ast(1) = 0.5 \times 100 = 50 . $$
Reading the policy off $Q$
| State | $R(s)$ | $Q^\ast(s, \leftarrow)$ | $Q^\ast(s, \rightarrow)$ | $V^\ast(s)$ | greedy action |
|---|---|---|---|---|---|
| 1 | 100 | 100 | 100 | 100 | terminal |
| 2 | 0 | 50 | 12.5 | 50 | $\leftarrow$ |
| 3 | 0 | 25 | 6.25 | 25 | $\leftarrow$ |
| 4 | 0 | 12.5 | 10 | 12.5 | $\leftarrow$ |
| 5 | 0 | 6.25 | 20 | 20 | $\rightarrow$ |
| 6 | 40 | 40 | 40 | 40 | terminal |
This is the payoff of the whole construction. Once $Q^\ast$ is known, the optimal policy needs no search, no planning and no model of the world — pick the larger number:
$$ \pi^\ast(s) = \arg\max_a Q^\ast(s,a). $$
Everything in the next note is an attempt to get $Q$ for problems where this table cannot be written down.
The Bellman equation
The equation
The pattern used in §6.2 — one step of reward, then the value of the next state — is general, and it is the equation the entire subject is built on:
where $s'$ is the state reached from $s$ by taking $a$. For terminal states there is no next state and $Q^\ast(s,a) = R(s)$.
Why one step is enough
It looks as though this can only see one step ahead, and the whole point was that rewards arrive late. The resolution is that $\max_{a'} Q^\ast(s',a')$ is itself defined by the same equation, so unrolling it gives
$$ Q^\ast(s,a) = R(s) + \gamma R(s') + \gamma^2 R(s'') + \cdots $$
— the full discounted return, reconstructed one link at a time. The recursion has folded an infinite sum into a relation between neighbours, and that is what makes it computable.
Check it on a row. Take $Q^\ast(4, \leftarrow)$. Bellman says $R(4) + \gamma \max_{a'} Q^\ast(3, a') = 0 + 0.5 \times 25 = 12.5$, which is what the table says. The 25 already contains the discounted 100 two steps further on.
When actions do not always work
Expected return
Real actuators slip. Suppose each command succeeds with probability $1 - p$ and sends the rover the wrong way with probability $p = 0.1$. Now the return is a random variable, and what gets maximised is its expectation:
Solving the chain with missteps
With the optimal actions being left at 2, 3, 4 and right at 5, the four unknown values satisfy a small linear system:
$$ \begin{aligned} V_2 &= 0.5\left(0.9 \times 100 + 0.1 V_3\right) = 45 + 0.05 V_3, & V_3 &= 0.5\left(0.9 V_2 + 0.1 V_4\right) = 0.45 V_2 + 0.05 V_4, \[4pt] V_4 &= 0.5\left(0.9 V_3 + 0.1 V_5\right) = 0.45 V_3 + 0.05 V_5, & V_5 &= 0.5\left(0.9 \times 40 + 0.1 V_4\right) = 18 + 0.05 V_4 . \end{aligned} $$
Solving gives the values, and from them every $Q$:
| State | $Q(s,\leftarrow)$ | $Q(s,\rightarrow)$ | $V(s)$ | greedy | margin |
|---|---|---|---|---|---|
| 1 | 100 | 100 | 100 | terminal | — |
| 2 | 46.06 | 14.56 | 46.06 | $\leftarrow$ | 31.50 |
| 3 | 21.25 | 7.02 | 21.25 | $\leftarrow$ | 14.23 |
| 4 | 10.49 | 9.40 | 10.49 | $\leftarrow$ | 1.09 |
| 5 | 6.72 | 18.52 | 18.52 | $\rightarrow$ | 11.80 |
| 6 | 40 | 40 | 40 | terminal | — |
What the numbers say
State 4 is where the interesting thing happens. In the reliable world the choice was $12.5$ against $10$ — a margin of $2.5$, or a fifth of the winning value. With a ten percent misstep rate it is $10.49$ against $9.40$, a margin of $1.09$: the decision has become nearly a coin flip. Unreliability hurts the long journey to the +100 more than the short one to the +40, because the long path has three chances to slip instead of two.
Sweeping the misstep rate rather than fixing it at ten percent shows where that is heading:
That is a general lesson, not an artefact of this toy. Adding noise to the dynamics systematically favours the near, safe reward over the distant, larger one — and it does so without changing a single reward value. What Figure 6 adds is that the effect has a threshold: the preference does not merely weaken, it eventually reverses. A rover reliable enough is worth sending across the map for the big prize; the same rover with a one-in-four chance of stepping wrong should take the small one nearby.
Where this goes next
Everything above assumed the state was one of six numbered cells, so $Q$ could be written as a table with twelve entries. That assumption dies immediately in any real problem:
- Continuous states. A vehicle's state is a vector of positions, angles and velocities. There are infinitely many of them, so there is no table — $Q$ has to become a function, and a neural network is the obvious candidate.
- Learning $Q$ instead of solving for it. The chain above was solved with algebra because the rewards and transitions were known. An agent that must discover them can only sample transitions and fit — which turns the Bellman equation from an identity into a regression target.
- Exploration. An agent following $\arg\max_a Q(s,a)$ from the start will never try the action it currently underrates, and so will never learn that it was wrong.
All three are the subject of the next note, which builds a deep Q-network for the lunar lander.
Backwards from here: linear regression has the gradient descent that fits the network there, and model development has the diagnostics that still apply once the network exists.