Series
An infinite series is defined through its partial sums: it has a sum exactly when the sequence of partial sums converges. This lesson proves the geometric series formula, the divergence of the harmonic series and the th-term test, and then uses geometric series where programmers meet them most: discounted rewards in reinforcement learning and the amortised cost of a growing array.
The survey lesson Series and Convergence has a gentler introduction.
Definition
Given a sequence , its partial sums are . The series converges with sum if as a sequence (see Sequences), and diverges otherwise. A series is therefore a sequence in disguise, and everything about limits of sequences applies to .
The geometric series
Theorem. For , the series converges if and only if , and then
Proof. If , . Otherwise subtract from ; everything cancels but two terms:
If then and . If or , has no limit, so neither does . ∎
A repeating decimal is a geometric series: .
Telescoping series
When , the partial sums collapse: . Partial fractions often expose this structure:
import sympy as sp
from fractions import Fraction
n, k = sp.symbols("n k", integer=True, positive=True)
print("partial fractions:", sp.apart(1 / (k * (k + 1)), k))
print("s_n =", sp.simplify(sp.summation(1 / (k * (k + 1)), (k, 1, n))))
print("geometric, r = 2/3:", sp.summation(5 * sp.Rational(2, 3)**(k - 1), (k, 1, sp.oo)))
print("0.272727... =", Fraction(27, 100) / (1 - Fraction(1, 100)))partial fractions: -1/(k + 1) + 1/k
s_n = n/(n + 1)
geometric, r = 2/3: 15
0.272727... = 3/11
The harmonic series diverges
diverges even though its terms go to 0. Group the terms in blocks of length :
Each block has terms, each at least , so each block contributes at least . Hence . The growth is very slow: the integral test shows .
The nth-term test
Theorem. If converges, then .
Proof. , and both and tend to the same , so . ∎
Its contrapositive is the test for divergence: if (or does not exist), diverges. So and diverge at once. The converse is false; the harmonic series is the counterexample.
Linearity. If and , then and (apply the limit laws to the partial sums). Changing or dropping finitely many terms never affects convergence, only the sum.
In software: discounting in reinforcement learning
An RL agent that receives rewards values them with a discount factor :
If , comparison with the geometric series (see The Comparison Tests) gives . That bound is why makes infinite-horizon problems well-defined, and why is called the effective horizon: with the agent effectively looks about 100 steps ahead. Returns are computed backwards with .
import numpy as np
gamma = 0.99
print(f"constant reward 1 forever: G = 1/(1 − γ) = {1 / (1 - gamma):.1f}")
# weight of the first H steps in the total: 1 − γ^H
for H in (10, 100, 459):
print(f"first {H:3d} steps carry {1 - gamma**H:.1%} of the weight")
rewards = np.array([0, 0, 1, 0, 0, 0, 5, 0, 0, 10], dtype=float)
G = 0.0
returns = []
for r in rewards[::-1]: # backwards: G_t = r_t + γ G_{t+1}
G = r + gamma * G
returns.append(G)
print("G_0 =", round(returns[-1], 4))
print("direct ∑ γ^k r_k =", round(float(np.sum(gamma ** np.arange(10) * rewards)), 4))constant reward 1 forever: G = 1/(1 − γ) = 100.0
first 10 steps carry 9.6% of the weight
first 100 steps carry 63.4% of the weight
first 459 steps carry 99.0% of the weight
G_0 = 14.8227
direct ∑ γ^k r_k = 14.8227
In software: amortised array growth
A dynamic array (Python list, C++ std::vector, Rust Vec) that doubles its capacity when full copies elements to reach items. That is a finite geometric sum, and it is why appends are amortised (see time complexity). Growing by a factor copies about elements; growing by a constant amount instead gives the arithmetic sum , which is quadratic.
def copies(n, grow):
cap, size, moved = 1, 0, 0
for _ in range(n):
if size == cap:
moved += size
cap = grow(cap)
size += 1
return moved
n = 1_000_000
print("doubling :", copies(n, lambda c: 2 * c), "copies")
print("factor 1.5 :", copies(n, lambda c: max(c + 1, int(c * 1.5))), "copies")
print("+1000 per grow:", copies(n, lambda c: c + 1000), "copies")doubling : 1048575 copies
factor 1.5 : 2099753 copies
+1000 per grow: 499501000 copies
Exercises
Exercise 1: Geometric sums
Find the sums, or say why they diverge: a) , b) , c) .
Answer
a) 2, b) , c) diverges.
Solution
Exercise 2: A repeating decimal
Write (45 repeating) as a fraction.
Answer
.
Solution
Exercise 3: Telescoping
Find .
Answer
.
Solution
import sympy as sp
n = sp.symbols("n", integer=True, positive=True)
print(sp.summation(2 / (n**2 + 2*n), (n, 1, sp.oo)))3/2
Exercise 4: Effective horizon
An agent gets reward 1 at every step. With , what is its return? After how many steps have at least 95% of that return been collected? Check with a loop.
Answer
; after 29 steps ().
Solution
g, total, H = 0.9, 0.0, 0
while total < 0.95 * 10:
total += g**H
H += 1
print(H, round(total, 4))29 9.529
Test yourself
What is ?
- 2
- It diverges
. What can you conclude about ?
- It converges
- It diverges
- Its sum is 0
- Nothing yet
In RL with and rewards bounded by 1, what bound holds for the return?
- is unbounded