Generating Functions

⚡ Key Takeaways

A generating function encodes a sequence (an)(a_n) as the coefficients of a formal power series ∑anxn\sum a_n x^n, turning sequence operations into algebra. Key result: counting, recurrences, and convolutions become multiplication, differentiation, and substitution of functions. Used across combinatorics, probability, number theory, and analysis of algorithms. Introduced by Abraham de Moivre (1730) and developed extensively by Euler and Laplace.

💡 The Core Insight

A sequence is hard to manipulate term-by-term, but the whole sequence at once, viewed as a power series, lives in a ring where you can add, multiply, invert, differentiate, and compose. The clothesline metaphor (Wilf): a generating function is a clothesline on which we hang up the terms of a sequence for display. The power of xnx^n is just a label remembering position nn; the coefficient is the datum. Combinatorial operations on objects (disjoint union, pairing, sequencing) correspond exactly to algebraic operations on their generating functions.

Formal Statement

DefinitionOrdinary Generating Function (OGF)

Given a sequence (an)n≥0(a_n)_{n\ge0} of numbers (in a commutative ring RR, typically Q\mathbb{Q}, R\mathbb{R}, or C\mathbb{C}), its ordinary generating function is the formal power series

1
A(x)=∑n=0∞anxn=a0+a1x+a2x2+⋯\quad A(x) = \sum_{n=0}^{\infty} a_n x^n = a_0 + a_1 x + a_2 x^2 + \cdots

Derivation hint: This is a definition; xx is a formal indeterminate, not a number, and convergence is not required. Here an=[xn]A(x)a_n = [x^n]A(x) denotes the coefficient extraction operator: [xn][x^n] returns the coefficient of xnx^n.

DefinitionExponential Generating Function (EGF)

The exponential generating function of (an)n≥0(a_n)_{n\ge0} is

2
A^(x)=∑n=0∞anxnn!\quad \hat{A}(x) = \sum_{n=0}^{\infty} a_n \frac{x^n}{n!}

Derivation hint: The n!n! weighting is chosen so that labelled-structure operations (where order of labels matters) become clean products. EGFs are preferred for labelled combinatorial structures (permutations, labelled graphs); OGFs for unlabelled ones (partitions, integer compositions).

DefinitionThe Ring of Formal Power Series

The set R[[x]]R[[x]] of all formal power series over RR forms a commutative ring under coefficientwise addition and Cauchy-product multiplication:

3
(∑nanxn) ⁣(∑nbnxn)=∑n=0∞(∑k=0nak bn−k)xn\quad \left(\sum_n a_n x^n\right)\!\left(\sum_n b_n x^n\right) = \sum_{n=0}^\infty\left(\sum_{k=0}^{n} a_k\, b_{n-k}\right)x^n

Derivation hint: Collect all products akxk⋅bn−kxn−ka_k x^k \cdot b_{n-k}x^{n-k} contributing to xnx^n; the inner sum is the convolution (a∗b)n(a*b)_n. A series A(x)A(x) is invertible in R[[x]]R[[x]] if and only if a0a_0 is a unit of RR (e.g. a0≠0a_0\ne 0 when RR is a field).

TheoremConvolution Theorem

If A(x)=∑anxnA(x)=\sum a_n x^n and B(x)=∑bnxnB(x)=\sum b_n x^n are OGFs, then the product A(x)B(x)A(x)B(x) is the OGF of the convolution sequence cn=∑k=0nakbn−kc_n = \sum_{k=0}^n a_k b_{n-k}. For EGFs, the product A^(x)B^(x)\hat A(x)\hat B(x) is the EGF of the binomial convolution:

4
cn=∑k=0n(nk)ak bn−k\quad c_n = \sum_{k=0}^{n}\binom{n}{k} a_k\, b_{n-k}

Derivation hint: Multiply ∑akxk/k!\sum a_k x^k/k! by ∑bjxj/j!\sum b_j x^j/j!, set n=k+jn=k+j, and multiply and divide by n!n! to recover the binomial coefficient.

TheoremFundamental Operations Dictionary

