taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Volume C1 · Basic concepts · TAOCP Section 1.2.10

Analysis of an algorithm

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Read it next to Section 1.2.10 of the book. The nodes and the exercises of the section.

Contents
  1. Analysis of an algorithm

Analysis of an algorithm

Overview

Section 1.2.10 of TAOCP studies one small algorithm, Algorithm M, which scans a table from its end and finds the largest entry. Its running time is a sum of step counts, and all counts are fixed by nn except one: AA, the number of times that the current maximum changes. The section finds the least value, the greatest value, the average and the standard deviation of AA when the input is a random order of distinct numbers. For this it sets up a recursion for the probabilities, turns it into a recursion for a generating function, and reads the mean and the variance from the derivatives of the generating function at 11. It then proves that the mean and the variance of a product of generating functions are sums, uses this on two more distributions (coin tossing and the uniform distribution), and relates the method to the classical language of probability: characteristic functions, convolution, cumulants and the tail inequalities. The section uses the sums and the harmonic numbers of Sections 1.2.3 and 1.2.7, the Stirling numbers of Section 1.2.5, and the generating functions of Section 1.2.9.

The ideas

The cost of a program is a list of step counts. Take each step of the algorithm and count how often it runs. Multiply each count by the time of the step on a given machine, and add. Four of the five counts of Algorithm M are fixed by nn. The fifth, AA, depends on the data. The analysis of the algorithm is the analysis of AA.

Four numbers describe a data-dependent count. The least value says what happens in the best case. The greatest value says what happens in the worst case. The average says what happens in the typical case, and it needs a model of the input: here, all n!n! orders of nn distinct numbers are equally likely. The standard deviation says how far a single run can be from the average. The four numbers together are the quadruple (min⁡,ave,max⁡,dev)(\min, \mathrm{ave}, \max, \mathrm{dev}) of the book.

Probabilities by recursion, then by generating function. Let pnkp_{nk} be the probability that A=kA = k. The first element of the input either is the largest or it is not, and this gives a recursion in nn. A recursion on a table of numbers becomes a recursion on one polynomial Gn(z)=∑kpnkzkG_n(z) = \sum_k p_{nk} z^k. For Algorithm M the polynomial recursion is a product with a linear factor, so GnG_n itself is a product of linear factors.

The mean and the variance are derivatives at 11. If G(z)=∑kpkzkG(z) = \sum_k p_k z^k with ∑kpk=1\sum_k p_k = 1, then G(1)=1G(1) = 1, the mean is G′(1)G'(1) and the variance is G′′(1)+G′(1)−G′(1)2G''(1) + G'(1) - G'(1)^2. These are cheap to compute even when the probabilities themselves have no simple form. For the uniform distribution the derivatives at 11 come from the Taylor series of G(1+z)G(1 + z), with no limit of a ratio.

A product is a sum for these two numbers. The mean and the variance of GHGH are the sums of the means and of the variances. The reason is that ln⁡G\ln G turns the product into a sum, and the mean and the variance are the first two derivatives of ln⁡G(et)\ln G(e^t) at t=0t = 0. This is the property that makes the generating function method work: a random quantity that is a sum of independent parts has a generating function that is a product.

Tail inequalities bound the chance of a large deviation. The generating function also bounds Pr⁡(X≤r)\Pr(X \le r) and Pr⁡(X≥r)\Pr(X \ge r) by x−rG(x)x^{-r} G(x), for a free parameter xx. The idea is the same as in Chebyshev's inequality: a rare event must have a small share of a sum of nonnegative terms.

C1 follows the order of the section. It proves the equations by the shortest route, and it checks each result against an exact count of all permutations of n≤7n \le 7 elements. Where the section defers a proof (Theorem A), C1 gives the proof at the place of the theorem. For eq. 1.2.10(13) and (21) C1 gives both the algebraic proof and the check by a small case.

Definitions, results and proofs

Definition C1.1.2.10.1 (the notation for a random quantity, TAOCP Section 1.2.10). Let XX be a quantity that takes real values with a probability. The notation X=(min⁡a,ave b,max⁡c,dev d)X = (\min a, \mathrm{ave}\, b, \max c, \mathrm{dev}\, d) states four facts: the least value that XX takes is aa, the average is bb, the greatest is cc, and the standard deviation is dd. The standard deviation is the square root of the variance, and the variance is the average of (X−b)2(X - b)^2.

Node: TA0BFM039A · status: not stated yet

Explanation. The notation puts the whole analysis of one quantity in one line. The book uses it for the rest of the volume. A quantity such as the number of comparisons of a sort gets its line, and a reader can compare two algorithms by comparing two lines. The four numbers are not independent: always a≤b≤ca \le b \le c, and d≤(c−a)/2d \le (c - a)/2 (a variable that stays in an interval of length LL has a standard deviation at most L/2L/2).

Example. For a fair die, the faces 1,…,61, \ldots, 6 are equally likely, and the quadruple is (min⁡1,ave 7/2,max⁡6,dev 35/12)(\min 1, \mathrm{ave}\, 7/2, \max 6, \mathrm{dev}\, \sqrt{35/12}), which is about (1,3.5,6,1.708)(1, 3.5, 6, 1.708). The variance is (1+4+9+16+25+36)/6−(7/2)2=91/6−49/4=35/12(1 + 4 + 9 + 16 + 25 + 36)/6 - (7/2)^2 = 91/6 - 49/4 = 35/12. For a quantity that is always 55, the quadruple is (5,5,5,0)(5, 5, 5, 0).

Proof of the bound d≤(c−a)/2d \le (c - a)/2. Let the values lie in [a,c][a, c] with mean bb. Then (X−a)(c−X)≥0(X - a)(c - X) \ge 0, so E[X2]≤(a+c)E[X]−acE[X^2] \le (a + c)E[X] - ac. The variance is E[X2]−b2≤(a+c)b−ac−b2=(b−a)(c−b)E[X^2] - b^2 \le (a + c)b - ac - b^2 = (b - a)(c - b). The product (b−a)(c−b)(b - a)(c - b) of two numbers with a fixed sum c−ac - a is at most ((c−a)/2)2((c - a)/2)^2. Take square roots. □\square

Definition C1.1.2.10.2 (the probabilities of the count AA, TAOCP eq. 1.2.10(1)). Let pnkp_{nk} be the probability that AA is kk when the entries X[1],…,X[n]X[1], \ldots, X[n] are distinct and each of the n!n! orders is equally likely:

pnk=the number of permutations of n objects for which A=kn!.p_{nk} = \frac{\text{the number of permutations of } n \text{ objects for which } A = k}{n!}.

Node: TA3YXY6HR1 · Lean: TAOCP.S1_2_10.prob, TAOCP.S1_2_10.permutations'_range · status: not stated yet

Explanation. Algorithm M uses the entries only by comparing them. So only the relative order of the entries matters, and the entries can be taken to be 1,2,…,n1, 2, \ldots, n in some order. The probability of an event is then the share of the permutations that belong to the event. The model assumes distinct entries and a uniform order. Exercises 1.2.10-7 to 10 and 18 change the model.

Example. For n=4n = 4 the 2424 permutations of {1,2,3,4}\{1, 2, 3, 4\} give A=0,1,2,3A = 0, 1, 2, 3 in 6,11,6,16, 11, 6, 1 cases. So p40=1/4p_{40} = 1/4, p41=11/24p_{41} = 11/24, p42=1/4p_{42} = 1/4 and p43=1/24p_{43} = 1/24. The sum is 11. For instance the order 3 1 4 23\,1\,4\,2 gives A=1A = 1: the scan starts with m=2m = 2, the entry 44 is larger and AA becomes 11, and then 11 and 33 are smaller than 44. A direct count over all permutations of n≤7n \le 7 objects, done by machine, agrees with the numbers of eq. 1.2.10(9) below.

Definition C1.1.2.10.3 (the average of AA, TAOCP eq. 1.2.10(2)). The average, also the mean or the expected value, is

An=∑kk pnk.A_n = \sum_k k\, p_{nk}.

Node: TA6Q6S7X2X · Lean: TAOCP.S1_2_10.eq2_M · status: not stated yet

Explanation. It is the sum of the values weighted by their probabilities. Since 0≤A≤n−10 \le A \le n - 1, the average lies between 00 and n−1n - 1. The sum has finitely many nonzero terms for each nn.

Example. For n=4n = 4: A4=0⋅624+1⋅1124+2⋅624+3⋅124=2624=1312A_4 = 0 \cdot \frac{6}{24} + 1 \cdot \frac{11}{24} + 2 \cdot \frac{6}{24} + 3 \cdot \frac{1}{24} = \frac{26}{24} = \frac{13}{12}. For n=3n = 3 the three nonzero probabilities are 1/3,1/2,1/61/3, 1/2, 1/6 for k=0,1,2k = 0, 1, 2, so A3=1/2+2/6=5/6A_3 = 1/2 + 2/6 = 5/6.

Definition C1.1.2.10.4 (the variance of AA, TAOCP eq. 1.2.10(3)). The variance is the average of (A−An)2(A - A_n)^2, and it has a second form. The standard deviation is σn=Vn\sigma_n = \sqrt{V_n}.

Vn=∑k(k−An)2pnk=∑kk2pnk−An2.V_n = \sum_k (k - A_n)^2 p_{nk} = \sum_k k^2 p_{nk} - A_n^2.

