ZWZixi Wang
Writing

Writing / Information Theory

A Complete Guide to Shannon Information Theory

From surprisal, entropy, and mutual information to compression limits, channel capacity, coding theorems, and modern machine learning.

Shannon information theory does not ask what a message means. It asks:

In the presence of uncertainty, how can information be measured, and how quickly can it be compressed or transmitted reliably?

Claude Shannon's 1948 paper, A Mathematical Theory of Communication, gave communication, compression, noise, and coding a common mathematical framework.

1. The decisive abstraction

A communication system can be represented as:

sourceencoderchanneldecoderreceiver\text{source}\rightarrow\text{encoder}\rightarrow\text{channel} \rightarrow\text{decoder}\rightarrow\text{receiver}

For a text message, the source is the text, the encoder turns it into bytes, packets, and physical signals, the channel is the network, noise includes loss and bit flips, and the decoder reconstructs the message.

Shannon deliberately sets meaning aside. The mathematical questions are:

  1. How uncertain is the receiver before the message arrives?
  2. How much uncertainty disappears after it arrives?
  3. What is the minimum number of bits needed to represent it?
  4. In a noisy channel, how quickly can it be transmitted reliably?

The central idea is that information is not the symbol itself; it is the uncertainty removed by observing the symbol.

2. Surprisal: unlikely events carry more information

For an event xx with probability p(x)p(x), its self-information, or surprisal, is:

I(x)=log2p(x)I(x)=-\log_2p(x)

The unit is the bit. An event with probability 1 carries zero new information. One outcome of a fair coin carries:

log212=1 bit-\log_2\frac{1}{2}=1\text{ bit}

An event with probability 10610^{-6} carries about 19.9319.93 bits.

The logarithm is important because independent probabilities multiply while information should add. If xx and yy are independent:

p(x,y)=p(x)p(y)p(x,y)=p(x)p(y)

and therefore:

I(x,y)=logp(x,y)=I(x)+I(y)I(x,y)=-\log p(x,y)=I(x)+I(y)

3. Entropy: average information produced by a source

Taking the expectation of surprisal gives entropy:

H(X)=xp(x)log2p(x)H(X)=-\sum_xp(x)\log_2p(x)

Entropy can be read as average uncertainty, average surprise, average information, or the lower bound on the average number of bits needed by an ideal code.

A fair coin has entropy 1 bit. For a heavily biased coin with p(0)=0.99p(0)=0.99 and p(1)=0.01p(1)=0.01:

H(X)=0.99log20.990.01log20.010.081H(X)=-0.99\log_2 0.99-0.01\log_2 0.01\approx0.081

The output still takes one of two values, but it is highly predictable. Over long sequences it can be represented using far less than one bit per toss.

For nn possible outcomes, entropy is maximized by the uniform distribution:

H(X)=log2nH(X)=\log_2n

Entropy is therefore not a strict synonym for disorder. It is uncertainty under a probability distribution.

4. The source coding theorem

How far can a random source be compressed? Its average code length cannot remain below H(X)H(X), but for sufficiently long sequences it can be made arbitrarily close to H(X)H(X).

Suppose p(A)=0.9p(A)=0.9 and p(B)=0.1p(B)=0.1. A direct one-bit code assigns one bit to every character, but the source entropy is:

H(X)=0.9log20.90.1log20.10.469H(X)=-0.9\log_2 0.9-0.1\log_2 0.1\approx0.469

The theoretical average is about 0.4690.469 bits per symbol because only a comparatively small set of typical sequences carries most of the probability mass.

Compression does not mean discarding information. Lossless compression exploits non-uniformity and redundancy. Huffman coding, arithmetic coding, Lempel-Ziv, ZIP, and learned compressors all give shorter representations to more probable events.

This connects directly to language models. If a model assigns probability p(x)p(x) to the true token, its ideal code length is log2p(x)-\log_2p(x). Cross-entropy can therefore be interpreted as the average number of bits needed to encode data using the model.

5. Joint and conditional entropy

The joint entropy of XX and YY is:

H(X,Y)=x,yp(x,y)logp(x,y)H(X,Y)=-\sum_{x,y}p(x,y)\log p(x,y)

It obeys the chain rule:

H(X,Y)=H(X)+H(YX)H(X,Y)=H(X)+H(Y|X)

First describe XX, then describe the uncertainty still left in YY once XX is known. If Y=XY=X, then H(YX)=0H(Y|X)=0. If XX and YY are independent, then H(YX)=H(Y)H(Y|X)=H(Y).

6. Mutual information

Mutual information measures how much observing one variable reduces uncertainty about another:

