Skip to content

Generating functions

Write a die’s chances as a polynomial, multiply two of them to add two dice, and read the mean and variance from derivatives.

Helps to know firstSums of random variables

Helps to know first
This topic

First, the picture

Write a die as a polynomial, one power of zz per face. Watch the polynomial multiply by itself: its numbers become the chances of two dice.

Adding dice by multiplying

The polynomial of one die, multiplied by itself: its numbers are the chances of the total.

One die: (z + z^2 + z^3 + z^4 + z^5 + z^6)/6. Each power of z is a face, and its number, 1/6, is that face's chance.

dice n
1
mean, 3.5n
3.5
variance, 35n/12
2.9167
0.00 / 10.20 s
Describe this picture

Bars of the numbers of a polynomial, one bar for each power of zz, on an axis of powers from 0 to 36 and numbers from 0 to 0.2, the tallest labelled with its value. The clip opens on one die, 16(z+z2+z3+z4+z5+z6)\tfrac16(z+z^2+z^3+z^4+z^5+z^6), written in the caption: six bars of height 16\tfrac16. The polynomial is multiplied by itself and the bars change into the triangle of two dice, from z2z^2 to z12z^{12} with the tallest bar, 636\tfrac6{36}, at z7z^7. It is multiplied once more, for three dice: the bars run from z3z^3 to z18z^{18}, and the two tallest, 27216=0.125\tfrac{27}{216}=0.125, are at z10z^{10} and z11z^{11}. Readouts give the number of dice nn, the mean 3.5n3.5n and the variance 35n/1235n/12. When the clip has finished, a slider, “Number of dice, n”, sets nn from 1 to 6, and the caption gives the range of powers, the tallest bar, the mean and the variance.

A die as a polynomial

On the page Sums of random variables, the chances of a two-dice total came from sliding one die’s chances along the other’s: a convolution. That works, but it is slow to do by hand, and slower for three dice. Here is a trick that does it by ordinary multiplication.

Write the chances of a die as the numbers in front of the powers of a letter zz, one power per face:

G(z)=16z+16z2+16z3+16z4+16z5+16z6.G(z)=\tfrac16z+\tfrac16z^2+\tfrac16z^3+\tfrac16z^4+\tfrac16z^5+\tfrac16z^6 .

The letter zz is only a place-holder: the power says which face, and the number in front says its chance. Now multiply G(z)G(z) by itself. A term 16za\tfrac16z^a from the first copy times a term 16zb\tfrac16z^b from the second gives 136za+b\tfrac1{36}z^{a+b}: the powers add, as the faces do. So the number in front of z7z^7 in G(z)2G(z)^2 collects every pair of faces that adds up to 7. There are six of them, so it is 636\tfrac6{36}, the chance of a total of 7.

Multiplying the polynomials did the convolution for me. That is the picture at the top of the page.

The generating function

For any random variable XX that takes whole-number values k=0,1,2,…k=0,1,2,\dots, the generating function is

GX(z)=∑kpX(k) zk=E{zX}.G_X(z)=\sum_k p_X(k)\,z^k=\mathbb{E}\{z^X\}.

The second form says the same thing: it is the mean of zXz^X. A coin that lands heads (X=1X=1) with chance pp has GX(z)=(1−p)+pzG_X(z)=(1-p)+pz. Putting z=1z=1 adds up all the chances, so GX(1)=1G_X(1)=1 for every XX.

The product rule. If XX and YY are independent, then

GX+Y(z)=E{zX+Y}=E{zXzY}=E{zX} E{zY}=GX(z) GY(z).G_{X+Y}(z)=\mathbb{E}\{z^{X+Y}\}=\mathbb{E}\{z^X z^Y\}=\mathbb{E}\{z^X\}\,\mathbb{E}\{z^Y\}=G_X(z)\,G_Y(z).

The third step uses independence: the mean of a product of independent variables is the product of their means. So adding independent variables multiplies their generating functions. For nn coin flips, the number of heads has G(z)=(1−p+pz)nG(z)=(1-p+pz)^n, the generating function of Bin(n,p)\mathrm{Bin}(n,p).

A common slip is to add the generating functions instead. Adding them does not describe a sum of variables at all.

Moments from derivatives

The generating function also holds the mean and variance. Differentiate:

GX′(z)=∑kk pX(k) zk−1,soGX′(1)=∑kk pX(k)=E{X}.\begin{aligned} G_X'(z)&=\sum_k k\,p_X(k)\,z^{k-1},\qquad\text{so}\\ G_X'(1)&=\sum_k k\,p_X(k)=\mathbb{E}\{X\}. \end{aligned}