Node: TAWKTCXWTM · Lean: TAOCP.S1_2_10.eq3, TAOCP.S1_2_10.eq3_M · status: proved

Explanation. The variance measures the spread around the average. The second form is the one for calculation, since it needs only the sum of k2pnkk^2 p_{nk}.

Example. For n=4n = 4: ∑kk2p4k=(11+24+9)/24=11/6\sum_k k^2 p_{4k} = (11 + 24 + 9)/24 = 11/6, and A42=169/144A_4^2 = 169/144. So V4=264/144−169/144=95/144≈0.660V_4 = 264/144 - 169/144 = 95/144 \approx 0.660, and σ4≈0.812\sigma_4 \approx 0.812.

Proof of the second form. Expand the square: (k−An)2=k2−2Ank+An2(k - A_n)^2 = k^2 - 2 A_n k + A_n^2. Sum with the weights pnkp_{nk}. The first part is ∑k2pnk\sum k^2 p_{nk}. The second is −2An∑kpnk=−2An2-2A_n \sum k p_{nk} = -2 A_n^2 by eq. 1.2.10(2). The third is An2∑pnk=An2A_n^2 \sum p_{nk} = A_n^2, since the probabilities add to 11. The total is ∑k2pnk−An2\sum k^2 p_{nk} - A_n^2. □\square