I(X;Y)=H(X)H(XY)I(X;Y)=H(X)-H(X|Y)

Equivalently:

I(X;Y)=H(X)+H(Y)H(X,Y)I(X;Y)=H(X)+H(Y)-H(X,Y)

If XX and YY are independent, I(X;Y)=0I(X;Y)=0. If Y=XY=X, then I(X;Y)=H(X)I(X;Y)=H(X). A noisy copy lies between those cases.

Unlike correlation, mutual information is not limited to linear dependence. For example, if Y=X2Y=X^2 and XX is symmetric around zero, linear correlation may vanish even though YY is fully determined by XX.

7. KL divergence and cross-entropy

The Kullback-Leibler divergence is:

DKL(PQ)=xP(x)logP(x)Q(x)D_{\mathrm{KL}}(P\|Q)=\sum_xP(x)\log\frac{P(x)}{Q(x)}

It is the extra coding cost incurred when data really follow PP but are encoded using QQ. It is non-negative and equals zero only when the distributions agree, but it is not a true distance: it is asymmetric and does not generally satisfy the triangle inequality.

Cross-entropy is:

H(P,Q)=xP(x)logQ(x)H(P,Q)=-\sum_xP(x)\log Q(x)

and:

H(P,Q)=H(P)+DKL(PQ)H(P,Q)=H(P)+D_{\mathrm{KL}}(P\|Q)

When the data distribution PP is fixed, minimizing cross-entropy is equivalent to minimizing DKL(PQ)D_{\mathrm{KL}}(P\|Q). This is why cross-entropy appears throughout classification and language modeling.

8. Noise and the channel model

Now consider:

XchannelYX\rightarrow\text{channel}\rightarrow Y

Because of noise, the received YY may differ from the transmitted XX. In a binary symmetric channel, each bit flips with probability ϵ\epsilon:

P(YX)=ϵP(Y\neq X)=\epsilon

Can communication still approach zero error? Shannon's answer is yes, provided the transmission rate stays below the channel capacity.

9. Channel capacity

Channel capacity is:

C=maxp(x)I(X;Y)C=\max_{p(x)}I(X;Y)

It is the largest amount of information that can be transmitted reliably per channel use. The maximization over input distributions matters because different input distributions may use the channel with different efficiency.

For a binary symmetric channel:

C=1H2(ϵ)C=1-H_2(\epsilon)

where:

H2(ϵ)=ϵlog2ϵ(1ϵ)log2(1ϵ)H_2(\epsilon)=-\epsilon\log_2\epsilon-(1-\epsilon)\log_2(1-\epsilon)

At ϵ=0\epsilon=0, capacity is 1 bit per use. At ϵ=0.5\epsilon=0.5, output is independent of input and capacity is zero. At ϵ=1\epsilon=1, every bit flips deterministically and capacity returns to 1: the receiver can simply invert every bit.

10. The noisy-channel coding theorem

Let CC be channel capacity and RR the communication rate.

  • If R<CR<C, codes exist whose error probability approaches zero as block length grows.
  • If R>CR>C, no code can make communication arbitrarily reliable.

Noise does not make reliable communication impossible. It creates a rate limit.

Reliability comes from structured redundancy. Repetition codes show the basic idea, while Hamming, Reed-Solomon, Turbo, LDPC, and Polar codes achieve far better efficiency. Shannon did not construct the best code; he proved that good codes must exist and that a hard limit separates the possible from the impossible.

11. Source coding and channel coding point in opposite directions

Source coding removes predictable redundancy:

source datashorter representation\text{source data}\rightarrow\text{shorter representation}

Channel coding adds structured redundancy for recovery:

compressed dataerror-correctable representation\text{compressed data}\rightarrow\text{error-correctable representation}

A complete system often looks like:

datacompressioncompact formerror correctionrobust form\text{data}\xrightarrow{\text{compression}}\text{compact form} \xrightarrow{\text{error correction}}\text{robust form}

The two operations are not contradictory: one removes redundancy that carries no new source information, while the other adds redundancy designed to reveal transmission errors.

12. Source-channel separation

If a source has entropy rate HH and a channel has capacity CC, reliable communication is possible in principle when:

H<CH<C

Optimal compression and optimal channel coding may be designed separately. This separation is one foundation of modular digital communication.

Finite block lengths, strict latency limits, and semantic communication can make joint source-channel methods preferable in practice, but they do not erase the asymptotic theorem.

13. Typical sets

Take nn independent samples X1,,XnX_1,\dots,X_n from p(x)p(x). Most probability mass concentrates in a typical set containing roughly:

