RL Foundations
Rewards, returns, value functions and exploration.
A gridworld agent stands one step from an exit worth and five safe steps from one worth . Give it the map and value iteration sends it to the every time; make it learn from experience and it may never discover the at all. This lesson builds the core of reinforcement learning, from returns and value functions through Bellman equations, occupancy measures and sample-based evaluation, to the exploration problem that makes learning harder than planning.
What you’ll learn
- Formalize the RL problem: the interaction loop, episodic and continuing returns, discounting as a random horizon, and the objective .
- Define , and the advantage , derive the Bellman expectation and optimality equations for stochastic policies, and solve policy evaluation as a linear system.
- Write the return as a linear function of the discounted occupancy measure, derive the flow constraints, and use the resulting linear program to explain deterministic optimal policies and constrained problems.
- Compare Monte Carlo and bootstrapped evaluation, and model-based and model-free learning, through bias, variance and certainty equivalence.
- Explain why exploration in MDPs is harder than in bandits, and compute how fast undirected exploration fails on a concrete gridworld.
The reinforcement learning problem
In reinforcement learning an agent interacts with an environment in discrete time. At step it observes the state , picks an action from its policy, and receives a reward and the next state . The model is a finite Markov decision process , as in Decisions: Utility, MDPs & Bandits, with expected reward and start distribution . What makes the problem reinforcement learning rather than planning is that and are unknown: the agent sees only the transitions it generates.
A policy and the environment together define a distribution over trajectories ,
Three features separate this from supervised prediction. Feedback is evaluative: a reward, never the correct action. It is often delayed, so credit must be assigned across many steps. And the data are generated by the policy itself, so changing the policy changes what the agent sees next.
Let the episode stop after each step independently with probability . Its length is geometric, , and independent of everything else. The undiscounted reward collected before the stop is
Take expectations. Independence lets each indicator's expectation factor out (bounded rewards justify exchanging sum and expectation):
Maximizing the discounted return is therefore the same as maximizing total reward under a constant hazard of the episode ending. The expected lifetime is the effective horizon, 100 steps for :
Value functions and the Bellman equations
The value function of is and its action-value function is . They are linked by , and their difference is the advantage : how much better is than 's average action in . By construction . Advantages drive policy improvement here and policy gradients in Policy Gradients & Actor–Critic.
Condition the recursion on , then on the next state and action. By the Markov property, the expected return from onward is , whatever happened before:
Average over . With and , a fixed policy turns the MDP into a Markov reward process with a linear value equation:
is a stochastic matrix, so has spectral radius at most and the Neumann series converges (the argument of Decisions: Utility, MDPs & Bandits). Policy evaluation is one linear solve, , or repeated application of the affine map :
Optimality replaces the average over by a maximum. is the unique fixed point of the Bellman optimality operator , and every policy that is greedy for is optimal:
A policy induces this chain: in state the agent receives reward 1 and moves to ; in it receives 0 and moves to or stays in with probability each. With , what is ?
Show the answer
B.
The Bellman equations are and . The second gives , so , hence and . That is for a system.
At noise 0 the exit is one step from the start and the exit five steps away; all other rewards are 0. Above what discount factor does the optimal policy cross the bridge?
Show the answer
0.56
Exiting west is worth and crossing , since an exit's reward counts on the step that enters it. Crossing wins when , that is . In the widget at noise 0, still exits west and crosses. A smaller is a shorter effective horizon, over which a distant reward fades below a near one.
Occupancy measures: the linear-programming view
Expand by linearity and group the terms by the state–action pair visited:
Which are possible? The state at comes from the pair at : . Multiply by , sum over , and add the term :
These flow constraints, with , say that the discounted mass leaving equals the mass injected there at the start plus the discounted mass flowing in. Planning is therefore a linear program over a polytope:
Proof sketch. The first claim is the derivation above, which used only that depends on . For the converse, let , so (define arbitrarily where ). The flow constraints become , whose unique solution is 's own discounted state distribution. Hence .
Three consequences follow. Deterministic optimal policies exist: a linear objective over a polytope is maximized at a vertex, and the vertices are the occupancy measures of deterministic policies. Values are prices: the dual linear program has one variable per state and constraints , the Bellman LP of MDPs: Value & Policy Iteration. Constraints are cheap: a budget on an expected cost keeps the problem a linear program, the constrained MDP (Altman, 1999), but its optimum need not be a vertex. The view also shows why RL data are not i.i.d. for long: moves whenever does.
Think first In a constrained MDP the best policy can need to randomize, which never happens without constraints. Can you build the smallest example?
One state with a self-loop and two actions: action 1 earns reward 1 at cost 1, action 2 earns 0 at cost 0, and the budget is . The flow constraint forces , so the deterministic policies get reward 1 at cost 1 (infeasible) or reward 0 at cost 0. Playing action 1 with probability meets the budget exactly and earns half the maximum. The budget slices the polytope, and the optimum sits on an edge between two vertices. In general a basic optimal solution has at most positive entries for constraints, so when every state is visited, some optimal policy randomizes in at most states.
Learning from samples: Monte Carlo, bootstrapping, models
Without and , values must be estimated from experience, and two ideas compete. Monte Carlo (MC) methods average complete observed returns: is the sample mean of over visits to . Bootstrapping methods update a guess toward another guess: temporal-difference learning moves toward the one-step target , which is the dynamic-programming backup with a sampled transition in place of the expectation.
function MC-EVALUATE(π, episodes): # first-visit Monte Carlo
N(s) ← 0; V(s) ← 0 for every state s
for each episode S0, A0, R1, ..., ST generated by π:
G ← 0
for t = T−1 down to 0:
G ← R(t+1) + γ · G # return from step t
if S(t) does not occur among S0, ..., S(t−1):
N(S(t)) ← N(S(t)) + 1
V(S(t)) ← V(S(t)) + (G − V(S(t))) / N(S(t))
return V
function TD0-EVALUATE(π, α): # bootstraps, updates online
for each transition (S, A, R, S′) generated by π:
V(S) ← V(S) + α · (R + γ · V(S′) − V(S)) # V(terminal) = 0First-visit MC averages i.i.d. returns, so it is unbiased and converges by the law of large numbers, but it must wait for episodes to end. TD(0) updates after every transition and works in continuing tasks; its convergence under Robbins–Monro step sizes is proved in Temporal-Difference Learning & Q-Learning.
| Method | Model? | Target for | Bias | Variance |
|---|---|---|---|---|
| Dynamic programming | needed | , averaged over | none: an exact expectation | none |
| Monte Carlo | not needed | none | high: the sum of many random rewards and transitions | |
| TD(0) | not needed | yes, while is wrong | low: one random transition |
Eight episodes are observed, undiscounted: one goes with rewards 0 then 0; six start in and end with reward 1; one starts in and ends with reward 0. Batch Monte Carlo and batch TD(0) both estimate . What do they estimate for ?
- MC gives and TD gives
- Both give
- MC gives and TD gives
- Both give
Show the answer
C. MC gives and TD gives
MC averages the returns actually observed from : one episode, return 0. Batch TD converges to the values of the maximum-likelihood Markov model of the data, in which always moves to with reward 0, so : the certainty-equivalence estimate (Sutton & Barto, Example 6.4). If the process really is Markov, TD's answer is the better one, because it uses the seven other episodes' evidence about ; MC fits the observed returns and ignores that structure.
The check reveals a third route. A model-based learner counts transitions, , estimates , and plans in the estimated MDP with dynamic programming; batch TD reaches this certainty-equivalence answer without storing the model. Model-based methods extract more from each sample, and Dyna (Sutton, 1990) interleaves real updates with updates on simulated transitions. Their risk is model error, which compounds over the effective horizon: the simulation lemma in Bellman Equations & Contraction bounds every policy's value error by when each transition distribution is off by in . Model-free methods such as Q-learning and policy gradients never model the observations, which is why they scaled to raw pixels first; learned latent models, as in MuZero and Dreamer (World Models & Video Generation), narrow the gap.
Exploration: why learning is harder than planning
A bandit learner must try arms to learn about them, but each try is a single step. In an MDP, information can be many steps away: to learn what the far side of the bridge pays, the agent must first walk across, choosing actions that look worthless by its current estimates. This is the problem of deep exploration, and the exploration strategies of Decisions: Utility, MDPs & Bandits do not solve it on their own.
Take a uniformly random agent at noise 0: the widget at , or any agent before it has seen a reward. Let be the probability of reaching the before any other exit from the cell steps east of the exit; the start is and the is . Each move goes east or west with probability and falls with probability :
The characteristic equation has roots , where and so . The boundary conditions select the sinh solution:
A random episode crosses about once in 780 tries. With the far exit steps from the near one, : undirected exploration costs exponentially many episodes in the distance to the reward. An -greedy agent that already prefers the does worse still, since it leaves the start eastward with probability only .
What works instead is directed exploration. Optimism in the face of uncertainty treats poorly known state–action pairs as if they were excellent, so that the greedy policy travels to them on purpose. R-MAX (Brafman & Tennenholtz, 2002) plans in a model in which every insufficiently tried pair leads to a fictitious state paying the maximum reward, and UCRL2 plans with confidence sets, reaching regret in an MDP with diameter . Posterior sampling generalizes Thompson sampling: draw an MDP from the posterior and act optimally for it for a whole episode. In deep RL the same ideas return as count-based and pseudo-count bonuses and as curiosity rewards for prediction error.
Key takeaways
- RL maximizes from sampled interaction, without knowing or ; discounting is equivalent to a random horizon that ends with probability per step.
- , and satisfy linear Bellman equations, ; optimality replaces the average over actions by a max, and both operators are -contractions.
- The return is linear in the discounted occupancy measure, which the flow constraints characterize exactly. Planning is a linear program whose vertices are deterministic policies and whose dual variables are values; extra constraints can make randomization necessary.
- Monte Carlo targets are unbiased but noisy; TD targets bootstrap, trading bias for variance, and batch TD computes the certainty-equivalence estimate of a model-based learner.
- Undirected exploration can need exponentially many episodes, about 780 for a random walk across a five-cell bridge; optimism and posterior sampling explore on purpose.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
For any policy and state , what is ?
- It depends on the rewards
Show the answer
B.
, so the -weighted advantages sum to . Some actions are better than the policy's average and some worse, and a policy can be improved exactly where some action has positive advantage.
An episode stops after each step with probability , independently of everything else. Maximizing the expected total (undiscounted) reward is equivalent to maximizing the discounted return with which ?
Show the answer
D.
Reward is collected only if the episode survives steps, which has probability , so the expected total is : the discounted return with , whose effective horizon is the expected lifetime of 50 steps.
Why does the linear-programming view guarantee that some optimal policy is deterministic?
- Every occupancy measure puts all its mass on one action per state
- The return is linear in the occupancy measure, so it is maximized at a vertex of the occupancy polytope, and vertices are the occupancy measures of deterministic policies
- Value iteration only ever outputs deterministic policies
- Randomizing always lowers the expected return
Show the answer
B. The return is linear in the occupancy measure, so it is maximized at a vertex of the occupancy polytope, and vertices are the occupancy measures of deterministic policies
The flow constraints and cut out a polytope, is linear on it, and a linear function attains its maximum at a vertex, which puts mass on one action in each visited state. Add one more linear constraint, as in a constrained MDP, and the optimum can move to an edge between vertices: a randomized policy.
Which statement about the Monte Carlo target and the TD(0) target for estimating is correct?
- Both are unbiased for any current estimate
- The TD target is unbiased and is biased
- is unbiased but has higher variance; the TD target has lower variance but is biased while is inaccurate
- They have the same variance, since both come from the same episode
Show the answer
C. is unbiased but has higher variance; the TD target has lower variance but is biased while is inaccurate
by definition, but accumulates the randomness of every later step. The TD target has one random transition, and , which equals only when the estimate is right on average over the next state.
On the noiseless bridge, value iteration finds the crossing policy in a few sweeps, yet -greedy Q-learning with almost never does. What is the main reason?
- A Q-table cannot represent the crossing policy
- The is reached only through a specific multi-step sequence that the agent's estimates give it no reason to try, and random steps rarely string it together
- Q-learning diverges at
- The learning rate is too small
Show the answer
B. The is reached only through a specific multi-step sequence that the agent's estimates give it no reason to try, and random steps rarely string it together
After the has been found, the greedy action at the start is west, so only an exploratory step leads east, and several more steps must then keep going east without falling. Undirected exploration finds such sequences with probability exponentially small in their length, about per episode even for a purely random agent here. Optimism or posterior sampling would send the agent to the untried far side deliberately.
Go deeper
- Bellman Equations & Contraction · Why value iteration converges.
- Dynamic Programming in RL · Policy evaluation, improvement and iteration.
- Exploration Strategies · ε-greedy, optimism, curiosity and count bonuses.
Further reading
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 3–6 (MIT Press, 2nd ed., 2018).
- M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, ch. 6 (Wiley, 1994).
- C. Szepesvári, Algorithms for Reinforcement Learning (Morgan & Claypool, 2010).
- E. Altman, Constrained Markov Decision Processes (Chapman & Hall/CRC, 1999).