Lemma C1.1.2.10.5 (Chebyshev's inequality, a fact that TAOCP Section 1.2.10 states in the text). Let XX have mean μ\mu and standard deviation σ>0\sigma > 0. For each real r≥1r \ge 1, the probability that ∣X−μ∣>rσ|X - \mu| > r\sigma is less than 1/r21/r^2.

Node: none · Lean: ProbabilityTheory.meas_ge_le_variance_div_sq, from Mathlib

Explanation. The standard deviation is a true unit of distance for a random quantity: more than rr units from the mean has a chance below 1/r21/r^2, for each distribution. It is crude. For the normal distribution the chance of more than two units is 4.55%4.55\%, but the inequality says only less than 25%25\%. Its value is that it holds for each distribution with a variance.

Example. For n=10n = 10 the count AA of Algorithm M has μ=4861/2520≈1.929\mu = 4861/2520 \approx 1.929 and σ≈1.174\sigma \approx 1.174. Take r=2r = 2. The inequality says Pr⁡(∣A−μ∣>2.349)<1/4\Pr(|A - \mu| > 2.349) < 1/4. Since μ−2.349<0\mu - 2.349 < 0 and μ+2.349≈4.28\mu + 2.349 \approx 4.28, the event is A≥5A \ge 5. By eq. 1.2.10(9) its chance is ([106]+⋯+[1010])/10!=(63273+9450+870+45+1)/3628800=73639/3628800≈0.0203(\left[{10 \atop 6}\right] + \cdots + \left[{10 \atop 10}\right])/10! = (63273 + 9450 + 870 + 45 + 1)/3628800 = 73639/3628800 \approx 0.0203.

Proof. Let pp be the probability that ∣X−μ∣>rσ|X - \mu| > r\sigma. If p=0p = 0, there is nothing to prove. If p>0p > 0, then the variance, which is the average of (X−μ)2(X - \mu)^2, is at least the part of the average that comes from the outcomes with ∣X−μ∣>rσ|X - \mu| > r\sigma. On these outcomes (X−μ)2>r2σ2(X - \mu)^2 > r^2\sigma^2, so the part is more than p r2σ2p\, r^2 \sigma^2. Hence σ2>p r2σ2\sigma^2 > p\, r^2 \sigma^2, and p<1/r2p < 1/r^2. □\square

Theorem C1.1.2.10.6 (the recursion of the probabilities, TAOCP eq. 1.2.10(4)). For n≥2n \ge 2 and all integers kk,

pnk=1npn−1,k−1+n−1npn−1,k.p_{nk} = \frac{1}{n} p_{n-1,k-1} + \frac{n-1}{n} p_{n-1,k}.

Node: TAS16HR5JE · Lean: TAOCP.S1_2_10.eq4_succ, TAOCP.S1_2_10.eq4_zero, TAOCP.S1_2_10.N_cons_succ, TAOCP.S1_2_10.N_cons_zero (partial) · status: not stated yet

Explanation. Look at the first entry x1x_1 of the order. With probability 1/n1/n it is the largest entry. In this case it is a new maximum when the scan reaches it, so AA is one more than it is on the other n−1n - 1 entries. With probability (n−1)/n(n-1)/n it is not the largest. Then some later entry is larger and the scan never changes mm at x1x_1, so AA equals its value on the other n−1n - 1 entries. The other n−1n - 1 entries are in random order again, so their AA has the probabilities pn−1,⋅p_{n-1,\cdot}.

Example. For n=4n = 4 and k=1k = 1: p41=14p30+34p31=14⋅13+34⋅12=112+38=1124p_{41} = \frac{1}{4} p_{30} + \frac{3}{4} p_{31} = \frac{1}{4} \cdot \frac{1}{3} + \frac{3}{4} \cdot \frac{1}{2} = \frac{1}{12} + \frac{3}{8} = \frac{11}{24}. This is the number counted above.

Proof. Count permutations instead of probabilities. Let Pnk=n! pnkP_{nk} = n!\, p_{nk} be the number of permutations x1…xnx_1 \ldots x_n of {1,…,n}\{1, \ldots, n\} with A=kA = k. Split them by x1x_1. Case x1=nx_1 = n: the tail x2…xnx_2 \ldots x_n is any permutation of {1,…,n−1}\{1, \ldots, n - 1\}. The scan reaches x1x_1 last, and x1x_1 is larger than all that it has seen, so it counts as a change of mm. Hence A(x1…xn)=A(x2…xn)+1A(x_1 \ldots x_n) = A(x_2 \ldots x_n) + 1, and there are Pn−1,k−1P_{n-1,k-1} such permutations with A=kA = k. Case x1≠nx_1 \ne n: there are n−1n - 1 choices of x1x_1. Since nn is in the tail, x1x_1 is not larger than the maximum of the tail, so the scan does not change mm at x1x_1 and A(x1…xn)=A(x2…xn)A(x_1 \ldots x_n) = A(x_2 \ldots x_n). The count AA of the tail depends only on the relative order of the tail, so for each choice of x1x_1, exactly Pn−1,kP_{n-1,k} tails have A=kA = k. The total is Pnk=Pn−1,k−1+(n−1)Pn−1,kP_{nk} = P_{n-1,k-1} + (n-1) P_{n-1,k}. Divide by n!=n⋅(n−1)!n! = n \cdot (n-1)!. □\square

Theorem C1.1.2.10.7 (the start of the recursion, TAOCP eq. 1.2.10(5)). p1k=δ0kp_{1k} = \delta_{0k}, and pnk=0p_{nk} = 0 for k<0k < 0.

Node: TA17B4ZV4M · Lean: TAOCP.S1_2_10.eq5 (partial) · status: proved

Explanation. With one entry the algorithm never changes mm in step M4, so A=0A = 0 with probability 11. The count AA is never negative. These two facts and the recursion of eq. 1.2.10(4) fix all pnkp_{nk}, by induction on nn. The recursion gives, for example, pn0=n−1npn−1,0p_{n0} = \frac{n-1}{n} p_{n-1,0} (since pn−1,−1=0p_{n-1,-1} = 0), so pn0=1/np_{n0} = 1/n, which is exercise 1.2.10-1: A=0A = 0 exactly when the last entry is the largest, and this has probability 1/n1/n.

Example. From p1⋅=(1)p_{1\cdot} = (1): p2⋅=(12,12)p_{2\cdot} = (\frac{1}{2}, \frac{1}{2}), since p20=12p10p_{20} = \frac{1}{2} p_{10} and p21=12p10p_{21} = \frac{1}{2} p_{10}. Then p3⋅=(13,12,16)p_{3\cdot} = (\frac{1}{3}, \frac{1}{2}, \frac{1}{6}), and p4⋅=(14,1124,14,124)p_{4\cdot} = (\frac{1}{4}, \frac{11}{24}, \frac{1}{4}, \frac{1}{24}).

Proof. With n=1n = 1 the scan has no step M3 to M5, and M4 never runs. So A=0A = 0. The count AA counts runs of a step, so it is not negative. □\square

Definition C1.1.2.10.8 (the generating function of the probabilities, TAOCP eq. 1.2.10(6)).

Gn(z)=pn0+pn1z+pn2z2+⋯=∑kpnkzk.G_n(z) = p_{n0} + p_{n1} z + p_{n2} z^2 + \cdots = \sum_k p_{nk} z^k.

Node: TAMMRQ99SY · Lean: TAOCP.S1_2_10.pgf, TAOCP.S1_2_10.coeff_pgf · status: not stated yet

Explanation. This is the ordinary generating function of Section 1.2.9, with the probabilities as coefficients. Since pnk=0p_{nk} = 0 for k≥nk \ge n, GnG_n is a polynomial of degree at most n−1n - 1, and the infinite sum is only a convenient way to write it.

Example. G1(z)=1G_1(z) = 1, G2(z)=(1+z)/2G_2(z) = (1 + z)/2, and G4(z)=(6+11z+6z2+z3)/24G_4(z) = (6 + 11 z + 6 z^2 + z^3)/24.

Theorem C1.1.2.10.9 (the recursion of the generating functions, TAOCP eq. 1.2.10(7)). For n≥2n \ge 2,

Gn(z)=z+n−1nGn−1(z),G1(z)=1.G_n(z) = \frac{z + n - 1}{n} G_{n-1}(z), \qquad G_1(z) = 1.

Node: TAH26XMEDX · Lean: TAOCP.S1_2_10.eq7 · status: proved

Explanation. The recursion of eq. 1.2.10(4) has a shift in the index kk in one term. A shift is a multiplication by zz in the generating function. This turns the two-term recursion of numbers into a product with a linear factor.

Example. G3(z)=z+23⋅1+z2=z2+3z+26G_3(z) = \frac{z + 2}{3} \cdot \frac{1 + z}{2} = \frac{z^2 + 3z + 2}{6}. The coefficients 26,36,16\frac{2}{6}, \frac{3}{6}, \frac{1}{6} are the p3kp_{3k}.

Proof. Multiply eq. 1.2.10(4) by zkz^k and sum over kk. The left side gives Gn(z)G_n(z). The first term on the right gives 1n∑kpn−1,k−1zk=zn∑kpn−1,k−1zk−1=znGn−1(z)\frac{1}{n} \sum_k p_{n-1,k-1} z^k = \frac{z}{n} \sum_k p_{n-1,k-1} z^{k-1} = \frac{z}{n} G_{n-1}(z). The second term gives n−1nGn−1(z)\frac{n-1}{n} G_{n-1}(z). Add. The start comes from eq. 1.2.10(5): G1=∑kδ0kzk=1G_1 = \sum_k \delta_{0k} z^k = 1. □\square

Theorem C1.1.2.10.10 (the product form, TAOCP eq. 1.2.10(8)).

Gn(z)=(z+1)(z+2)⋯(z+n−1)n!=1z+n(z+nn).G_n(z) = \frac{(z+1)(z+2)\cdots(z+n-1)}{n!} = \frac{1}{z+n} \binom{z+n}{n}.

Node: TAYW2YGGYP · Lean: TAOCP.S1_2_10.eq8 · status: proved

Explanation. The generating function of AA is, up to the factor n!n!, a rising power. It is also a binomial coefficient with a real upper index (Section 1.2.6), divided by z+nz + n. The form as a product is the useful one: a product of the linear polynomials Qk(z)=(z+k−1)/kQ_k(z) = (z + k - 1)/k for k=2,…,nk = 2, \ldots, n. Each QkQ_k is the generating function of a quantity that is 11 with probability 1/k1/k and 00 otherwise.

Example. For n=4n = 4: (z+1)(z+2)(z+3)/24=(z3+6z2+11z+6)/24(z+1)(z+2)(z+3)/24 = (z^3 + 6z^2 + 11z + 6)/24, as in the example of eq. 1.2.10(6). For z=1z = 1 the product is 2⋅3⋅4/24=12 \cdot 3 \cdot 4/24 = 1, as it must be.

Proof. Apply eq. 1.2.10(7) again and again, down to G1=1G_1 = 1: Gn=z+n−1n⋅z+n−2n−1⋯z+12⋅G1G_n = \frac{z+n-1}{n} \cdot \frac{z+n-2}{n-1} \cdots \frac{z+1}{2} \cdot G_1. The denominators multiply to n!/1n!/1. For the second form, (z+nn)=(z+n)(z+n−1)⋯(z+1)n!\binom{z+n}{n} = \frac{(z+n)(z+n-1)\cdots(z+1)}{n!} by the definition of the binomial coefficient in Section 1.2.6, and division by z+nz + n removes the first factor. □\square

Theorem C1.1.2.10.11 (the probabilities by Stirling numbers, TAOCP eq. 1.2.10(9)). For n≥1n \ge 1 and k≥0k \ge 0,

pnk=[nk+1]/n!.p_{nk} = \left[{n \atop k+1}\right] \Big/ n!.

Node: TAT2XETC9Z · Lean: TAOCP.S1_2_10.eq9 · status: proved

Explanation. The number of permutations of nn objects with exactly k+1k + 1 cycles is the Stirling number of the first kind [nk+1]\left[{n \atop k+1}\right] (Section 1.2.6). The theorem says that the permutations with A=kA = k are equinumerous with the permutations with k+1k + 1 cycles. This is a hint for a bijection. In fact the number of records of a permutation has the same distribution as the number of cycles, by a standard bijection between the two (see Section 1.3.3 on the cycle form). It also explains why the scan from the right counts k+1k + 1 records: one record is the entry X[n]X[n] at the start, and kk records are the changes.

Example. For n=5n = 5: [51]=24\left[{5 \atop 1}\right] = 24, [52]=50\left[{5 \atop 2}\right] = 50, [53]=35\left[{5 \atop 3}\right] = 35, [54]=10\left[{5 \atop 4}\right] = 10, [55]=1\left[{5 \atop 5}\right] = 1, and the sum is 120=5!120 = 5!. So p5k=24/120,50/120,35/120,10/120,1/120p_{5k} = 24/120, 50/120, 35/120, 10/120, 1/120 for k=0,…,4k = 0, \ldots, 4. A direct count of all 120120 permutations by a program gives the same five numbers.

Proof. Put x=−zx = -z in eq. 1.2.6(44), the expansion of xn‾x^{\underline{n}} by Stirling numbers, and multiply by (−1)n(-1)^n. This gives z(z+1)⋯(z+n−1)=∑k[nk]zkz(z+1)\cdots(z+n-1) = \sum_k \left[{n \atop k}\right] z^k. Divide by z n!z\, n!. By eq. 1.2.10(8), Gn(z)=1n!∑k[nk]zk−1G_n(z) = \frac{1}{n!} \sum_{k} \left[{n \atop k}\right] z^{k-1}, and the term with k=0k = 0 is zero for n≥1n \ge 1. Compare the coefficient of zkz^k: it is [nk+1]/n!\left[{n \atop k+1}\right]/n!. □\square

Definition C1.1.2.10.12 (the mean and the variance of a generating function, TAOCP eq. 1.2.10(10)). For G(z)=p0+p1z+p2z2+⋯G(z) = p_0 + p_1 z + p_2 z^2 + \cdots with pk≥0p_k \ge 0 and ∑pk=1\sum p_k = 1,

mean(G)=∑kk pk,var(G)=∑kk2pk−mean(G)2.\mathrm{mean}(G) = \sum_k k\, p_k, \qquad \mathrm{var}(G) = \sum_k k^2 p_k - \mathrm{mean}(G)^2.

Node: TAGZ4M2YDD · Lean: TAOCP.S1_2_10.mean, TAOCP.S1_2_10.var, TAOCP.S1_2_10.eq10_mean (partial) · status: not stated yet

Explanation. This is eq. 1.2.10(2) and (3) with a general table of probabilities pkp_k in place of pnkp_{nk}. The sums are finite for a polynomial. For an infinite series they can be infinite, and then the mean or the variance does not exist.

Example. For G(z)=(1+z+z2)/3G(z) = (1 + z + z^2)/3 (the values 0,1,20, 1, 2 equally likely), mean(G)=(0+1+2)/3=1\mathrm{mean}(G) = (0 + 1 + 2)/3 = 1, ∑k2pk=(0+1+4)/3=5/3\sum k^2 p_k = (0 + 1 + 4)/3 = 5/3, and var(G)=5/3−1=2/3\mathrm{var}(G) = 5/3 - 1 = 2/3.

Theorem C1.1.2.10.13 (the value at 11, TAOCP eq. 1.2.10(11)). If G(z)=∑kpkzkG(z) = \sum_k p_k z^k is a generating function of probabilities, then G(1)=1G(1) = 1.

Node: TAAYG65D26 · Lean: TAOCP.S1_2_10.eq11, TAOCP.S1_2_10.eval_one_sum (partial) · status: proved

Explanation. All outcomes together have probability 11. This is the test that a guess of a generating function must pass, and it is the one fact that Theorem A needs.

Example. G4(1)=(6+11+6+1)/24=1G_4(1) = (6 + 11 + 6 + 1)/24 = 1, and Gn(1)=n!/n!=1G_n(1) = n!/n! = 1 by eq. 1.2.10(8).

Proof. G(1)=p0+p1+p2+⋯G(1) = p_0 + p_1 + p_2 + \cdots, which is the sum of the probabilities of all values, and this is 11. □\square

Theorem C1.1.2.10.14 (the mean is the derivative at 11, TAOCP eq. 1.2.10(12)). mean(G)=G′(1)\mathrm{mean}(G) = G'(1).

Node: TAD1TRG25A · Lean: TAOCP.S1_2_10.mean, TAOCP.S1_2_10.eq10_mean (partial) · status: not stated yet

Explanation. Differentiation brings down the exponent. When the series is infinite, the derivative at 11 is read as the limit from the left, and it exists exactly when the mean exists.

Example. For G4G_4: G4′(z)=(11+12z+3z2)/24G_4'(z) = (11 + 12 z + 3 z^2)/24, so G4′(1)=26/24=13/12G_4'(1) = 26/24 = 13/12, the mean found from the definition.

Proof. For a polynomial, G′(z)=∑kkpkzk−1G'(z) = \sum_k k p_k z^{k-1}, and at z=1z = 1 this is ∑kkpk\sum_k k p_k. □\square

Theorem C1.1.2.10.15 (the variance by the first two derivatives, TAOCP eq. 1.2.10(13)). var(G)=G′′(1)+G′(1)−G′(1)2\mathrm{var}(G) = G''(1) + G'(1) - G'(1)^2.

Node: TAGKJ1H01A · Lean: TAOCP.S1_2_10.var, TAOCP.S1_2_10.eq13 (partial) · status: proved

Explanation. The second derivative at 11 is the average of k(k−1)k(k-1), a factorial moment. Adding G′(1)G'(1) gives the average of k2k^2.

Example. For G4G_4: G4′′(z)=(12+6z)/24G_4''(z) = (12 + 6z)/24, so G4′′(1)=18/24=3/4G_4''(1) = 18/24 = 3/4. Then var=3/4+13/12−169/144=(108+156−169)/144=95/144\mathrm{var} = 3/4 + 13/12 - 169/144 = (108 + 156 - 169)/144 = 95/144, the value of eq. 1.2.10(3). For G(z)=(1+z+z2)/3G(z) = (1 + z + z^2)/3: G′(1)=1G'(1) = 1, G′′(1)=2/3G''(1) = 2/3, and var=2/3+1−1=2/3\mathrm{var} = 2/3 + 1 - 1 = 2/3.

Proof. (Exercise 1.2.10-2.) G′′(z)=∑kk(k−1)pkzk−2G''(z) = \sum_k k(k-1) p_k z^{k-2}, so G′′(1)=∑kk2pk−∑kkpk=∑kk2pk−G′(1)G''(1) = \sum_k k^2 p_k - \sum_k k p_k = \sum_k k^2 p_k - G'(1). Hence ∑kk2pk=G′′(1)+G′(1)\sum_k k^2 p_k = G''(1) + G'(1). Subtract mean(G)2=G′(1)2\mathrm{mean}(G)^2 = G'(1)^2 in eq. 1.2.10(10). □\square

Theorem C1.1.2.10.16 (the average of AA, TAOCP eq. 1.2.10(14)). An=Gn′(1)=Hn−1A_n = G_n'(1) = H_n - 1, where Hn=1+12+⋯+1nH_n = 1 + \frac{1}{2} + \cdots + \frac{1}{n}.

Node: TA89E5J489 · Lean: TAOCP.S1_2_10.eq14, TAOCP.S1_2_10.eq2_M · status: proved

Explanation. The current maximum changes about ln⁡n\ln n times on a random input of nn entries, not about n/2n/2 or n\sqrt{n} times. So on the average the work of step M4 is small next to the n−1n - 1 comparisons of step M3. Since Hn=ln⁡n+γ+O(1/n)H_n = \ln n + \gamma + O(1/n) (Section 1.2.11.2), An=ln⁡n+γ−1+O(1/n)A_n = \ln n + \gamma - 1 + O(1/n), with γ−1≈−0.4228\gamma - 1 \approx -0.4228.

Example. A3=H3−1=11/6−1=5/6A_3 = H_3 - 1 = 11/6 - 1 = 5/6, A4=25/12−1=13/12A_4 = 25/12 - 1 = 13/12, H10=7381/2520H_{10} = 7381/2520, so A10=4861/2520≈1.9290A_{10} = 4861/2520 \approx 1.9290. For n=1000n = 1000, An=H1000−1≈6.4855A_n = H_{1000} - 1 \approx 6.4855, so the current maximum changes about 6.56.5 times in 10001000 entries.

Proof. Differentiate eq. 1.2.10(7) with the product rule: Gn′(z)=1nGn−1(z)+z+n−1nGn−1′(z)G_n'(z) = \frac{1}{n} G_{n-1}(z) + \frac{z+n-1}{n} G_{n-1}'(z). Put z=1z = 1. Since Gn−1(1)=1G_{n-1}(1) = 1 and z+n−1n=1\frac{z + n - 1}{n} = 1 at z=1z = 1, the result is Gn′(1)=1n+Gn−1′(1)G_n'(1) = \frac{1}{n} + G_{n-1}'(1). With G1′(1)=0G_1'(1) = 0, induction on nn gives Gn′(1)=12+⋯+1n=Hn−1G_n'(1) = \frac{1}{2} + \cdots + \frac{1}{n} = H_n - 1. □\square

Theorem C1.1.2.10.17 (mean and variance of a product, TAOCP Theorem 1.2.10A). Let GG and HH be generating functions with G(1)=H(1)=1G(1) = H(1) = 1 and with two derivatives at 11. Then mean(GH)=mean(G)+mean(H)\mathrm{mean}(GH) = \mathrm{mean}(G) + \mathrm{mean}(H) and var(GH)=var(G)+var(H)\mathrm{var}(GH) = \mathrm{var}(G) + \mathrm{var}(H). The variance is the one of eq. 1.2.10(13).

Node: TAZGCK2RXE · Lean: TAOCP.S1_2_10.theoremA_mean, TAOCP.S1_2_10.theoremA_var (partial) · status: not stated yet

Explanation. If two independent random quantities have the generating functions GG and HH, then their sum has the generating function GHGH. The theorem says that the mean and the variance of the sum are the sums of the means and of the variances, which is the familiar fact about independent quantities. The proof here works with the formulas alone: it needs only G(1)=H(1)=1G(1) = H(1) = 1.

Example. Take G(z)=(1+z)/2G(z) = (1 + z)/2 and H(z)=(2+z)/3H(z) = (2 + z)/3. Then mean(G)=1/2\mathrm{mean}(G) = 1/2, var(G)=1/4\mathrm{var}(G) = 1/4, mean(H)=1/3\mathrm{mean}(H) = 1/3, and var(H)=2/9\mathrm{var}(H) = 2/9 (from H′′=0H'' = 0: 0+1/3−1/90 + 1/3 - 1/9). The product is GH=(2+3z+z2)/6=G3GH = (2 + 3z + z^2)/6 = G_3, with mean 5/6=1/2+1/35/6 = 1/2 + 1/3 and variance 17/36=1/4+2/917/36 = 1/4 + 2/9. The check: 9/36+8/36=17/369/36 + 8/36 = 17/36.

Proof of the mean. By the product rule, (GH)′(1)=G′(1)H(1)+G(1)H′(1)=G′(1)+H′(1)(GH)'(1) = G'(1) H(1) + G(1) H'(1) = G'(1) + H'(1), since G(1)=H(1)=1G(1) = H(1) = 1. The variance is proved under eq. 1.2.10(15). □\square

Theorem C1.1.2.10.18 (the variance of a product, TAOCP eq. 1.2.10(15)). Under the assumptions of Theorem 1.2.10A, var(GH)=var(G)+var(H)\mathrm{var}(GH) = \mathrm{var}(G) + \mathrm{var}(H).

Node: TAVHTPH4AY · Lean: TAOCP.S1_2_10.theoremA_var (partial) · status: not stated yet

Explanation. The proof of C1 differentiates the product twice and uses eq. 1.2.10(13). The book gives the same result through the logarithm, as shown under eq. 1.2.10(23): the variance is the second derivative of ln⁡G(et)\ln G(e^t) at 00, and the logarithm of a product is a sum, so both the mean and the variance, and all the cumulants, add.

Example. The variance of Gn=Q2Q3⋯QnG_n = Q_2 Q_3 \cdots Q_n with Qk(z)=(z+k−1)/kQ_k(z) = (z + k - 1)/k: each QkQ_k has Qk′=1/kQ_k' = 1/k, Qk′′=0Q_k'' = 0, so var(Qk)=1/k−1/k2\mathrm{var}(Q_k) = 1/k - 1/k^2. For n=4n = 4 the sum is (1/2−1/4)+(1/3−1/9)+(1/4−1/16)=1/4+2/9+3/16=(36+32+27)/144=95/144(1/2 - 1/4) + (1/3 - 1/9) + (1/4 - 1/16) = 1/4 + 2/9 + 3/16 = (36 + 32 + 27)/144 = 95/144. This is var(G4)\mathrm{var}(G_4).

Proof. By the product rule twice, (GH)′′=G′′H+2G′H′+GH′′(GH)'' = G'' H + 2 G' H' + G H''. At 11, with G(1)=H(1)=1G(1) = H(1) = 1: (GH)′′(1)=G′′(1)+2G′(1)H′(1)+H′′(1)(GH)''(1) = G''(1) + 2G'(1)H'(1) + H''(1). Also (GH)′(1)=G′(1)+H′(1)(GH)'(1) = G'(1) + H'(1). By eq. 1.2.10(13), var(GH)=G′′(1)+2G′(1)H′(1)+H′′(1)+G′(1)+H′(1)−(G′(1)+H′(1))2\mathrm{var}(GH) = G''(1) + 2G'(1)H'(1) + H''(1) + G'(1) + H'(1) - (G'(1) + H'(1))^2. Expand the square: (G′+H′)2=G′2+2G′H′+H′2(G' + H')^2 = G'^2 + 2G'H' + H'^2. The cross terms 2G′H′2G'H' cancel, and the rest is [G′′+G′−G′2]+[H′′+H′−H′2]=var(G)+var(H)[G'' + G' - G'^2] + [H'' + H' - H'^2] = \mathrm{var}(G) + \mathrm{var}(H). □\square

Analysis C1.1.2.10.19 (the min, the average, the maximum and the deviation of AA, TAOCP eq. 1.2.10(16)).

A=(min⁡0, ave Hn−1, max⁡n−1, dev Hn−Hn(2)).A = \left(\min 0,\ \mathrm{ave}\ H_n - 1,\ \max n - 1,\ \mathrm{dev}\ \sqrt{H_n - H_n^{(2)}}\right).

Node: TAFP8Q7YC6 · Lean: TAOCP.S1_2_10.eq14, TAOCP.S1_2_10.eq16_var, TAOCP.S1_2_10.eq3_M, TAOCP.S1_2_10.changes_eq_zero_iff, TAOCP.S1_2_10.changes_lt, TAOCP.S1_2_10.changes_of_pairwise (partial) · status: not stated yet

Explanation. The least value 00 happens when X[n]X[n] is the largest entry. The greatest value n−1n - 1 happens when X[1]>X[2]>⋯>X[n]X[1] > X[2] > \cdots > X[n]: then each step finds a larger entry than the last. The mean is eq. 1.2.10(14). The variance is a sum over the factors QkQ_k: var(Gn)=∑k=2n(1/k−1/k2)\mathrm{var}(G_n) = \sum_{k=2}^{n} (1/k - 1/k^2), and the term k=1k = 1 is zero, so the sum from 22 can be written from 11: ∑k=1n(1/k−1/k2)=Hn−Hn(2)\sum_{k=1}^{n}(1/k - 1/k^2) = H_n - H_n^{(2)}, with Hn(2)=1+1/4+⋯+1/n2H_n^{(2)} = 1 + 1/4 + \cdots + 1/n^2 of Section 1.2.7. For large nn, Hn(2)→π2/6H_n^{(2)} \to \pi^2/6, so the variance is ln⁡n+γ−π2/6+o(1)\ln n + \gamma - \pi^2/6 + o(1). The deviation grows like ln⁡n\sqrt{\ln n}, so AA is concentrated near its mean.

Example. For n=4n = 4: (min⁡0,ave 13/12,max⁡3,dev 95/12)(\min 0, \mathrm{ave}\ 13/12, \max 3, \mathrm{dev}\ \sqrt{95}/12), and 95/12≈0.812\sqrt{95}/12 \approx 0.812. For n=1000n = 1000: H1000≈7.485471H_{1000} \approx 7.485471 and H1000(2)≈1.643935H_{1000}^{(2)} \approx 1.643935, so the deviation is 5.841536≈2.417\sqrt{5.841536} \approx 2.417. For n=10n = 10 the mean is 4861/2520≈1.92904861/2520 \approx 1.9290, the variance is H10−H10(2)=350339/254016≈1.3792H_{10} - H_{10}^{(2)} = 350339/254016 \approx 1.3792, and the deviation is 1.17441.1744. Exact counts over all permutations for n=1,…,7n = 1, \ldots, 7 agree with Hn−1H_n - 1 and Hn−Hn(2)H_n - H_n^{(2)}, for example n=7n = 7 gives the mean 223/140223/140 and the variance 190699/176400190699/176400.

Proof. The mean is eq. 1.2.10(14). For the variance, apply eq. 1.2.10(15) to the product Gn=∏k=2nQkG_n = \prod_{k=2}^n Q_k with Qk(z)=(z+k−1)/kQ_k(z) = (z + k - 1)/k, which satisfies Qk(1)=1Q_k(1) = 1, Qk′=1/kQ_k' = 1/k and Qk′′=0Q_k'' = 0. Then var(Qk)=0+1/k−1/k2\mathrm{var}(Q_k) = 0 + 1/k - 1/k^2 by eq. 1.2.10(13). Sum over k=2,…,nk = 2, \ldots, n, add the zero term for k=1k = 1, and split the sum. The extreme values were shown above. □\square

Theorem C1.1.2.10.20 (the recursion of the binomial distribution, TAOCP eq. 1.2.10(17)). Let a coin show heads with probability pp, let q=1−pq = 1 - p, and let pnkp_{nk} be the probability of kk heads in nn independent tosses. Then pnk=p pn−1,k−1+q pn−1,kp_{nk} = p\, p_{n-1,k-1} + q\, p_{n-1,k}.

Node: TAP1SR9VC2 · Lean: TAOCP.S1_2_10.eq17, TAOCP.S1_2_10.eq17_zero · status: proved

Explanation. Look at the last toss. With probability pp it is a head, and then the first n−1n - 1 tosses have k−1k - 1 heads. With probability qq it is a tail, and they have kk heads. The tosses are independent, so the probabilities multiply. The form is the same as that of eq. 1.2.10(4), where 1/n1/n and (n−1)/n(n-1)/n take the place of pp and qq.

Example. Take p=1/4p = 1/4 and n=2n = 2. p20=q2=9/16p_{20} = q^2 = 9/16, p21=2pq=6/16p_{21} = 2pq = 6/16, p22=p2=1/16p_{22} = p^2 = 1/16. By the recursion, p21=p⋅p10+q⋅p11=14⋅34+34⋅14=616p_{21} = p \cdot p_{10} + q \cdot p_{11} = \frac{1}{4} \cdot \frac{3}{4} + \frac{3}{4} \cdot \frac{1}{4} = \frac{6}{16}.

Proof. The event "kk heads in nn tosses" is the disjoint union of "head at toss nn and k−1k - 1 heads in the first n−1n - 1" and "tail at toss nn and kk heads in the first n−1n - 1". By independence, the probabilities of the two parts are p pn−1,k−1p\, p_{n-1,k-1} and q pn−1,kq\, p_{n-1,k}. □\square

Theorem C1.1.2.10.21 (the generating function of the binomial distribution, TAOCP eq. 1.2.10(18)). Gn(z)=(q+pz)nG_n(z) = (q + pz)^n.

Node: TA9C24E238 · Lean: TAOCP.S1_2_10.coin, TAOCP.S1_2_10.eval_coin · status: not stated yet

Explanation. This is the binomial theorem in the form of a generating function, and it shows that pnk=(nk)pkqn−kp_{nk} = \binom{n}{k} p^k q^{n-k} (exercise 1.2.10-4). The number of heads is the sum of nn independent counts, each 00 or 11 with the generating function q+pzq + pz.

Example. For n=20n = 20 and p=1/4p = 1/4, p20,5=(205)(1/4)5(3/4)15=15504⋅315/420=13904090883/68719476736≈0.20233p_{20,5} = \binom{20}{5} (1/4)^5 (3/4)^{15} = 15504 \cdot 3^{15}/4^{20} = 13904090883/68719476736 \approx 0.20233. This is the most likely value, and 55 is also the mean npnp.

Proof. As in the proof of eq. 1.2.10(7), multiply eq. 1.2.10(17) by zkz^k and sum over kk: Gn(z)=pz Gn−1(z)+q Gn−1(z)=(q+pz) Gn−1(z)G_n(z) = pz\, G_{n-1}(z) + q\, G_{n-1}(z) = (q + pz)\, G_{n-1}(z). With G1(z)=q+pzG_1(z) = q + pz (from p10=qp_{10} = q, p11=pp_{11} = p), induction gives Gn(z)=(q+pz)nG_n(z) = (q + pz)^n. □\square

Theorem C1.1.2.10.22 (the statistics of the number of heads, TAOCP eq. 1.2.10(19)). The number of heads in nn tosses has (min⁡0,ave pn,max⁡n,dev pqn)(\min 0, \mathrm{ave}\ pn, \max n, \mathrm{dev}\ \sqrt{pqn}).

Node: TAB875092J · Lean: TAOCP.S1_2_10.eq19 (partial) · status: proved

Explanation. The mean is nn times the chance of a head. The deviation grows like n\sqrt{n} while the mean grows like nn, so the relative spread q/(pn)\sqrt{q/(pn)} shrinks: the number of heads is concentrated near pnpn, which is the law of large numbers in its elementary form. The least value 00 and the greatest value nn are reached (they have the probabilities qnq^n and pnp^n) when 0<p<10 < p < 1.

Example. For n=20n = 20 and p=1/4p = 1/4: mean 55, variance 20⋅14⋅34=15/4=3.7520 \cdot \frac{1}{4} \cdot \frac{3}{4} = 15/4 = 3.75, deviation 15/2≈1.936\sqrt{15}/2 \approx 1.936. The relative spread is q/(pn)=3/20≈0.387\sqrt{q/(pn)} = \sqrt{3/20} \approx 0.387.

Proof. The generating function G1(z)=q+pzG_1(z) = q + pz has G1′(1)=pG_1'(1) = p and G1′′(1)=0G_1''(1) = 0, so mean(G1)=p\mathrm{mean}(G_1) = p and var(G1)=p−p2=pq\mathrm{var}(G_1) = p - p^2 = pq. By Theorem 1.2.10A applied n−1n - 1 times to Gn=G1nG_n = G_1^n, mean(Gn)=np\mathrm{mean}(G_n) = np and var(Gn)=npq\mathrm{var}(G_n) = npq. □\square

Theorem C1.1.2.10.23 (the generating function of the uniform distribution, TAOCP eq. 1.2.10(20)). If the values 1,2,…,n1, 2, \ldots, n are equally likely, the generating function is

G(z)=1n(z+z2+⋯+zn)=1n⋅zn+1−zz−1.G(z) = \frac{1}{n}(z + z^2 + \cdots + z^n) = \frac{1}{n} \cdot \frac{z^{n+1} - z}{z - 1}.

Node: TA25H78F6R · Lean: TAOCP.S1_2_10.unif, TAOCP.S1_2_10.eq20 (partial) · status: proved

Explanation. The second form is the sum of a finite geometric series. It has the factor z−1z - 1 in the denominator, so G(1)G(1), G′(1)G'(1) and G′′(1)G''(1) come out as the ratio 0/00/0 and need a limit. The next theorem avoids this.

Example. For n=4n = 4: G(z)=(z+z2+z3+z4)/4G(z) = (z + z^2 + z^3 + z^4)/4, and (z5−z)/(4(z−1))=z(z4−1)/(4(z−1))=z(z3+z2+z+1)/4(z^5 - z)/(4(z - 1)) = z(z^4 - 1)/(4(z - 1)) = z(z^3 + z^2 + z + 1)/4, the same polynomial.

Proof. Each of the nn values has the probability 1/n1/n, so G(z)=1n∑k=1nzkG(z) = \frac{1}{n} \sum_{k=1}^{n} z^k. The geometric sum is ∑k=1nzk=z⋅zn−1z−1=zn+1−zz−1\sum_{k=1}^n z^k = z \cdot \frac{z^n - 1}{z - 1} = \frac{z^{n+1} - z}{z - 1} for z≠1z \ne 1. □\square

Theorem C1.1.2.10.24 (the Taylor series at 11, TAOCP eq. 1.2.10(21)). If GG is a polynomial or a convergent power series near 11, then

G(1+z)=G(1)+G′(1)z+G′′(1)2!z2+⋯ .G(1 + z) = G(1) + G'(1) z + \frac{G''(1)}{2!} z^2 + \cdots.

Node: TA48NMJNPG · status: not stated yet

Explanation. This is Taylor's theorem at the point 11. It gives the use of G(1+z)G(1 + z): replace zz by 1+z1 + z in the formula for GG, expand it, and read the first coefficients. The coefficient of zz is G′(1)G'(1). The coefficient of z2z^2 is G′′(1)/2G''(1)/2. No limit is needed.

Example. For G3(z)=(z2+3z+2)/6G_3(z) = (z^2 + 3z + 2)/6: G3(1+z)=((1+z)2+3(1+z)+2)/6=(6+5z+z2)/6=1+56z+16z2G_3(1 + z) = ((1 + z)^2 + 3(1 + z) + 2)/6 = (6 + 5z + z^2)/6 = 1 + \frac{5}{6} z + \frac{1}{6} z^2. So G3′(1)=5/6G_3'(1) = 5/6 and G3′′(1)=2⋅16=1/3G_3''(1) = 2 \cdot \frac{1}{6} = 1/3.

Proof. For a polynomial of degree dd, the Taylor expansion at 11 ends at zdz^d and is exact. It follows from the binomial theorem: (1+z)k=∑j(kj)zj(1 + z)^k = \sum_j \binom{k}{j} z^j, so the coefficient of zjz^j in G(1+z)=∑kpk(1+z)kG(1 + z) = \sum_k p_k (1 + z)^k is ∑kpk(kj)\sum_k p_k \binom{k}{j}. For j=0j = 0 this is G(1)G(1). For j=1j = 1 it is ∑kkpk=G′(1)\sum_k k p_k = G'(1), and for j=2j = 2 it is ∑k(k2)pk=G′′(1)/2\sum_k \binom{k}{2} p_k = G''(1)/2. □\square

Theorem C1.1.2.10.25 (the statistics of the uniform distribution, TAOCP eq. 1.2.10(22)). For equally likely values 1,…,n1, \ldots, n,

(min⁡1, ave n+12, max⁡n, dev (n+1)(n−1)12).\left(\min 1,\ \mathrm{ave}\ \frac{n+1}{2},\ \max n,\ \mathrm{dev}\ \sqrt{\frac{(n+1)(n-1)}{12}}\right).

Node: TA65GVJ49X · Lean: TAOCP.S1_2_10.eq22_derivs, TAOCP.S1_2_10.eq22 (partial) · status: proved

Explanation. The mean is the middle of the range. The deviation is about 0.289n0.289 n for large nn, since 1/12≈0.28871/\sqrt{12} \approx 0.2887. The spread is a fixed share of the range, so a single sample says little about the next. The book calls this an unstable situation.

Example. For n=10n = 10: mean 5.55.5, variance 99/12=8.2599/12 = 8.25, deviation ≈2.872\approx 2.872. For a die (n=6n = 6) the deviation is 35/12≈1.708\sqrt{35/12} \approx 1.708, as in the first example of this section.

Proof. By eq. 1.2.10(20), G(1+z)=1n⋅(1+z)n+1−(1+z)zG(1 + z) = \frac{1}{n} \cdot \frac{(1 + z)^{n+1} - (1 + z)}{z}, since z+1−1=zz + 1 - 1 = z is the denominator after the shift. Expand (1+z)n+1=1+(n+1)z+(n+12)z2+(n+13)z3+⋯(1 + z)^{n+1} = 1 + (n+1) z + \binom{n+1}{2} z^2 + \binom{n+1}{3} z^3 + \cdots. Subtract 1+z1 + z and divide by zz: G(1+z)=1n(n+(n+12)z+(n+13)z2+⋯ )G(1 + z) = \frac{1}{n}\bigl(n + \binom{n+1}{2} z + \binom{n+1}{3} z^2 + \cdots\bigr). The first coefficient is 11. The next two are (n+12)/n=(n+1)/2\binom{n+1}{2}/n = (n+1)/2 and (n+13)/n=(n+1)(n−1)/6\binom{n+1}{3}/n = (n+1)(n-1)/6. By eq. 1.2.10(21), G′(1)=(n+1)/2G'(1) = (n+1)/2 and G′′(1)=2⋅(n+1)(n−1)/6=(n+1)(n−1)/3G''(1) = 2 \cdot (n+1)(n-1)/6 = (n+1)(n-1)/3. Then var=(n+1)(n−1)3+n+12−(n+1)24=n+112(4(n−1)+6−3(n+1))=(n+1)(n−1)12\mathrm{var} = \frac{(n+1)(n-1)}{3} + \frac{n+1}{2} - \frac{(n+1)^2}{4} = \frac{n+1}{12}\bigl(4(n-1) + 6 - 3(n+1)\bigr) = \frac{(n+1)(n-1)}{12}. □\square

Definition C1.1.2.10.26 (the cumulants, TAOCP eq. 1.2.10(23)). The cumulants κ1,κ2,κ3,…\kappa_1, \kappa_2, \kappa_3, \ldots of a generating function GG with G(1)=1G(1) = 1 are defined by the series

κ1t1!+κ2t22!+κ3t33!+⋯=ln⁡G(et).\frac{\kappa_1 t}{1!} + \frac{\kappa_2 t^2}{2!} + \frac{\kappa_3 t^3}{3!} + \cdots = \ln G(e^t).

Node: TARFKMZAQD · status: not stated yet

Explanation. The cumulants (also called semi-invariants) are the derivatives of ln⁡G(et)\ln G(e^t) at t=0t = 0. The first is the mean, the second is the variance, and the third is a measure of the skew. They have two properties. They add under the product of generating functions, since the logarithm turns a product into a sum: this holds for all the κn\kappa_n, and Theorem 1.2.10A is the case n=1,2n = 1, 2. A distribution is normal when all its cumulants after the second are zero.

Example. For the Bernoulli factor Qk(z)=(z+k−1)/kQ_k(z) = (z + k - 1)/k the third cumulant is κ3=1k(1−1k)(1−2k)\kappa_3 = \frac{1}{k}(1 - \frac{1}{k})(1 - \frac{2}{k}), the third cumulant of a coin with p=1/kp = 1/k (exercise 1.2.10-6). By additivity, the third cumulant of AA for n=4n = 4 is 0+227+332=1458640 + \frac{2}{27} + \frac{3}{32} = \frac{145}{864}. This agrees with the formula κ3=M3−3M1M2+2M13\kappa_3 = M_3 - 3 M_1 M_2 + 2 M_1^3 from the moments M1=13/12M_1 = 13/12, M2=11/6M_2 = 11/6, M3=43/12M_3 = 43/12 of the distribution of AA for n=4n = 4, computed exactly.

Proof of κ1\kappa_1 and κ2\kappa_2. Let L(t)=ln⁡G(et)L(t) = \ln G(e^t). By the chain rule, L′(t)=etG′(et)G(et)L'(t) = \frac{e^t G'(e^t)}{G(e^t)}, and at t=0t = 0 this is G′(1)/G(1)=G′(1)G'(1)/G(1) = G'(1). Differentiating again: L′′(t)=e2tG′′(et)G(et)+etG′(et)G(et)−e2tG′(et)2G(et)2L''(t) = \frac{e^{2t} G''(e^t)}{G(e^t)} + \frac{e^t G'(e^t)}{G(e^t)} - \frac{e^{2t} G'(e^t)^2}{G(e^t)^2}, and at t=0t = 0 this is G′′(1)+G′(1)−G′(1)2G''(1) + G'(1) - G'(1)^2. So κ1=mean(G)\kappa_1 = \mathrm{mean}(G) and κ2=var(G)\kappa_2 = \mathrm{var}(G) (this is a second proof of eq. 1.2.10(13)). For a product, ln⁡(GH)(et)=ln⁡G(et)+ln⁡H(et)\ln (GH)(e^t) = \ln G(e^t) + \ln H(e^t), so each cumulant of GHGH is the sum of the cumulants of GG and HH. □\square

