In reinforcement learning, an agent collects rewards at each timestep. The discounted return at timestep t is the sum of all future rewards, where each reward is discounted by a factor of gamma raised to the power of how far in the future it occurs. This captures the idea that immediate rewards are worth more than distant ones.
Given a list of rewards collected over T timesteps and a discount factor gamma, compute the discounted return for every timestep.
The discounted return at timestep t is:
Gt=rt+γ⋅rt+1+γ2⋅rt+2+⋯+γT−1−t⋅rT−1This can be computed efficiently using the backward recursive relation:
Gt=rt+γ⋅Gt+1 GT−1=rT−1Return the discounted returns as a list of floats with the same length as rewards.
Input: rewards = [1, 1, 1], gamma = 1
Output: [3.0, 2.0, 1.0]
Explanation: With no discounting, each position contains the sum of all rewards from that position onward.
Input: rewards = [0, 0, 0, 10], gamma = 0.9
Output: [7.29, 8.1, 9.0, 10.0]
Traverse rewards from right to left while carrying the next return.
Store reward plus gamma times the carried return at each position.
Accepts: array
Accepts: number
In reinforcement learning, an agent collects rewards at each timestep. The discounted return at timestep t is the sum of all future rewards, where each reward is discounted by a factor of gamma raised to the power of how far in the future it occurs. This captures the idea that immediate rewards are worth more than distant ones.
Given a list of rewards collected over T timesteps and a discount factor gamma, compute the discounted return for every timestep.
The discounted return at timestep t is:
Gt=rt+γ⋅rt+1+γ2⋅rt+2+⋯+γT−1−t⋅rT−1This can be computed efficiently using the backward recursive relation:
Gt=rt+γ⋅Gt+1 GT−1=rT−1Return the discounted returns as a list of floats with the same length as rewards.
Input: rewards = [1, 1, 1], gamma = 1
Output: [3.0, 2.0, 1.0]
Explanation: With no discounting, each position contains the sum of all rewards from that position onward.
Input: rewards = [0, 0, 0, 10], gamma = 0.9
Output: [7.29, 8.1, 9.0, 10.0]
Traverse rewards from right to left while carrying the next return.
Store reward plus gamma times the carried return at each position.
Accepts: array
Accepts: number