Information Theory

How much information is in a message, how compactly it can be written, and how fast it can be sent through noise. Drag a distribution and watch its entropy, decompose a joint law into information diagrams, guess against the maximum entropy principle, push bits through a noisy channel, watch a Huffman code being built, and find the capacity of a Gaussian channel.

Follows D. Applebaum, Probability and Information: An Integrated Approach, 2nd ed. (Cambridge University Press, 2008): Chapter 6 (information and entropy), Chapter 7 (communication), §8.7 (entropy in the continuous case), §§9.6–9.7 (conditional entropy, mutual information and channel capacity for densities) and §10.6 (entropy rates). The probability chapters of the same book are on the Probability page. Chapter and exercise numbers refer to that book; the questions and examples here are written for this page. For how this subject connects to the others, see the roadmap.

InteractiveApplebaum, Ch. 6: Information and entropy

Information and entropy

A rare event is more informative than a common one. Entropy is the average information in the outcome of a random variable: how many yes/no questions, on average, it takes to pin down the outcome. Drag the bars below and find the shapes with the most and the least entropy.

The idea

Information. The information in an event of probability pp is I=−log⁡pI=-\log p bits. Two independent events carry the sum of their informations, because probabilities multiply and logarithms turn products into sums.
Entropy. For XX with law p1,…,pnp_1,\dots,p_n,
H(X)=E(I(X))=−∑j=1npjlog⁡pj,with 0log⁡0=0.H(X)=E\big(I(X)\big)=-\sum_{j=1}^n p_j\log p_j, \qquad\text{with } 0\log 0=0.

It depends only on the probabilities, not on the values XX takes. For a Bernoulli variable, Hb(p)=−plog⁡p−(1−p)log⁡(1−p)H_b(p)=-p\log p-(1-p)\log(1-p), which is 1 bit at p=12p=\frac12.

Bounds. 0≤H(X)≤log⁡n0\le H(X)\le\log n. The lower bound holds exactly when one outcome is certain, the upper exactly when XX is uniform.
Maximum entropy principle. Among all laws on values x1,…,xnx_1,\dots,x_n with a given mean, the one of largest entropy is the Gibbs law
pj=e−βxjZ(β),Z(β)=∑je−βxj,p_j=\frac{e^{-\beta x_j}}{Z(\beta)}, \qquad Z(\beta)=\sum_j e^{-\beta x_j},

with β\beta chosen to give the right mean. It is the "least committal" law consistent with what you know. In statistical mechanics the xjx_j are energies and this is the Boltzmann distribution.

Explore

Drag the bars (outcomes j=1,…,6j=1,\dots,6) up and down; the heights are rescaled to sum to 1. Above each bar is the information −log⁡pj-\log p_j of that outcome, in bits. The dashed outline is the maximum-entropy law with the same mean as yours.

Worked example

Three fair coins are tossed. Let XX be the number of heads.

  1. Write down the law of XX.

    P(X=0)=P(X=3)=18P(X=0)=P(X=3)=\frac18 and P(X=1)=P(X=2)=38P(X=1)=P(X=2)=\frac38.

  2. How much information is there in learning X=0X=0? In learning X=1X=1?

    −log⁡18=3-\log\frac18=3 bits, and −log⁡38=log⁡83≈1.415-\log\frac38=\log\frac83\approx1.415 bits.

  3. Compute H(X)H(X).

    H(X)=2⋅18⋅3+2⋅38⋅1.415≈0.75+1.061=1.811 bits.H(X)=2\cdot\tfrac18\cdot3+2\cdot\tfrac38\cdot1.415\approx0.75+1.061=1.811\ \text{bits}.
  4. The full outcome (e.g. HTH) has 3 bits of entropy. Where did the other 1.19 bits go?

    The full outcome is uniform on 8 sequences, so it has log⁡8=3\log8=3 bits. XX is a function of it and forgets the order of the heads, which is why H(X)<3H(X)<3. In general H(f(Y))≤H(Y)H(f(Y))\le H(Y): processing can only lose information. The missing 3−1.811=1.1893-1.811=1.189 bits is the average information in "which order", given the count.

Check your understanding

Which law on four outcomes has more entropy: (0.5,0.5,0,0)(0.5,0.5,0,0) or (0.4,0.2,0.2,0.2)(0.4,0.2,0.2,0.2)?

  1. The first, because it is uniform.It is uniform only on two of the four outcomes, which gives log⁡2=1\log2=1 bit.
  2. The second
  3. They are equal.Compute both: one is exactly 1 bit.
H(0.5,0.5,0,0)=1H(0.5,0.5,0,0)=1 bit, while H(0.4,0.2,0.2,0.2)=0.4log⁡2.5+0.6log⁡5≈1.922H(0.4,0.2,0.2,0.2)=0.4\log2.5+0.6\log5\approx1.922 bits. Spreading probability over more outcomes matters more than being exactly uniform.

XX takes values 1,…,61,\dots,6. Let Y=10X+7Y=10X+7. How does H(Y)H(Y) compare with H(X)H(X)?

  1. H(Y)=H(X)H(Y)=H(X)
  2. H(Y)=10H(X)H(Y)=10\,H(X)That is how the variance would scale (by 10210^2, in fact). Entropy only sees the probabilities.
  3. H(Y)>H(X)H(Y)>H(X), since YY is more spread out.Entropy does not measure spread of values; relabelling the outcomes changes nothing.
x↦10x+7x\mapsto10x+7 is one-to-one, so YY has the same list of probabilities as XX and the same entropy. Entropy measures uncertainty about which outcome occurs, not how far apart the outcomes are.

Among all laws on {1,…,6}\{1,\dots,6\} with mean 3.5, which has the largest entropy?

  1. The uniform law
  2. p3=p4=12p_3=p_4=\frac12It has mean 3.5 but only 1 bit of entropy. Try it in the explorer.
  3. A Gibbs law with β>0\beta>0With β>0\beta>0 the weights e−βje^{-\beta j} favour small values, so the mean is below 3.5.
The uniform law has the largest entropy of all, log⁡6≈2.585\log6\approx2.585 bits, and its mean is 3.5. It is the Gibbs law with β=0\beta=0. Press "Uniform", then "Replace with the max-entropy law": nothing changes.

Practice

The Bernoulli entropyA calculus check

Show that Hb(p)H_b(p) has its maximum at p=12p=\frac12 and is symmetric about it. Compute Hb(0.1)H_b(0.1).

Solution

Hb(p)=Hb(1−p)H_b(p)=H_b(1-p) by its formula. In natural logs, ddp(−pln⁡p−(1−p)ln⁡(1−p))=ln⁡1−pp\frac{d}{dp}\big(-p\ln p-(1-p)\ln(1-p)\big)=\ln\frac{1-p}{p}, which is positive for p<12p<\frac12, zero at 12\frac12 and negative after. So the maximum is Hb(12)=1H_b(\frac12)=1 bit. Hb(0.1)=0.1log⁡10+0.9log⁡109≈0.332+0.137=0.469H_b(0.1)=0.1\log10+0.9\log\frac{10}9\approx0.332+0.137=0.469 bits.

Why uniform is the maximumThe Gibbs inequality

Using ln⁡x≤x−1\ln x\le x-1 (equality only at x=1x=1), show that H(X)≤log⁡nH(X)\le\log n, with equality only for the uniform law.

Hint

Look at H(X)−log⁡n=∑jpjlog⁡1npjH(X)-\log n=\sum_j p_j\log\frac{1}{np_j}, summing over the jj with pj>0p_j>0.

Solution

In natural logs, ∑jpjln⁡1npj≤∑jpj(1npj−1)=∑pj>01n−1≤0\sum_j p_j\ln\frac1{np_j}\le\sum_j p_j\big(\frac1{np_j}-1\big)=\sum_{p_j>0}\frac1n-1\le0. Equality needs npj=1np_j=1 for every jj with pj>0p_j>0, and needs every jj to have pj>0p_j>0; that is the uniform law. Dividing by ln⁡2\ln2 converts to bits.

From Applebaum, Probability and Information: An Integrated Approach (2nd ed., Cambridge, 2008)

Numbers match the book. Each question is restated in our own words; the hints and solutions are written for this page.

Exercise 6.5Two entropies

Find the entropy of (a) the number of heads in two tosses of a fair coin; (b) a Poisson variable with mean 0.50.5.

Hint

(b) has infinitely many terms, but they shrink fast; sum the first several.

Solution

(a) The distribution is (14,12,14)(\tfrac14,\tfrac12,\tfrac14), so H=14⋅2+12⋅1+14⋅2=1.5H=\tfrac14\cdot2+\tfrac12\cdot1+\tfrac14\cdot2=1.5 bits.

(b) pk=e−0.50.5k/k!p_k=e^{-0.5}0.5^k/k!: 0.607,0.303,0.076,0.013,0.0016,…0.607,0.303,0.076,0.013,0.0016,\dots. Summing −pklog2⁡pk-p_k\log_2p_k gives H≈1.34H\approx1.34 bits (the terms beyond k=5k=5 contribute less than 0.0010.001).

Exercise 6.6The binary entropy function

For Hb(p)=−plog⁡p−(1−p)log⁡(1−p)H_b(p)=-p\log p-(1-p)\log(1-p): (a) show Hb(0)=Hb(1)=0H_b(0)=H_b(1)=0; (b) show Hb(12−q)=Hb(12+q)H_b(\tfrac12-q)=H_b(\tfrac12+q); (c) show the maximum is at p=12p=\tfrac12 and sketch the graph.

Hint

Use the convention 0log⁡0=00\log0=0 (the limit of xlog⁡xx\log x). For (c), differentiate.

Solution

(a) Both terms vanish at p=0p=0 and p=1p=1, using xlog⁡x→0x\log x\to0.

(b) Replacing pp by 1−p1-p swaps the two terms, and 1−(12−q)=12+q1-(\tfrac12-q)=\tfrac12+q.

(c) Hb′(p)=log⁡1−ppH_b'(p)=\log\frac{1-p}p, which is positive for p<12p<\tfrac12, zero at 12\tfrac12, negative after. So HbH_b rises from 00 to its maximum Hb(12)=1H_b(\tfrac12)=1 bit and falls back symmetrically; it is concave (Hb′′<0H_b''<0) with vertical tangents at the ends.

Exercise 6.13Maximum entropy with a mean constraint

Three states have energies 1, 2 and 3 J and the mean energy is 2.4 J. (a) Find the maximum-entropy distribution. (b) Find the corresponding temperature and comment.

Hint

The maximiser is a Gibbs distribution pi∝e−βEip_i\propto e^{-\beta E_i}; choose β\beta to match the mean. Temperature is T=1/(kβ)T=1/(k\beta).

Solution

