Introduction to Robot Learning · Lecture 5 of 25 · CMU 16-831

MDP Basics & Imitation Learning, Part 1

The six-symbol language that every RL algorithm is written in — and the most tempting wrong idea in robotics: just copy the expert. We build the MDP from zero, then watch behavior cloning fall off a cliff.

Prerequisites: basic vectors + a little Python. Lecture 1's "compounding error" helps but isn't required.
10
Chapters
5
Simulations
3
Code Labs

Chapter 0: A Robot in a Hallway

You're a robot at one end of a narrow hallway. The exit — a charging dock — is at the far end. Every step costs you a little battery. Reaching the dock pays off big. At each tile you face the same plain question: which way do I move?

That sounds trivial until you notice the trap. Moving toward the dock burns battery now, but the reward only arrives later. A short-sighted robot, hating the cost of every step, might just freeze in place to avoid spending anything — and never get paid. A far-sighted robot endures the small costs because it sees the big payoff at the end. The whole art of sequential decision-making lives in that tension: pay now, get rewarded later.

The one question this whole field asks. Given everything I've sensed, which action do I take now so that the total reward over the long run is as large as possible? Everything in this course — imitation learning, Q-learning, policy gradients — is a different answer to exactly this question. Today we install the vocabulary to even state it precisely.

Play with the hallway below. You pick how far-sighted the robot is — that's the discount, our first character. Watch how its total score changes. When the robot barely cares about the future, the distant dock looks worthless and the step costs dominate. When it cares a lot, the dock's payoff shines through and the journey is clearly worth it.

The hallway — how far-sighted should the robot be?

A 6-tile hallway. Each move costs −0.1 battery; the dock pays +1.0. Slide the discount: near 0 the robot is myopic (the far dock looks worthless); near 1 it is far-sighted (the dock is worth the trip).

Discount γ0.90

By the end of this lesson you'll be able to compute that score by hand, prove a value with a single backup, and understand why the obvious shortcut — just record an expert robot and copy it — is a much worse idea than it looks.

The hallway robot pays a small battery cost on every step but only collects the big reward at the very end. Why does the discount change whether it bothers to make the trip at all?

Chapter 1: The MDP Tuple — Six Characters

To state the hallway problem precisely we need a formal object. That object is the Markov Decision Process (MDP) — the mathematical container for "sequential decisions." It is just a tuple of pieces, and we'll define each one with an analogy before we ever use it.

SymbolNameAnalogy
sStateThe complete situation of the world right now — "where the robot stands, what it's holding." A snapshot.
aActionThe lever the robot pulls — "step left", "step right", a motor torque.
r(s,a)RewardA scorecard: one number saying how good doing a in s was. Bigger is better.
p(s′|s,a)TransitionThe world's rulebook (its dynamics): if I do a in s, where do I land next?
π(a|s)PolicyThe robot's habit — its decision rule mapping a state to an action. This is what we learn.
γDiscountA patience dial in [0,1): how much a reward later is worth compared to one now.

Formally, the instructor writes an MDP as the tuple (S, A, p, r) — the state space S (all possible states), the action space A (all possible actions), the transition p, and the reward r. The discount γ rides along in the objective. The goal is always the same: learn a policy π(a|s).

Why "Markov"? — the amnesia trick

The "M" is load-bearing. The Markov property says: the next state depends only on the current state and action, not on the entire history of how you got here. In symbols, the transition is p(s′ | s, a) — just this state and this action, no past.

The amnesia trick. Markov means: once you know the state, you can throw the whole history away. A chess position tells you everything — you don't need the move list that produced it. This is why "state" is such a strong word: a good state is a sufficient summary of the past for predicting the future. Choosing what to put in the state is half the engineering of an MDP.

When you can't see the state — the POMDP

