S1446 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S14 · Lesson 46 · Reinforcement learning

Learning to act from rewards, Markov decision processes with discounted returns, Bellman's equations, value and policy iteration, Q-learning, exploration in bandit problems with upper confidence bounds, and policy gradients

Every model so far learned from examples labeled with the right answer. A battery-powered sensor deciding when to transmit, a controller tuning a power converter or a program playing a game gets no labels, only rewards, often long after the action that earned them. Reinforcement learning turns that into an optimization over policies. Its central equation, Bellman's, also underlies the optimal control of S9: the linear–quadratic regulator is a reinforcement learning problem whose answer happens to have a closed form.

  • An agent discounts a reward t steps away by γt. The solid curve is the share of a steady reward stream's total discounted value collected within the first T steps, 1−γT.
  • The dashed curve is γT, the factor by which T sweeps of value iteration shrink the error in a value estimate: the same discount sets both how far ahead the agent looks and how fast planning converges.
  • With γ = 0.99 the agent effectively plans about 100 steps ahead, and value iteration needs hundreds of sweeps.

Predict

An agent's discount factor is 0.99. Roughly how far ahead does it effectively plan?

Name it

Markov decision processes

At each step the agent sees a state s, takes an action a, receives a reward r and moves to a new state with probability P(s′|s,a). It seeks a policy π, a rule for choosing actions, that maximizes the expected return G=∑tγtrt, with discount 0 ≤ γ < 1.

Values and Bellman's equations

A policy's value Vπ(s) is its expected return from s; the action value Qπ(s,a) is the return from taking a first and following π afterward. Values satisfy

Vπ(s)=∑aπ(a|s)[R(s,a)+γ∑s′P(s′|s,a)Vπ(s′)]

a linear system, V=(I−γPπ)−1Rπ, for a fixed policy. The optimal values satisfy Bellman's optimality equation

V*(s)=maxa⁡[R(s,a)+γ∑s′P(s′|s,a)V*(s′)]

and acting greedily with respect to V* is optimal.

Planning with a model

Value iteration applies the optimality equation as an update. Each sweep shrinks the largest error by at least γ, so reaching a fraction ε of the starting error takes ln⁡ϵ/ln⁡γ sweeps. Policy iteration alternates solving for Vπ exactly and making π greedy; it usually converges in a handful of iterations.

Learning without a model

Q-learning updates after every step from what actually happened:

Q(s,a)←Q(s,a)+α[r+γmaxa′⁡Q(s′,a′)−Q(s,a)]

It converges to Q* if every state–action pair keeps being tried, which needs exploration: the ε-greedy rule takes a random action a fraction ε of the time. With a neural network in place of the table, this is deep Q-learning.

Exploration in bandits

With one state and K actions of unknown mean reward (a bandit), the UCB1 rule picks the action maximizing

x¯a+2ln⁡tna

where na is how often a has been tried in t steps. The bonus shrinks as an action is tried, so poorly known actions get tested, and the total shortfall against always choosing the best action (the regret) grows only as ln t.

Policy gradients

A parameterized policy πθ can be improved directly by gradient ascent on its expected return: ∇J=𝔼[G∇ln⁡πθ(a|s)], estimated from sampled episodes (REINFORCE). Subtracting a baseline, such as an estimate of V(s), from G reduces the estimate's variance without biasing it.

Worked example

A sensor earns a reward of 1 per step while charged, and goes low with probability 0.2 per step; while low it earns nothing and recharges with probability 0.5. With γ = 0.9, what is each state worth?

Show the solution
  1. Bellman equations. Vc=1+0.9(0.8Vc+0.2Vl)Vl=0.9(0.5Vc+0.5Vl)
  2. Solve. The second gives Vl=0.818Vc; put that in the first: Vc=11−0.72−0.18×0.818=7.53Vl=0.818×7.53=6.16
  3. Read it. The low state is worth almost as much as the charged one because it recharges quickly.
  4. Check. Over the long run the sensor is charged 0.5/0.7 = 71% of the time, and 0.71/(1−0.9)=7.1 is the average of the two values, weighted by that share.

Answer the charged state is worth 7.53 and the low state 6.16.

Worked example

Q(s, a) = 2.0. The agent takes a, receives r = 1 and lands where the best action value is 3.0. With γ = 0.9 and α = 0.1, what is the update? And how many value-iteration sweeps shrink an initial error of 10 below 0.001?

Show the solution
  1. Target. target=1+0.9×3.0=3.7
  2. Update. The error is 1.7, so Q=2.0+0.17=2.17
  3. Value-iteration sweeps. 0.9k≤10−4 needs k≥ln⁡10−4/ln⁡0.9=87.4 so 88 sweeps.
  4. Compare. Each Q-learning step moves only one entry a tenth of the way, which is why model-free learning needs far more experience than planning with a known model needs sweeps.

Answer Q becomes 2.17, and value iteration needs 88 sweeps.

Worked example

After 1000 pulls of a bandit, action A has averaged 0.60 over 100 pulls and action B 0.50 over 10. Which does UCB1 pick next?

Show the solution
  1. Bonuses. bA=2ln⁡1000/100=0.37bB=2ln⁡1000/10=1.18
  2. Indices. 0.60+0.37=0.970.50+1.18=1.68 so UCB1 tries B again: with only 10 pulls, B's true mean could well exceed A's.
  3. Later on. As B's count grows its bonus falls, and if B really is worse, it gets tried only about ln t times more.

Answer UCB1 picks B, whose index of 1.68 beats A's 0.97.

Check

An agent with discount 0.95 receives a steady reward of 1 per step.

(a)

What is the stream's discounted value?

(b)

What is its effective horizon?

Check

Value iteration runs with discount 0.95.

(a)

How many sweeps shrink the error by a factor of 0.01?

Check

Q(s, a) = 0. The agent receives r = −1 and lands where the best action value is 10; γ = 0.99 and the learning rate is 0.05.

(a)

What is the temporal-difference error?

(b)

What does Q(s, a) become?

Check

A fixed policy earns 1 per step in state 1 and 0 in state 2. It moves from state 1 to 2 with probability 0.3 and from 2 to 1 with probability 0.6; γ = 0.8.

(a)

What is state 1 worth?

(b)

And state 2?

Check

A bandit action has averaged 0.3 over 500 of 100 pulls.

(a)

What is its UCB1 index?

Check

An ε-greedy agent with ε = 0.1 chooses among 2 actions.

(a)

How often does it take the greedy action?

Check

Why does Q-learning need exploration even though it learns the value of the greedy policy?

Check

A fixed policy earns r in state 1 and u in state 2, moving 1 → 2 with probability a and 2 → 1 with probability b; discount γ.

Step 1

What is V1?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Check

Write value_iteration(P, R, gamma, tol) for a finite MDP: P[s][a] is a list of (probability, next state) pairs and R[s][a] the reward. Iterate until no value changes by more than tol, and return (values, greedy policy) as lists.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does every action's value depend on what the agent will do afterward?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.