Machine learningLoading progress…

MDPs and returns

Distinguish sequential decision-making, temporal prediction, recommendation and causal estimation.

From one choice to a sequence of states

A Markov decision process adds what a bandit lacks: a state. At each step the agent sees a state, picks an action, receives a reward and lands in a new state. The Markov part means the next state and reward depend only on the current state and action, not on the whole history.

Actions now have consequences that last. Walking toward a distant goal may cost a little reward every step, and the payoff only arrives at the end, so you must compare rewards that arrive at different times.

Return and discounting

The return is the total reward collected from a time step onward. With a discount factor gamma between 0 and 1, later rewards count for less:

G = r0 + gamma * r1 + gamma^2 * r2 + ...

rewards 1 then 2, gamma = 0.5:   G = 1 + 0.5 * 2 = 2

Gamma near 0 makes the agent short-sighted, since only the immediate reward matters. Gamma near 1 makes it patient. Discounting also keeps the sum finite for tasks that could go on forever.

+1wall-1S
Presets:
gamma0.9
tmovereward rgamma^tgamma^t rrunning G
0↑-0.041-0.04-0.04
1↑-0.040.9-0.036-0.076
2→-0.040.81-0.0324-0.1084
3→-0.040.729-0.0292-0.1376
4→10.65610.65610.5185
Return G = 0.5185 (the episode ended) · with gamma 1: 0.84 · with gamma 0: -0.04
Walk a path on the grid and watch the return build up reward by reward. Change gamma and compare two paths.

Predict first. With gamma = 0, which rewards does the return include?

Value iteration: planning when you know the rules

If the transitions and rewards are known, you can compute how good each state is. The value of a state is the best return reachable from it. Value iteration repeatedly applies one rule to every state until nothing changes:

V(s) = max over actions a of  sum over outcomes of  prob * (reward + gamma * V(next state))
terminal states keep V = 0
000+10wall0-10S000
sweep 0
gamma0.9step reward-0.04

Click a non-terminal cell to see how its next value is built from the current numbers. Green arrows show the best moves given the current values. Terminal cells are worth 0 afterwards because the episode ends.

Press Sweep +1 to run one sweep, or run to convergence. Values spread outward from the goal like ripples, and the arrows show the best action in each state.
Chapter context

These topics change the structure of the learning problem. Their first lesson is to identify what a prediction or action is allowed to depend on and how feedback is produced. Each section is a bridge to a larger specialist subject.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

Gt=∑k≥0γkrt+k+1,Q(s,a)←Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)].G_t=\sum_{k\ge0}\gamma^k r_{t+k+1},\quad Q(s,a)\leftarrow Q(s,a)+\alpha[r+\gamma\max_{a'}Q(s',a')-Q(s,a)].

For a terminal transition the continuation term is zero. A policy-gradient estimator uses a term proportional to ∇log⁡πθ(a∣s)\nabla\log\pi_\theta(a\mid s) times a return or advantage estimate.

Worked example

For a nonterminal transition, current Q=2, reward=1, discount=0.9 and best next Q=4 give target 1+0.9×4=4.6. With alpha=0.5, updated Q is 2+0.5×(4.6−2)=3.3. Reusing the same target for a terminal transition would be incorrect.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
q, reward, discount, next_q, rate = 2, 1, .9, 4, .5
target = reward + discount*next_q
assert isclose(target, 4.6)
assert isclose(q+rate*(target-q), 3.3)
terminal_target = reward
assert q+rate*(terminal_target-q) == 1.5

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Prediction is not intervention. Randomly splitting overlapping time windows can leak future information. An environment reward is a modeling choice and may omit important real-world consequences.