MathLabs

Applied and computational mathematics

Information theory

Quantifies information and communication limits using entropy, founded by Claude Shannon.

IntuitionGuessing games and how much a message really tells you

Imagine someone flips a coin and tells you the result. If the coin is fair, you learn something genuinely surprising each time — heads and tails are equally likely, so you could not have guessed the outcome in advance. If the coin almost always lands heads, hearing "heads" tells you almost nothing (you expected it), while hearing the rare "tails" is a small shock carrying much more information. Information theory turns this everyday intuition — rare, surprising outcomes carry more information than expected, predictable ones — into an exact number.

A downward-opening parabola peaking at the center of the horizontal axis and dropping to low values at both edges, illustrating the shape of the binary entropy function which is zero at the extremes and maximal at probability one half.
The plotted curve y=−2x2+1y = -2x^2 + 1 has the same qualitative shape as the binary entropy function H(p)H(p): it is small near the two edges and reaches a single peak in the middle. H(p)H(p) itself vanishes at p=0p=0 and p=1p=1 (no surprise — the outcome is certain) and peaks at p=0.5p=0.5 (maximum surprise — a fair coin).

SchoolBits, surprise, and the entropy formula

Definition: Shannon entropy

For a random variable XX taking value ii with probability pip_i (for i=1,…,ni = 1, \dots, n), the Shannon entropy H(X)H(X) is the average number of bits of surprise per outcome: H(X)=−∑i=1npilog⁡2piH(X) = -\sum_{i=1}^n p_i \log_2 p_i. The term −log⁡2pi-\log_2 p_i is the "surprise" of outcome ii — large when pip_i is small (rare, surprising) and 00 when pi=1p_i = 1 (certain, no surprise) — and H(X)H(X) averages this surprise, weighted by how often each outcome actually occurs.

H(X)=−∑i=1npilog⁡2piH(X) = -\sum_{i=1}^n p_i \log_2 p_i

Here nn is the number of possible outcomes, pip_i is the probability of outcome ii (with ∑ipi=1\sum_i p_i = 1), and the logarithm base 22 means H(X)H(X) is measured in bits. The most important special case is a coin with two outcomes, probability pp and 1−p1-p: H(p)=−plog⁡2p−(1−p)log⁡2(1−p)H(p) = -p\log_2 p - (1-p)\log_2(1-p), called the binary entropy function.

H(p)=−plog⁡2p−(1−p)log⁡2(1−p)H(p) = -p\log_2 p - (1-p)\log_2(1-p)
Entropy for a few example distributions, in bits
DistributionEntropy H(X)H(X)
Fair coin, p=(0.5, 0.5)p=(0.5,\,0.5)11 bit (maximal for 2 outcomes)
Biased coin, p=(0.9, 0.1)p=(0.9,\,0.1)≈0.469\approx 0.469 bits
Uniform over 88 outcomes33 bits (=log⁡28=\log_2 8)
Deterministic, p=(1, 0)p=(1,\,0)00 bits (no surprise at all)

UndergraduateGibbs' inequality, optimal codes, and channel capacity

Given two probability distributions pp and qq on the same nn outcomes, the Kullback–Leibler divergence D(p∥q)D(p\|q) measures how much is "wasted" by assuming outcomes follow qq when they really follow pp: D(p∥q)=∑ipilog⁡2piqiD(p\|q) = \sum_{i} p_i \log_2\frac{p_i}{q_i}. This quantity underlies why entropy behaves the way it does, and why no code can beat the entropy bound — both are consequences of the following inequality.

For any two probability distributions p=(p1,…,pn)p=(p_1,\dots,p_n) and q=(q1,…,qn)q=(q_1,\dots,q_n) on the same nn outcomes (all pi,qi>0p_i, q_i > 0), D(p∥q)≥0D(p\|q) \ge 0, with equality D(p∥q)=0D(p\|q) = 0 if and only if p=qp = q.

Why is it true?

The logarithm is concave, so "on average" it lies below its tangent line. Gibbs' inequality is exactly Jensen's inequality applied to the concave function log⁡2\log_2, weighted by pp: it says that replacing each ratio qi/piq_i/p_i by its pp-weighted average (which equals 11) inside the logarithm can only increase the sum, forcing the divergence to be nonnegative.

Proof

