Effective Horizon of a Discount Factor
Problem Statement
The effective planning horizon implied by a discount factor is often summarized as 1 / (1 - gamma) β the sum of the geometric discount series sum_{t=0}^inf gamma^t. For gamma == 1 the horizon is infinite; represent that with float('inf').
Implement effective_horizon(gamma) returning a float.
Example:
effective_horizon(0.9)
10.0
- Check if the discount factor Ξ³=0.9 is greater than or equal to 1.0 to determine if the horizon is infinite; since 0.9<1.0, the horizon is finite.
- Calculate the denominator of the geometric series sum by subtracting the discount factor from 1: 1.0β0.9=0.1.
- Compute the effective horizon by dividing 1 by this denominator: 1.0/0.1=10.0.
- The final output is 10.0
Constraints:
0.0 <= gamma <= 1.0gamma == 1.0returnsfloat('inf').- Otherwise return
1 / (1 - gamma).
1. Background Knowledge
In Markov Decision Processes (MDPs), the discount factor Ξ³β[0,1] controls how much an agent values future rewards relative to immediate ones. The total expected return from time t=0 onward is defined as:
Gtβ=k=0βββΞ³kRt+kβThe term Ξ³k acts as a weight that decays exponentially. When Ξ³ is close to 1, the agent is "patient" and considers distant rewards almost as important as near-term ones. When Ξ³ is small, the agent is "myopic" and mostly cares about immediate outcomes.
The effective horizon is a heuristic measure of how many steps into the future the agent effectively "sees." It is derived from the sum of the pure discount weights (ignoring actual reward magnitudes):
H=k=0βββΞ³kThis is a geometric series. For β£Ξ³β£<1, the series converges to a finite value. For Ξ³=1, every term equals 1, so the sum diverges to infinity. This distinction is critical in reinforcement learning: a finite horizon implies the agent's planning is bounded, while an infinite horizon implies the agent must consider all future consequences equally.
2. Algorithm Approach
This problem is a direct application of the closed-form solution for a geometric series. Rather than summing terms iteratively (which would be inefficient and prone to floating-point accumulation errors), you should use the analytical formula:
k=0βββrk=1βr1βforΒ β£rβ£<1The algorithmic pattern here is:
- Check for the special case where the series diverges (Ξ³=1).
- Apply the closed-form formula for all other valid inputs.
This is a constant-time mathematical evaluation, not an iterative computation.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.