Theorem C1.1.2.10.27 (a tail bound from below, TAOCP eq. 1.2.10(24)). If the nonnegative integer quantity XX has the generating function G(z)G(z), then for every real rr and every xx with 0<x≤10 < x \le 1,

Pr⁡(X≤r)≤x−r G(x).\Pr(X \le r) \le x^{-r}\, G(x).

Node: TAGHCHPQPW · Lean: TAOCP.S1_2_10.eq24 (partial) · status: proved

Explanation. The inequality holds for each xx in the range, so the best bound is the smallest value of the right side. An exact minimum is often hard to find, and a value of xx near the minimum gives a good bound. The idea is that for x≤1x \le 1, the power xkx^k is large for small kk, so the terms k≤rk \le r have a big share of the sum G(x)G(x).

Example. Let XX be the number of heads in 2020 tosses with p=1/4p = 1/4, so G(x)=(0.75+0.25x)20G(x) = (0.75 + 0.25x)^{20}. The true value of Pr⁡(X≤1)\Pr(X \le 1) is 0.0243130.024313. The bound x−1(0.75+0.25x)20x^{-1}(0.75 + 0.25x)^{20} equals 0.06110.0611 at x=0.1x = 0.1, 0.05760.0576 at x=0.2x = 0.2, and 0.07110.0711 at x=0.3x = 0.3. The derivative of its logarithm, −r/x+np/(q+px)-r/x + np/(q + px), is zero at x=rq/(p(n−r))=3/19≈0.158x = rq/(p(n - r)) = 3/19 \approx 0.158, where the bound is 0.05600.0560. The best bound is about 2.32.3 times the true value, but it is simple and it is valid for each rr.