For OGFs, the following correspondences hold (with A(x)=∑anxnA(x)=\sum a_n x^n):

5
∑nan+1xn=A(x)−a0x,∑nn anxn=xA′(x),∑n(∑k=0nak)xn=A(x)1−x\quad \sum_n a_{n+1}x^n = \frac{A(x)-a_0}{x},\qquad \sum_n n\,a_n x^n = x A'(x),\qquad \sum_n\Big(\sum_{k=0}^n a_k\Big)x^n = \frac{A(x)}{1-x}

Derivation hint: Shift by reindexing m=n+1m=n+1; the factor nn comes from ddxxn=nxn−1\frac{d}{dx}x^n = n x^{n-1}; the partial-sum identity is convolution of (an)(a_n) with the all-ones sequence 1/(1−x)1/(1-x).

Why It Holds / Derivation

ProofSolving a Linear Recurrence via OGF: the Fibonacci case

We derive the closed form of the Fibonacci numbers F0=0, F1=1, Fn=Fn−1+Fn−2F_0=0,\ F_1=1,\ F_n=F_{n-1}+F_{n-2}.

Step 1. Define F(x)=∑n≥0FnxnF(x)=\sum_{n\ge0}F_n x^n.

Step 2. Encode the recurrence. Multiply Fn=Fn−1+Fn−2F_n=F_{n-1}+F_{n-2} (valid for n≥2n\ge2) by xnx^n and sum:

∑n≥2Fnxn=∑n≥2Fn−1xn+∑n≥2Fn−2xn.\sum_{n\ge2}F_n x^n = \sum_{n\ge2}F_{n-1}x^n + \sum_{n\ge2}F_{n-2}x^n.

Step 3. Express each sum via F(x)F(x). The left side is F(x)−F0−F1x=F(x)−xF(x)-F_0 - F_1 x = F(x)-x. The first right sum is x∑n≥2Fn−1xn−1=x(F(x)−F0)=xF(x)x\sum_{n\ge2}F_{n-1}x^{n-1}=x(F(x)-F_0)=xF(x). The second is x2∑n≥2Fn−2xn−2=x2F(x)x^2\sum_{n\ge2}F_{n-2}x^{n-2}=x^2 F(x).

Step 4. Assemble: F(x)−x=xF(x)+x2F(x)F(x)-x = xF(x)+x^2 F(x), hence

6
F(x)=x1−x−x2.\quad F(x) = \frac{x}{1-x-x^2}.

Derivation hint: Collect F(x)F(x) terms on the left and factor.

Step 5. Partial fractions. Factor 1−x−x2=(1−φx)(1−ψx)1-x-x^2 = (1-\varphi x)(1-\psi x) where φ=1+52\varphi=\frac{1+\sqrt5}{2}, ψ=1−52\psi=\frac{1-\sqrt5}{2}. Decompose:

F(x)=15(11−φx−11−ψx).F(x)=\frac{1}{\sqrt5}\left(\frac{1}{1-\varphi x}-\frac{1}{1-\psi x}\right).

Step 6. Expand each geometric series 11−rx=∑nrnxn\frac{1}{1-r x}=\sum_n r^n x^n and read off coefficients:

