TExcelTAD
Home › Calculus Essentials › Series and Convergence

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 ∑an=a1+a2+a3+…\sum a_n = a_1 + a_2 + a_3 + \ldots is defined through its partial sums:

S1=a1,S2=a1+a2,…,Sn=a1+…+anS_1 = a_1, \quad S_2 = a_1 + a_2, \quad \ldots, \quad S_n = a_1 + \ldots + a_n

If the sequence of partial sums SnS_n converges to SS, the series converges and its sum is SS. 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 rr:

a+ar+ar2+ar3+…=a1−rif ∣r∣<1(diverges if ∣r∣≥1)a + ar + ar^2 + ar^3 + \ldots = \frac{a}{1 - r} \qquad \text{if } \lvert r \rvert < 1 \quad (\text{diverges if } \lvert r \rvert \ge 1)

Why: Sn=a(1−rn)/(1−r)S_n = a(1 - r^n)/(1 - r), and rn→0r^n \to 0 when ∣r∣<1\lvert r \rvert < 1. For example 1/2+1/4+1/8+…=(1/2)/(1−1/2)=11/2 + 1/4 + 1/8 + \ldots = (1/2)/(1 - 1/2) = 1.

The harmonic series diverges

The terms of 1+1/2+1/3+1/4+…1 + 1/2 + 1/3 + 1/4 + \ldots go to 0, yet the sum is infinite. Group the terms:

1+12+(13+14)+(15+…+18)+(19+…+116)+…≥1+12+12+12+12+…=∞\begin{aligned} &1 + \tfrac{1}{2} + \left(\tfrac{1}{3} + \tfrac{1}{4}\right) + \left(\tfrac{1}{5} + \ldots + \tfrac{1}{8}\right) + \left(\tfrac{1}{9} + \ldots + \tfrac{1}{16}\right) + \ldots \\ &\ge 1 + \tfrac{1}{2} + \tfrac{1}{2} + \tfrac{1}{2} + \tfrac{1}{2} + \ldots = \infty \end{aligned}

It grows like ln⁡n\ln n, so slowly that a million terms only reach about 14.4, but it never stops growing.

python · try it yourself
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 1/(n(n+1))=1/n−1/(n+1)1/(n(n + 1)) = 1/n - 1/(n + 1):

Sn=(1−12)+(12−13)+…+(1n−1n+1)=1−1n+1  →  1S_n = \left(1 - \tfrac{1}{2}\right) + \left(\tfrac{1}{2} - \tfrac{1}{3}\right) + \ldots + \left(\tfrac{1}{n} - \tfrac{1}{n + 1}\right) = 1 - \frac{1}{n + 1} \;\to\; 1

Rules

Convergent series can be added and scaled term by term: ∑(c⋅an+bn)=c∑an+∑bn\sum(c \cdot a_n + b_n) = c\sum a_n + \sum b_n. 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 r0,r1,r2,…r_0, r_1, r_2, \ldots with a discount factor γ\gamma (gamma) between 0 and 1:

G=r0+γr1+γ2r2+γ3r3+…G = r_0 + \gamma r_1 + \gamma^2 r_2 + \gamma^3 r_3 + \ldots

With a constant reward rr this is a geometric series, G=r/(1−γ)G = r / (1 - \gamma). Discounting guarantees a finite value even for tasks that never end, and 1/(1−γ)1/(1 - \gamma) acts as the agent's effective horizon: γ=0.99\gamma = 0.99 cares about roughly 100 steps ahead.

python · try it yourself
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 hh bounces back with a fraction ee of its speed each time (the coefficient of restitution). The flight times form a geometric series, so infinitely many bounces finish in finite time:

first fall:t0=2h/gk-th bounce:2⋅ek⋅t0(up and down)total time:t0+2t0(e+e2+…)=t0(1+e)/(1−e)total path:h(1+e2)/(1−e2)\begin{aligned} \text{first fall:}\quad & t_0 = \sqrt{2h/g} \\ k\text{-th bounce:}\quad & 2 \cdot e^k \cdot t_0 && \text{(up and down)} \\ \text{total time:}\quad & t_0 + 2t_0(e + e^2 + \ldots) = t_0 (1 + e) / (1 - e) \\ \text{total path:}\quad & h (1 + e^2) / (1 - e^2) \end{aligned}
python · try it yourself
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 1/(1−r)1/(1 - r) pattern appears in damped camera shakes and in UI springs that settle.

Exercises

Exercise 1: Geometric sum

Find ∑n=0∞3⋅(1/4)n\sum_{n=0}^{\infty} 3 \cdot (1/4)^n.

Answer

4

Solution
a=3, r=1/4 ⁣:3/(1−1/4)=3/(3/4)=4a = 3,\ r = 1/4\colon \quad 3 / (1 - 1/4) = 3 / (3/4) = 4

Exercise 2: A repeating decimal

Write 0.272727…0.272727\ldots as a fraction using a geometric series.

Answer

3/113/11

Solution
0.2727…=27/100+27/1002+…,a=27/100, r=1/100sum=(27/100)/(99/100)=27/99=3/11\begin{aligned} &0.2727\ldots = 27/100 + 27/100^2 + \ldots, \quad a = 27/100,\ r = 1/100 \\ &\text{sum} = (27/100) / (99/100) = 27/99 = 3/11 \end{aligned}

Exercise 3: Telescoping

Find ∑n=1∞2/(n(n+2))\sum_{n=1}^{\infty} 2/(n(n + 2)). Hint: 2/(n(n+2))=1/n−1/(n+2)2/(n(n + 2)) = 1/n - 1/(n + 2).

Answer

3/23/2

Solution
Sn=(1−1/3)+(1/2−1/4)+(1/3−1/5)+…+(1/n−1/(n+2))=1+1/2−1/(n+1)−1/(n+2)  →  3/2\begin{aligned} S_n &= (1 - 1/3) + (1/2 - 1/4) + (1/3 - 1/5) + \ldots + (1/n - 1/(n+2)) \\ &= 1 + 1/2 - 1/(n+1) - 1/(n+2) \;\to\; 3/2 \end{aligned}
python · try it yourself
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 γ\gamma

An agent gets reward 1 every step forever. Which γ\gamma makes the total discounted return exactly 20? And what share of that return comes from the first 20 steps?

Answer

γ=0.95\gamma = 0.95; the first 20 steps give 1−0.9520≈64.2%1 - 0.95^{20} \approx 64.2\% of it.

Solution
1/(1−γ)=20  ⇒  γ=0.95first 20 terms: (1−γ20)/(1−γ),share=1−0.9520\begin{aligned} &1/(1 - \gamma) = 20 \;\Rightarrow\; \gamma = 0.95 \\ &\text{first 20 terms: } (1 - \gamma^{20})/(1 - \gamma), \quad \text{share} = 1 - 0.95^{20} \end{aligned}
python · try it yourself
gamma = 0.95
print(f"{1 - gamma**20:.4f}")
0.6415

Test yourself

What is 1+1/3+1/9+1/27+…1 + 1/3 + 1/9 + 1/27 + \ldots?

  1. 4/34/3
  2. 3/23/2
  3. 2
  4. It diverges

The terms of a series go to 0. What can you conclude?

  1. It converges
  2. Its sum is 0
  3. Nothing yet: it might still diverge
  4. It diverges

In RL with reward 1 per step and γ=0.99\gamma = 0.99, what is the return of an endless episode?

  1. 99
  2. 0.99
  3. Infinite
  4. 100