Proof. For k≤rk \le r and 0<x≤10 < x \le 1, xk−r≥1x^{k - r} \ge 1. So Pr⁡(X≤r)=∑k≤rpk≤∑k≤rpkxk−r\Pr(X \le r) = \sum_{k \le r} p_k \le \sum_{k \le r} p_k x^{k-r}. The terms with k>rk > r are nonnegative, so ∑k≤rpkxk−r≤∑kpkxk−r=x−rG(x)\sum_{k \le r} p_k x^{k - r} \le \sum_{k} p_k x^{k - r} = x^{-r} G(x). □\square

Theorem C1.1.2.10.28 (a tail bound from above, TAOCP eq. 1.2.10(25)). Under the same assumptions, for every real rr and every x≥1x \ge 1,

Pr⁡(X≥r)≤x−r G(x).\Pr(X \ge r) \le x^{-r}\, G(x).

Node: TAHY3MSH7A · Lean: TAOCP.S1_2_10.eq25 (partial) · status: proved

Explanation. This is the mirror of eq. 1.2.10(24). For x≥1x \ge 1 the power xkx^k is large for large kk. The method is the one that gives the Chernoff bounds. Exercise 1.2.10-21 uses it to prove Pr⁡(X≥n(p+ϵ))≤e−ϵ2n/(2q)\Pr(X \ge n(p + \epsilon)) \le e^{-\epsilon^2 n/(2q)} for the binomial distribution.