Real robots rarely see the true state s directly. A camera gives pixels, not "the mug is at coordinate (0.3, 0.5)." So we add an observation o, drawn from the true state through an observation model h(o|s). When the robot must act on observations instead of states, the tuple grows to (S, A, O, p, h, r) — a Partially Observed MDP (POMDP), and the policy becomes π(a|o). For most of today we'll assume the robot can see the state (a plain MDP); just know the observation layer is always lurking underneath.

The MDP loop — click each piece to see it light up

The five moving parts of one step. Click a button to highlight where it sits in the ring. Notice the arrow that closes the loop: the action feeds the transition, which produces the next state.

Click a piece above.
A robot's transition is written p(s′ | s, a) — with no dependence on st−1, st−2, or any earlier state. What is this assumption called, and what does it buy us?

Chapter 2: Policy and Trajectory

We have the pieces; now let's see them in motion. A policy π is the robot's brain: feed it the current state, it hands back an action. It comes in two flavors.

Now run the policy forward. Start in some state, the policy picks an action, the transition lands you in a new state, the policy picks again… The sequence it traces out is a trajectory (also called a rollout or episode):

τ = (s0, a0, s1, a1, s2, a2, …, sT)

Read it left to right: state, the action the policy chose there, the resulting next state, its action, and so on. A trajectory is one full life of the robot under one policy. Change the policy and you get different trajectories from the same start.

State st
where the robot is now
↓ feed into the policy
Policy π(a|st)
pick an action — this is what we learn
↓ emit
Action at
step left / right / torque
↓ collect reward r(st,at) and…
Transition st+1 ∼ p(·|st,at)
the world's rulebook lands you somewhere new
↻ repeat from the new state
The horizon. If the episode runs for a fixed number of steps T, we call it finite-horizon. If it can run forever (T = ∞), it's infinite-horizon — and that's exactly when the discount γ earns its keep, by keeping an endless sum of rewards from blowing up. We'll see that in the next chapter.

A stochastic policy in tile 3 outputs: right with probability 0.8, left with 0.2. Two robots start identically in tile 3 and run this same policy. What can you say about their trajectories?

Chapter 3: The Objective — Discounted Return

A policy traces a trajectory, and along that trajectory the robot collects a reward at every step. Add them all up and you get the return — the single number we actually want to make large.

But we don't add them equally. We bring back the discount γ from Chapter 0: a reward arriving t steps in the future is multiplied by γt. The total is the discounted return:

G = r0 + γ r1 + γ² r2 + γ³ r3 + … = ∑t γt rt

The goal of reinforcement learning and control, stated in full: find the policy that maximizes the expected discounted return. "Expected" because transitions and the policy can be random, so we average over all the trajectories the policy could produce.

Why discount at all? Two reasons. (1) Math: with γ < 1, an infinite sum of bounded rewards stays finite instead of exploding to infinity. (2) Meaning: γ encodes "a reward now beats the same reward later" — the exact reason a dollar today beats a dollar next year. A robot with γ near 0 is impatient and myopic; near 1 it is patient and far-sighted.

Compute a return by hand

Back to the hallway. Say the robot takes 3 steps (each costing −0.1) and then reaches the dock (+1.0). The reward sequence is r = (−0.1, −0.1, −0.1, +1.0). With γ = 0.9, weight each reward by γt:

G = −0.1 · (0.9)0 − 0.1 · (0.9)1 − 0.1 · (0.9)2 + 1.0 · (0.9)3
G = −0.1 − 0.09 − 0.081 + 0.729 = 0.458

Look closely at that last term. The dock's +1.0, arriving 3 steps away, is "worth" only 0.729 today — that's exactly 0.9³. The near-term costs bite at nearly full strength; the far-off payoff is shrunk by patience. That arithmetic is the pay-now-rewarded-later tension, made into a number — and it's the objective every RL algorithm in this course tries to maximize.

In an infinite-horizon problem (T = ∞), why is the discount γ < 1 mathematically necessary, not just a modeling choice?