Differentiate again and put z=1z=1: GX′′(1)=∑kk(k−1) pX(k)=E{X(X−1)}G_X''(1)=\sum_k k(k-1)\,p_X(k)=\mathbb{E}\{X(X-1)\}. Since Var{X}=E{X2}−E{X}2\mathrm{Var}\{X\}=\mathbb{E}\{X^2\}-\mathbb{E}\{X\}^2 and E{X2}=E{X(X−1)}+E{X}\mathbb{E}\{X^2\}=\mathbb{E}\{X(X-1)\}+\mathbb{E}\{X\},

Var{X}=GX′′(1)+GX′(1)−GX′(1)2.\mathrm{Var}\{X\}=G_X''(1)+G_X'(1)-G_X'(1)^2 .

For one die, G′(1)=16(1+2+3+4+5+6)=216=3.5G'(1)=\tfrac16(1+2+3+4+5+6)=\tfrac{21}6=3.5 and G′′(1)=16(0+2+6+12+20+30)=706=11.67G''(1)=\tfrac16(0+2+6+12+20+30)=\tfrac{70}6=11.67. So the variance is 11.67+3.5−12.25=2.91711.67+3.5-12.25=2.917, which is 3512\tfrac{35}{12}, the die’s variance.

For nn dice, G(z)nG(z)^n gives mean 3.5n3.5n and variance 3512n\tfrac{35}{12}n, as the readouts show when you drag the number of dice after the clip. With three dice, the mean is 10.5 and the variance is 8.75.

Two relatives

The same idea works with other functions in place of zXz^X.

The moment generating function is MX(t)=E{etX}M_X(t)=\mathbb{E}\{e^{tX}\}. It is the generating function with z=etz=e^t, and it works for variables that are not whole numbers. Its derivative at t=0t=0 is the mean.

The characteristic function is χX(Ω)=E{ejΩX}\chi_X(\Omega)=\mathbb{E}\{e^{j\Omega X}\}, where jj is the square root of −1-1, as in the signal processing pages. It always exists, even when MXM_X does not, and it is the tool behind the usual proof of The central limit theorem. The product rule holds for both: adding independent variables multiplies their functions.

Worked example

  1. One die. G′(1)=3.5G'(1)=3.5, G′′(1)=70/6=11.67G''(1)=70/6=11.67, variance 11.67+3.5−12.25=2.91711.67+3.5-12.25=2.917.
  2. Two dice. The number in front of z7z^7 in G(z)2G(z)^2 is 636\tfrac6{36}; in front of z12z^{12} it is 136\tfrac1{36}.
  3. Three dice. The chances of 10 and of 11 are both 27216=0.125\tfrac{27}{216}=0.125. The mean is 10.5 and the variance 8.75.
  4. A coin. G(z)=1−p+pzG(z)=1-p+pz, so G′(1)=pG'(1)=p, the mean.
  5. Binomial. nn flips: G(z)=(1−p+pz)nG(z)=(1-p+pz)^n.
  6. Always. GX(1)=1G_X(1)=1.

Where you’ll meet this

Generating functions turn counting problems into algebra: how many ways a total can be made from coins or dice, for example. They give the chance that a family name dies out, where each person has a random number of children and the generating function of the whole tree is built by substituting one generating function into another. And they give exact distributions of sums where simulation gives only estimates.

In signal processing · transforms of a pmf

A pmf is a list of numbers, like a signal. Its generating function is the z-transform of The z-transform with zz replaced by 1/z1/z: signal processing writes z−kz^{-k} where this page writes zkz^k. Its characteristic function is the DTFT of The DTFT evaluated at −Ω-\Omega, because the DTFT uses e−jΩke^{-j\Omega k}. In both worlds, convolving two lists multiplies their transforms.

Reference card

QuantityFormulaNotes
Generating functionGX(z)=∑kpX(k)zk=E{zX}G_X(z)=\sum_k p_X(k)z^k=\mathbb{E}\{z^X\}whole-number XX
AlwaysGX(1)=1G_X(1)=1the chances add to 1
SumGX+Y=GX GYG_{X+Y}=G_X\,G_Yindependent XX, YY
MeanGX′(1)=E{X}G_X'(1)=\mathbb{E}\{X\}
VarianceGX′′(1)+GX′(1)−GX′(1)2G_X''(1)+G_X'(1)-G_X'(1)^2
Coin, binomial1−p+pz1-p+pz; (1−p+pz)n(1-p+pz)^n
Moment generatingMX(t)=E{etX}M_X(t)=\mathbb{E}\{e^{tX}\}MX′(0)=E{X}M_X'(0)=\mathbb{E}\{X\}
CharacteristicχX(Ω)=E{ejΩX}\chi_X(\Omega)=\mathbb{E}\{e^{j\Omega X}\}the DTFT of the pmf at −Ω-\Omega

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look