Example. Take the same XX and r=10r = 10. The true value of Pr⁡(X≥10)\Pr(X \ge 10) is 0.0138640.013864. The bound x−10(0.75+0.25x)20x^{-10}(0.75 + 0.25x)^{20} is 0.08470.0847 at x=2x = 2, 0.06120.0612 at x=2.5x = 2.5, 0.05630.0563 at x=3x = 3, and 0.06920.0692 at x=4x = 4. The best xx is rq/(p(n−r))=3rq/(p(n - r)) = 3, so the best bound is 0.05630.0563, about 4.14.1 times the true value.

Proof. For k≥rk \ge r and x≥1x \ge 1, xk−r≥1x^{k - r} \ge 1. So Pr⁡(X≥r)=∑k≥rpk≤∑k≥rpkxk−r≤∑kpkxk−r=x−rG(x)\Pr(X \ge r) = \sum_{k \ge r} p_k \le \sum_{k \ge r} p_k x^{k-r} \le \sum_k p_k x^{k-r} = x^{-r} G(x), since the added terms are nonnegative. □\square

Algorithms and programs

Algorithm C1.1.2.10.29 (find the maximum, TAOCP Algorithm 1.2.10M). The input is a table X[1],…,X[n]X[1], \ldots, X[n] with n≥1n \ge 1. The output is a pair (m,j)(m, j) with m=X[j]=max⁡iX[i]m = X[j] = \max_i X[i]. When the maximum occurs more than once, jj is its last position.