Chapter 4: Value Functions — "How Good Is This Spot?"

The return G scores one whole trajectory. But a robot standing in a particular state wants a more useful number: "From right here, following my policy, how much reward should I expect to collect over the rest of my life?" That number is the value of the state.

Vπ(s) = expected discounted return, starting in state s and following policy π thereafter

Read the superscript π carefully: value is always value-under-a-policy. The same tile is worth a lot under a good policy and very little under a bad one. There's no such thing as "the value of a state" in a vacuum — only the value of a state given how you'll behave from there on.

The analogy. Think of V(s) as a real-estate appraisal of each tile. A tile right next to the dock appraises high — little cost left, big payoff soon. A tile far from the dock appraises lower — many costly steps still ahead. The appraisal depends entirely on the neighborhood's pricing rules, i.e. your policy. Change how you'll act, and every appraisal updates.

The cousin: the action-value

There's a closely related quantity that asks a slightly different question: "If I take action a right now in state s, and then follow π forever after, what's my expected return?" That's the action-value or Q-function, Qπ(s,a). Where V scores a state, Q scores a state-action pair — it lets you compare "step left here" against "step right here." Q is the star of Lecture 9; today we just meet it.

Value heatmap — appraise every tile in the hallway

A 6-tile hallway, dock on the right. The number in each tile is V(s) under the "always step toward the dock" policy. Slide the discount and watch the appraisals shift: a patient robot values far tiles highly; a myopic one only values tiles near the dock.

Discount γ0.90

Notice the gradient: value climbs as you near the dock. That smooth uphill toward reward is the signal an RL agent secretly chases. But where do these numbers come from? They aren't guessed — they're computed, by the single most important equation in the field, which we build next.

Why does the value function carry a superscript π — Vπ(s) rather than just V(s)?

Chapter 5: The Bellman Idea — Value, One Step at a Time

Here's the trick that makes value computable. The value of a state is not a mysterious sum over all futures. It's just the immediate reward, plus the discounted value of wherever you land next. Reward now, plus a peek one step ahead. That self-referential rule is the Bellman equation.

For a deterministic policy in a deterministic world (one action, one next state s′), it reads simply:

Vπ(s) = r(s, π(s)) + γ · Vπ(s′)

where s′ is the state you transition into. When the world is random, you average the future term over where you might land: V(s) = r + γ · (expected V of the next state). The shape is the same — this step's reward, plus a discounted look at the rest.

Why this is magical. It turns one impossible computation (sum over all infinite futures) into one easy computation (look one step ahead) — as long as you already know the neighbor's value. Of course you don't, at first. So you guess all values, then repeatedly apply the rule, letting each state pull its number from its neighbor. Each pass is a backup. The values flow backward from the reward and settle into the truth. That iterative process is called policy evaluation.

One backup, by hand

