Week 03 · Returns, values, and Bellman equations

Solve a policy value equation and check it with simulated returns.

← Week 2 · Subject contents · Week 4 →

Solve a policy value equation and check it with simulated returns.

Work through the problem

A return is G₀ = r₁+γr₂+γ²r₃+…. For a fixed policy, v(s)=E[r+γv(s′)|s]. This is a recursive expression for expected return, with a terminal state assigned value zero.

Suppose a state pays 1 each step and independently returns to itself with probability 0.5, otherwise ending. With discount 0.9, v=1+0.45v, so v=1/0.55 ≈ 1.818. A geometric episode length gives an independent way to simulate the discounted sum.

Run the Python lab

import numpy as np
gamma = .9
exact = 1/(1-.5*gamma)
n = np.random.default_rng(24).geometric(.5,40000)
returns = (1-gamma**n)/(1-gamma)
print("Bellman and simulation:",round(exact,3),round(returns.mean(),3))
assert abs(returns.mean()-exact)<.025

Output from the lab

Bellman and simulation: 1.818 1.82

Practice

  1. If γ=1, what is the value?

Check your reasoning

  1. The expected episode length is 1/0.5 = 2, which is also the total expected reward.

← Week 2 · Subject contents · Week 4 →

Further reading · Sutton and Barto · Reinforcement Learning