Node: TA2D92P953 · Lean: TAOCP.S1_2_10.runM, TAOCP.S1_2_10.runM_eq, TAOCP.S1_2_10.changes, TAOCP.S1_2_10.chgAux (partial) · status: not stated yet

The idea. Scan the table from the end. Keep the largest entry seen so far and its index. Replace them only when a strictly larger entry comes. Because the scan goes from the end and the test is strict, the kept index is the largest among the positions of the maximum.

The steps.

FindMax(X, n):                          -- n >= 1
  j := n;  k := n - 1;  m := X[n]
  while k > 0:
    if X[k] > m:
      j := k;  m := X[k]                -- a new current maximum
    k := k - 1
  return (m, j)

A worked run. Take n=6n = 6 and X=(4,9,2,9,5,7)X = (4, 9, 2, 9, 5, 7). This input has a repeated entry, so it also shows the rule for ties.

State at the test kk X[k]X[k] mm jj Action
start 5 5 7 6 5≤75 \le 7, keep
2nd 4 9 7 6 9>79 > 7, so m:=9m := 9, j:=4j := 4
3rd 3 2 9 4 keep
4th 2 9 9 4 9≤99 \le 9, keep (the tie does not move jj)
5th 1 4 9 4 keep
end 0 9 4 return (9,4)(9, 4)

The result is (9,4)(9, 4) with A=1A = 1. The project has no Lean definition of Algorithm 1.2.10M yet, so there is no #eval to show.

Correctness. Let JJ be the statement: k<j≤nk < j \le n, m=X[j]m = X[j], m≥X[i]m \ge X[i] for k<i≤nk < i \le n, and m>X[i]m > X[i] for j<i≤nj < i \le n. JJ holds at the first test of the loop, with k=n−1k = n - 1 and j=nj = n: the range k<i≤nk < i \le n is {n}\{n\}, and the strict range j<i≤nj < i \le n is empty. Suppose that JJ holds and k>0k > 0. If X[k]>mX[k] > m, the body sets j:=kj := k and m:=X[k]m := X[k]. Now mm is larger than X[i]X[i] for k<i≤nk < i \le n, so the strict range j<i≤nj < i \le n holds with the new jj and the weak range k≤i≤nk \le i \le n holds. After k:=k−1k := k - 1, the statement JJ holds again with the new kk. If X[k]≤mX[k] \le m, then jj and mm do not change, and m≥X[i]m \ge X[i] now holds for k≤i≤nk \le i \le n. The strict range j<i≤nj < i \le n does not contain kk, since k<jk < j. After k:=k−1k := k - 1, JJ holds. When the loop ends, k=0k = 0, and JJ says that m=X[j]≥X[i]m = X[j] \ge X[i] for all 1≤i≤n1 \le i \le n, with strict inequality after jj. So mm is the maximum, and jj is the largest index at which the maximum occurs.

Termination. The variable kk is an integer that falls by 11 at each pass of the loop and the loop ends when k=0k = 0. So there are exactly n−1n - 1 passes.

Cost. See the next block.

Analysis C1.1.2.10.30 (the running time of Algorithm M, TAOCP Section 1.2.10, Fig. 9). The table of the counts of the steps of Algorithm M is: M1 once, M2 nn times, M3 n−1n - 1 times, M4 AA times, and M5 n−1n - 1 times.

Node: TAV36Q7KCT · status: not stated yet

The idea. Each step of the algorithm has a count. If the time of a step on a given machine is tit_i, the total time is the sum of the counts times the times. All counts except the count of M4 are fixed by nn, so only AA must be analyzed. The counts are tied by Kirchhoff's first law: at each box, the flow in equals the flow out.

Proof of the counts. The five steps of Algorithm M, in order, are the set-up (M1), the test for the end (M2), the comparison (M3), the change of mm (M4) and the decrement of kk (M5). Step M2 is reached once from M1, and once from M5 after each pass, and the loop makes n−1n - 1 passes (the termination argument above, with kk from n−1n - 1 to 11). So M2 runs nn times, M3 runs n−1n - 1 times, M5 runs n−1n - 1 times (it follows each M3 either directly or after M4), and M4 runs AA times by the definition of AA. Kirchhoff: the flow out of M3 is (n−1)=A+(n−1−A)(n - 1) = A + (n - 1 - A) (to M4, and past it to M5); the flow into M5 is the same (n−1−A)+A(n - 1 - A) + A.