(a) pi=e−βEi/Zp_i=e^{-\beta E_i}/Z. With u=e−βu=e^{-\beta}, the mean condition u+2u2+3u3u+u2+u3=2.4\frac{u+2u^2+3u^3}{u+u^2+u^3}=2.4 becomes 1+2u+3u2=2.4(1+u+u2)1+2u+3u^2=2.4(1+u+u^2), i.e. 3u2−2u−7=03u^2-2u-7=0, so u=1+223≈1.897u=\tfrac{1+\sqrt{22}}3\approx1.897 and β=−ln⁡u≈−0.640 J−1\beta=-\ln u\approx-0.640\ \mathrm{J^{-1}}. Then p≈(0.154, 0.292, 0.554)p\approx(0.154,\ 0.292,\ 0.554).

(b) T=1/(kBβ)≈−1.1×1023T=1/(k_B\beta)\approx-1.1\times10^{23} K. The temperature is negative: the mean energy is above the uniform-distribution value 22, so higher energies must be more likely, which a Gibbs distribution can only do with β<0\beta<0. Negative temperatures do occur in systems with bounded energy (population inversion in lasers); the huge magnitude is because the energies are joules rather than atomic scales.

Exercise 6.16Evening out a distribution raises entropy

Replace two probabilities p1<p2p_1<p_2 by p1+εp_1+\varepsilon and p2−εp_2-\varepsilon, where 0<2ε<p2−p10<2\varepsilon<p_2-p_1. Prove the entropy increases.

Hint

Only two terms change. Differentiate in ε\varepsilon.

Solution

Let f(t)=−(p1+t)log⁡(p1+t)−(p2−t)log⁡(p2−t)f(t)=-(p_1+t)\log(p_1+t)-(p_2-t)\log(p_2-t). Then f′(t)=log⁡p2−tp1+tf'(t)=\log\frac{p_2-t}{p_1+t}, which is positive as long as p1+t<p2−tp_1+t<p_2-t, i.e. 2t<p2−p12t<p_2-p_1. So ff is strictly increasing on [0,ε][0,\varepsilon] and f(ε)>f(0)f(\varepsilon)>f(0). The other terms of the entropy are unchanged.

InteractiveApplebaum, Ch. 6: Information and entropy (§6.3)

Joint entropy and mutual information

How much does knowing XX tell you about YY? Mutual information answers in bits: it is the drop in uncertainty about YY once XX is known. Reshape a joint law by dragging the dividers of the mosaic below and watch the entropies add up.

The idea

Joint and conditional entropy. For a pair (X,Y)(X,Y) with joint law pjk=P(X=xj,Y=yk)p_{jk}=P(X=x_j,Y=y_k),
H(X,Y)=−∑j,kpjklog⁡pjk,H(Y|X)=∑jP(X=xj)H(Y|X=xj).H(X,Y)=-\sum_{j,k}p_{jk}\log p_{jk}, \qquad H(Y\mid X)=\sum_j P(X=x_j)\,H(Y\mid X=x_j).

H(Y|X=xj)H(Y\mid X=x_j) is the entropy of the conditional law of YY given X=xjX=x_j, and H(Y|X)H(Y\mid X) is its average over xjx_j. (The book writes HX(Y)H_X(Y).)

Chain rule. H(X,Y)=H(X)+H(Y|X)H(X,Y)=H(X)+H(Y\mid X): learning the pair is learning XX, then learning YY given XX.
Mutual information.
I(X;Y)=H(Y)−H(Y|X)=H(X)+H(Y)−H(X,Y)=∑j,kpjklog⁡pjkP(X=xj)P(Y=yk).I(X;Y)=H(Y)-H(Y\mid X)=H(X)+H(Y)-H(X,Y)=\sum_{j,k}p_{jk}\log\frac{p_{jk}}{P(X=x_j)P(Y=y_k)}.
Properties. I(X;Y)=I(Y;X)≥0I(X;Y)=I(Y;X)\ge0, with equality exactly when XX and YY are independent. Equivalently H(Y|X)≤H(Y)H(Y\mid X)\le H(Y): on average, conditioning cannot increase uncertainty.

Explore

Start from:
Each rectangle's area is pjkp_{jk}. Drag the vertical divider to set P(X=0)P(X=0), and each horizontal divider to set P(Y=0|X=x)P(Y=0\mid X=x).
H(X|Y)H(X\mid Y) I(X;Y)I(X;Y) H(Y|X)H(Y\mid X), in bits

Worked example

XX and YY take values 0 and 1 with joint law p00=12p_{00}=\frac12, p01=14p_{01}=\frac14, p10=0p_{10}=0, p11=14p_{11}=\frac14.

  1. Find the marginal laws and their entropies.

    P(X=0)=34P(X=0)=\frac34, P(X=1)=14P(X=1)=\frac14, so H(X)=Hb(14)≈0.811H(X)=H_b(\frac14)\approx0.811. P(Y=0)=P(Y=1)=12P(Y=0)=P(Y=1)=\frac12, so H(Y)=1H(Y)=1.

  2. Find H(X,Y)H(X,Y).

    H(X,Y)=12log⁡2+14log⁡4+14log⁡4=0.5+0.5+0.5=1.5H(X,Y)=\frac12\log2+\frac14\log4+\frac14\log4=0.5+0.5+0.5=1.5 bits.

  3. Compute H(Y|X)H(Y\mid X) from its definition, and check the chain rule.

    Given X=0X=0, YY is 0 or 1 with probabilities 23,13\frac23,\frac13, so H(Y|X=0)=Hb(13)≈0.918H(Y\mid X=0)=H_b(\frac13)\approx0.918. Given X=1X=1, Y=1Y=1 for certain, so H(Y|X=1)=0H(Y\mid X=1)=0. Then H(Y|X)=34⋅0.918≈0.689H(Y\mid X)=\frac34\cdot0.918\approx0.689, and indeed H(X)+H(Y|X)≈0.811+0.689=1.5H(X)+H(Y\mid X)\approx0.811+0.689=1.5.

  4. Find I(X;Y)I(X;Y) two ways.

    H(Y)−H(Y|X)≈1−0.689=0.311H(Y)-H(Y\mid X)\approx1-0.689=0.311, and H(X)+H(Y)−H(X,Y)≈0.811+1−1.5=0.311H(X)+H(Y)-H(X,Y)\approx0.811+1-1.5=0.311 bits. Set the mosaic to these numbers (P(X=0)=0.75P(X=0)=0.75, P(Y=0|X=0)=23P(Y=0\mid X=0)=\frac23, P(Y=0|X=1)=0P(Y=0\mid X=1)=0) to check.

Check your understanding

I(X;Y)=0I(X;Y)=0. What can you conclude?

  1. XX and YY are independent.
  2. YY is a function of XX.Then H(Y|X)=0H(Y\mid X)=0 and I(X;Y)=H(Y)I(X;Y)=H(Y), the largest it can be.
  3. One of them is constant.That is enough for I=0I=0 but not necessary. In the mosaic, line the two horizontal dividers up.
I(X;Y)=∑pjklog⁡pjkpjqkI(X;Y)=\sum p_{jk}\log\frac{p_{jk}}{p_jq_k} is zero exactly when pjk=pjqkp_{jk}=p_jq_k for every j,kj,k. In the mosaic, that means the two horizontal dividers are at the same height: the conditional law of YY doesn't depend on XX.

XX is a fair bit and Y=XY=X. What is I(X;Y)I(X;Y)?

  1. 0YY tells you XX exactly. That is the opposite of independence.
  2. 1 bit
  3. 2 bitsThat is H(X)+H(Y)H(X)+H(Y). But H(X,Y)=1H(X,Y)=1, not 0, so subtract it.
H(Y|X)=0H(Y\mid X)=0, so I=H(Y)=1I=H(Y)=1 bit. A variable shares all of its information with itself: I(X;X)=H(X)I(X;X)=H(X).

Which of these always holds?

  1. H(Y|X)≤H(Y)H(Y\mid X)\le H(Y)
  2. H(Y|X=x)≤H(Y)H(Y\mid X=x)\le H(Y) for every value xxTry "Y depends on X only sometimes": a rare value of XX after which YY is a fair coin.
  3. H(X,Y)≥H(X)+H(Y)H(X,Y)\ge H(X)+H(Y)The inequality goes the other way: H(X,Y)=H(X)+H(Y)−I(X;Y)H(X,Y)=H(X)+H(Y)-I(X;Y).
Only the average over xx is guaranteed to drop. If X=0X=0 with probability 0.9 and then Y=0Y=0, but X=1X=1 makes YY a fair coin, then H(Y)=Hb(0.05)≈0.286H(Y)=H_b(0.05)\approx0.286 while H(Y|X=1)=1H(Y\mid X=1)=1. Learning a particular, surprising value can make you less sure.

Practice

Two formulas for IExpanding the logarithm

Show that ∑j,kpjklog⁡pjkpjqk=H(X)+H(Y)−H(X,Y)\displaystyle\sum_{j,k}p_{jk}\log\frac{p_{jk}}{p_jq_k}=H(X)+H(Y)-H(X,Y), where pjp_j, qkq_k are the marginals.

Solution

Split the logarithm: ∑pjklog⁡pjk−∑pjklog⁡pj−∑pjklog⁡qk\sum p_{jk}\log p_{jk}-\sum p_{jk}\log p_j-\sum p_{jk}\log q_k. The first sum is −H(X,Y)-H(X,Y). In the second, summing over kk first gives ∑jpjlog⁡pj=−H(X)\sum_j p_j\log p_j=-H(X); similarly the third is −H(Y)-H(Y). So the total is −H(X,Y)+H(X)+H(Y)-H(X,Y)+H(X)+H(Y).

The last bitA function of a random variable

XX is uniform on {0,1,2,3}\{0,1,2,3\} and Y=Xmod2Y=X\bmod2. Find H(X)H(X), H(Y)H(Y), H(Y|X)H(Y\mid X), H(X|Y)H(X\mid Y) and I(X;Y)I(X;Y).

Solution

H(X)=2H(X)=2, H(Y)=1H(Y)=1. YY is determined by XX, so H(Y|X)=0H(Y\mid X)=0. Given YY, XX is uniform on two values, so H(X|Y)=1H(X\mid Y)=1. Then I(X;Y)=H(Y)−H(Y|X)=1I(X;Y)=H(Y)-H(Y\mid X)=1 bit, and the chain rule checks: H(X,Y)=H(X)=2=H(Y)+H(X|Y)H(X,Y)=H(X)=2=H(Y)+H(X\mid Y).

From Applebaum, Probability and Information: An Integrated Approach (2nd ed., Cambridge, 2008)

Numbers match the book. Each question is restated in our own words; the hints and solutions are written for this page.

Exercise 6.8Entropies of a pair of bits

XX is Bernoulli with P(X=1)=13P(X=1)=\tfrac13, and P(Y=0|X=0)=14P(Y=0\mid X=0)=\tfrac14, P(Y=1|X=1)=35P(Y=1\mid X=1)=\tfrac35. Compute H(X,Y)H(X,Y), HX(Y)H_X(Y), H(Y)H(Y), HY(X)H_Y(X) and I(X,Y)I(X,Y).