7
Fn=φn−ψn5(Binet’s formula).\quad F_n = \frac{\varphi^n - \psi^n}{\sqrt5}\qquad(\text{Binet's formula}).

Derivation hint: [xn]11−rx=rn[x^n]\frac{1}{1-rx}=r^n; subtract the two geometric expansions termwise. ■\qquad\blacksquare

∎
mathematician · Leonhard Euler | 1707–1783

Though de Moivre first used generating functions to solve recurrences, Euler made them a central tool of analysis and number theory. His 1748 Introductio in analysin infinitorum used the product ∏k(1−xk)−1\prod_k (1-x^k)^{-1} as the generating function for partitions, leading to the Pentagonal Number Theorem and deep partition identities. Euler treated formal series with a fearless algebraic spirit—manipulating divergent series and infinite products to extract finite truths—anticipating the rigorous formal-power-series framework formalized only in the 20th century.

Building Intuition

🧠 Intuition

Think of xnx^n as a box labelled nn, and ana_n as the number of marbles in that box. The series A(x)A(x) is the entire shelf of boxes. Multiplying two generating functions is like asking: in how many ways can I pick a marble from AA's box kk and one from BB's box n−kn-k so the labels add to nn? That's exactly a convolution, which is how counting "split this object into two parts" works. The variable xx "remembers size by exponent and adds sizes by multiplication"—it is a size-tracking accountant. You never plug in a number unless you want a value (e.g. setting x=1x=1 to sum the sequence, or differentiating then setting x=1x=1 to get a weighted sum/expectation).

Figure
OGF as a shelf of labelled coefficient-boxes; product = convolution
A(x) = a₀ + a₁x + a₂x² + a₃x³ + … a₀ a₁ a₂ a₃ x⁰ x¹ x² x³ Coefficient of xⁿ in A(x)B(x): cₙ = Σₖ aₖ b₍ₙ₋ₖ₎ (convolution) labels add to n

Worked Examples

ExampleCounting compositions of an integer

Given. How many ways can a positive integer nn be written as an ordered sum of positive integers (compositions)? E.g. 3=3=2+1=1+2=1+1+13 = 3 = 2+1 = 1+2 = 1+1+1, so 44 ways.

Step 1. A single positive part has OGF P(x)=x+x2+x3+⋯=x1−xP(x)=x+x^2+x^3+\cdots = \dfrac{x}{1-x} (one object of each size ≥1\ge1).

Step 2. A composition is a sequence of ≥1\ge1 parts. The "sequence of one or more PP" construction gives

C(x)=∑k≥1P(x)k=P(x)1−P(x)=x/(1−x)1−x/(1−x)=x1−2x.C(x)=\sum_{k\ge1}P(x)^k = \frac{P(x)}{1-P(x)} = \frac{x/(1-x)}{1-x/(1-x)} = \frac{x}{1-2x}.

Step 3. Expand: x1−2x=x∑m≥0(2x)m=∑m≥02mxm+1\dfrac{x}{1-2x} = x\sum_{m\ge0}(2x)^m = \sum_{m\ge0}2^m x^{m+1}, so [xn]C(x)=2 n−1[x^n]C(x)=2^{\,n-1} for n≥1n\ge1.

Answer.  2 n−1 compositions of n \boxed{\,2^{\,n-1}\text{ compositions of }n\,} (e.g. n=3⇒22=4n=3 \Rightarrow 2^2 = 4 ✓, dimensionless count).

ExampleMean of a probability distribution via PGF

Given. Roll a fair die; let XX be the outcome. Find E[X]E[X] using the probability generating function G(x)=E[xX]=∑kP(X=k)xkG(x)=E[x^X]=\sum_k P(X=k)x^k.

Step 1. Each face has probability 16\tfrac16, so

G(x)=16(x+x2+x3+x4+x5+x6)=x(1−x6)6(1−x).G(x)=\tfrac16(x+x^2+x^3+x^4+x^5+x^6)=\frac{x(1-x^6)}{6(1-x)}.

Step 2. Use the identity E[X]=G′(1)E[X]=G'(1).

G′(x)=16(1+2x+3x2+4x3+5x4+6x5).G'(x)=\tfrac16(1+2x+3x^2+4x^3+5x^4+6x^5).

Step 3. Evaluate at x=1x=1:

G′(1)=16(1+2+3+4+5+6)=16⋅21=216=3.5.G'(1)=\tfrac16(1+2+3+4+5+6)=\tfrac16\cdot 21 = \tfrac{21}{6}=3.5.

Answer. E[X]=3.5\boxed{E[X]=3.5} (dimensionless; matches direct computation). Note: variance follows from G′′(1)+G′(1)−G′(1)2G''(1)+G'(1)-G'(1)^2.

Common Misconceptions

⚠️ Common mistake

Generating functions are formal, not analytic—by default. You do not need ∑anxn\sum a_n x^n to converge anywhere for the algebra to be valid. The series ∑n! xn\sum n!\,x^n has radius of convergence 00, yet is a perfectly good element of Q[[x]]\mathbb{Q}[[x]] on which you may add, multiply, and invert. Convergence is required only when you actually substitute a numerical value of xx (e.g. setting x=1/2x=1/2, or applying analytic methods like singularity analysis to estimate ana_n). Confusing the formal and analytic viewpoints leads to false statements about "the value" of a divergent generating function.

Counterexample

Multiplication is not always valid for two-sided or Laurent series. The Cauchy product ∑n(∑kakbn−k)\sum_n(\sum_k a_k b_{n-k}) requires each coefficient to be a finite sum. If you allow doubly-infinite series ∑n∈Zanxn\sum_{n\in\mathbb{Z}} a_n x^n, the convolution ∑kakbn−k\sum_k a_k b_{n-k} can become an infinite sum with no formal meaning. For example, formally multiplying ∑n≥0xn\sum_{n\ge0}x^n by ∑n≥0x−n\sum_{n\ge0}x^{-n} gives a coefficient of x0x^0 equal to ∑k≥01⋅1=∞\sum_{k\ge0}1\cdot1=\infty. Thus the ring structure R[[x]]R[[x]] relies crucially on series being bounded below in degree.

Connections

connection

Generating functions sit at a crossroads of many fields. The OGF dictionary is the engine of Analytic Combinatorics, where the location and nature of singularities of A(x)A(x) control the asymptotic growth of ana_n (Flajolet–Sedgewick). In probability, the Probability Generating Function and Moment Generating Function are specializations giving moments and supporting limit theorems. The EGF formalism is the species-theoretic shadow of Exponential Formula for labelled structures. Number theory uses Dirichlet Series ∑an/ns\sum a_n/n^s—a "multiplicative" cousin where convolution corresponds to Dirichlet rather than Cauchy product. The recurrence-solving method generalizes via the Z-transform in signal processing and the Laplace Transform in the continuous setting.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1730 — Abraham de Moivre, in Miscellanea Analytica, introduces generating functions to solve linear recurrences, including a closed form for Fibonacci-type sequences.
- 1748 — Euler publishes Introductio in analysin infinitorum, using infinite products as generating functions for partitions; foundation of partition theory.
- 1750s — Euler discovers the Pentagonal Number Theorem, expressing ∏(1−xk)\prod(1-x^k) as a sparse signed series.
- 1782 — Pierre-Simon Laplace systematizes "fonctions génératrices" in his probability work, coining the modern term and applying them to difference equations.
- 1812 — Laplace's Théorie analytique des probabilités makes generating functions (and the Laplace transform) central to probability.
- 1900s–1930s — Formal power series receive rigorous algebraic foundations; the ring R[[x]]R[[x]] is studied abstractly.
- 1960s — Donald Knuth popularizes generating functions for rigorous algorithm analysis.
- 1970s–1980s — Gian-Carlo Rota, Joyal's theory of species, and the umbral calculus give combinatorics a structural, functorial language for generating functions.
- 1990s–2009 — Philippe Flajolet and Robert Sedgewick develop Analytic Combinatorics, linking singularities of generating functions to coefficient asymptotics; Wilf's generatingfunctionology becomes a standard text.

Subtleties & Limits

open-problem

Generating functions are most powerful when they have a nice closed form, but classifying which sequences admit which kind of GF is deep. A sequence is D-finite (holonomic) iff its OGF satisfies a linear ODE with polynomial coefficients; algebraic iff the OGF satisfies a polynomial equation; and these classes are properly nested. Determining whether a given combinatorially defined OGF is algebraic, D-finite, or "wild" can be extremely hard. Famous open territory: many sequences in enumerative combinatorics (e.g. certain self-avoiding-walk and lattice-animal counts) are conjectured to be non-D-finite, but proving non-holonomicity is notoriously difficult. The automatic/transcendence classification of multivariate generating functions, and effective asymptotics from implicit functional equations, remain active research areas.

See Also

Power Series · Recurrence Relations · Probability Generating Function · Moment Generating Function · Analytic Combinatorics · Partition Theory · Dirichlet Series · Z-transform · Catalan Numbers · Exponential Formula