Series and Convergence
A series is what you get when you add up all the terms of a sequence. Infinitely many positive numbers can add up to a finite total, and this lesson shows when that happens, with the geometric series as the star: it is behind discounted rewards in reinforcement learning, compound interest, and bouncing balls that come to rest.
Partial sums
A series is defined through its partial sums:
If the sequence of partial sums converges to , the series converges and its sum is . Otherwise it diverges. An infinite sum is therefore a limit of a sequence, nothing more mysterious.
The geometric series
Each term is the previous one times a fixed ratio :
Why: , and when . For example .
The harmonic series diverges
The terms of go to 0, yet the sum is infinite. Group the terms:
It grows like , so slowly that a million terms only reach about 14.4, but it never stops growing.
import math
geo, harm = 0.0, 0.0
checkpoints = {10, 100, 1000, 10**6}
for n in range(1, 10**6 + 1):
geo += 0.5 ** n
harm += 1 / n
if n in checkpoints:
print(f"n={n:>8}: geometric {geo:.10f} harmonic {harm:.4f} ln n = {math.log(n):.4f}")n= 10: geometric 0.9990234375 harmonic 2.9290 ln n = 2.3026
n= 100: geometric 1.0000000000 harmonic 5.1874 ln n = 4.6052
n= 1000: geometric 1.0000000000 harmonic 7.4855 ln n = 6.9078
n= 1000000: geometric 1.0000000000 harmonic 14.3927 ln n = 13.8155
Note: If a series converges, its terms must go to 0. The reverse is false: the harmonic series is the standard counterexample.
Telescoping series
Sometimes neighbouring terms cancel. Since :
Rules
Convergent series can be added and scaled term by term: . Adding or removing finitely many terms changes the sum but not whether it converges.
In software: discounting in reinforcement learning
An RL agent values a stream of future rewards with a discount factor (gamma) between 0 and 1:
With a constant reward this is a geometric series, . Discounting guarantees a finite value even for tasks that never end, and acts as the agent's effective horizon: cares about roughly 100 steps ahead.
for gamma in [0.9, 0.99]:
closed_form = 1 / (1 - gamma)
G = sum(gamma**k * 1.0 for k in range(10_000)) # reward 1 every step
print(f"γ = {gamma}: sum of 10,000 terms = {G:.6f}, 1/(1-γ) = {closed_form:.6f}")
# discounted return of a short episode, computed backwards like RL code does
rewards = [0, 0, 1, 0, 5]
G = 0.0
for r in reversed(rewards):
G = r + 0.9 * G
print("return of", rewards, "with γ=0.9:", round(G, 4))γ = 0.9: sum of 10,000 terms = 10.000000, 1/(1-γ) = 10.000000
γ = 0.99: sum of 10,000 terms = 100.000000, 1/(1-γ) = 100.000000
return of [0, 0, 1, 0, 5] with γ=0.9: 4.0905
In software: Zeno's bouncing ball
A ball dropped from height bounces back with a fraction of its speed each time (the coefficient of restitution). The flight times form a geometric series, so infinitely many bounces finish in finite time:
import math
h, e, g = 2.0, 0.8, 9.8
t0 = math.sqrt(2 * h / g)
print(f"total time {t0 * (1 + e) / (1 - e):.4f} s")
print(f"total path {h * (1 + e**2) / (1 - e**2):.4f} m")
t, bounces = t0, 0
while 2 * e ** (bounces + 1) * t0 > 1 / 60: # stop when a bounce is shorter than one frame
bounces += 1
t += 2 * e**bounces * t0
print(f"{bounces} bounces longer than a frame, ending at t = {t:.4f} s")total time 5.7499 s
total path 9.1111 m
19 bounces longer than a frame, ending at t = 5.6762 s
A physics engine cannot simulate infinitely many bounces, so it uses a threshold and puts the ball to sleep, see Stacking and Sleeping. The same pattern appears in damped camera shakes and in UI springs that settle.
Exercises
Exercise 1: Geometric sum
Find .
Answer
4
Solution
Exercise 2: A repeating decimal
Write as a fraction using a geometric series.
Answer
Solution
Exercise 3: Telescoping
Find . Hint: .
Answer
Solution
from fractions import Fraction
S = sum(Fraction(2, n * (n + 2)) for n in range(1, 2001))
print(float(S))1.499000749375562
Exercise 4: Choosing
An agent gets reward 1 every step forever. Which makes the total discounted return exactly 20? And what share of that return comes from the first 20 steps?
Answer
; the first 20 steps give of it.
Solution
gamma = 0.95
print(f"{1 - gamma**20:.4f}")0.6415
Test yourself
What is ?
- 2
- It diverges
The terms of a series go to 0. What can you conclude?
- It converges
- Its sum is 0
- Nothing yet: it might still diverge
- It diverges
In RL with reward 1 per step and , what is the return of an endless episode?
- 99
- 0.99
- Infinite
- 100