Hint

Build the joint table first, then everything follows from H(X,Y)=H(X)+HX(Y)=H(Y)+HY(X)H(X,Y)=H(X)+H_X(Y)=H(Y)+H_Y(X).

Solution

Joint probabilities: P(0,0)=23⋅14=16P(0,0)=\tfrac23\cdot\tfrac14=\tfrac16, P(0,1)=12P(0,1)=\tfrac12, P(1,0)=13⋅25=215P(1,0)=\tfrac13\cdot\tfrac25=\tfrac2{15}, P(1,1)=15P(1,1)=\tfrac15. So P(Y=0)=0.3P(Y=0)=0.3, P(Y=1)=0.7P(Y=1)=0.7. In bits:

(a) H(X,Y)≈1.783H(X,Y)\approx1.783. With H(X)=Hb(13)≈0.918H(X)=H_b(\tfrac13)\approx0.918: (b) HX(Y)=H(X,Y)−H(X)≈0.865H_X(Y)=H(X,Y)-H(X)\approx0.865. (c) H(Y)=Hb(0.3)≈0.881H(Y)=H_b(0.3)\approx0.881. (d) HY(X)=H(X,Y)−H(Y)≈0.902H_Y(X)=H(X,Y)-H(Y)\approx0.902. (e) I(X,Y)=H(Y)−HX(Y)≈0.017I(X,Y)=H(Y)-H_X(Y)\approx0.017.

Exercise 6.10Conditioning can only reduce entropy

Using Gibbs' inequality, show HX(Y)≤H(Y)H_X(Y)\le H(Y), with equality iff X,YX,Y are independent. Deduce I(X,Y)≥0I(X,Y)\ge0.

Hint

Write H(Y)−HX(Y)H(Y)-H_X(Y) as one sum over the joint distribution.

Solution
H(Y)−HX(Y)=∑x,yp(x,y)log⁡p(x,y)p(x)p(y).H(Y)-H_X(Y)=\sum_{x,y}p(x,y)\log\frac{p(x,y)}{p(x)p(y)}.

Gibbs' inequality −∑pilog⁡pi≤−∑pilog⁡qi-\sum p_i\log p_i\le-\sum p_i\log q_i for probability vectors p,qp,q, applied with p=p(x,y)p=p(x,y) and q=p(x)p(y)q=p(x)p(y), shows this is ≥0\ge0, with equality iff p(x,y)=p(x)p(y)p(x,y)=p(x)p(y) for all x,yx,y: independence. Since I(X,Y)=H(Y)−HX(Y)I(X,Y)=H(Y)-H_X(Y), I≥0I\ge0 with the same equality case.

InteractiveApplebaum, Ch. 6: Information and entropy (§§6.4–6.5)

The maximum entropy principle

With no information about a die, you assume every face is equally likely. Now suppose you are told only that its average roll is 4.5. Which probabilities should you assume? Pick the ones with the most entropy: the least committal law that still agrees with what you know.

The idea

The uniform law has the largest entropy among all laws on nn outcomes, H=log⁡nH=\log n. So the old principle of symmetry (“with no reason to prefer an outcome, treat them equally”) can be restated as: choose the law of maximum entropy. The restatement is more powerful, because it still makes sense when you do have partial information.

Principle of maximum entropy (Applebaum §6.4.1). Given a random variable XX with unknown law p1,…,pnp_1,\dots,p_n, choose the pjp_j to maximize the entropy H(X)H(X) subject to any known constraints.

Knowing only the mean. Let XX take values x1,…,xnx_1,\dots,x_n with 𝔼(X)=E\mathbb E(X)=E known. Maximize H=−∑jpjlog⁡pjH=-\sum_jp_j\log p_j subject to ∑jpj=1\sum_jp_j=1 and ∑jxjpj=E\sum_jx_jp_j=E. With Lagrange multipliers λ,μ\lambda,\mu,