Take a tiny 3-state chain: tile A → tile B → DOCK. The policy always steps right. Each step costs r = −0.1; arriving at the dock pays +1.0; the dock is terminal (value 0 once you're done). Use γ = 0.9. Work it backward:

That's it — two Bellman backups and the whole chain is appraised. Notice we computed V(B) first, then fed it into V(A). The information traveled backward from the reward. Now let's do it in code for a longer hallway, sweeping until the values stop moving (a fixed point).

Bellman backup stepper — watch values propagate one sweep at a time

Press Backup to apply one sweep of the Bellman rule across the hallway. Watch the reward at the dock flow leftward, one tile per sweep, until the appraisals stop moving.

All values start at 0. Press Backup.
For tile A → B → DOCK with step cost −0.1, dock reward +1.0, γ = 0.9, and a terminal dock (value 0), we found V(B) = 1.0. What is V(A), via one Bellman backup?

Chapter 6: Behavior Cloning — Just Copy the Expert?

We now have a precise language for "good behavior": a policy that maximizes the discounted return. But finding that policy from reward is hard. So here's the tempting shortcut that opens the imitation-learning half of this lecture:

The idea behind imitation learning. Don't learn from reward at all. Instead, (1) collect expert data — a pile of (state, action) pairs from a human or a controller that already does the task well — and (2) train a function that maps states to the expert's actions. That's it. Fit a policy to copy the expert. This is called behavior cloning (BC).

This is not a toy. The 1989 ALVINN system at CMU drove a van by behavior cloning: camera image in, steering angle out, trained on a human driver's demonstrations. Today's Mobile ALOHA and Diffusion Policy use the very same recipe with far bigger models. The data flow is dead simple:

Expert demos
pairs (s0,a0), (s1,a1), … — image → steering angle
↓ treat as a labeled dataset (state = input, action = label)
Supervised loss
make πθ(s) match the expert's a — e.g. squared error on the action
↓ minimize by gradient descent
Cloned policy πθ
run it on the robot: at = πθ(st)
Misconception alert: BC ≠ RL. Behavior cloning never sees the reward function. It doesn't know the task is "reach the dock"; it only knows "in states like this, the expert did that." It's pure supervised learning — input states, label actions. The MDP's reward r and the whole machinery of value and Bellman from Chapters 3–5 play no role in BC. That's its great convenience — and, as we'll see, its great weakness.

Fit a cloned policy by least squares

Concretely: suppose the expert's action is roughly a linear function of the state (say, steering ≈ a weighted sum of lane-position features). Behavior cloning then becomes ordinary least-squares regression — find weights so π(s) = w·s matches the expert's actions on the demo data. Let's fit exactly that.

On the demo data, the clone is excellent — the points sit right on the diagonal. So behavior cloning does work… on the states the expert visited. The catastrophe is what happens on the states the expert didn't. That's the next chapter.

A teammate says "behavior cloning is just reinforcement learning with demos." What's the precise correction?

Chapter 7: It's NOT Standard Supervised Learning

Behavior cloning looks exactly like supervised learning: a labeled dataset, a loss, gradient descent. So why does the instructor insist, in bold, that it is NOT standard supervised learning? One assumption breaks — and it breaks everything.

The i.i.d. assumption

Supervised learning rests on a quiet promise: training and test data are i.i.d.independent and identically distributed, drawn from the same distribution. Under that promise there's a clean chain: low training error → good test performance. Classify image #5 wrong, and image #6 arrives unchanged, drawn from the same pool. The mistakes don't talk to each other.

The promise is broken in robotics. A policy's own actions steer which states it sees next (that's the closed loop). So the distribution of states at test time — the states the cloned policy actually drives itself into — is not the same as the distribution at training time — the states the expert visited. Training and test draw from different distributions. The i.i.d. promise is void, and "low training error" no longer guarantees anything.

Why the gap is fatal, not just annoying

The expert data contains only good behavior — a skilled driver basically never drifts toward the curb, so the dataset has almost no examples of being near the curb, let alone recovering from it. Now the cloned policy makes one small error and edges toward the curb. It is now in a state the expert never visited, so the clone never learned what to do there. It guesses, errs again, edges further out… and the small mistake cascades.

The instructor's vivid phrase: statistically, imitation learning is cliff walking. One wrong step and you're off the edge, in territory your training never covered. This is the same distribution shift / compounding error we met in Lecture 1 — but now we see exactly where it comes from: the violated i.i.d. assumption.

Watch a cloned policy fall off the cliff

The teal band is the states the expert demonstrated. The warm dot is the cloned policy. Raise the per-step error and press Run: once it leaves the demonstrated band, it's in states it never trained on — and it spirals away.

Per-step error ε 0.050
Ready.
The paradox the instructor flags. In reality we often can recover from mistakes — the picture isn't always as grim as the worst case. And here's the twist: imitation can work better if the demos contain mistakes and their corrections, because then the policy has seen how to get back on track. We'll lean on exactly that idea next chapter and in Lecture 6.