2nH(X)2^{nH(X)}

sequences, each with probability about:

2nH(X)2^{-nH(X)}

The full space may contain Xn|\mathcal{X}|^n sequences, but only about 2nH(X)2^{nH(X)} are needed to cover almost all probability. Distinguishing them takes roughly nH(X)nH(X) bits. This is the intuition behind entropy as the compression limit.

14. Information theory is not a theory of meaning

Two sentences can have similar Shannon information because they have similar probability and symbol structure, even if one is trivial and the other is existentially important.

Shannon information does not measure truth, meaning, value, importance, or human understanding. It measures the statistical uncertainty removed by a message under a chosen probability model.

That limitation is also the source of the theory's generality: once semantics are excluded, the same mathematics can describe text, radio signals, storage, genomes, and model predictions.

15. Connections to machine learning

Cross-entropy and maximum likelihood

The common supervised loss:

L=logpθ(yx)\mathcal{L}=-\log p_\theta(y|x)

is the code length assigned to the true label by the model. Maximizing log likelihood is equivalently minimizing the encoded length of the observations under the model.

Language models and perplexity

For average token cross-entropy:

H=1Ni=1Nlog2p(xix<i)H=-\frac{1}{N}\sum_{i=1}^{N}\log_2p(x_i|x_{<i})

perplexity is:

PPL=2H\mathrm{PPL}=2^H

If H=3H=3, then PPL=8\mathrm{PPL}=8, roughly the uncertainty of choosing among eight equally likely candidates at each position.

Representation learning

The information bottleneck suggests keeping information about the target while discarding irrelevant detail:

maxI(Z;Y)βI(Z;X)\max I(Z;Y)-\beta I(Z;X)

The expression is conceptually useful, but mutual information in deterministic networks and continuous spaces can be difficult or ill-defined to estimate. It should not be applied too literally.

Contrastive and generative learning

InfoNCE is often interpreted through a lower bound on mutual information, although contrastive learning cannot be reduced to “maximize mutual information” alone.

The variational autoencoder objective contains:

Eq(zx)[logp(xz)]DKL(q(zx)p(z))\mathbb{E}_{q(z|x)}[\log p(x|z)] -D_{\mathrm{KL}}(q(z|x)\|p(z))

The KL term regulates how far the latent distribution moves from its prior. Similar ideas of likelihood, entropy, and coding run through diffusion models, flow models, and language models.

16. Information depends on the observer's model

The quantity logp(x)-\log p(x) depends on the probability model used to assign p(x)p(x). A weather event may surprise someone who knows nothing about the climate and be entirely expected to someone with a precise forecast.

Information is not solely an intrinsic property of the message. It is defined relative to a model.

This motivates anomaly scores such as logpθ(x)-\log p_\theta(x), but also explains likelihood paradoxes: deep generative models can assign high likelihood to out-of-distribution inputs because likelihood reflects low-level statistics, not necessarily semantic normality.

17. What made Shannon's theory revolutionary

Three consequences matter beyond the entropy formula:

  1. Information can be separated from its physical carrier. Text, sound, images, and video can all become bits.
  2. Noise does not prevent reliable communication. Coding can drive logical error rates arbitrarily low below capacity.
  3. Engineering has provable limits. Compression and communication have boundaries that no clever algorithm can cross.

Communication became a mathematical discipline with explicit limits rather than only an empirical craft.

18. The core quantities at a glance

QuantityFormulaInterpretation
SurprisalI(x)=logp(x)I(x)=-\log p(x)How unexpected one event is
EntropyH(X)=E[logp(X)]H(X)=\mathbb{E}[-\log p(X)]Average uncertainty in a random variable
Conditional entropyH(XY)H(X\mid Y)Uncertainty left in XX after observing YY
Mutual informationI(X;Y)=H(X)H(XY)I(X;Y)=H(X)-H(X\mid Y)Uncertainty about XX removed by YY
KL divergenceDKL(PQ)D_{\mathrm{KL}}(P\|Q)Extra coding cost from using the wrong model
Channel capacityC=maxp(x)I(X;Y)C=\max_{p(x)}I(X;Y)Maximum reliable information per channel use

19. Two conclusions to keep

The compression limit is:

The limiting average length of lossless codes is entropy.\boxed{\text{The limiting average length of lossless codes is entropy.}}

The communication limit is:

Below channel capacity, arbitrarily reliable communication is possible.\boxed{\text{Below channel capacity, arbitrarily reliable communication is possible.}}

Entropy describes how quickly a source produces information. Capacity describes how quickly a channel can carry it. When production stays below transport capacity, reliable communication is possible.