L=−∑jpjlog⁡pj+λ(∑jpj−1)+μ(∑jxjpj−E),∂L∂pj=0 ⇒ pj=eλ′+μ′xj.L=-\sum_jp_j\log p_j+\lambda\Big(\sum_jp_j-1\Big)+\mu\Big(\sum_jx_jp_j-E\Big),\qquad \frac{\partial L}{\partial p_j}=0\ \Rightarrow\ p_j=e^{\lambda'+\mu'x_j}.
Gibbs distribution. Normalizing removes λ′\lambda':
pj=eμ′xjZ(μ′),Z(μ′)=∑k=1neμ′xk(the partition function),p_j=\frac{e^{\mu'x_j}}{Z(\mu')},\qquad Z(\mu')=\sum_{k=1}^ne^{\mu'x_k}\quad\text{(the partition function)},
and μ′\mu' is the one number that makes the mean come out to EE. For μ′=0\mu'=0 it is the uniform law; μ′>0\mu'>0 tilts the weight toward large values, μ′<0\mu'<0 toward small ones, each probability a constant ratio eμ′e^{\mu'} times the one before.

Physics. If the xjx_j are the energies of the particles in a gas and the average energy is fixed, the same calculation gives Boltzmann's law, with μ′=−β=−1/kT\mu'=-\beta=-1/kT (Applebaum §6.5): the maximum-entropy law at a given temperature.

Explore

The maximum-entropy law with mean EE: a Gibbs distribution pj∝eμ′jp_j\propto e^{\mu'j}.
Your guess: drag the bars (they are rescaled to add to 1). Try to match the mean and beat the entropy on the left.

Worked example

Jaynes's dice problem (the “Brandeis dice”): a die has average roll 4.5. Find the maximum-entropy law.

  1. Write down the form of the answer.

    pj=rj/Zp_j=r^j/Z for j=1,…,6j=1,\dots,6, with r=eμ′r=e^{\mu'} and Z=r+r2+⋯+r6Z=r+r^2+\dots+r^6: a geometric progression.

  2. Which equation determines rr? Is rr above or below 1?

    ∑jjrj=4.5∑jrj\sum_jj\,r^j=4.5\sum_jr^j. The mean 4.5 is above the fair value 3.5, so the weights must increase: r>1r>1. Solving numerically gives r≈1.449r\approx1.449 (μ′≈0.371\mu'\approx0.371).

  3. Give the law and its entropy, and compare with another law of mean 4.5.

    p≈(0.054,0.079,0.114,0.165,0.240,0.348)p\approx(0.054,\,0.079,\,0.114,\,0.165,\,0.240,\,0.348), with H≈2.33H\approx2.33 bits (the fair die has log⁡6≈2.58\log6\approx2.58). The law “4 or 5, each with probability 12\tfrac12” also has mean 4.5, but only 1 bit of entropy: it claims to know much more than we were told.

Check your understanding

You know only that a die's mean is 3.5. The maximum-entropy law is:

  1. Uniform: 3.5 is the fair die's mean, so the constraint adds nothing, and μ′=0\mu'=0.
  2. Half the mass on 3 and half on 4.Mean 3.5, but only 1 bit of entropy: it assumes far more than you know.
When the constraint is already satisfied by the uniform law, maximum entropy gives back the symmetry principle.

In a Gibbs law on 1,…,61,\dots,6, the ratio pj+1/pjp_{j+1}/p_j is:

  1. The same for every jj, namely eμ′e^{\mu'}.
  2. Proportional to jj.pj∝eμ′xjp_j\propto e^{\mu'x_j} with equally spaced xjx_j gives a geometric progression.
pj+1/pj=eμ′(xj+1−xj)p_{j+1}/p_j=e^{\mu'(x_{j+1}-x_j)}, constant when the values are equally spaced, as on a die.

Why is the maximum-entropy law a sensible default, rather than any other law with the right mean?

  1. Any other law with the same mean has less entropy, so it encodes information you were not given.
  2. It is the law the die actually has.Nothing guarantees that. It is the best guess given what you know, not the truth.
Maximum entropy is a rule for honest guessing. New information (say, the variance too) adds a constraint and changes the answer.

Practice

Two outcomesThe smallest case

XX takes values 0 and 1, and 𝔼(X)=E\mathbb E(X)=E. What does the maximum-entropy principle give, and why is there nothing to maximize?

Solution

p1=Ep_1=E, p0=1−Ep_0=1-E: the two constraints already determine the law. The Gibbs form agrees: p1/p0=eμ′p_1/p_0=e^{\mu'} with eμ′=E/(1−E)e^{\mu'}=E/(1-E).

The constraint at workLagrange in full

Using natural logarithms, solve ∂L/∂pj=0\partial L/\partial p_j=0 for L=−∑pjln⁡pj+λ(∑pj−1)+μ(∑xjpj−E)L=-\sum p_j\ln p_j+\lambda(\sum p_j-1)+\mu(\sum x_jp_j-E), and show that λ\lambda is fixed by normalization.

Solution

−ln⁡pj−1+λ+μxj=0-\ln p_j-1+\lambda+\mu x_j=0, so pj=eλ−1eμxjp_j=e^{\lambda-1}e^{\mu x_j}. Summing to 1 gives eλ−1=1/Z(μ)e^{\lambda-1}=1/Z(\mu). (Base-2 logarithms only rescale λ\lambda and μ\mu, as in Applebaum's λ′,μ′\lambda',\mu'.) The second-derivative matrix is −diag(1/pj)-\mathrm{diag}(1/p_j), negative definite, so this critical point is the maximum.

InteractiveBuilds on Applebaum §6.4; Goodfellow–Bengio–Courville §6.2.2.3

Why e to the score: softmax

Classifiers, attention in language models, Boltzmann exploration in reinforcement learning and the Exp3 algorithm for adversarial bandits all turn real-valued scores into probabilities the same way: exponentiate, then normalize. Scores can be negative, so some positive transformation is needed, but why the exponential? Because the result is the maximum-entropy distribution of the previous module. The base ee itself turns out to be a choice of units.

The idea

Softmax. Scores s1,…,sn∈ℝs_1,\dots,s_n\in\R and a temperature τ>0\tau>0 give the probabilities
pi=esi/τZ,Z=∑j=1nesj/τ.p_i=\frac{e^{s_i/\tau}}{Z},\qquad Z=\sum_{j=1}^ne^{s_j/\tau}.
This is the Gibbs distribution of the maximum entropy module with the score playing the role of xjx_j and μ′=1/τ\mu'=1/\tau.
Softmax is the best trade-off between score and uncertainty (the Gibbs variational principle). Among all probability vectors pp, softmax maximizes
F(p)=∑ipisi⏟expected score+τ(−∑ipiln⁡pi)⏟entropy H(p), in nats,F(p)=\underbrace{\sum_ip_is_i}_{\text{expected score}}+\tau\,\underbrace{\Big(-\sum_ip_i\ln p_i\Big)}_{\text{entropy }H(p)\text{, in nats}},
and the maximum value is τln⁡Z=τln⁡∑jesj/τ\tau\ln Z=\tau\ln\sum_je^{s_j/\tau}, the “log-sum-exp”, a smooth version of maxj⁡sj\max_js_j.

Proof in one line. Write qq for softmax, so si=τln⁡qi+τln⁡Zs_i=\tau\ln q_i+\tau\ln Z. Substitute:

F(p)=τln⁡Z+τ∑ipiln⁡qi−τ∑ipiln⁡pi=τln⁡Z−τD(p‖q),F(p)=\tau\ln Z+\tau\sum_ip_i\ln q_i-\tau\sum_ip_i\ln p_i=\tau\ln Z-\tau\,D(p\,\|\,q),
where D(p‖q)=∑ipiln⁡piqi≥0D(p\|q)=\sum_ip_i\ln\frac{p_i}{q_i}\ge0 is the Kullback–Leibler divergence, zero only for p=qp=q (Gibbs' inequality). So every other distribution falls short of softmax by exactly τ\tau times its divergence from it.

What that means. Putting all the probability on the highest score maximizes the expected score but commits completely. The uniform law hedges completely but ignores the scores. Softmax is the law that is as uncertain as possible for the expected score it achieves; the temperature sets the exchange rate. As τ→0\tau\to0 it becomes the argmax; as τ→∞\tau\to\infty, the uniform law.

Why ee, and not 2 or 10? bs=esln⁡bb^{s}=e^{s\ln b}, so “base-bb softmax” is ordinary softmax at temperature 1/ln⁡b1/\ln b: for base 2, τ≈1.44\tau\approx1.44. Every base gives the same family of distributions. Choosing ee means measuring entropy in nats, and then the temperature is literally the weight on entropy and the derivatives come out clean: ∂ln⁡pi/∂sj=(δij−pj)/τ\partial\ln p_i/\partial s_j=(\delta_{ij}-p_j)/\tau, so the gradient of the cross-entropy loss −ln⁡py-\ln p_y with respect to the scores is p−eyp-e_y, predicted minus true. Base 2 would only put a factor ln⁡2\ln2 everywhere.
Why an exponential at all: only it ignores a common shift. Scores usually mean something only relative to each other, so adding the same constant cc to every score should not change the distribution. If ff is continuous and positive and f(si+c)/∑jf(sj+c)=f(si)/∑jf(sj)f(s_i+c)/\sum_jf(s_j+c)=f(s_i)/\sum_jf(s_j) for all scores and all cc, then f(x)=Abxf(x)=Ab^x for constants A>0A>0, b>0b>0: an exponential. Squaring the scores, or clipping the negative ones to zero (a ReLU) and dividing by the total, fails this test (try it in the figure). Note what the theorem does not say: it allows any base bb.

Where it shows up. The final layer of a classifier (multinomial logistic regression). Attention, softmax(QK⊤/d)\mathrm{softmax}(QK^\top/\sqrt d), where dividing by d\sqrt d is a temperature: the dot product of two dd-dimensional vectors with independent unit-variance entries has variance dd, and the scaling keeps the scores at variance about 1 so the softmax is neither nearly one-hot nor nearly uniform. Boltzmann (softmax) exploration in reinforcement learning. And Hedge/Exp3 for adversarial bandits, which put weight eηSae^{\eta S_a} on arm aa with SaS_a its total reward so far: by the theorem above, that is the law maximizing η⟨p,S⟩+H(p)\eta\langle p,S\rangle+H(p), an entropy-regularized “follow the leader” that never commits completely.

The same formula in economics. In discrete-choice models, a person picks option mm with value VmV_m, and the multinomial logit pm=eVm/∑m′eVm′p_m=e^{V_m}/\sum_{m'}e^{V_{m'}} is the standard model of their choice probabilities. It is the theorem above with τ=1\tau=1: logit maximizes ∑mVmpm+H(p)\sum_mV_mp_m+H(p) over the simplex, a concave objective, so a convex program with a unique solution. The entropy term has two readings: a value placed on variety (not committing to one option), or, in rational-inattention models, the cost of the information needed to tell the options apart (see M. Fosgerau, E. Melo, A. de Palma and M. Shum, “Discrete choice and rational inattention: a general equivalence result”, International Economic Review, 2020). A third route reaches the same formula without entropy: McFadden's random utility model, in the last practice problem below.

Explore

Turn scores into probabilities by
Four scores; drag the bars (they can be negative). The shift cc moves all of them together.
the probabilities from the chosen map; dashed: softmax at the same τ\tau.

Worked example

Scores s=(2,1,0)s=(2,1,0), temperature τ=1\tau=1.

  1. Compute the softmax probabilities.

    e2≈7.389e^2\approx7.389, e1≈2.718e^1\approx2.718, e0=1e^0=1, so Z≈11.107Z\approx11.107 and p≈(0.665,0.245,0.090)p\approx(0.665,\,0.245,\,0.090). Each step down in score divides the probability by ee.

  2. Check that F(p)=ln⁡ZF(p)=\ln Z.

    Expected score 2(0.665)+1(0.245)≈1.5752(0.665)+1(0.245)\approx1.575; entropy −∑piln⁡pi≈0.832-\sum p_i\ln p_i\approx0.832 nats; total ≈2.408\approx2.408, and ln⁡11.107≈2.408\ln11.107\approx2.408.

  3. Compare two other laws: all mass on the top score, and uniform.

    One-hot: F=2+0=2F=2+0=2. Uniform: F=1+ln⁡3≈2.099F=1+\ln3\approx2.099. Both are below 2.4082.408; the shortfalls are D(p‖q)D(p\|q), the divergences from softmax.

  4. Change the temperature to τ=0.5\tau=0.5 and τ=2\tau=2.

    τ=0.5\tau=0.5: p≈(0.867,0.117,0.016)p\approx(0.867,0.117,0.016), sharper. τ=2\tau=2: p≈(0.506,0.307,0.186)p\approx(0.506,0.307,0.186), flatter. Doubling all the scores has the same effect as halving τ\tau: only s/τs/\tau matters.

Check your understanding

You add 5 to every score before a softmax. The probabilities:

  1. Don't change: every esi+5e^{s_i+5} gets the same factor e5e^5, which cancels in ZZ.
  2. Move toward the highest score.That is what dividing by a smaller τ\tau does, not adding a constant.
This shift invariance is why implementations subtract maxj⁡sj\max_js_j before exponentiating (to avoid overflow) without changing the answer, and it characterizes the exponential among all positive maps.

A softmax with base 2 instead of ee is:

  1. The same as an ee-softmax at temperature 1/ln⁡2≈1.441/\ln2\approx1.44 times larger.
  2. A genuinely different family of distributions.2s=esln⁡22^s=e^{s\ln2}: only the scale of the scores changes.
The base is a unit, like measuring entropy in bits or nats. Since scores are usually learned, a different base would just be absorbed into the learned scale.

Why does attention divide QK⊤QK^\top by d\sqrt d?

  1. It keeps the scores' spread about constant as the dimension dd grows, so the softmax's effective temperature doesn't shrink with dd.
  2. To make every score positive.Positivity isn't needed: the exponential takes care of that.
A dot product of dd independent unit-variance products has standard deviation d\sqrt d. Without the scaling, large dd would make the softmax nearly one-hot, with tiny gradients everywhere else.

Practice

Only the exponentialA functional equation

Let f:ℝ→(0,∞)f:\R\to(0,\infty) be continuous and suppose that normalizing f(s1),…,f(sn)f(s_1),\dots,f(s_n) gives the same probabilities after adding any constant cc to all the scores. Show f(x)=Abxf(x)=Ab^x.

Hint

With two scores xx and 00, invariance says f(x+c)/f(c)=f(x)/f(0)f(x+c)/f(c)=f(x)/f(0).

Solution

Put g(x)=f(x)/f(0)g(x)=f(x)/f(0). The hint gives g(x+c)=g(x)g(c)g(x+c)=g(x)g(c) for all x,cx,c: Cauchy's multiplicative equation. Then h=ln⁡gh=\ln g is continuous and additive, h(x+c)=h(x)+h(c)h(x+c)=h(x)+h(c), so h(x)=kxh(x)=kx, and f(x)=f(0)ekx=Abxf(x)=f(0)e^{kx}=Ab^x with b=ekb=e^k. Every base is allowed; kk plays the role of 1/τ1/\tau.

The gradient of cross-entropyWhy training is easy

For p=softmax(s)p=\mathrm{softmax}(s) (τ=1\tau=1) and a true class yy, show that the gradient of L=−ln⁡pyL=-\ln p_y with respect to ss is p−eyp-e_y.

Solution

L=−sy+ln⁡∑jesjL=-s_y+\ln\sum_je^{s_j}, so ∂L/∂sk=−𝟏{k=y}+esk/Z=pk−𝟏{k=y}\partial L/\partial s_k=-\mathbf 1\{k=y\}+e^{s_k}/Z=p_k-\mathbf 1\{k=y\}. The update pushes up the true score and pushes down each other score in proportion to its probability. With base 2 the same calculation gives an extra factor ln⁡2\ln2.

Gumbel noise gives softmaxMcFadden's random utility

Each option's utility is Vm+GmV_m+G_m, with G1,…,GnG_1,\dots,G_n independent standard Gumbel variables (distribution function F(x)=exp⁡(−e−x)F(x)=\exp(-e^{-x})). The person picks the option with the largest utility. Show that option mm is picked with probability eVm/∑jeVje^{V_m}/\sum_je^{V_j}.

Hint

Condition on the value xx of Vm+GmV_m+G_m: every other option must come out below xx. Then substitute u=e−xu=e^{-x}.

Solution

With density F′(y)=e−yF(y)F'(y)=e^{-y}F(y), P(m wins)=∫F′(x−Vm)∏j≠mF(x−Vj)dx=∫e−(x−Vm)∏jF(x−Vj)dxP(m\text{ wins})=\int F'(x-V_m)\prod_{j\ne m}F(x-V_j)\,dx=\int e^{-(x-V_m)}\prod_jF(x-V_j)\,dx. Since ∏jF(x−Vj)=exp⁡(−e−xZ)\prod_jF(x-V_j)=\exp(-e^{-x}Z) with Z=∑jeVjZ=\sum_je^{V_j}, substituting u=e−xu=e^{-x} gives eVm∫0∞e−uZdu=eVm/Ze^{V_m}\int_0^\infty e^{-uZ}\,du=e^{V_m}/Z. The expected best utility is ln⁡Z+γ\ln Z+\gamma (Euler's constant): the log-sum-exp once more. In machine learning the same fact is the “Gumbel-max trick” for sampling from a softmax.

Hedge is entropy-regularizedAdversarial bandits and experts

In the experts problem, after tt rounds expert aa has total reward SaS_a. Hedge plays pa∝eηSap_a\propto e^{\eta S_a}. Show this is the pp maximizing ⟨p,S⟩+1ηH(p)\langle p,S\rangle+\frac1\eta H(p), and explain why plain “follow the leader” (put all weight on arg⁡max⁡S\arg\max S) is the η→∞\eta\to\infty limit.

Solution

It is the theorem with scores SaS_a and τ=1/η\tau=1/\eta. As η→∞\eta\to\infty the entropy term vanishes and the maximizer becomes the argmax. Follow-the-leader can be forced to switch every round by an adversary; the entropy term keeps Hedge's weights changing smoothly, which is what gives it O(Tlog⁡K)O(\sqrt{T\log K}) regret against any adversary.

InteractiveApplebaum, Ch. 7: Communication (§§7.1–7.2, 7.5)

Channels and capacity

A noisy channel garbles some of what is sent. The mutual information between input and output measures how much gets through, and the sender can tune it by choosing how often to send each symbol. The best achievable value is the channel's capacity, and Shannon's theorem says it is a hard speed limit for reliable communication.

The idea

Channel. A source sends a symbol XX; the receiver gets YY. The channel is described by the conditional probabilities P(Y=y|X=x)P(Y=y\mid X=x), which are fixed. The sender chooses the input law P(X=x)P(X=x).
Capacity. C=max⁡I(X;Y)C=\max I(X;Y), the maximum over all input laws.

In the binary symmetric channel each bit is flipped with probability ε\varepsilon, independently. If P(X=1)=pP(X=1)=p, then P(Y=1)=p(1−ε)+(1−p)εP(Y=1)=p(1-\varepsilon)+(1-p)\varepsilon, and H(Y|X)=Hb(ε)H(Y\mid X)=H_b(\varepsilon) whatever was sent, so

I(X;Y)=Hb(p(1−ε)+(1−p)ε)−Hb(ε),C=1−Hb(ε),I(X;Y)=H_b\big(p(1-\varepsilon)+(1-p)\varepsilon\big)-H_b(\varepsilon), \qquad C=1-H_b(\varepsilon),

with the maximum at p=12p=\frac12, which makes YY uniform.

Shannon's coding theorem. Send blocks of nn bits through the channel, using 2nR2^{nR} of the possible blocks as codewords (a rate of RR message bits per channel use). If R<CR<C, then for large nn there are codes whose probability of a decoding error is as small as you like. Above capacity this is impossible.

Explore

Right-hand plot
The channel. Line widths show the joint probabilities P(X=x,Y=y)P(X=x,Y=y).

Worked example

In a "Z-channel", a 0 is always received correctly, but a 1 arrives as 0 with probability 12\frac12. Find its capacity and the input law that achieves it.

  1. With P(X=1)=pP(X=1)=p, find the law of YY.

    Y=1Y=1 only if a 1 was sent and survived: P(Y=1)=p2P(Y=1)=\frac p2.

  2. Find H(Y|X)H(Y\mid X).

    Given X=0X=0, YY is certain: entropy 0. Given X=1X=1, YY is a fair coin: entropy 1. So H(Y|X)=(1−p)⋅0+p⋅1=pH(Y\mid X)=(1-p)\cdot0+p\cdot1=p.

  3. Write I(X;Y)I(X;Y) as a function of pp and maximise it.

    I(p)=Hb(p2)−pI(p)=H_b(\frac p2)-p. Using ddqHb(q)=log⁡1−qq\frac{d}{dq}H_b(q)=\log\frac{1-q}{q},

    I′(p)=12log⁡1−p/2p/2−1=0⟺2−pp=4⟺p=25.I'(p)=\tfrac12\log\frac{1-p/2}{p/2}-1=0 \iff \frac{2-p}{p}=4 \iff p=\tfrac25.
  4. What is the capacity? What is surprising about the optimal input?

    C=Hb(0.2)−0.4≈0.722−0.4=0.322C=H_b(0.2)-0.4\approx0.722-0.4=0.322 bits, which equals log⁡54\log\frac54. The best input sends 1 only 40% of the time: since 1s are the unreliable symbol, it pays to use them less. For the symmetric channel the optimum is always 12\frac12, but in general the capacity-achieving input need not be uniform.

Check your understanding

A binary symmetric channel has ε=12\varepsilon=\frac12. What is its capacity?

  1. 0
  2. 12\frac12 bit, since half the bits arrive correctlyThey do, but the receiver can't tell which half. Set ε to 0.5 and look at the output law.
  3. 1 bitThat is the capacity of a perfect channel.
With ε=12\varepsilon=\frac12, YY is a fair coin whatever was sent, so YY is independent of XX and I(X;Y)=0I(X;Y)=0 for every input law. Indeed 1−Hb(12)=01-H_b(\frac12)=0.

A binary symmetric channel flips each bit with probability 0.9. How does its capacity compare with a channel that flips with probability 0.1?

  1. It is the same.
  2. It is nearly zero.The receiver can flip every bit back. That turns it into a channel with ε = 0.1.
  3. It is lower than at ε = 0.5.The capacity is smallest at 0.5. Look at the capacity curve.
Hb(0.9)=Hb(0.1)H_b(0.9)=H_b(0.1), so both have capacity 1−0.469=0.5311-0.469=0.531 bits. A channel that is almost always wrong is almost as useful as one that is almost always right; only one that is unpredictable is useless.

Why is the capacity defined as a maximum over input laws?

  1. The channel's noise is fixed, but the sender can choose how often to use each input symbol.
  2. Because I(X;Y)I(X;Y) is the same for every input law, so the maximum is just that value.Move the p slider: I(X;Y)I(X;Y) changes with the input law.
  3. To make the capacity independent of the noise.The capacity depends on the noise; for the symmetric channel it is 1−Hb(ε)1-H_b(\varepsilon).
I(X;Y)I(X;Y) depends on the channel and on the input law. Maximising over the part the sender controls leaves a number that describes the channel alone.

Practice

A ternary channelCapacity by symmetry

Inputs and outputs are {0,1,2}\{0,1,2\}. Each input jj arrives as jj or as j+1(mod3)j+1\pmod3, with probability 12\frac12 each. Find the capacity.

Hint

H(Y|X)H(Y\mid X) does not depend on the input law. Which input law makes H(Y)H(Y) as large as possible?

Solution

H(Y|X=j)=1H(Y\mid X=j)=1 for each jj, so H(Y|X)=1H(Y\mid X)=1. H(Y)≤log⁡3H(Y)\le\log3, with equality when YY is uniform, which happens when XX is uniform. So C=log⁡3−1≈0.585C=\log3-1\approx0.585 bits.

The repetition codeReliable, but slow

Over a binary symmetric channel with ε=0.1\varepsilon=0.1, each message bit is sent three times and decoded by majority vote. Find the probability of a decoding error and the rate. How does this compare with what Shannon's theorem allows?

Solution

Decoding fails if at least two of the three copies are flipped: 3ε2(1−ε)+ε3=0.027+0.001=0.0283\varepsilon^2(1-\varepsilon)+\varepsilon^3=0.027+0.001=0.028. The rate is 13\frac13 bit per use. The capacity is 1−Hb(0.1)≈0.5311-H_b(0.1)\approx0.531, so Shannon's theorem promises codes with rate up to about 0.53 and error probability as small as we like. Repetition buys reliability by giving up rate, and longer repetition makes the rate tend to 0; good codes avoid this.

From Applebaum, Probability and Information: An Integrated Approach (2nd ed., Cambridge, 2008)

Numbers match the book. Each question is restated in our own words; the hints and solutions are written for this page.

Exercise 7.1The binary erasure channel

In the binary erasure channel each input bit is received correctly with probability 1−ε1-\varepsilon and erased (output E) with probability ε\varepsilon; let p=P(S=1)p=P(S=1). Find I(0,0)I(0,0), I(0,E)I(0,E), I(1,1)I(1,1), the mutual information I(R,S)I(R,S), and the capacity.

Hint

Output 00 or 11 reveals the input; output E reveals nothing, so P(S=0|E)=1−pP(S=0\mid E)=1-p.

Solution

Q(0)=(1−p)(1−ε)Q(0)=(1-p)(1-\varepsilon), Q(E)=εQ(E)=\varepsilon, Q(1)=p(1−ε)Q(1)=p(1-\varepsilon), and P(S=0|R=0)=1P(S=0\mid R=0)=1, P(S=0|R=E)=1−pP(S=0\mid R=E)=1-p, P(S=1|R=1)=1P(S=1\mid R=1)=1.

With I(a,b)=log⁡P(a|b)P(a)I(a,b)=\log\frac{P(a\mid b)}{P(a)}: I(0,0)=−log⁡(1−p)I(0,0)=-\log(1-p), I(0,E)=log⁡1−p1−p=0I(0,E)=\log\frac{1-p}{1-p}=0, I(1,1)=−log⁡pI(1,1)=-\log p.

I(R,S)=H(S)−HR(S)I(R,S)=H(S)-H_R(S). Given R∈{0,1}R\in\{0,1\} there's no uncertainty; given R=ER=E the uncertainty is Hb(p)H_b(p). So HR(S)=εHb(p)H_R(S)=\varepsilon H_b(p) and I(R,S)=(1−ε)Hb(p)I(R,S)=(1-\varepsilon)H_b(p). This is maximal at p=12p=\tfrac12: C=1−εC=1-\varepsilon bits. The fraction ε\varepsilon of erased bits is simply lost.

Exercise 7.2Output entropy of the binary symmetric channel

For the binary symmetric channel with error probability ε\varepsilon and p=P(S=1)p=P(S=1), confirm H(R)=Hb(x)H(R)=H_b(x) with x=ε+p−2εpx=\varepsilon+p-2\varepsilon p. Show x=12x=\tfrac12 iff p=12p=\tfrac12 when ε≠12\varepsilon\ne\tfrac12. Why doesn't the case ε=12\varepsilon=\tfrac12 matter for the capacity?

Hint

x=P(R=1)x=P(R=1).

Solution

P(R=1)=p(1−ε)+(1−p)ε=ε+p−2εp=xP(R=1)=p(1-\varepsilon)+(1-p)\varepsilon=\varepsilon+p-2\varepsilon p=x, so H(R)=Hb(x)H(R)=H_b(x).

x=12⟺p(1−2ε)=12(1−2ε)⟺p=12x=\tfrac12\iff p(1-2\varepsilon)=\tfrac12(1-2\varepsilon)\iff p=\tfrac12, dividing by 1−2ε≠01-2\varepsilon\ne0.

If ε=12\varepsilon=\tfrac12, the output is independent of the input: x=12x=\tfrac12 for every pp, H(R)=1=HS(R)H(R)=1=H_S(R), so I(R,S)=0I(R,S)=0 whatever pp is, and the capacity 1−Hb(ε)=01-H_b(\varepsilon)=0. The maximisation over pp is trivial, so nothing in the capacity calculation depends on solving x=12x=\tfrac12.

Exercise 7.5A cascade of two channels

Two binary symmetric channels A→B→CA\to B\to C with error probabilities ε1=0.14\varepsilon_1=0.14, ε2=0.21\varepsilon_2=0.21 are connected in series, with P(A=1)=0.4P(A=1)=0.4. Compute I(A,B)I(A,B) and I(A,C)I(A,C) and check I(A,B)>I(A,C)I(A,B)>I(A,C).

Hint

The cascade is itself a binary symmetric channel with error ε=ε1+ε2−2ε1ε2\varepsilon=\varepsilon_1+\varepsilon_2-2\varepsilon_1\varepsilon_2. For a BSC, I=Hb(P(output=1))−Hb(error)I=H_b(P(\text{output}=1))-H_b(\text{error}).

Solution

P(B=1)=0.14+0.4−2(0.14)(0.4)=0.428P(B=1)=0.14+0.4-2(0.14)(0.4)=0.428, so I(A,B)=Hb(0.428)−Hb(0.14)≈0.985−0.584=0.401I(A,B)=H_b(0.428)-H_b(0.14)\approx0.985-0.584=0.401 bits.

The cascade error is ε=0.14+0.21−2(0.14)(0.21)=0.2912\varepsilon=0.14+0.21-2(0.14)(0.21)=0.2912, and P(C=1)=0.2912+0.4−2(0.2912)(0.4)≈0.458P(C=1)=0.2912+0.4-2(0.2912)(0.4)\approx0.458. So I(A,C)=Hb(0.458)−Hb(0.2912)≈0.995−0.870=0.125I(A,C)=H_b(0.458)-H_b(0.2912)\approx0.995-0.870=0.125 bits.

I(A,B)−I(A,C)≈0.276>0I(A,B)-I(A,C)\approx0.276>0: passing through a second noisy channel loses information (the data processing inequality).

Animated lessonApplebaum, Ch. 7: Communication (§§7.3–7.4)

Prefix codes and Huffman coding

To compress a source, give common symbols short codewords and rare ones long codewords. Entropy is the floor: no code can beat HH bits per symbol on average. Huffman's algorithm builds the best possible prefix code by repeatedly merging the two least likely symbols. Watch it work.

The idea

Prefix-free code. A binary code in which no codeword is the beginning of another. Such a code can be decoded as the bits arrive, with no separators. Codewords correspond to leaves of a binary tree: 0 for left, 1 for right.
Kraft–McMillan inequality. A binary prefix-free code with codeword lengths l1,…,lnl_1,\dots,l_n exists if and only if ∑j2−lj≤1\sum_j 2^{-l_j}\le1. (McMillan: the inequality also holds for every uniquely decodable code.)
Noiseless coding theorem. For a source with law p1,…,pnp_1,\dots,p_n, every uniquely decodable code has average length E(L)=∑jpjlj≥HE(L)=\sum_j p_jl_j\ge H. There is a prefix-free code with E(L)<H+1E(L)<H+1: take lj=⌈−log⁡pj⌉l_j=\lceil-\log p_j\rceil (Shannon–Fano lengths).
Huffman's algorithm. Start with each symbol as a one-node tree with its probability. Repeatedly join the two trees of smallest probability under a new root, whose probability is their sum, until one tree remains. The resulting code has the smallest E(L)E(L) of all prefix-free codes.

E(L)=HE(L)=H exactly when every pjp_j is a power of 12\frac12. Otherwise the gap can be shrunk by coding blocks of mm symbols at a time, which gives H≤E(L)/m<H+1mH\le E(L)/m<H+\frac1m bits per symbol.

Watch

Source (six symbols)
the two trees being merged trees still waiting codeword lengths at the end
  1. Six symbols, sorted by probability. Each one starts as a tree on its own.
  2. Merge the two least likely trees. The new node's probability is their sum.
  3. Repeat with the trees that remain: again take the two smallest.
  4. And again. A merged node is treated exactly like a symbol.
  5. After this merge only two trees are left.
  6. The last merge makes the root.
  7. Label left branches 0 and right branches 1. Each codeword is the path from the root to its symbol. Compare the average length with the entropy.

Worked example

A source emits five symbols with probabilities 0.4, 0.2, 0.15, 0.15, 0.10.4,\ 0.2,\ 0.15,\ 0.15,\ 0.1. Build a Huffman code and compare it with the entropy and with the Shannon–Fano lengths.

  1. Carry out the merges.

    0.1+0.15=0.250.1+0.15=0.25. Now the smallest are 0.150.15 and 0.20.2: 0.350.35. Then 0.25+0.35=0.60.25+0.35=0.6. Finally 0.4+0.6=10.4+0.6=1.

  2. Read off the codeword lengths and the average length.

    The symbol of probability 0.4 joins only at the last merge: length 1. The other four sit two merges below the 0.6 node: length 3 each. So E(L)=0.4⋅1+0.6⋅3=2.2E(L)=0.4\cdot1+0.6\cdot3=2.2 bits. One valid code is 0, 100, 101, 110, 1110,\ 100,\ 101,\ 110,\ 111. Kraft: 12+4⋅18=1\frac12+4\cdot\frac18=1.

  3. Compute the entropy.

    H=0.4log⁡2.5+0.2log⁡5+2⋅0.15log⁡10.15+0.1log⁡10≈0.529+0.464+0.821+0.332=2.146H=0.4\log2.5+0.2\log5+2\cdot0.15\log\frac1{0.15}+0.1\log10\approx0.529+0.464+0.821+0.332=2.146 bits. So H≤E(L)<H+1H\le E(L)<H+1, with a gap of only about 0.05 bits.

  4. What do the Shannon–Fano lengths ⌈−log⁡pj⌉\lceil-\log p_j\rceil give?

    Lengths 2,3,3,3,42,3,3,3,4, so E(L)=0.8+0.6+0.45+0.45+0.4=2.7E(L)=0.8+0.6+0.45+0.45+0.4=2.7 bits. It also lies within 1 bit of HH, as the theorem promises, but Huffman does half a bit better.

Check your understanding

Is there a binary prefix-free code for four symbols with codeword lengths 1, 2, 2, 3?

  1. Yes, for example 0, 10, 11, 110110 begins with 11, which is another codeword.
  2. No
  3. Yes, as long as the shortest word goes to the most likely symbolWhether the lengths are possible doesn't depend on the probabilities at all.
12+14+14+18=98>1\frac12+\frac14+\frac14+\frac18=\frac98>1, so Kraft–McMillan rules it out. Lengths 1, 2, 3, 3 would work: 0, 10, 110, 111.

A source has two symbols with probabilities 0.9 and 0.1, so H≈0.469H\approx0.469 bits. What is the average length of its Huffman code?

  1. 0.469 bitsCodeword lengths are whole numbers, and each symbol needs at least one bit.
  2. 1 bit
  3. Somewhere between 0.469 and 1, depending on the codeWith two symbols the only sensible code is 0 and 1.
The code is {0,1}\{0,1\}, with E(L)=1E(L)=1, more than twice the entropy. Coding pairs of symbols brings it down to 0.645 bits per symbol (see the first practice problem); longer blocks get closer still.

For which sources does the Huffman code achieve E(L)=HE(L)=H exactly?

  1. When every probability is a power of 12\frac12
  2. When the source is uniformTry three equally likely symbols: H=log⁡3≈1.585H=\log3\approx1.585, but the best code has lengths 1, 2, 2 and E(L)=53E(L)=\frac53.
  3. Always, since Huffman codes are optimalOptimal among codes, but codes have whole-number lengths, and HH usually isn't of that form.
E(L)−H=∑pj(lj+log⁡pj)E(L)-H=\sum p_j(l_j+\log p_j) vanishes only if lj=−log⁡pjl_j=-\log p_j for each jj, which needs pj=2−ljp_j=2^{-l_j}. Try the "dyadic" source in the animation.

Practice

Coding pairsThe extension of a source

For the source with probabilities 0.9 and 0.1, code pairs of independent symbols. Build the Huffman code for the four pairs and find the average length per original symbol.

Solution

The pairs have probabilities 0.81, 0.09, 0.09, 0.01. Merge 0.01+0.09=0.100.01+0.09=0.10, then 0.09+0.10=0.190.09+0.10=0.19, then 0.19+0.810.19+0.81. Lengths: 1, 2, 3, 3, so E(L)=0.81+0.18+0.27+0.03=1.29E(L)=0.81+0.18+0.27+0.03=1.29 bits per pair, or 0.645 bits per symbol, against H≈0.469H\approx0.469. The bound says blocks of length mm are within 1m\frac1m of HH per symbol.

Likely symbols get short wordsAn exchange argument

Show that in a code of minimal average length, if pi>pjp_i>p_j then li≤ljl_i\le l_j.

Solution

Suppose pi>pjp_i>p_j but li>ljl_i>l_j. Swap the two codewords. The code is still prefix-free, and the average length changes by pilj+pjli−pili−pjlj=−(pi−pj)(li−lj)<0p_il_j+p_jl_i-p_il_i-p_jl_j=-(p_i-p_j)(l_i-l_j)<0. So the original code was not minimal.

From Applebaum, Probability and Information: An Integrated Approach (2nd ed., Cambridge, 2008)

Numbers match the book. Each question is restated in our own words; the hints and solutions are written for this page.

Exercise 7.9A ternary prefix-free code

Show that a prefix-free code over {0,1,2}\{0,1,2\} with word lengths 1,2,2,2,2,3,3,3,3,4,41,2,2,2,2,3,3,3,3,4,4 exists, and find one.

Hint

Check Kraft's inequality ∑3−li≤1\sum3^{-l_i}\le1, then assign words greedily from shortest to longest.

Solution

13+49+427+281=27+36+12+281=7781≤1\tfrac13+\tfrac49+\tfrac4{27}+\tfrac2{81}=\tfrac{27+36+12+2}{81}=\tfrac{77}{81}\le1, so such a code exists. One choice:

0;10, 11, 12, 20;210, 211, 212, 220;2210, 2211.0;\quad 10,\ 11,\ 12,\ 20;\quad 210,\ 211,\ 212,\ 220;\quad 2210,\ 2211.

No word is a prefix of another: after using 00, the length-2 words start with 1 or 2; after 10,11,12,2010,11,12,20 the unused prefixes are 21,2221,22; and so on.

Exercise 7.12Shannon–Fano versus Huffman

A source has probabilities 0.27,0.31,0.14,0.18,0.100.27,0.31,0.14,0.18,0.10. Find (a) a prefix-free binary code by Shannon–Fano coding, (b) an optimal code by Huffman coding.

Hint

Shannon–Fano: lj=⌈−log2⁡pj⌉l_j=\lceil-\log_2p_j\rceil. Huffman: repeatedly merge the two least likely.

Solution

(a) Lengths 2,2,3,3,42,2,3,3,4 (e.g. −log2⁡0.10≈3.32-\log_20.10\approx3.32). A code: 0.27→000.27\to00, 0.31→010.31\to01, 0.14→1000.14\to100, 0.18→1010.18\to101, 0.10→11000.10\to1100. Mean length 2.522.52.

(b) Merge 0.10+0.14=0.240.10+0.14=0.24, then 0.18+0.24=0.420.18+0.24=0.42, then 0.27+0.31=0.580.27+0.31=0.58. Code: 0.31→100.31\to10, 0.27→110.27\to11, 0.18→000.18\to00, 0.14→0100.14\to010, 0.10→0110.10\to011. Mean length 2.242.24, against entropy H≈2.21H\approx2.21 bits.

Exercise 7.15Two Huffman codes

(a) For probabilities 0.02,0.09,0.15,0.21,0.26,0.270.02,0.09,0.15,0.21,0.26,0.27, show the Huffman procedure can be carried out in two different ways; write down both codes. Is either better? (b) Find the redundancy 1−η1-\eta of the optimal code, where η=H/𝔼(L)\eta=H/\mathbb E(L).

Hint

After two merges there is a tie between two nodes of probability 0.260.26.

Solution

(a) Merge 0.02+0.09=0.110.02+0.09=0.11, then 0.11+0.15=0.260.11+0.15=0.26. Now 0.210.21 must be merged with one of two 0.260.26's: the symbol or the merged node. Either way the remaining two are merged next, then the root.

Merging 0.210.21 with the merged node gives, for instance, 0.26→000.26\to00, 0.27→010.27\to01, 0.21→100.21\to10, 0.15→1100.15\to110, 0.02→11100.02\to1110, 0.09→11110.09\to1111. Merging it with the symbol gives 0.21→000.21\to00, 0.26→010.26\to01, 0.27→100.27\to10, 0.15→1100.15\to110, 0.02→11100.02\to1110, 0.09→11110.09\to1111. The codewords differ but the length profile is the same, (4,4,3,2,2,2)(4,4,3,2,2,2), with 𝔼(L)=2.37\mathbb E(L)=2.37. Both are optimal; neither is better.

(b) H≈2.324H\approx2.324 bits, so η≈2.324/2.37≈0.981\eta\approx2.324/2.37\approx0.981 and the redundancy is about 0.0190.019.

InteractiveApplebaum, Ch. 8 (§8.7) and Ch. 9 (§§9.6–9.7)

Entropy for densities and the Gaussian channel

Real signals (voltages, waveforms) take continuous values. Entropy carries over to densities only awkwardly, but mutual information carries over cleanly, and with it the capacity of the most important channel in practice: a signal plus Gaussian noise.

The idea

Entropy of a density (Applebaum (8.25)). For XX with pdf ff, H(X)=−∫f(x)log⁡f(x)dxH(X)=-\int f(x)\log f(x)\,dx. For X∼U(a,b)X\sim U(a,b), H(X)=log⁡(b−a)H(X)=\log(b-a), which is negative when b−a<1b-a<1: unlike the discrete case, this “entropy” can be below zero.

Why it is awkward. Chop the range into bins of width Δ\Delta and let X^\hat X be the bin XX falls in. Then H(X^)≈H(X)−log⁡ΔH(\hat X)\approx H(X)-\log\Delta, which goes to +∞+\infty as Δ→0\Delta\to0: describing a continuous value exactly takes infinitely many bits. H(X)H(X) is what remains after subtracting the infinite part, so it is a relative quantity, not an amount of information.

The Gaussian has the most entropy for its variance (Applebaum Thm 8.10). If XX has a pdf, mean μ\mu and variance σ2\sigma^2, then
H(X)≤12log⁡(2πeσ2),H(X)\le\tfrac12\log(2\pi e\sigma^2),
with equality exactly when X∼N(μ,σ2)X\sim N(\mu,\sigma^2). It is the maximum entropy principle again: knowing only the mean and variance, assume a normal law.
Mutual information (Applebaum §9.7). I(X,Y)=∬fW(x,y)log⁡fW(x,y)fX(x)fY(y)dxdy=H(Y)−HX(Y)I(X,Y)=\iint f_W(x,y)\log\frac{f_W(x,y)}{f_X(x)f_Y(y)}\,dx\,dy=H(Y)-H_X(Y). It is always ≥0\ge0, zero exactly for independence, and the infinite parts cancel under discretization: I(X^,Y^)→I(X,Y)I(\hat X,\hat Y)\to I(X,Y). For a bivariate normal with correlation ρ\rho, I(X,Y)=−12log⁡(1−ρ2)I(X,Y)=-\tfrac12\log(1-\rho^2).
The Gaussian channel (Applebaum Thm 9.8). Send XX with mean 0 and variance (power) σX2\sigma_X^2; receive Y=X+ZY=X+Z, with noise Z∼N(0,σZ2)Z\sim N(0,\sigma_Z^2) independent of XX. The capacity, the largest I(X,Y)I(X,Y) over input laws, is
C=12log⁡(1+σX2σZ2) bits per use,C=\tfrac12\log\Big(1+\frac{\sigma_X^2}{\sigma_Z^2}\Big)\ \text{bits per use},
attained when XX is normal. The proof is three lines: I=H(Y)−HX(Y)=H(Y)−H(Z)I=H(Y)-H_X(Y)=H(Y)-H(Z), since once XX is known all uncertainty in YY is noise; Var(Y)=σX2+σZ2\mathrm{Var}(Y)=\sigma_X^2+\sigma_Z^2 is fixed, so H(Y)H(Y) is largest when YY is normal, which happens when XX is.

The ratio σX2/σZ2\sigma_X^2/\sigma_Z^2 is the signal-to-noise ratio. Capacity grows only logarithmically in it: quadrupling the power buys one more bit per use, at most.

Explore

Four densities, all with mean 0 and the same variance σ2\sigma^2. normal triangular Laplace uniform. The bars show the normal law discretized into bins of width Δ\Delta.
Capacity C=12log⁡(1+SNR)C=\tfrac12\log(1+\mathrm{SNR}) of the Gaussian channel, with the chosen SNR marked. The dashed line is 12log⁡SNR\tfrac12\log\mathrm{SNR}: at high SNR, each doubling of power adds half a bit.

Worked example

Three short calculations, in bits.

  1. X∼U(0.1,0.5)X\sim U(0.1,\,0.5). Find H(X)H(X).

    log2⁡(0.4)≈−1.32\log_2(0.4)\approx-1.32 bits. Negative: a density squeezed into an interval shorter than 1 has negative entropy.

  2. X,YX,Y are jointly normal with correlation ρ=0.8\rho=0.8. Find I(X,Y)I(X,Y).

    −12log2⁡(1−0.64)=−12log2⁡(0.36)≈0.74-\tfrac12\log_2(1-0.64)=-\tfrac12\log_2(0.36)\approx0.74 bits. It depends only on ρ\rho, not on the variances.

  3. A channel has signal power 15 and Gaussian noise power 1. Find the capacity, and the capacity if the power is doubled.

    C=12log2⁡(16)=2C=\tfrac12\log_2(16)=2 bits per use. With power 30: 12log2⁡(31)≈2.48\tfrac12\log_2(31)\approx2.48 bits: doubling the power bought less than half a bit.

Check your understanding

Doubling σ\sigma changes the entropy of a normal random variable by:

  1. +1+1 bit.
  2. A factor of 2.H=log⁡(σ2πe)H=\log(\sigma\sqrt{2\pi e}): scaling σ\sigma adds to the logarithm.
H(cX)=H(X)+log⁡|c|H(cX)=H(X)+\log|c| for any density. The mean does not appear at all: shifting a density doesn't change its uncertainty.

As the bin width Δ\Delta shrinks, the entropy H(X^)H(\hat X) of the discretized normal:

  1. Grows without bound, like H(X)−log⁡ΔH(X)-\log\Delta.
  2. Converges to H(X)H(X).Only H(X^)+log⁡ΔH(\hat X)+\log\Delta does.
Each halving of the bins adds about one bit: you need one more binary digit to say which half of a bin the value is in. That is why mutual information, a difference in which this drift cancels, is the meaningful quantity for continuous signals.

For a fixed signal power, which input law gives the Gaussian channel its capacity?

  1. Uniform, because it spreads the signal evenly.The uniform has less entropy than the normal for the same variance.
  2. Normal: it makes Y=X+ZY=X+Z normal, the variance-(σX2+σZ2)(\sigma_X^2+\sigma_Z^2) law of maximum entropy.
I=H(Y)−H(Z)I=H(Y)-H(Z) and H(Z)H(Z) is fixed, so the best input makes H(Y)H(Y) as large as possible, and Theorem 8.10 says that is the normal law.

Practice

The normal's entropyA short integral

Show that H(X)=12log⁡(2πeσ2)H(X)=\tfrac12\log(2\pi e\sigma^2) for X∼N(μ,σ2)X\sim N(\mu,\sigma^2).

Solution

−log⁡f(x)=log⁡(σ2π)+(x−μ)22σ2log⁡e-\log f(x)=\log(\sigma\sqrt{2\pi})+\frac{(x-\mu)^2}{2\sigma^2}\log e. Take expectations: 𝔼(X−μ)2=σ2\mathbb E(X-\mu)^2=\sigma^2, so H=log⁡(σ2π)+12log⁡e=12log⁡(2πeσ2)H=\log(\sigma\sqrt{2\pi})+\tfrac12\log e=\tfrac12\log(2\pi e\sigma^2).

Noise sets the floorCorollary 9.6

For Y=X+ZY=X+Z with X,ZX,Z independent, explain why HX(Y)=H(Z)H_X(Y)=H(Z), and deduce that if ZZ is normal with variance σZ2\sigma_Z^2, no input can make I(X,Y)I(X,Y) exceed 12log⁡(1+σX2/σZ2)\tfrac12\log(1+\sigma_X^2/\sigma_Z^2).

Solution

Given X=xX=x, Y=x+ZY=x+Z is ZZ shifted by xx, and shifting doesn't change entropy, so HX(Y)=H(Z)H_X(Y)=H(Z). Then I=H(Y)−H(Z)≤12log⁡(2πe(σX2+σZ2))−12log⁡(2πeσZ2)I=H(Y)-H(Z)\le\tfrac12\log(2\pi e(\sigma_X^2+\sigma_Z^2))-\tfrac12\log(2\pi e\sigma_Z^2), by Theorem 8.10 applied to YY, whose variance is σX2+σZ2\sigma_X^2+\sigma_Z^2.

SimulationApplebaum, Ch. 10: Markov chains and their entropy

Markov chains and entropy rate

A Markov chain forgets everything except where it is now. That one rule makes it settle into an equilibrium from any start, and it makes the information it produces per step easy to compute. Set the two switching probabilities and watch the chain converge.

The idea

Markov chain. A sequence X0,X1,…X_0,X_1,\dots on a finite state space with P(Xn+1=j|Xn=i, Xn−1,…,X0)=PijP(X_{n+1}=j\mid X_n=i,\ X_{n-1},\dots,X_0)=P_{ij}, depending only on ii and jj. The transition matrix PP has non-negative entries and rows summing to 1.
Chapman–Kolmogorov. The nn-step transition probabilities satisfy Pij(m+n)=∑kPik(m)Pkj(n)P^{(m+n)}_{ij}=\sum_kP^{(m)}_{ik}P^{(n)}_{kj}, so P(n)=PnP^{(n)}=P^n. If π(n)\pi^{(n)} is the law of XnX_n, written as a row vector, then π(n)=π(0)Pn\pi^{(n)}=\pi^{(0)}P^n.
Invariant distribution. A law ρ\rho with ρP=ρ\rho P=\rho. If the chain starts with law ρ\rho, every XnX_n has law ρ\rho and the process is stationary. If some power PmP^m has all entries positive, ρ\rho is unique and π(n)→ρ\pi^{(n)}\to\rho from every starting law.
Entropy rate. h=limn→∞⁡1nH(X0,…,Xn−1)h=\lim_{n\to\infty}\frac1nH(X_0,\dots,X_{n-1}). For a stationary Markov chain,
h=H(X1|X0)=−∑iρi∑jPijlog⁡Pij,h=H(X_1\mid X_0)=-\sum_i\rho_i\sum_jP_{ij}\log P_{ij},

the average, under ρ\rho, of the entropies of the rows of PP. It satisfies h≤H(ρ)h\le H(\rho), with equality exactly when all the rows are equal, that is, when the XnX_n are i.i.d.

Explore

The chain, and a simulated path: state 0 state 1
P(Xn=0|X0=0)P(X_n=0\mid X_0=0) P(Xn=0|X0=1)P(X_n=0\mid X_0=1) ρ0\rho_0

Worked example

A particle moves on the states 0,1,20,1,2 with transition matrix

P=(1212014121401212).P=\begin{pmatrix}\frac12&\frac12&0\\ \frac14&\frac12&\frac14\\ 0&\frac12&\frac12\end{pmatrix}.
  1. Starting from 0, what is the law of X2X_2?

    It is the first row of P2P^2: (12⋅12+12⋅14, 12⋅12+12⋅12, 12⋅14)=(38,12,18)\big(\frac12\cdot\frac12+\frac12\cdot\frac14,\ \frac12\cdot\frac12+\frac12\cdot\frac12,\ \frac12\cdot\frac14\big)=\big(\frac38,\frac12,\frac18\big).

  2. Find the invariant distribution.

    Solve ρP=ρ\rho P=\rho. The first column gives ρ0=12ρ0+14ρ1\rho_0=\frac12\rho_0+\frac14\rho_1, so ρ1=2ρ0\rho_1=2\rho_0; by symmetry ρ1=2ρ2\rho_1=2\rho_2. With ρ0+ρ1+ρ2=1\rho_0+\rho_1+\rho_2=1: ρ=(14,12,14)\rho=(\frac14,\frac12,\frac14). P2P^2 has all entries positive, so this is the limit from any start.

  3. Compute the entropy rate.

    The rows have entropies 1, 1.5 and 1 bits. So h=14⋅1+12⋅1.5+14⋅1=1.25h=\frac14\cdot1+\frac12\cdot1.5+\frac14\cdot1=1.25 bits per step.

  4. Compare with H(ρ)H(\rho) and interpret.

    H(ρ)=14⋅2+12⋅1+14⋅2=1.5H(\rho)=\frac14\cdot2+\frac12\cdot1+\frac14\cdot2=1.5 bits. An i.i.d. sequence with the same one-step law would produce 1.5 bits per step; the chain produces only 1.25, because each step is partly predictable from the last. A source like this can be compressed to about 1.25 bits per symbol, but only by a code that uses the previous symbol.

Check your understanding

The two-state chain above has P(0→1)=aP(0\to1)=a and P(1→0)=bP(1\to0)=b, with a+b>0a+b>0. What is ρ0\rho_0, the long-run fraction of time in state 0?

  1. ba+b\dfrac{b}{a+b}
  2. aa+b\dfrac{a}{a+b}A large aa means the chain leaves 0 quickly, so it should spend less time there, not more.
  3. 12\frac12, by symmetryThe two states are only symmetric when a=ba=b.
In equilibrium the flow out of 0 balances the flow in: ρ0a=ρ1b\rho_0a=\rho_1b. With ρ0+ρ1=1\rho_0+\rho_1=1 this gives ρ0=b/(a+b)\rho_0=b/(a+b).

Take a=b=1a=b=1: the chain switches state at every step. Starting from 0, does P(Xn=0)P(X_n=0) converge?

  1. Yes, to 12\frac12Starting from 0, the chain is at 0 at every even time and at 1 at every odd time. Set both sliders to 1.
  2. No, it alternates between 1 and 0, although (12,12)(\frac12,\frac12) is invariant.
  3. No, and there is no invariant distribution.Check (12,12)P(\frac12,\frac12)P.
PnP^n alternates between the identity and the swap matrix, so no power has all entries positive and the convergence theorem doesn't apply. The chain is periodic. Its invariant law (12,12)(\frac12,\frac12) still gives the long-run fraction of time in each state.

For a stationary Markov chain with invariant law ρ\rho, which is always true?

  1. h≤H(ρ)h\le H(\rho)
  2. h≥H(ρ)h\ge H(\rho)h=H(X1|X0)h=H(X_1\mid X_0), and conditioning cannot increase entropy on average.
  3. h=H(ρ)h=H(\rho)Only when X1X_1 is independent of X0X_0. Try a=0.1, b=0.3a=0.1,\ b=0.3.
h=H(X1|X0)≤H(X1)=H(ρ)h=H(X_1\mid X_0)\le H(X_1)=H(\rho), with equality exactly when X1X_1 and X0X_0 are independent, which for a Markov chain means every row of PP equals ρ\rho. For two states that is the line a+b=1a+b=1.

Practice

A walk around a cycleDoubly stochastic matrices

A particle on the cycle 0,1,…,N−10,1,\dots,N-1 (N≥3N\ge3) steps one place clockwise or anticlockwise with probability 12\frac12 each. Show that the uniform law is invariant and find the entropy rate. For which NN does P(Xn=0|X0=0)P(X_n=0\mid X_0=0) converge?

Solution

Every column of PP also sums to 1, so 1N(1,…,1)P=1N(1,…,1)\frac1N(1,\dots,1)P=\frac1N(1,\dots,1). Each row has two entries 12\frac12, so h=1h=1 bit per step, while H(ρ)=log⁡NH(\rho)=\log N. For even NN the chain alternates between even and odd states, so P(Xn=0|X0=0)=0P(X_n=0\mid X_0=0)=0 for odd nn and there is no convergence. For odd NN some power of PP is positive and the probability tends to 1N\frac1N.

A sticky chainEntropy rate of two states

Compute ρ\rho, hh and H(ρ)H(\rho) for the two-state chain with a=0.1a=0.1, b=0.3b=0.3. Check your answers in the explorer.

Solution

ρ=(0.30.4,0.10.4)=(0.75,0.25)\rho=(\frac{0.3}{0.4},\frac{0.1}{0.4})=(0.75,0.25). h=0.75Hb(0.1)+0.25Hb(0.3)≈0.75⋅0.469+0.25⋅0.881=0.572h=0.75\,H_b(0.1)+0.25\,H_b(0.3)\approx0.75\cdot0.469+0.25\cdot0.881=0.572 bits, while H(ρ)=Hb(0.25)≈0.811H(\rho)=H_b(0.25)\approx0.811 bits. The chain stays put most of the time, which makes it more predictable than an i.i.d. sequence spending the same fraction of time in each state.

From Applebaum, Probability and Information: An Integrated Approach (2nd ed., Cambridge, 2008)

Numbers match the book. Each question is restated in our own words; the hints and solutions are written for this page.

Exercise 10.14Invariant distributions

Decide whether each chain has an invariant distribution and find it: (a) (0.70.30.40.6)\begin{pmatrix}0.7&0.3\\0.4&0.6\end{pmatrix}; (b) (1/21/41/41/81/23/83/81/43/8)\begin{pmatrix}1/2&1/4&1/4\\1/8&1/2&3/8\\3/8&1/4&3/8\end{pmatrix}; (c) (01002/31/31/65/60)\begin{pmatrix}0&1&0\\0&2/3&1/3\\1/6&5/6&0\end{pmatrix}.

Hint

Solve πP=π\pi P=\pi with ∑πi=1\sum\pi_i=1. A finite chain always has at least one invariant distribution; all three here are irreducible, so it's unique.

Solution

(a) 0.3π1=0.4π20.3\pi_1=0.4\pi_2, so π=(4/7,3/7)\pi=(4/7,3/7).

(b) Each column sums to 11 (the matrix is doubly stochastic), so the uniform π=(1/3,1/3,1/3)\pi=(1/3,1/3,1/3) is invariant.

(c) The equations give π1=16π3\pi_1=\tfrac16\pi_3 and π3=13π2\pi_3=\tfrac13\pi_2. So π∝(1,18,6)\pi\propto(1,18,6): π=(0.04,0.72,0.24)\pi=(0.04,0.72,0.24).

Exercise 10.23Entropy rates

Compute the entropy rates h=∑iπiH(Pi⋅)h=\sum_i\pi_iH(P_{i\cdot}) of the stationary chains in Exercise 10.14.

Hint

Weight the entropy of each row by the invariant probability of that state.

Solution

(a) h=47Hb(0.3)+37Hb(0.4)≈47(0.881)+37(0.971)≈0.920h=\tfrac47H_b(0.3)+\tfrac37H_b(0.4)\approx\tfrac47(0.881)+\tfrac37(0.971)\approx0.920 bits.

(b) Row entropies 1.51.5, ≈1.406\approx1.406, ≈1.561\approx1.561; averaged uniformly, h≈1.489h\approx1.489 bits.

(c) Row entropies 00, Hb(1/3)≈0.918H_b(1/3)\approx0.918, Hb(1/6)≈0.650H_b(1/6)\approx0.650. So h≈0.72(0.918)+0.24(0.650)≈0.817h\approx0.72(0.918)+0.24(0.650)\approx0.817 bits.

Exercise 10.24Bounds on the entropy rate

For a stationary Markov chain on NN states, show 0≤h(X)≤log⁡N0\le h(X)\le\log N. When does equality hold?

Hint

h=∑iπiH(Pi⋅)h=\sum_i\pi_iH(P_{i\cdot}) is an average of entropies of distributions on NN points.

Solution

Each H(Pi⋅)H(P_{i\cdot}) lies in [0,log⁡N][0,\log N], and the πi\pi_i are weights summing to 1, so h∈[0,log⁡N]h\in[0,\log N].

h=0h=0 iff every row with πi>0\pi_i>0 is deterministic (the chain moves deterministically from every state it visits). h=log⁡Nh=\log N iff every row with πi>0\pi_i>0 is uniform; for an irreducible chain this means Pij=1/NP_{ij}=1/N for all i,ji,j, i.e. i.i.d. uniform symbols.