Start from the elementary inequality ln⁡x≤x−1\ln x \le x - 1 for all x>0x > 0, with equality only at x=1x=1 (this follows since f(x)=ln⁡x−(x−1)f(x) = \ln x - (x-1) has f(1)=0f(1)=0, f′(x)=1/x−1f'(x) = 1/x - 1, which is positive for x<1x<1 and negative for x>1x>1, so ff has a unique maximum at x=1x=1). Dividing by ln⁡2\ln 2 gives log⁡2x≤(x−1)/ln⁡2\log_2 x \le (x-1)/\ln 2.

Apply this with x=qi/pix = q_i/p_i for each ii: log⁡2(qi/pi)≤(qi/pi−1)/ln⁡2\log_2(q_i/p_i) \le (q_i/p_i - 1)/\ln 2. Multiply both sides by pi>0p_i > 0 (which does not flip the inequality) and sum over ii: ∑ipilog⁡2(qi/pi)≤1ln⁡2∑i(qi−pi)=1ln⁡2(∑iqi−∑ipi)=1ln⁡2(1−1)=0\sum_i p_i \log_2(q_i/p_i) \le \frac{1}{\ln 2}\sum_i (q_i - p_i) = \frac{1}{\ln 2}\left(\sum_i q_i - \sum_i p_i\right) = \frac{1}{\ln 2}(1 - 1) = 0, since both pp and qq are probability distributions summing to 11.

The left side is exactly −D(p∥q)-D(p\|q) (note the sign flip from qi/piq_i/p_i versus pi/qip_i/q_i inside the log), so this proves D(p∥q)≥0D(p\|q) \ge 0.

Equality throughout requires equality in ln⁡x≤x−1\ln x \le x-1 for every ii with pi>0p_i > 0, which happens only when qi/pi=1q_i/p_i = 1 for every such ii, i.e. p=qp = q.

To store or transmit outcomes of XX efficiently, we assign each outcome ii a binary codeword of length lil_i, chosen so no codeword is a prefix of another (a prefix code), which lets a receiver decode a stream of codewords instantly without ambiguity. Not every list of lengths is achievable: the lengths of a prefix code must satisfy the Kraft inequality, ∑i=1n2−li≤1\sum_{i=1}^n 2^{-l_i} \le 1.

∑i=1n2−li≤1\sum_{i=1}^n 2^{-l_i} \le 1

For a source XX with entropy H(X)H(X), every prefix code has average codeword length LL satisfying L≥H(X)L \ge H(X), and there exists a prefix code with H(X)≤L<H(X)+1H(X) \le L < H(X) + 1.

Why is it true?

The lower bound says entropy is a hard floor: no lossless prefix code can, on average, beat H(X)H(X) bits per symbol, because doing better would require the code-induced distribution to differ from XX's true distribution in a way Gibbs' inequality forbids. The upper bound says this floor is nearly achievable: rounding each ideal length −log⁡2pi-\log_2 p_i up to an integer wastes less than one bit on average.

Proof

For the lower bound, let lil_i be the lengths of any prefix code, so ∑i=1n2−li≤1\sum_{i=1}^n 2^{-l_i} \le 1. Define qi=2−li/Zq_i = 2^{-l_i}/Z where Z=∑j2−lj≤1Z=\sum_j 2^{-l_j} \le 1, so qq is a probability distribution. By Gibbs' inequality, 0≤D(p∥q)=∑ipilog⁡2(pi/qi)=∑ipilog⁡2pi−∑ipilog⁡2(2−li/Z)=−H(X)+∑ipili+log⁡2Z0 \le D(p\|q) = \sum_i p_i \log_2(p_i/q_i) = \sum_i p_i\log_2 p_i - \sum_i p_i \log_2(2^{-l_i}/Z) = -H(X) + \sum_i p_i l_i + \log_2 Z. Since Z≤1Z \le 1, log⁡2Z≤0\log_2 Z \le 0, so 0≤−H(X)+L+log⁡2Z≤−H(X)+L0 \le -H(X) + L + \log_2 Z \le -H(X) + L, which rearranges to L≥H(X)L \ge H(X).

For the upper bound (achievability), choose li=⌈−log⁡2pi⌉l_i = \lceil -\log_2 p_i \rceil for each ii (round the ideal length up to the nearest integer). These lengths satisfy Kraft's inequality since 2−li≤2−(−log⁡2pi)=pi2^{-l_i} \le 2^{-(-\log_2 p_i)} = p_i, so ∑i2−li≤∑ipi=1\sum_i 2^{-l_i} \le \sum_i p_i = 1; by the Kraft–McMillan construction such lengths can always be realized as an actual prefix code (build the code by assigning binary strings level by level in a binary tree).

By definition of ceiling, li<−log⁡2pi+1l_i < -\log_2 p_i + 1. Multiplying by pi≥0p_i \ge 0 and summing over ii gives L=∑ipili<∑ipi(−log⁡2pi+1)=H(X)+1L = \sum_i p_i l_i < \sum_i p_i(-\log_2 p_i + 1) = H(X) + 1.

Combining both parts, this construction achieves H(X)≤L<H(X)+1H(X) \le L < H(X) + 1.

Now suppose messages are sent over a noisy channel: the receiver sees YY when XX was sent, and noise can flip symbols. The mutual information I(X;Y)=H(X)−H(X∣Y)I(X;Y) = H(X) - H(X\mid Y) measures how many bits about XX survive the noise, on average — it is the entropy of XX minus the leftover uncertainty H(X∣Y)H(X\mid Y) after seeing YY. The channel capacity C=max⁡p(x)I(X;Y)C = \max_{p(x)} I(X;Y) is the largest mutual information achievable by choosing the best input distribution p(x)p(x).

I(X;Y)=H(X)−H(X∣Y)I(X;Y) = H(X) - H(X\mid Y)
C=max⁡p(x)I(X;Y)C = \max_{p(x)} I(X;Y)

The simplest noisy channel is the binary symmetric channel (BSC): each transmitted bit is flipped independently with crossover probability pp. Its capacity works out to C=1−H(p)C = 1 - H(p), using the same binary entropy function H(p)H(p) from before — a clean bridge between the source-coding and channel-coding halves of the theory.

For a discrete memoryless channel with capacity CC, reliable communication (error probability Pe→0P_e \to 0) is possible at any transmission rate RR bits per channel use with R<CR < C, using sufficiently long block codes; conversely, no coding scheme can achieve Pe→0P_e \to 0 at any rate R>CR > C.

Why is it true?

Capacity is not merely a convenient formula — it is a genuine wall. Below it, spreading each message over a long enough block lets random-looking codewords stay distinguishable despite noise, so errors can be driven to zero; above it, the channel destroys information faster than any code can protect it, no matter how clever the code.

Proof

**Converse (R>C⇒Pe↛0R > C \Rightarrow P_e \not\to 0).** Fano's inequality bounds the receiver's remaining uncertainty about the message given the noisy output: H(X∣Y)≤1+Pelog⁡2(n−1)H(X\mid Y) \le 1 + P_e \log_2(n-1), where nn is the number of possible messages. Meanwhile, the data-processing structure of the channel forces I(X;Y)≤CI(X;Y) \le C for every use, so over a block of NN channel uses the total information the receiver can extract is at most NCNC bits. If the true rate is R>CR > C bits per use, the message carries NRNR bits of entropy but the channel supplies only about NC<NRNC < NR bits about it; Fano's inequality then forces PeP_e to stay bounded away from 00 as NN grows, since the leftover uncertainty H(X∣Y)H(X\mid Y) cannot be made to vanish.

**Achievability sketch (R<C⇒Pe→0R < C \Rightarrow P_e \to 0).** Fix an input distribution p(x)p(x) achieving the capacity and generate a codebook of 2NR2^{NR} codewords of length NN independently at random from p(x)Np(x)^N. To decode, the receiver looks for the unique codeword that is jointly typical with the received sequence (i.e. behaves, in its statistical patterns, like a genuine input–output pair). Because R<CR < C, the number of candidate codewords (2NR2^{NR}) is far smaller than the number of statistically distinguishable channel outputs (≈2NC\approx 2^{NC}), so as N→∞N \to \infty the chance that some other, unsent codeword accidentally looks jointly typical with the received sequence shrinks to 00, and so does the decoding error probability, averaged over the random choice of codebook — hence at least one codebook must achieve arbitrarily small PeP_e.

Capacity of a few standard discrete channels
ChannelCapacity CC
Noiseless binary channel11 bit per use
Binary symmetric channel, crossover ppC=1−H(p)C = 1 - H(p)
Binary erasure channel, erasure probability ppC=1−pC = 1-p
Binary symmetric channel, p=0.5p=0.5 (fully noisy)C=0C = 0 (no information gets through)

UndergraduateReal-World Applications and Worked Examples

Every digital technology that compresses or transmits data leans on these two theorems. Source coding underlies ZIP, JPEG, and MP3 compression (get close to the entropy of the data); channel coding underlies Wi-Fi, deep-space communication, and QR codes (add just enough redundancy to survive noise, but no more than CC demands).

Example: Building a code that meets the entropy bound exactly

A sensor reports one of four states with probabilities p=(0.5, 0.25, 0.125, 0.125)p=(0.5,\,0.25,\,0.125,\,0.125). Find H(X)H(X), propose prefix-code lengths lil_i satisfying Kraft's inequality with equality, and check whether the resulting average length LL matches the entropy bound.

Solution

First compute the entropy directly from the formula: each term is −pilog⁡2pi-p_i\log_2 p_i, giving −0.5log⁡20.5=0.5-0.5\log_2 0.5 = 0.5, −0.25log⁡20.25=0.5-0.25\log_2 0.25 = 0.5, and two terms −0.125log⁡20.125=0.375-0.125\log_2 0.125 = 0.375 each. Summing: H(X)=0.5+0.5+0.375+0.375=1.75H(X) = 0.5+0.5+0.375+0.375 = 1.75 bits.

Because every probability here is a power of two (2−1,2−2,2−3,2−32^{-1}, 2^{-2}, 2^{-3}, 2^{-3}), the ideal codeword lengths −log⁡2pi-\log_2 p_i are already integers: l=(1,2,3,3)l = (1, 2, 3, 3). Check Kraft's inequality: 2−1+2−2+2−3+2−3=0.5+0.25+0.125+0.125=12^{-1}+2^{-2}+2^{-3}+2^{-3} = 0.5+0.25+0.125+0.125 = 1, satisfied with equality, so a prefix code with exactly these lengths exists (for instance 0,10,110,1110, 10, 110, 111).

The average codeword length is L=∑ipili=0.5(1)+0.25(2)+0.125(3)+0.125(3)=0.5+0.5+0.375+0.375=1.75L = \sum_i p_i l_i = 0.5(1) + 0.25(2) + 0.125(3) + 0.125(3) = 0.5+0.5+0.375+0.375 = 1.75 bits.

So L=H(X)=1.75L = H(X) = 1.75 exactly, meeting the lower bound of the noiseless source coding theorem with equality — this happens precisely because the distribution is dyadic (every probability a power of two), so no bit is ever wasted rounding lengths to integers.

Example: How much redundancy does a noisy memory chip need?

A flash-memory cell has a raw bit-flip probability p=0.11p=0.11 per stored bit, modeled as a binary symmetric channel. Compute the channel capacity CC, and interpret what it means for how much of the raw storage must be spent on error-correcting redundancy to read data back reliably.

Solution

First compute the binary entropy at p=0.11p=0.11: −0.11log⁡20.11≈0.350-0.11\log_2 0.11 \approx 0.350 and −0.89log⁡20.89≈0.150-0.89\log_2 0.89 \approx 0.150, so H(0.11)≈0.500H(0.11) \approx 0.500 bits.

The capacity is then C=1−H(0.11)≈1−0.500=0.500C = 1 - H(0.11) \approx 1 - 0.500 = 0.500 bits per stored bit.

This means that, in principle, only about half of the raw storage capacity can carry reliable information once error-correcting redundancy is added — a rate RR close to C≈0.5C \approx 0.5 bits per raw cell is the best any error-correcting code can achieve while still driving the error probability toward zero on this channel; codes attempting a higher rate cannot be made reliable, no matter how sophisticated their design.

In practice, real flash-memory controllers use error-correcting codes such as BCH or LDPC codes at rates chosen safely below this 0.50.5-bit theoretical ceiling, trading some of that margin for practical decoding complexity and finite block length.

A source has three equally likely outcomes, p=(1/3, 1/3, 1/3)p=(1/3,\,1/3,\,1/3). What is H(X)H(X)?

According to Gibbs' inequality, what is the smallest possible value of D(p∥q)D(p\|q) for two probability distributions p,qp, q?

For a source with entropy H(X)H(X), which statement about the average length LL of an optimal prefix code is guaranteed by Shannon's noiseless source coding theorem?

A binary symmetric channel has crossover probability p=0.5p=0.5 (each bit is flipped with probability exactly one half). What is its capacity CC?

References

  1. Claude E. Shannon (1948). A Mathematical Theory of Communication
  2. Thomas M. Cover, Joy A. Thomas (2006). Elements of Information Theory
  3. David J. C. MacKay (2003). Information Theory, Inference, and Learning Algorithms
  4. Erdal Arıkan (2009). Channel Polarization: A Method for Constructing Capacity-Achieving Codes for Symmetric Binary-Input Memoryless Channels · arXiv:0807.3917