Learn › Part B: Choices and consequences
5. Value functions and the Bellman equation
How good is it to be here?
Imagine two students in the evening from the last chapter. One is fresh and one is tired. Without doing any sums, who is better off?
You probably said the fresh one. A fresh student can study well and earn more. But how much better off? Is the gap small, or big? And does the answer depend on what each student plans to do?
Those questions are the heart of reinforcement learning. Instead of asking "what did I get just now?", we ask "how good is it to be here, given what I will do from now on?" The answer, as a number, is called a value.
The value of a state
Let us define it carefully. Suppose the agent follows a policy (the Greek letter pi, here standing for "policy"). Start it in some state . It will go on to collect a stream of rewards, and the total, with discounting, is the return you met last chapter. But that return is random, because transitions are random. Run the same plan twice and you can get different totals.
So we take the expected value of the return, the average over everything that might happen:
Read it aloud: "the value of state under policy is the expected return, given that we start in ." The little bar means "given that".
Last chapter we estimated this by simulation: play thousands of evenings and average. Now we will see how to get the exact number without simulating anything.
The value of an action
Sometimes we also want to know how good a particular action is. The action value is the expected return if you start in state , take action right now, and then follow the policy from there on.
The difference between and is small but useful. asks "how good is this situation, if I follow my plan?" asks "how good is this particular first move, and then my plan?" We will use to compare moves, soon.
The Bellman equation
Here is the key trick. Remember the backwards rule from last chapter:
The return from now is the next reward plus the discounted return from the next step on. Now take the expected value of both sides. The expected return from now is the value of the current state, so the left side becomes . On the right, the expected reward is what the policy's action pays, and the expected future return is the average of the values of the states we might land in. In symbols, for a policy that takes action in state :
This is the Bellman equation, named after the mathematician Richard Bellman. Let us read it in plain words:
value of a state = the reward you get now + γ × the average value of the state you land in.
The sum just means "add over every state you might land in", and is the chance of landing in . So the sum is an average of the next states' values, weighted by how likely each one is.
Why is this so powerful? Because it says a state's value is consistent with its neighbours'. You do not need to look infinitely far ahead. You only need the reward right now and the values of the next states. And those values are in turn consistent with theirs. Every state in the problem has one such equation, so if there are states, we have equations in unknowns.
Solving it by hand
Let us do it for the student, following the policy "study when fresh, rest when tired", with . Write for the value of being fresh, and for the value of being tired. Recall the table: studying while fresh pays 3 and leads to fresh with chance 0.6 and tired with chance 0.4. Resting while tired pays 0 and leads to fresh with chance 0.7 and tired with chance 0.3.
The Bellman equation for each state is
Multiply out the brackets and collect the unknowns on the left:
(For the first line: and . For the second: and .)
That is just two simultaneous equations, which you have solved many times at school. By substitution: the second equation gives . Put that into the first:
so , and then .
Or with determinants (Cramer's rule). The determinant of the coefficients is . Then
Same answer. Notice that these match what we found by simulating 4,000 evenings in the last chapter (about 20.09 from the fresh state). The equations gave us in a few lines what took thousands of simulated evenings.
So a fresh student following this plan can expect a total, discounted, reward of about 20.1, and a tired student about 17.3. The fresh one is better off, but only by about 2.75 points: a good plan gets the tired student back on their feet fairly quickly.
It works for any policy
The same method values any policy. Choose what the student does in each condition and see how much each state is worth.
Value explorer
Choose what the student does in each condition. The numbers show how much each situation is worth under that plan, found by solving the Bellman equations exactly.
| Condition | Value under this plan | Q if you study | Q if you rest |
|---|---|---|---|
| Fresh | 15.94 | 15.94 (plan) | 14.99 |
| Tired | 12.03 | 12.03 (plan) | 13.29 |
This plan can be improved: when tired the student should rest instead.
Try the four combinations. "Study when fresh, rest when tired" gives about 20.1 and 17.3. "Always study" gives about 15.9 and 12.0, much worse when tired. "Always rest" gives about 8.9 and 7.7. And "rest when fresh, study when tired" is the worst of all, about 8.8 and 7.4: exactly the wrong way round. Notice the slider too: when is near 0 the student only cares about now, and studying wins everywhere.
The matrix form
When there are many states, writing out every equation by hand gets tedious, so we pack them into one. Collect the values into a list , the rewards into a list , and the transition probabilities into a grid (a matrix, as in the maths toolkit). The Bellman equations for all states at once are
Here is the identity matrix, which is 1s down the diagonal and 0 elsewhere. For our student under the "study, then rest" policy,
These are exactly the two equations we wrote above. In Python, one line solves them for any number of states:
V = np.linalg.solve(np.eye(2) - gamma * P, r)
That is fine for a few thousand states. For the enormous problems RL really cares about, solving a giant system directly is impractical, which is why the next chapter shows a way to approach the answer step by step instead.
From values to better decisions
Values do more than score a plan. They tell you how to improve it.
Under the plan "study when fresh, rest when tired", we can work out the value of each action using . This takes one reward now plus the discounted values of where you could land. We found these:
| Condition | Q if you study | Q if you rest |
|---|---|---|
| Fresh | 20.09 | 18.83 |
| Tired | 16.60 | 17.34 |
Look at each row. When fresh, studying is worth 20.09 and resting 18.83, so the plan's choice (study) is the better one. When tired, resting is worth 17.34 and studying 16.60, so the plan's choice (rest) is again the better one. In both conditions, the plan already picks the best-looking action. No single change makes things better. (Notice that the Q of the plan's own action equals the state's value: 20.09 when fresh and 17.34 when tired. That is just the Bellman equation again.)
Now try the same on the plan "always study", which had values 15.94 and 12.03. Its when tired for resting works out higher than for studying, so switching to rest when tired improves it. That is the idea of policy improvement: look at the values, and switch to any action that looks better than the one you are using. The Value Explorer above does this check for you, and tells you when a plan can be improved.
This is a big idea. Evaluate a plan, improve it, evaluate again, and repeat. It is how a whole family of RL methods works, and the next chapter turns it into an algorithm.
The best possible values
So far the policy was fixed. What if we want the best value any policy can achieve? Call it . Following the same logic, in each state we should take the action that leads to the highest expected return:
This is the Bellman optimality equation. It looks like the one before, with a "pick the best action" step. For our student, the best values turn out to be exactly the ones for "study when fresh, rest when tired", about 20.09 and 17.34: you can check by looking at the table above, where the larger entry in each row equals the value.
There is one catch: the makes the equations no longer a plain system you can solve with matrices. So we need a different method, which is where the next chapter begins.
Run it yourself
At the bottom of this page you can download:
bellman.pysolves for the value of every policy, both with a direct solver and with Cramer's rule, then computes the values and checks for improvements. It needs only NumPy.check_numbers.pychecks every number quoted in this chapter.
Try changing the discount factor from 0.9 to 0.5, or to 0.99, and watch how the values and the best policy respond.
Check yourself
Write the Bellman equation for the tired state under the plan 'rest when tired', using the table from the last chapter. Then say in words what it means.
Resting while tired pays 0 and leads to fresh with chance 0.7 and tired with chance 0.3. So . In words: the value of being tired is the reward now (nothing) plus 0.9 times the average value of where you land, which is 70% likely to be the fresh state and 30% likely to be tired again.
Two states A and B. From A you get reward 1 and move to B. From B you get reward 0 and move back to A. With a discount factor of 0.5, what are the values of A and B?
The equations are and . Substitute the second into the first: , so and . Then .
Under the plan 'study when fresh, rest when tired', the Q-value of resting when fresh is 18.83 and the value of the fresh state is 20.09. What does that tell you?
It says that following the plan is better than resting once and then following the plan: 20.09 is more than 18.83. So the plan's choice to study when fresh is the right one, and switching would make things worse.
If the discount factor is 0, what is the value of each state under 'study when fresh, rest when tired'?
With a discount factor of 0, nothing after the first step counts, so each value is just the immediate reward: 3 for the fresh state (studying pays 3) and 0 for the tired state (resting pays 0).
Why can we solve the Bellman equation for a fixed policy with matrices, but not the Bellman optimality equation?
For a fixed policy, each equation is a plain linear equation in the values, so we get a standard system of linear equations. The optimality equation contains a max over actions, and taking a max is not a linear operation, so it cannot be written as a simple matrix equation. We need an iterative method instead.
Recap
- The value of a state is the expected return from that state if you follow the policy . It answers "how good is it to be here?"
- The action value is the expected return if you take action now and follow afterwards.
- The Bellman equation says: value of a state = reward now + γ × average value of the next state. One equation per state gives a small system you can solve by hand, or as .
- Values tell you how to improve a policy: switch to any action whose is higher than the one you use.
- The Bellman optimality equation replaces "follow the policy" with "take the best action". It gives the best possible values, but it needs a new way of solving.
Where next
Now we know what a value is, and how to get it exactly for small problems. Next we meet dynamic programming: a way of finding values, and the best policy, step by step, that works even when solving the equations directly would be too much.
Code for this chapter
Run these on your own machine. Each one is short and commented.
- bellman.py 3.6 KB
- check_numbers.py 3.2 KB