Lesson 2
RL 5-7
These are my original lecture notes, kept as they were. They're being rebuilt into the Reinforcement learning course.
Foundational Concepts Review
Before diving into the lectures, make sure you understand these core RL concepts:
Markov Decision Process (MDP)
An MDP is defined by the tuple :
- : Set of states
- : Set of actions
- : Transition probability
- or : Reward function
- : Discount factor
Value Functions
- State Value Function: = expected return starting from state and following policy
- Action Value Function: = expected return starting from state , taking action , then following
Bellman Equations
The Bellman equation relates the value of a state to the values of successor states:
Key insight: This equation says "the value of taking action in state equals the immediate reward plus the discounted expected value of where you end up."
Lecture 5: Advanced Algorithmic Analysis (AAA)
This lecture covers three main algorithms for solving MDPs: Value Iteration (VI), Linear Programming (LP), and Policy Iteration (PI). It also establishes key theoretical properties about convergence and optimality.
Value Iteration Deep Dive
What is Value Iteration?
Value Iteration is an iterative algorithm that repeatedly applies the Bellman operator to converge to the optimal value function.
Bellman Operator : Given a value function , the Bellman operator produces a new value function:
Key Properties of Value Iteration
Property 1: Contraction Mapping
The Bellman operator is a contraction mapping with contraction factor :
What this means: Applying the Bellman operator brings any two value functions closer together by at least a factor of . This guarantees convergence to a unique fixed point.
Intuition: Think of it like two people walking toward the same destination. Each step, they get at least times closer. Eventually, they converge.
Property 2: K-Step Contraction
Applying the Bellman operator times has contraction factor :
Practical implication: Running 10 steps of VI is like running one step of "10-step VI" with much stronger contraction.
Convergence Results
Result 1: Greedy Policy Converges in Polynomial Time
There exists a finite , polynomial in:
- (number of states)
- (number of actions)
- Magnitude of rewards
- Bits of precision in transition probabilities
such that after iterations, the greedy policy with respect to is optimal.
Important distinction: The value function converges only in the limit, but the greedy policy becomes optimal in finite time.
Why does this work? Once you have enough precision in your Q-values to distinguish the best action from the second-best action in every state, the greedy policy will be correct. This gap exists because of the finite precision in the MDP specification (follows from Cramer's rule in linear algebra).
Result 2: Stopping Criterion
If consecutive value functions satisfy:
Then the greedy policy with respect to satisfies:
Why this matters: This gives us a practical stopping criterion! We can check if our value function has "stopped changing" and bound how far from optimal we are.
Intuition for the bound:
- The error at each step could compound over the effective horizon
- Effective horizon is
- The accounts for error in either direction
- The accounts for one-step delay
The Role of Gamma
| Small (e.g., 0.1) | Large (e.g., 0.99) |
|---|---|
| Short-sighted behavior | Far-sighted behavior |
| Easier to solve | Harder to solve |
| Faster convergence | Slower convergence |
| Effective horizon ≈ 1 | Effective horizon ≈ 100 |
Effective horizon: roughly tells you "how far into the future matters."
Trade-off: You want large enough to capture long-term consequences but not so large that computation becomes infeasible.
Linear Programming for MDPs
Why Linear Programming?
Value iteration is not polynomial in the true sense because convergence time depends on , which can be exponentially large in the bits needed to represent .
Linear Programming provides the only known truly polynomial time algorithm for solving MDPs.
The Challenge
We want to solve the Bellman equation:
Problem: The operator is non-linear!
The Key Insight: Max as Optimization
For scalars: is equivalent to:
- Minimize
- Subject to: , ,
The minimum satisfying all constraints equals the maximum of the values.
Primal LP Formulation
Subject to:
Variables: for each state (one variable per state)
Constraints: One constraint per state-action pair
Interpretation: We're finding the smallest value function that satisfies all the Bellman inequalities. The minimum that satisfies all constraints is exactly the optimal value function.
Dual LP Formulation
Through LP duality (swapping variables and constraints), we get:
Subject to:
Variables: for each state-action pair
Interpretation - Policy Flow:
- represents "how much agent-ness flows through" each state-action pair
- The constraints ensure flow conservation: what flows into a state must flow out
- We maximize expected reward (objective function directly corresponds to reward!)
Why the dual is intuitive: We're directly maximizing reward rather than minimizing values (which felt counterintuitive in the primal).
Practical Considerations
LP is theoretically elegant but often:
- Has overhead compared to VI/PI
- Is useful when you need additional linear constraints
- Provides a rigorous polynomial-time guarantee
Policy Iteration
The Algorithm
Policy Iteration alternates between two steps:
1. Policy Evaluation: Given policy , compute its value function
2. Policy Improvement: Take the greedy policy with respect to :
Repeat until the policy stops changing.
Why Policy Iteration Works: The Theoretical Foundation
Concept 1: Domination
Definition: Policy dominates policy (written ) if:
Strict domination (): dominates AND there exists some state where the inequality is strict.
Key insight: Domination is state-wise, not just aggregate. This prevents cycling.
Concept 2: Bellman Operators for Fixed Policies
For a fixed policy , define operator :
Properties of :
- Contraction:
- Unique fixed point:
- Monotonicity: If (pointwise), then
Concept 3: Value Improvement Lemma
The crucial lemma: If is greedy with respect to , then:
Proof sketch:
- is greedy, so
- Therefore
- The Bellman equation for says
- So taking one greedy step can only improve (or maintain) value
The Policy Iteration Proof
Theorem: Each step of policy iteration produces
Proof (the four-step argument):
-
Value Improvement: (One step with greedy policy improves or maintains value)
-
Monotonicity (applied repeatedly): (Each additional step maintains the ordering)
-
Transitivity: (Chaining the inequalities)
-
Fixed point: (Repeated application converges to 's value function)
Therefore:
Why Policy Iteration Doesn't Get Stuck
Key insight: Value improvement is state-wise, not just aggregate.
If is not optimal, there exists some state where improvement is strict. This prevents cycling between policies.
Finite convergence:
- There are only deterministic policies (finite!)
- Each iteration strictly improves the policy (unless we're at optimal)
- Therefore, we must converge in finite iterations
Convergence Bounds
Lower Bound: iterations (Some MDPs require at least iterations)
Upper Bound: iterations (At most the number of policies)
Open question: The exact complexity of policy iteration remains unknown!
Value Iteration vs Policy Iteration
| Aspect | Value Iteration | Policy Iteration |
|---|---|---|
| Per-iteration cost | ||
| Number of iterations | Can be large (depends on ) | Often very few |
| Memory | ||
| Convergence | Asymptotic | Exact in finite time |
Rule of thumb: PI often converges in surprisingly few iterations (sometimes 5-10) but each iteration is expensive. VI iterations are cheap but you may need many.
Lecture 6: Messing with Rewards
This lecture explores how we can modify reward functions while preserving optimal behavior—and why this matters for learning efficiency.
Why Modify Reward Functions?
The reward function is like a programming language for specifying desired behavior:
- Different "programs" (reward functions) can specify the same behavior
- Some are easier to learn from than others
- Some are easier to represent or reason about
Goals of reward modification:
- Make learning faster (fewer samples needed)
- Make computation cheaper (less planning required)
- Make representation simpler
- Guide exploration more effectively
Reward Function Transformations
Transformation 1: Multiply by Positive Constant
Claim: If for , then
Proof:
Since , we can factor it out of the max:
Why must be positive: If , the max becomes min (flipping reward and punishment). If , all information is lost.
Optimal policy preserved: Since we multiply all Q-values by the same positive constant, the ordering (and thus the argmax) is unchanged.
Transformation 2: Add a Constant
Claim: If , then
Intuition: Adding to every reward adds at every step. The sum of over an infinite discounted horizon is:
Proof: Substitute and into Bellman:
The constant doesn't affect the max (doesn't depend on ):
Simplify (using ):
Optimal policy preserved: Adding the same constant to all Q-values doesn't change the argmax.
Potential-Based Reward Shaping
The Problem with Naive Shaping
Example - Robot Soccer:
- True reward: +100 for scoring a goal
- Naive shaping: +10 for touching the ball
Problem: The robot might learn to "shiver" against the ball, repeatedly touching it to accumulate +10 rewards, never actually scoring!
This creates a suboptimal positive feedback loop—you can gain infinite reward without achieving the true objective.
The Solution: Potential Functions
Key insight: Design shaping rewards so they telescope (cancel out over trajectories).
Definition: A potential function assigns a "potential value" to each state.
Potential-based shaping reward:
New reward function:
Interpretation:
- You get for entering state
- You lose for leaving state
- Net effect over a trajectory: only endpoints matter!
Why Potential-Based Shaping Preserves Optimal Policy
Theorem (Ng, Harada, Russell 1999): If shaping rewards have the form , then:
Proof: Consider a trajectory:
Shaping rewards along the trajectory:
- Step 0:
- Step 1: (discounted)
- Step 2:
- ...
Sum them up (this is a telescoping sum):
Since and is bounded:
Total shaping reward =
Therefore:
Why optimal policy is preserved: The shift depends only on the current state, not on the action. So the argmax over actions is unchanged!
Designing Good Potential Functions
Robot Soccer Example:
- Higher potential when robot is closer to ball
- Higher potential when ball is closer to goal
- The factor of 2 makes ball-to-goal more important than robot-to-ball
The shaping reward:
- Moving toward the ball: positive shaping reward
- Ball moving toward goal: positive shaping reward
- Moving away: negative shaping reward (you "pay back" what you gained)
No more shivering: Repeatedly touching and releasing the ball gives net zero shaping reward!
Potential Shaping and Q-Learning
Key result: Running Q-learning with potential-based shaping is equivalent to running Q-learning with a different initialization.
Specifically: Q-learning with shaping function and produces the same sequence of policies as Q-learning without shaping but with .
Implication:
- Good potential function ≈ good initialization
- Random initialization ≈ asserting a random (probably bad) potential
- Inject knowledge, not noise!
Magic Potential: The Oracle Case
What if (the optimal value function)?
Then:
Result:
- for optimal action
- for suboptimal actions
With , the agent immediately behaves optimally and only needs to verify that suboptimal actions are indeed suboptimal!
Of course: If we knew , we wouldn't need to learn. But this shows the theoretical power of good shaping.
Lecture 7: Exploring Exploration
Exploration is the distinguishing feature of RL compared to other ML paradigms. This lecture develops the theory of efficient exploration.
K-Armed Bandits
The Setup
- slot machines (arms), each with unknown payoff probability
- At each time step, choose one arm to pull
- Receive stochastic reward: 1 with probability , 0 otherwise
- Goal: Maximize total reward (or identify the best arm)
Key insight: This is RL without state transitions—pure exploration/exploitation tradeoff.
The Exploration-Exploitation Dilemma
Exploration: Try different arms to learn their payoffs Exploitation: Pull the arm you believe is best to maximize reward
| Strategy | Problem |
|---|---|
| Always exploit (greedy) | May lock onto suboptimal arm with 70% probability |
| Always explore (round-robin) | Never uses learned information |
| Maximum likelihood | Gets stuck if best-looking arm isn't actually best |
| Maximum confidence | Just shuffles arms without preference |
| Minimum confidence | Explores everything but never exploits |
Example: After pulling arm A once (0 payoff) and arm B twice (1 payoff):
- B looks better:
- But B is actually better only ~70% of the time!
- We need to balance learning more vs. using what we know
Metrics for Bandit Algorithms
Metric 1: Identify Optimal Arm in the Limit
Problem: Round-robin achieves this but never exploits.
Metric 2: Maximize Discounted Reward
Optimal solution exists: Gittins Index
What is the Gittins Index? For each arm, compute a value based on (successes, trials) that captures both exploitation value and exploration value. Always pull the arm with highest index.
Intuition: The index equals "what guaranteed payoff would make you indifferent between that and this uncertain arm?"
Warning: Gittins Index is beautiful for bandits but does NOT generalize to MDPs. Many have tried; all have failed.
Metric 3: Maximize Finite Horizon Reward
Computable but expensive (exponential in horizon).
Metric 4: PAC-Style (proximally approximately correct) - Find Near-Optimal Arm
With probability , identify an -optimal arm within pulls.
Metric 5: PAC-Style - Get Near-Optimal Reward
With probability , achieve per-step reward within of optimal after steps.
Metric 6: Mistake Bound
Total number of suboptimal arm pulls is bounded by .
Key result: Metrics 4, 5, and 6 are equivalent! An algorithm for one gives algorithms for the others.
The Core Problem
You have a slot machine (bandit arm) with unknown win probability . You pull it times and win times. Your estimate is .
Question: How close is to the true ?
You can never be certain — maybe you got lucky or unlucky. But you can make probabilistic guarantees.
The Two Parameters
| Parameter | Meaning | Example |
|---|---|---|
| Accuracy: how close is your estimate? | "within 0.05 of truth" | |
| Failure probability: how often can you be wrong? | "wrong at most 5% of the time" |
The guarantee you want:
"The probability that my estimate is off by more than is at most ."
Equivalently: "With probability , my estimate is within of truth."
Hoeffding Bound
This tells you how , , and relate.
For i.i.d. samples from a distribution on with true mean :
The RHS is our . So:
This lets you solve for any variable given the other two:
Given and , what's ?
Given and , how many samples ?
Given and , what accuracy ?
Concrete Example
You pull an arm times, win 30 times. So .
How confident are you that ? (i.e., )
That's bigger than 1, so the bound is useless here (probability can't exceed 1).
What about ? (i.e., )
So with ~73% confidence, the true is in .
What about samples with ?
~84% confidence that .
Key Intuitions
- More samples → tighter bounds ( decreases exponentially in )
- Tighter accuracy is expensive ( scales as — halving requires 4× more samples)
- Higher confidence is cheap ( scales as — going from 90% to 99% confidence doesn't cost much)
Union Bound
Now suppose you have arms, and you want all estimates to be accurate.
- Arm 1 estimate is -accurate with probability
- Arm 2 estimate is -accurate with probability
- ...
- Arm K estimate is -accurate with probability
What's the probability that ALL are accurate?
Naive Attempt (Wrong!)
You might think:
But this assumes independence, which may not hold!
Union Bound (Always Works)
Why? Think Venn diagram — the "any failure" region is contained in the union of individual failure regions. The union's size is at most the sum of individual sizes.
Application
You want overall failure probability . If you set each arm's failure probability to :
So: to get overall confidence across arms, each arm needs confidence .
Putting It Together: PAC Bandits
Goal: Find the best arm (within ) with probability .
Strategy
- Pull each arm times
- Pick the arm with highest sample mean
Step 1: For the best arm to win, we need all estimates within of truth.
(Why ? Worst case: best arm's estimate is too low, second-best is too high. They could swap. But if true gap is , we're still okay.)
Step 2: Apply union bound — need each arm at confidence .
Step 3: Apply Hoeffding — how many samples per arm?
Total samples:
Interpretation
| Factor | Meaning |
|---|---|
| More arms → more samples (linear) | |
| Tighter accuracy → many more samples (quadratic) | |
| More arms or higher confidence → slightly more samples (logarithmic) |
Summary
| Concept | What it does |
|---|---|
| Hoeffding | Relates samples (), accuracy (), confidence () for ONE estimate |
| Union Bound | Lets you combine guarantees across MULTIPLE estimates |
| Together | Lets you say "with arms and samples each, I find an -optimal arm with probability " |
The - framework is the language of PAC (Probably Approximately Correct) learning — you get approximate answers with high probability, and you can quantify exactly how approximate and how probable.
Key observations:
- Dependence on : Quadratic ()—being very precise is expensive!
- Dependence on : Logarithmic—being very confident is cheap
- Dependence on : Linear—more arms = more samples
Deterministic MDPs and Rmax
The Challenge
In MDPs, unlike bandits:
- Actions affect future states
- Random exploration can get you stuck
- You can't teleport to unexplored regions
Example - Chain MDP: Need to take the "right" action times in a row to reach a high-reward state. Random exploration has probability of success!
The Rmax Algorithm
Key insight: Be optimistic about the unknown!
Algorithm:
- Maintain current knowledge of MDP (transitions and rewards seen so far)
- For any unknown state-action pair: assume it leads to a self-loop with reward (the maximum possible reward)
- Solve this "optimistic MDP"
- Execute the optimal policy
- When you visit unknown state-action pairs, update knowledge and return to step 2
Why it works:
Case 1: The optimistic assumption is correct (or close enough)
- Then we're getting good reward!
Case 2: The optimistic assumption is wrong
- Then we'll visit the "unknown" state-action pair
- We learn something new
- We can only be wrong times (finite!)
The "Learn or Earn" Lemma
Theorem: At any time, either:
- We're achieving near-optimal reward, OR
- We'll reach an unknown state-action pair within polynomially many steps
Implication: We can't be stuck doing poorly without learning. Either we're doing well or we're about to learn something that helps us do better.
Analysis of Rmax
Mistake bound: The number of -suboptimal actions is:
Key property: Polynomial in all relevant quantities!
General Stochastic MDPs
The Challenge
Now we have BOTH:
- Sequential decision making (from deterministic MDPs)
- Stochastic outcomes (from bandits)
Combining the Ideas
Rmax for stochastic MDPs:
-
Known vs Unknown: A state-action pair is "known" if we've visited it times (where is determined by Hoeffding bounds)
-
Estimate transitions: After visits, our transition probability estimates are accurate enough
-
Optimistic about unknown: Unknown state-action pairs are assumed to lead to the "Rmax state" with probability 1
-
Solve and execute: Solve the optimistic MDP, execute the policy
The Parameter C
How many times must we visit each state-action pair?
Requirements:
- Transition probability estimates must be -accurate
- Must hold for all pairs simultaneously
- Must hold with overall probability
Result:
PAC-MDP Guarantee
Theorem: Rmax is PAC-MDP efficient:
With probability , the total number of -suboptimal actions is polynomial in , , , , and .
The magic: We're either:
- Getting near-optimal reward (good!)
- Learning something new that will help us get near-optimal reward (also good!)
There's no "dark middle" where we're stuck being bad without learning.
Practical Considerations
Theory vs Practice:
- Theoretical values are often very large
- In practice, much smaller values work
- Rmax is actually a very effective algorithm!
Why it works well:
- Makes good use of all data
- Naturally balances exploration/exploitation
- Optimism provides principled guidance
Key Equations Summary
Bellman Equations
Contraction Mapping
Value Iteration Stopping Criterion
If , then:
Reward Transformations
Scaling:
Shifting:
Potential Shaping:
Hoeffding Bound
Sample Complexity (Bandits)