Cost. The total time is T=t1+nt2+(n−1)t3+At4+(n−1)t5T = t_1 + n t_2 + (n - 1) t_3 + A t_4 + (n - 1) t_5. For a machine where each step takes one unit, T=3n−1+AT = 3n - 1 + A. The minimum is 3n−13n - 1 at A=0A = 0, the maximum is 4n−24n - 2 at A=n−1A = n - 1, and the average is 3n−2+Hn3n - 2 + H_n by eq. 1.2.10(14). The deviation of TT is the deviation of AA, since T−AT - A is fixed, and so the time of the algorithm is (min⁡3n−1,ave 3n−2+Hn,max⁡4n−2,dev Hn−Hn(2))(\min 3n - 1, \mathrm{ave}\ 3n - 2 + H_n, \max 4n - 2, \mathrm{dev}\ \sqrt{H_n - H_n^{(2)}}). In the worked run above (n=6n = 6, A=1A = 1), the counts are 1,6,5,1,51, 6, 5, 1, 5 and the total is 18=3⋅6−1+118 = 3 \cdot 6 - 1 + 1. For n=1000n = 1000 the average is 3000−2+7.485≈3005.53000 - 2 + 7.485 \approx 3005.5 units, and the deviation is 2.4172.417: nearly all of the time is the fixed part. Program 1.3.2M is Algorithm M in MIX, and Section 1.3.2 repeats this analysis with the times of the MIX instructions.

The exercises

The table has the exercises of Section 1.2.10. The kind is the class of spec 06.3, and the indexer of S0 fixes it in the map.

Exercise Rating Markers Kind Lean Solution
1.2.10-1 10 K3 not yet stated X1, 1.2.10-1
1.2.10-2 16 HM K1 not yet stated X1, 1.2.10-2
1.2.10-3 15 M K3 not yet stated X1, 1.2.10-3
1.2.10-4 10 M K3 not yet stated X1, 1.2.10-4
1.2.10-5 13 M K3 not yet stated X1, 1.2.10-5
1.2.10-6 27 HM K3 not yet stated X1, 1.2.10-6
1.2.10-7 27 M K7 not yet stated X1, 1.2.10-7
1.2.10-8 20 M K3 not yet stated X1, 1.2.10-8
1.2.10-9 25 M K3 not yet stated X1, 1.2.10-9
1.2.10-10 20 M K7 not yet stated X1, 1.2.10-10
1.2.10-11 15 M K3 not yet stated X1, 1.2.10-11
1.2.10-12 21 HM K1 not yet stated X1, 1.2.10-12
1.2.10-13 38 HM K1 not yet stated X1, 1.2.10-13
1.2.10-14 30 HM K1 not yet stated X1, 1.2.10-14
1.2.10-15 23 HM K1 not yet stated X1, 1.2.10-15
1.2.10-16 25 M K3 not yet stated X1, 1.2.10-16
1.2.10-17 27 M K1 not yet stated X1, 1.2.10-17
1.2.10-18 28 M K1 not yet stated X1, 1.2.10-18
1.2.10-19 21 M K1 not yet stated X1, 1.2.10-19
1.2.10-20 22 M K1 not yet stated X1, 1.2.10-20
1.2.10-21 21 HM K1 not yet stated X1, 1.2.10-21
1.2.10-22 22 HM K1 not yet stated X1, 1.2.10-22

The order to do them: start with 1.2.10-1 and 3, which use the formulas of the section directly. Then do 1.2.10-4, 5 and 2, the coin and the formula for the variance. Exercises 1.2.10-7 to 10 change the model of the input in steps, from repeated values to a random choice from a set: do them in this order, since each uses the one before. Exercise 1.2.10-11 and 16 to 17 are short exercises on generating functions. Exercise 1.2.10-6 and 12 are about the cumulants of eq. 1.2.10(23). Exercises 1.2.10-13 to 15 prove that distributions approach the normal distribution and need Fourier analysis. Exercises 1.2.10-21 and 22 use the tail bounds of eqs. (24) and (25). Exercises 1.2.10-19 and 20 are about records of permutations.

Lean

The Lean code of tamnd/taocp-lean at main has the file TAOCP/V1/S1_2_10.lean, in the namespace TAOCP.S1_2_10. It states eqs. (1) to (20), (22), (24) and (25), Theorem A, the statistics of Algorithm M and a model of the algorithm. The Node lines name the declarations. Where the Lean statement is weaker, the Node line says "partial". Chebyshev's inequality is ProbabilityTheory.meas_ge_le_variance_div_sq of Mathlib, and its statement bounds the chance with a weak inequality, so it is a little weaker than the strict form of the section.

Not yet stated: the notation for a random quantity, the Taylor series at 11 (eq. (21)), the cumulants (eq. (23)) and the running time of Algorithm M with its step counts.

The model of Lean is a finite exact model, so the points where it differs from the book are these.

  • The probabilities are rational numbers. TAOCP.S1_2_10.prob n k is the number of permutations of 0,…,n−10, \ldots, n - 1 with A=kA = k, divided by n!n!. The generating functions are polynomials over ℚ, TAOCP.S1_2_10.pgf, not power series, and the sum of pgf n stops at k=n−1k = n - 1.
  • Eq. (4) is stated for n≥1n \ge 1 with n+1n + 1 in place of nn, in two parts: TAOCP.S1_2_10.eq4_zero for A=0A = 0 and TAOCP.S1_2_10.eq4_succ for A=k+1A = k + 1. The case of a negative kk is not stated. TAOCP.S1_2_10.N_cons_zero and TAOCP.S1_2_10.N_cons_succ are the same step as a count of permutations.
  • Eq. (5) is stated for natural kk, and it does not say that pnk=0p_{nk} = 0 for k<0k < 0. TAOCP.S1_2_10.prob_eq_zero says that pnk=0p_{nk} = 0 for k≥nk \ge n.
  • Eq. (7) is TAOCP.S1_2_10.eq7, with the factor TAOCP.S1_2_10.Q.
  • Eqs. (10) to (13) are for polynomials over ℚ: TAOCP.S1_2_10.mean is G′(1)G'(1) and TAOCP.S1_2_10.var is G′′(1)+G′(1)−G′(1)2G''(1) + G'(1) - G'(1)^2. Eq. (11) is stated for pgf n, and TAOCP.S1_2_10.eval_one_sum states it for a polynomial with the coefficients pkp_k.
  • Theorem A and eq. (15) are for two polynomials with G(1)=H(1)=1G(1) = H(1) = 1. The book allows each function that has two derivatives at 11.
  • Eq. (16): the mean is TAOCP.S1_2_10.eq14 and the variance is TAOCP.S1_2_10.eq16_var, with Hn−Hn(2)H_n - H_n^{(2)}. The minimum 00 and the maximum n−1n - 1 are TAOCP.S1_2_10.changes_eq_zero_iff, TAOCP.S1_2_10.changes_lt and TAOCP.S1_2_10.changes_of_pairwise. The deviation as a square root is not stated.
  • Eqs. (19) and (22) state the mean and the variance only. Eq. (20) has no division: (z−1)G(z)=(zn+1−z)/n(z - 1) G(z) = (z^{n+1} - z)/n.
  • Eqs. (24) and (25) are for pk≥0p_k \ge 0 with k<Nk < N and a natural bound rr. The book has a real rr.
  • Algorithm M is TAOCP.S1_2_10.runM, on a list of natural numbers. The list is X[n],X[n−1],…,X[1]X[n], X[n-1], \ldots, X[1] in the order of the scan. It returns the maximum and the count AA, and not the position jj. TAOCP.S1_2_10.runM_eq shows that the result is the fold of max and TAOCP.S1_2_10.chgAux.

The Mathlib names that the statements use are harmonic for HnH_n, Polynomial.derivative for the derivatives at 11 and TAOCP.stirling1 for the Stirling numbers of the first kind. The statements of TAOCP.S1_2_10.eq3_M and TAOCP.S1_2_10.eq2_M use the finite uniform model, and a bridge to ProbabilityTheory.variance is not stated.

Notes and history

The inequality in the section is named after P. L. Chebyshev, but J. Bienaymé found it earlier (1853), and the book gives this credit. The cumulants (semi-invariants) were introduced by T. N. Thiele (1889). The approach of the normal distribution by the binomial distribution is the theorem of A. de Moivre (1733), later improved by P. S. Laplace. The generating function of the number of left-to-right maxima, a product of linear factors, is the simplest case of the family of Stirling-number distributions that also count cycles of permutations.

The number AA of Algorithm M is the number of records of a random permutation, and its mean Hn−1H_n - 1 is one of the standard examples of the harmonic numbers in the analysis of algorithms. The same distribution counts the cycles of a random permutation (eq. 1.2.10(9)) and the changes in a random scan for a minimum.

The tail bounds (24) and (25) are the method of S. N. Bernstein and of H. Chernoff: choose the parameter xx to make the bound small. Exercise 1.2.10-22 gives the form that applies to a sum of independent unequal coins.

The project has no errata of Section 1.2.10 to report.

Section 1.2.10 of TAOCP shows how to study the running time of a small algorithm in full: Algorithm M, which finds the largest of nn numbers. The time depends on one count AA, and the section finds the least, the average, the greatest and the spread of AA. To do this it uses probability generating functions, and it builds the first tools of probability that the later chapters use. This chapter of C1 explains each item of the section, proves each equation, and adds checks by exact counting for small nn.

The source of this page. Something is wrong? Report it.