In our last lesson, we established how to determine the probability of a specific trajectory unfolding within an MDP. This gives us a way to reason about how likely any sequence of states and actions is. Now, we move from "how likely?" to "how good?". To make intelligent decisions, an agent needs a way to quantify the desirability of the outcomes it experiences. This is where rewards come into play.
This lesson is dedicated to the concept of return, which is the mechanism for accumulating rewards over time. You will learn to calculate two fundamental types of returns: the finite-horizon return, a simple sum over a fixed period, and the discounted return, a more nuanced sum for ongoing tasks. Mastering this will be crucial for understanding how we define what an agent is trying to optimize.
From Rewards to Return
First, let's formalize the idea of an immediate reward. In an MDP, when an agent in state takes an action and transitions to state , it receives a scalar reward, . This reward is determined by a reward function. The most general form depends on the full transition, , but it's often simplified to depend only on the state-action pair, , or even just the new state, .
An agent's goal is not to maximize a single, immediate reward, but rather the cumulative rewards it collects over a trajectory. This cumulative reward is what we call the return. Let's denote the return from time step onwards as .
The OpenAI Spinning Up documentation provides a concise introduction to both rewards and the two primary forms of return.
Part 1: Key Concepts in RL — Spinning Up documentation
This reading formally defines the reward function and presents the mathematical formulas for the two types of return we will study: finite-horizon undiscounted and infinite-horizon discounted.
Please read the section on Reward and Return. Pay close attention to the two distinct summation formulas provided for calculating the return, R(\tau). Note that this resource uses R(\tau) for the total return of a trajectory, whereas we will more commonly use G_t to denote the return from a specific time t.
As you saw, there are two main ways to aggregate rewards into a return. Let's examine each one.
1. Finite-Horizon Undiscounted Return
This is the simplest way to calculate a return. For a process that is known to terminate after a fixed number of steps, called the horizon , the total return is simply the sum of all rewards received.
The return from time step in a horizon of length is:
If we are considering a full trajectory (or episode) of length , the return from the beginning () is:
Example:
Suppose an agent completes a task in 4 steps and receives the following reward sequence: . The finite-horizon undiscounted return for this trajectory is simply . This approach is common in episodic tasks where only the final outcome matters, like winning a game of chess.
2. Infinite-Horizon Discounted Return
What happens if the process doesn't have a clear endpoint? If an agent could potentially operate forever, summing the rewards could lead to an infinite, and therefore useless, value. This is a common problem in "continuing" tasks, like balancing a robot or managing an investment portfolio.
The solution is to introduce a discount factor, (gamma), where . The idea is that rewards received sooner are more valuable than the same rewards received further in the future.
The discounted return from time step is defined as:
With this formula, a reward received steps in the future is multiplied by . Since , this weight gets exponentially smaller as increases, ensuring that the infinite sum converges to a finite value, provided the rewards are bounded.
Let's watch a short video that explains these two concepts with clear examples and visuals.
Reinforcement Learning Made Simple - Reward
This video by Edan Meyer offers a straightforward explanation of finite-horizon undiscounted and infinite-horizon discounted returns.
First, watch the segment on undiscounted returns, where the concept is explained with a simple tic-tac-toe example. Then, continue with the section on discounted returns, which introduces the discount factor \gamma and walks through a numerical calculation.
Why Discount?
The use of a discount factor might seem like a mathematical convenience, and it certainly is, but there are deeper reasons for it. The following reading and video lecture explain the issues with simple additive utility in infinite horizons and justify the use of discounting.
This excerpt from the Berkeley CS188 textbook provides a clear motivation for why infinite, un-discounted returns can be problematic and shows mathematically why discounting solves this problem.
Start by reading the brief paragraphs under the heading 4.1 Markov Decision Processes to see the formula for additive utility. Then, read the section 4.1.1 Finite Horizons and Discounting from the beginning down to the boxed equation showing convergence.
As the reading highlights, your background with geometric series is directly applicable here. If the agent were to receive a constant maximum reward at every step, the discounted return would be . This is a geometric series that converges to , guaranteeing that the total return is finite.
David Silver's lecture provides a broader perspective on the motivations for discounting, which extend beyond mathematical convenience.
RL Course by David Silver - Lecture 2: Markov Decision Process
In this classic lecture, David Silver, a key researcher in reinforcement learning, discusses the return and provides several compelling reasons for using a discount factor.
Please watch the segment from return and discounting. He explains the formula for the return G_t and then fields a question from the audience about why we should discount, leading to a rich discussion of the reasons.
As Silver discusses, the key reasons for discounting include:
- Mathematical convenience: It prevents infinite returns in cyclic MDPs and ensures convergence.
- Model uncertainty: The future is uncertain. A discount factor implicitly reduces the importance of rewards that depend on distant, and therefore less reliable, predictions of our model.
- Economic and biological analogy: Interest rates in finance and the preference for immediate gratification in humans and animals both reflect a form of discounting.
Computing a Discounted Return: An Example
Let's solidify this with the same reward sequence as before: . We'll assume the episode ends after step 4. Let's use a discount factor of .
The discounted return from the start () is:
Notice that the return is no longer 10. The reward of +10 was "discounted" because it was received four steps into the future (at , which corresponds to the term).
If we calculate the return from a later time step, say , we are looking for the discounted sum of rewards from that point on:
From the perspective of , the reward of +10 is only two steps away, so it is discounted less heavily.
Conclusion
In this lesson, we have defined how to measure the "goodness" of a trajectory by accumulating the rewards it generates. This accumulated reward is called the return.
Here are the key takeaways:
- The return () is the sum of rewards from time step onwards, forming the basis for an agent's objective.
- Finite-horizon undiscounted return is the simple sum of rewards over a fixed number of steps. It is suitable for clearly defined episodic tasks.
- Infinite-horizon discounted return is a weighted sum of all future rewards, where the discount factor makes distant rewards less valuable. This is essential for continuing tasks and provides mathematical stability.
- The choice of reflects a preference for immediate versus future rewards. A close to 0 creates a "myopic" agent focused on immediate rewards, while a close to 1 creates a "farsighted" agent that values future rewards almost as much as present ones.
We have introduced the discount factor as a critical parameter. In the next lesson, we will delve deeper into its role and formally explore how changing alters the agent's effective planning horizon.