Precisely which assumption of standard supervised learning does behavior cloning violate, and why?

Chapter 8: Showcase — Measuring the Cliff (Quadratic Cost)

The instructor states a sharp result: for behavior cloning, the test-time cost is quadratic in the number of timesteps — it grows like ε·H², not the ε·H you'd expect from naive supervised reasoning. Let's not take it on faith. Let's measure it.

The logic, made simple: with per-step error ε, the policy first slips off-distribution after about 1/ε steps. From that moment it's on the cliff and stays lost for essentially the rest of the episode. So the expected number of "lost" steps grows with how much episode remains after the first slip — and summing that over a horizon of length H gives the famous H² shape.

The one fact that bends the curve. Naive supervised learning counts only the mistakes themselves — that's the linear ε·H. Reality is worse because once you make a mistake, you stay lost: every remaining step is now in unfamiliar territory. Encode that single fact and the cost curve bends upward into a quadratic.

The warm curve pulls away from the teal line and bends upward — the quadratic, measured. The longer the episode, the more disproportionately a tiny per-step error hurts. This is the headline result of the imitation-learning half of the lecture, and the whole reason Lecture 6 exists.

Your cloned policy errs 2% of the time per step. You extend episodes from 50 to 200 steps. Roughly how does the expected trajectory cost change, in the cliff-walking regime?

Chapter 9: Connections & Cheat Sheet

You've assembled the entire vocabulary of sequential decision-making and met imitation learning's central drama. Everything later in 16-831 either uses this MDP language or fights the cliff you just measured.

The lecture, in one ladder

MDP tuple (S, A, p, r)
the language: state, action, transition, reward, policy, discount.
↓ what do we want?
Maximize discounted return
G = ∑ γt rt; appraise states with Vπ, computed by Bellman backups.
↓ a shortcut: skip reward, copy a human
Behavior cloning
supervised fit of π to expert (state, action) pairs — ALVINN to Diffusion Policy.
↓ but the i.i.d. promise breaks…
Distribution shift → cliff walking
cost grows like ε·H²; one slip lands you off-distribution and lost.
↓ how to fix it? — Lecture 6
DAgger & better data
add corrections, augment data, put the expert back in the loop.
Cheat sheet — the vocabulary you now own.
  • MDP = tuple (S, A, p, r); add observations + h to get a POMDP (S, A, O, p, h, r).
  • Markov property = next state depends only on current (state, action); throw the history away.
  • Policy π(a|s) = the decision rule we learn; running it yields a trajectory (s0,a0,s1,…).
  • Discounted return G = ∑ γt rt; the goal is to maximize its expectation. γ < 1 keeps it finite and means "sooner beats later."
  • Value Vπ(s) = expected return from s under π; the Q-function Qπ(s,a) scores a state-action pair.
  • Bellman V(s) = r(s,π(s)) + γ V(s′); iterate it (policy evaluation) until a fixed point.
  • Behavior cloning = supervised fit of π to expert (s,a) pairs; no reward (BC ≠ RL).
  • Why BC breaks = the i.i.d. assumption fails; the policy steers its own state distribution → cliff walking → cost ≈ ε·H².

Where to go next on this site

This is Lecture 5 of the 16-831 series. The natural next step is Lecture 6 — Imitation Learning, Part 2, which fixes exactly the cliff you measured here (DAgger, data augmentation, powerful models). To go deeper on individual threads, these companion Gleams expand each idea in full:

The test of this lesson. Open the original Lecture 5 slides. You should now be able to explain, from memory: every symbol in the MDP tuple, why we discount, what a value function is and how one Bellman backup computes it, what behavior cloning is and why it's NOT standard supervised learning, and where the quadratic cost comes from. If you can, you're ready for Lecture 6.

"Statistically speaking, imitation learning is cliff walking — once you make one mistake, you are outside of the training distribution."