Information, Entropy, Relative Entropy, and Mutual Information

Fundamental definitions, Relationships between them, and Chain rules

· 34 min read · 6,765 words

History

In the early 20th century, laying copper wires and building radio infrastructure was very expensive, and physical bandwidth (range of frequencies a wire could reliably carry) was a strictly finite resource. Telecom companies needed to maximize their return on investment, so engineers had to figure out how much “payload” (actually useful bits) these lines were truly carrying, instead of just measuring electrical pulses.

People were designing different physical systems to transmit information (like a simple on-off based Morse code system, or a decoder for 10 different voltage levels), and they needed way to compare these systems to find out which one had the maximum throughput while minimizing physical costs.

Engineering pressureWhy it mattered
ThroughputThis means customer messages per unit time, and mattered because telecoms charged per word.
Pulse rateLong physical wires blurred different pulses together, so sending 50 pulses per second was easier on the wire than 100.
Symbol alphabet sizeThe wires were very noisy, so it was easier to tell the difference between on and off than between many voltage levels.

The physical wire worked exactly the same whether it was transmitting speech or a string of random numbers, so we couldn’t use human semantics (like abbreviations) to design a system. The natural next idea was to base it on the likelihood of the symbols used.

Hartley (1928) said: if a source emits nn symbols from an alphabet of size DD, there are DnD^n possible messages. But since cost and time scale linearly with message length, we would like our information measure to do so too, so the amount of information transferred is better represented as nlogDn \log D (taking the log of DnD^n).

IntuitionPhysical interpretation

Here Hartley’s idea was that any system that needs to handle sending 2 digit numbers (in base 10, so n=2 and D=10) repeatedly, would have 100 possible messages so would need some physical system capable of distinguishing 100 different states, which in binary hardware requires exactly log2(100)=7\lceil \log_2(100) \rceil = 7 on/off pulses for every 2 digit number that the system would transmit.

This measure assumed every symbol is equally likely, but real sources like English are not, there are many more e’s than z’s (or in the Hartley example, 00 is probably more likely than 73), and you could design schemes to take advantage of these asymmetries in the DnD^n possibilities using the statistical structure.

Shannon (1948) exploited this by making information a function of the probability of symbols instead of alphabet size.


Information (surprise)

Setup: Let XX be a discrete random variable mapping a physical action (like pressing a telegraph key) to a specific symbol xx in an alphabet (26 letters of English + space + punctuation). Its likelihood is given by the probability mass function p(x)p(x).

Motivation: Because physical wires ignore human semantics, every symbol’s “information” depends on how predictable it is, expected symbols (like E at p(E)=12%p(E) = 12\%) carry less information than rare symbols (like Z at p(Z)=0.01%p(Z) = 0.01\%). To quantify this, we define hh as the measure of the “surprise”, or information, contained in a single event such as seeing the letter E.

ComponentDetail
Signatureh:(0,1][0,)h : (0, 1] \rightarrow [0, \infty)
Domainhh does not take the symbol as its input (to avoid tying the math to the application, and to allow relabelling to a different scenario), it takes only the probability of that symbol, p(x)p(x).
CodomainThe set of positive reals, intuitively in some inversely proportional way.

The three conditions we need on hh

  1. Additivity on independent events: h(pq)=h(p)+h(q)h(pq) = h(p) + h(q) for all p,q(0,1]p, q \in (0,1]. Learning two unrelated facts should yield the sum of their information. Without this there is no well-defined information rate per symbol, and a message of doubled length would not have doubled cost.
  2. Monotonically decreasing in pp: for pqh(p)h(q)p \le q \Rightarrow h(p) \ge h(q). Being told a symbol you already knew in your shared prior must cost nothing, so h(1)=0h(1) = 0 (this also comes out of additivity, since h(11)=h(1)+h(1)h(1)=0h(1 \cdot 1) = h(1) + h(1) \Rightarrow h(1) = 0), and rarer outcomes are more informative.
  3. Normalization: h(1/2)=1h(1/2) = 1. This is just to decide on a unit for information: observing an event with 50% probability (a fair coin flip) provides exactly 1 bit.

TheoremUniqueness of the surprise function

If a function hh satisfies Additivity and Monotonicity, then h(p)=Clnph(p) = -C \ln p for some constant C0C \ge 0. Adding Normalization fixes the base of the logarithm, resulting in exactly

h(p)=log2ph(p) = -\log_2 p

ProofAdditivity gives the rationals, monotonicity gives the reals

Converting multiplication into addition using Cauchy’s equation

The additivity rule h(pq)=h(p)+h(q)h(pq) = h(p) + h(q) operates on probabilities (fractions multiplying together). We can simplify this by mapping the domain (0,1](0,1] to the non-negative real numbers [0,)[0, \infty) using the substitution p=eup = e^{-u}. Then we define a new function g(u)=h(eu)g(u) = h(e^{-u}), which means h(p)=g(lnp)h(p) = g(-\ln p). Because e(u+v)=eueve^{-(u+v)} = e^{-u}e^{-v}, our multiplicative rule for hh turns into a purely additive rule for gg, known as Cauchy’s functional equation:

g(u+v)=g(u)+g(v)for all u,v0g(u+v) = g(u) + g(v) \quad \text{for all } u,v \ge 0

Also, because h(p)h(p) goes down as pp gets larger (monotonicity), our new function g(u)g(u) must go up as uu gets larger. Let C=g(1)C = g(1). Since gg is increasing and g(0)=0g(0) = 0, CC must be 0\ge 0.

Solving for integers

Using the new rule, g(2)=g(1)+g(1)=2Cg(2) = g(1) + g(1) = 2C. By simple induction, for any integer nn:

g(n)=nCg(n) = nC

Solving for rationals

Let q=m/nq = m/n be a rational number. Using the integer rule from above and the observation that g(x+x++xn times)=g(x)+g(x)++g(x)n times=ng(x)g(\underbrace{x + x + \dots + x}_{n \text{ times}}) = \underbrace{g(x) + g(x) + \dots + g(x)}_{n \text{ times}} = n \cdot g(x):

ng(mn)=g(nmn)=g(m)=mCn \cdot g\left(\frac{m}{n}\right) = g\left(n \cdot \frac{m}{n}\right) = g(m) = mC

Divide both sides by nn:

g(q)=qCg(q) = qC

Using just additivity, we have a closed form of the function for every rational number.

Solving for reals using monotonicity

For irrational numbers like π\pi, without the monotonicity rule g(u)g(u) could have been a wildly scattered, discontinuous fractal on the irrationals that still satisfies additivity (this can be proven using a Hamel basis, a concept from linear algebra).

Because we declared gg must be monotonic (smoothly increasing without jumping around), we can take any irrational number uu and “squeeze” it between two rational numbers aa and bb with auba \le u \le b. Because g(a)=aCg(a) = aC and g(b)=bCg(b) = bC, as we squeeze aa and bb infinitely close to uu, they both converge to CuCu. Therefore:

g(u)=Cufor all u0g(u) = Cu \quad \text{for all } u \ge 0

Back-substituting and normalizing

Undo the initial substitution to get back to h(p)h(p):

h(p)=g(lnp)=Clnph(p) = g(-\ln p) = -C \ln p

Finally, apply the normalization rule h(1/2)=1h(1/2) = 1:

1=Cln(1/2)=Cln2C=1ln21 = -C \ln(1/2) = C \ln 2 \quad \Longrightarrow \quad C = \frac{1}{\ln 2}

Substituting CC back in:

h(p)=lnpln2=log2ph(p) = -\frac{\ln p}{\ln 2} = -\log_2 p

So log2p-\log_2 p is not just a convenient measure of information, given our three basic logical constraints, it is the only possible measure.

IntuitionPhysical interpretation

In the Hartley scheme, transmitting 3-letter English words required a fixed 1515 bits for every single word (log2(263)\lceil \log_2(26^3) \rceil) because all sequences were treated uniformly. Under Shannon’s definition we now have a way to replace this uniform representation with a variable-length prefix code based on distributions:

A word like “THE” appears frequently (p("THE")0.07p(\text{"THE"}) \approx 0.07). Its surprise is h("THE")=log2(1/0.07)3.8h(\text{"THE"}) = \log_2(1 / 0.07) \approx 3.8 bits. The physical line should send this using only 4\approx 4 pulses.

A valid sequence like “SPY” might appear with p("SPY")0.00005p(\text{"SPY"}) \approx 0.00005. Its surprise is h("SPY")=log2(1/0.00005)14.3h(\text{"SPY"}) = \log_2(1 / 0.00005) \approx 14.3 bits, justifying 1415\approx 14\text{--}15 pulses.

Where this goes next: We now have a measure for the information contained in a single keystroke. But while building a radio tower we care about the overall long-term behaviour of a message source, not a single keystroke, so we need a single number to represent the average information rate of the entire system. That is the average information over all keystrokes, using the expectation E\mathbb{E} from statistics:

E[h(X)]=p(x)h(x)\mathbb{E}[h(X)] = \sum p(x)\, h(x)

Substituting the h(x)h(x) term we derived gives the expected surprise from the message source, fundamental enough to be given its own name: Entropy.


Entropy

Setup: Let XX be a discrete random variable mapping the event of a human pressing a telegraph machine key to one symbol xx in our alphabet X\mathcal{X}, with pmf p(x)p(x). This XX is the same one as for self-information, since the message source is just a machine that rolls the random variable XX over and over again (X1,X2,X3,,XnX_1, X_2, X_3, \dots, X_n).

Motivation: We want the expected surprise from any generic keystroke, so entropy ranges over the entire alphabet. This means that unlike self-information h(p)h(p), which takes a single probability (a number between 0 and 1) and outputs the surprise of one specific symbol, entropy HH is a functional: it takes the entire probability distribution of the random variable and maps it to a single number representing the average surprise.

ComponentDetail
SignatureH:P(X)[0,)H : \mathcal{P}(\mathcal{X}) \rightarrow [0, \infty)
DomainThe entire probability mass function p(x)p(x) of the source, giving the full blueprint like E is 12%, Z is 0.01%, T is 9%, etc.
CodomainA single non-negative real number, the average bits of surprise per keystroke.

FormulaEntropy

H(X)=E[h(p(X))]=xXp(x)log2p(x)H(X) = \mathbb{E}[h(p(X))] = - \sum_{x \in \mathcal{X}} p(x) \log_2 p(x)

ConventionSymbols of zero probability

If we run into p(x)=0p(x) = 0 we use the limit convention from calculus. If a symbol is physically impossible for the machine to print, it will never be observed on the wire, and therefore contributes exactly 0 bits of surprise to the long-term average.

Properties

  1. Non-negativity : H(X)0H(X) \ge 0. You can never have negative entropy. Because every probability p(x)p(x) is a fraction between 0 and 1, the fraction 1p(x)\frac{1}{p(x)} is always 1 or greater. Equality holds only if the outcome is deterministic, the telegraph machine is broken and only prints E (p(E)=1.0p(E) = 1.0, and all other letters p=0p = 0).
  2. Maximum entropy bound : H(X)log2XH(X) \le \log_2 \lvert \mathcal{X} \rvert. Entropy hits its absolute mathematical ceiling when every single symbol in the alphabet X\mathcal{X} is equally likely (a uniform distribution). If you have a 32-key telegraph machine and the operator presses keys completely at random, you have maximum uncertainty of Hmax=log2(32)=5 bitsH_{\text{max}} = \log_2(32) = 5 \text{ bits}. Any structure in a language would make it lower.

From one source to a channel: the pair (X,Y)(X, Y)

We now have a mathematical model for a single message source XX. But a real communication system is not just one isolated source. It is connected to a copper wire with electrical interference, which terminates at a receiver. Because of this noise, the receiver experiences a new physical event (e.g. hearing a distorted click on the sounder).

flowchart LR
  O[Operator presses key] --> X["X: sent symbol, x in alphabet"]
  X -->|noisy copper wire| Y["Y: received symbol, y in alphabet"]
  Y --> T[Receiver's tape]

We define a new discrete random variable YY, which maps this received physical event to a symbol yy in the receiver’s alphabet Y\mathcal{Y} (usually, but not always, the same 26-letter alphabet as the sender’s).

To evaluate the communication system as a whole, factoring in the source behaviour, the wire noise, and the receiver observation all at once, we can treat the sender’s input and the receiver’s output as a single combined physical event. This makes a single vector-valued random variable (X,Y)(X, Y), mapping the dual physical event (the sender pressing a key and the receiver observing a click) to a pair of symbols (x,y)(x, y) in the combined alphabet X×Y\mathcal{X} \times \mathcal{Y}.

The statistical blueprint of this entire system is the joint probability mass function p(x,y)p(x,y): the likelihood that the sender transmits xx AND the receiver simultaneously gets yy, capturing both the sender’s vocabulary habits and the wire’s physical noise profile.


Joint entropy H(X,Y)H(X, Y)

Motivation: Joint entropy measures the expected surprise of the entire end-to-end system.

ComponentDetail
SignatureH:P(X×Y)[0,)H : \mathcal{P}(\mathcal{X} \times \mathcal{Y}) \rightarrow [0, \infty)
DomainThe entire joint probability distribution, the complete grid of probabilities for every possible sent-and-received pair.
CodomainA single non-negative real number, the average bits of surprise generated by any complete transmission cycle.

Since we are treating the pair as a single, slightly more complex telegraph machine, we reuse the exact same expected-value framework we used for H(X)H(X). The surprise of a specific pair (x,y)(x,y) occurring is simply h(p(x,y))=log2p(x,y)h(p(x,y)) = -\log_2 p(x,y). To find the average over both the sent and received alphabets, in code we would have to use a double summation to iterate through the grid:

FormulaJoint entropy

H(X,Y)=E[h(p(X,Y))]=xXyYp(x,y)log2p(x,y)H(X, Y) = \mathbb{E}[h(p(X,Y))] = - \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 p(x, y)

ExampleThe double summation code
joint_entropy.py
def joint_entropy(p): # p[x][y] = P(X=x, Y=y)
total = 0.0
for i in range(len(p)):
for j in range(len(p[i])):
prob = p[i][j]
if prob > 0: # the 0 log 0 = 0 convention
total -= prob * log2(prob)
return total

Where this goes next: Now suppose we want to isolate the effect of the noisy wire itself. One way is to freeze the sender’s variable, so that we know which symbol XX was transmitted, the uncertainty left over in the system is then about which symbol YY pops out at the receiver’s end, based on wire noise only. In a perfect, noiseless wire, knowing XX means you perfectly know YY, so the leftover uncertainty is exactly 0 bits, but in a real wire there may be flips due to static.


Conditional entropy H(YX)H(Y \mid X)

Like the other entropies, this is a functional too, but it operates on a different set of probabilities.

ComponentDetail
SignatureH(YX):P(X×Y)[0,)H(Y \mid X) : \mathcal{P}(\mathcal{X} \times \mathcal{Y}) \rightarrow [0, \infty)
DomainTwo things are needed, the marginal probability of the sender p(x)p(x) (how often they send E), and the conditional probability matrix of the wire p(yx)p(y \mid x) which is the physical noise profile: if E is sent, what is the probability of receiving E, T, Z, etc.
CodomainA single non-negative real number, the average bits of surprise introduced strictly by the noise in the wire during any one transmission.

For any specific symbol such as the letter E, the entropy of the receiver’s outcomes, using the standard entropy formula, is:

H(YX=x)=yYp(yx)log2p(yx)H(Y \mid X = x) = - \sum_{y \in \mathcal{Y}} p(y \mid x) \log_2 p(y \mid x)

But since we cannot just look at E, we need to average this leftover uncertainty across every symbol the sender could possibly transmit, weighting each one by how often the sender actually transmits it, p(x)p(x):

FormulaConditional entropy

H(YX)=xXp(x)H(YX=x)=xXyYp(x,y)log2p(yx)H(Y \mid X) = \sum_{x \in \mathcal{X}} p(x)\, H(Y \mid X = x) = - \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 p(y \mid x)

DerivationExpanding to the direct form

Substituting the inner entropy:

H(YX)=xXp(x)[yYp(yx)log2p(yx)]H(Y \mid X) = \sum_{x \in \mathcal{X}} p(x) \left[ - \sum_{y \in \mathcal{Y}} p(y \mid x) \log_2 p(y \mid x) \right]

Since p(x)p(x) does not depend on yy, we can pull it inside the inner summation:

H(YX)=xXyYp(x)p(yx)log2p(yx)H(Y \mid X) = - \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x)\, p(y \mid x) \log_2 p(y \mid x)

Applying the rule of conditional probability, p(x)p(yx)=p(x,y)p(x) p(y \mid x) = p(x,y):

H(YX)=xXyYp(x,y)log2p(yx)H(Y \mid X) = - \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 p(y \mid x)

IntuitionWhy the two probabilities inside the formula’s RHS differ

We use the joint probability p(x,y)p(x,y) on the outside as the statistical weight, because we are averaging over the real-world occurrence of both events happening together. But inside the logarithm we strictly use the conditional probability p(yx)p(y \mid x), because the actual “surprise” we are measuring is strictly the surprise of YY happening given that XX has already happened.


Chain rule of entropy (two variables)

Since joint entropy is for the sender and the wire system as a whole, and the individual components both have entropies we have discussed, there is a natural relation:

TheoremChain rule of entropy

H(X,Y)=H(X)+H(YX)=H(Y)+H(XY)H(X, Y) = H(X) + H(Y \mid X) = H(Y) + H(X \mid Y)

Symmetry of the chain rule

Because joint entropy H(X,Y)H(X,Y) looks at the whole system from the outside, it doesn’t matter which side you start on:

H(X,Y)=H(Y)+H(XY)H(X, Y) = H(Y) + H(X \mid Y)

Consider the case where the sender XX uses a very complex, erratic language (high entropy, say 10 bits per symbol), but the receiver YY is a broken machine that only prints two symbols, A or B (low entropy, max 1 bit per symbol). We would need:

10 bits+H(YX)=1 bit+H(XY)10 \text{ bits} + H(Y \mid X) = 1 \text{ bit} + H(X \mid Y)

This makes sense from either direction:

DirectionReading
Forward : H(X)+H(YX)H(X) + H(Y \mid X)The sender has 1,024 different keys on their complex keyboard (H(X)=10H(X) = 10 bits). When they press a key, the signal travels down the wire to the broken 2-symbol receiver. Because the receiver only has two outputs, the wire acts like a massive funnel. If you know exactly which of the 1,024 keys the sender pressed, guessing which of the 2 receiver keys pops out is relatively easy, so the uncertainty added strictly by the wire’s mechanism, H(YX)H(Y \mid X), is very small.
Backward : H(Y)+H(XY)H(Y) + H(X \mid Y)If the broken receiver machine prints an A (H(Y)=1H(Y) = 1 bit), you now have to guess what the sender originally typed. Because the wire funnelled 1,024 different inputs down into just 2 outputs, a massive amount of information was destroyed. Knowing that A printed tells you almost nothing about which of the 1,024 keys was pressed, so your leftover surprise H(XY)H(X \mid Y) is very high.
ProofBy direct evaluation

Starting from the definition of joint entropy:

H(X,Y)=xXyYp(x,y)log2p(x,y)H(X, Y) = - \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 p(x, y)

Applying the conditional probability rule p(x,y)=p(x)p(yx)p(x,y) = p(x)p(y \mid x) to the probability inside the logarithm:

=xXyYp(x,y)log2[p(x)p(yx)]= - \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 \big[ p(x)\, p(y \mid x) \big]

=xXyYp(x,y)log2p(x)    xXyYp(x,y)log2p(yx)= - \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 p(x) \;-\; \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 p(y \mid x)

In the first term, log2p(x)\log_2 p(x) does not contain a yy. Therefore, if we sum the joint probability p(x,y)p(x,y) over all possible values of yy, the yy simply collapses out by marginalization, leaving only the marginal probability p(x)p(x):

=xXp(x)log2p(x)    xXyYp(x,y)log2p(yx)= - \sum_{x \in \mathcal{X}} p(x) \log_2 p(x) \;-\; \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 p(y \mid x)

Replacing with the terms we know:

=H(X)+H(YX)= H(X) + H(Y \mid X)

Alternative proof. Take the expectation of both sides of the basic logarithmic probability rule logp(X,Y)=logp(X)+logp(YX)\log p(X,Y) = \log p(X) + \log p(Y \mid X).

WarningConditional entropy is directional

As discussed above, H(YX)H(XY)H(Y \mid X) \neq H(X \mid Y). The amount of noise the wire adds to the sent signal is not the same number as the receiver’s confusion about the sent signal. They are measuring uncertainty from two different directions.

With a third variable

The chain rule holds even if we add a third variable ZZ to the system (which could be a secondary sensor monitoring the wire):

H(X,YZ)=H(XZ)+H(YX,Z)H(X, Y \mid Z) = H(X \mid Z) + H(Y \mid X, Z)


Relative entropy (Kullback–Leibler distance)

Motivation: Now that we have a model capable of representing the telegraph system, we design a compression code for it to minimize cost to the telecom companies. One logical way to do this is to assign the shortest codes to the most common letters, based on a blueprint pmf q(x)q(x) for the alphabet. However, in reality your sender may be transmitting messages in a different language, or talking about a specific topic, meaning their true probability blueprint is p(x)p(x). Because your compression code is optimized for qq but reality follows pp, your system is inefficient and transmits wasted bits. We need a term that measures these wasted bits arising from different assumptions about the underlying language.

Relative entropy, also called the Kullback–Leibler distance and denoted D(pq)D(p \,\Vert\, q), measures this exact inefficiency.

ComponentDetail
SignatureD:P(X)×P(X)[0,]D : \mathcal{P}(\mathcal{X}) \times \mathcal{P}(\mathcal{X}) \rightarrow [0, \infty]
DomainA pair of probability mass functions from the space of all possible distributions over the alphabet.
CodomainAn extended non-negative real number: the exact penalty, in extra bits per keystroke, you pay for assuming qq when the truth is pp.

WarningAn impossible symbol costs infinity

If qq assumes a letter is physically impossible (q=0q = 0) but it actually happens in reality (p>0p > 0), the penalty is infinite. This is why the codomain is [0,][0, \infty] rather than [0,)[0, \infty).

Why something like H(p)H(q)H(p) - H(q) would not work

Imagine two operators:

  • Operator P types A 99% of the time, and B 1% of the time.
  • Operator Q types A 1% of the time, and B 99% of the time.

Because both operators have the exact same ratio of predictability (just flipped), their total average uncertainty is identical: H(p)=H(q)0.08H(p) = H(q) \approx 0.08 bits, so any difference-of-entropies measure reports zero penalty.

But say Operator Q’s codebook assigns a tiny 1-bit code to B and a massive 7-bit code to A. If you force Operator P (who almost only types A) to use Operator Q’s codebook, Operator P will be forced to send a massive 7-bit code 99% of the time, causing a high number of wasted bits.

Building the right measure

To find the real penalty, we must look at the cost of a single keystroke, and then average it out.

Codebook built forCode length assigned to xx
The true reality pp (ideal)log2p(x)-\log_2 p(x)
The false assumption qq (actual)log2q(x)-\log_2 q(x)

The extra wasted bits for typing that specific symbol xx is actual cost minus ideal cost:

[log2q(x)][log2p(x)][-\log_2 q(x)] - [-\log_2 p(x)]

Using logarithm rules, this simplifies to:

log2p(x)log2q(x)=log2p(x)q(x)\log_2 p(x) - \log_2 q(x) = \log_2 \frac{p(x)}{q(x)}

This fraction is the exact number of extra bits you waste every single time you type the letter xx, but you only type the letter xx according to the true reality of the system, p(x)p(x). Therefore, to find the average total penalty of using the wrong codebook, we weight that per-keystroke penalty by the true probability p(x)p(x) and sum over the whole alphabet:

FormulaRelative entropy (Kullback–Leibler distance)

D(pq)=Ep[log2p(X)q(X)]=xXp(x)log2p(x)q(x)D(p \,\Vert\, q) = \mathbb{E}_p \left[ \log_2 \frac{p(X)}{q(X)} \right] = \sum_{x \in \mathcal{X}} p(x) \log_2 \frac{p(x)}{q(x)}

WarningNot a true distance

While we often call this a “distance” between two distributions, it is not a true geometric metric, because it is not symmetric: D(pq)D(qp)D(p \,\Vert\, q) \neq D(q \,\Vert\, p). Optimizing for English but transmitting French carries a different bit penalty than optimizing for French but transmitting English.


Mutual information

Motivation (the severed-wire view): In our communication system with sender XX and receiver YY, suppose we cut the wire and look at the system. Because the wire is cut, XX and YY have absolutely nothing to do with each other, they are independent, and the probability blueprint of this severed system is simply the product of their individual probabilities, p(x)p(y)p(x)p(y).

One way to measure the ability of the physical connection to communicate the information of XX to YY is to measure how far away the connected system is from being a severed system. “Far away” implies distance, so we can use the relative entropy tool D(pq)D(p \,\Vert\, q) to measure the distance between the true, connected reality p(x,y)p(x,y) and the false, severed assumption p(x)p(y)p(x)p(y).

Motivation (the engineer’s view): If the wire is cut, the engineer has to compress, transmit, and store XX and YY as two completely independent streams of data: sending XX takes H(X)H(X) bits and sending YY takes H(Y)H(Y) bits. But if the wire is connected, YY is largely just a noisy echo of XX, instead of sending YY from scratch, they can just send the differences (the noise), and storing them together as a joint system only takes H(X,Y)H(X,Y) bits. The “penalty” of assuming independence is the wasted bandwidth or hard drive space from failing to exploit their physical correlation.

This exact distance from independence is called mutual information, I(X;Y)I(X;Y).

ComponentDetail
SignatureI:P(X×Y)[0,)I : \mathcal{P}(\mathcal{X} \times \mathcal{Y}) \rightarrow [0, \infty)
DomainLike joint entropy, the entire joint probability distribution of the communication system.
CodomainA non-negative real number: the average actual amount of useful bits shared between XX and YY per keystroke. If I(X;Y)=0I(X;Y) = 0 the wire is dead (zero correlation); the higher the number, the more perfectly synced the two ends are.

Substituting the connected reality p(x,y)p(x,y) for pp, and the severed assumption p(x)p(y)p(x)p(y) for qq, in relative entropy:

FormulaMutual information

I(X;Y)=D(p(x,y)p(x)p(y))=xXyYp(x,y)log2p(x,y)p(x)p(y)=H(X)H(XY)I(X; Y) = D\big(p(x,y) \,\Vert\, p(x)p(y)\big) = \sum_{x \in \mathcal{X}} \sum_{y \in \mathcal{Y}} p(x, y) \log_2 \frac{p(x, y)}{p(x)p(y)} = H(X) - H(X \mid Y)

DerivationFrom the definition to the entropy-difference form

While the formula above defines what mutual information is, we can expand the logarithm to reveal exactly how it relates to the uncertainty of the sender and receiver.

Apply the conditional probability rule p(x,y)=p(y)p(xy)p(x,y) = p(y)p(x \mid y); the p(y)p(y) on top cancels the p(y)p(y) on the bottom:

=x,yp(x,y)log2p(xy)p(x)= \sum_{x, y} p(x, y) \log_2 \frac{p(x \mid y)}{p(x)}

=x,yp(x,y)log2p(xy)    x,yp(x,y)log2p(x)= \sum_{x, y} p(x, y) \log_2 p(x \mid y) \;-\; \sum_{x, y} p(x, y) \log_2 p(x)

In the second term, because log2p(x)\log_2 p(x) does not care about yy, we can marginalize (collapse) the joint probability by summing over yy, so p(x,y)p(x,y) turns back into the marginal probability p(x)p(x):

=xp(x)log2p(x)(x,yp(x,y)log2p(xy))= - \sum_{x} p(x) \log_2 p(x) - \left( - \sum_{x, y} p(x, y) \log_2 p(x \mid y) \right)

Replacing the terms:

I(X;Y)=H(X)H(XY)I(X; Y) = H(X) - H(X \mid Y)

IntuitionWhat the difference is measuring

H(X)H(X) is your total uncertainty about what the sender typed before you look at the receiver’s tape. H(XY)H(X \mid Y) is the leftover confusion you still have after you look at the receiver’s tape. So the difference I(X;Y)I(X;Y) is the exact mathematical amount by which your uncertainty was reduced — the amount of knowledge successfully gained.

Equivalent forms

I(X;Y)I(X;Y) is perfectly symmetrical, so XX says exactly as much about YY as YY says about XX:

I(X;Y)=I(Y;X)I(X; Y) = I(Y; X)

FormReading
I(X;Y)=H(X)H(XY)I(X;Y) = H(X) - H(X \mid Y)Sender’s uncertainty, minus what’s left after seeing the receiver’s tape.
I(X;Y)=H(Y)H(YX)I(X;Y) = H(Y) - H(Y \mid X)From the receiver’s perspective: the useful info equals the total bits received, minus the bits that were generated purely by the wire’s noise.
I(X;Y)=H(X)+H(Y)H(X,Y)I(X;Y) = H(X) + H(Y) - H(X,Y)Using joint entropy: the sum of the two isolated systems, minus their combined total uncertainty, which subtracts out the double-counted overlap.
I(X;X)=H(X)I(X;X) = H(X)Since I(X;X)=H(X)H(XX)=H(X)0I(X;X) = H(X) - H(X \mid X) = H(X) - 0. The amount of information a variable shares with itself is exactly its entire entropy, which is also why entropy is called self-information.

Chain rule for entropy (nn variables)

Motivation: Imagine calculating the total uncertainty of an entire telegraph message of length nn. You don’t just add up the independent uncertainty of each letter, because letters depend on each other (grammar, spelling, the letter Q may be unlikely but after Q appears, the letter U is very likely). The total uncertainty of a book is the uncertainty of the 1st letter, plus the remaining uncertainty of the 2nd letter given you know the 1st, plus the uncertainty of the 3rd given you know the first two, and so on.

TheoremChain rule for entropy

Let X1,X2,,XnX_1, X_2, \ldots, X_n be drawn according to a joint probability distribution p(x1,,xn)p(x_1, \ldots, x_n). The joint entropy of the entire collection is the sum of the conditional entropies:

H(X1,X2,,Xn)=i=1nH(XiXi1,,X1)H(X_1, X_2, \ldots, X_n) = \sum_{i=1}^n H(X_i \mid X_{i-1}, \ldots, X_1)

ProofBy direct evaluation

We start with the definition of the joint entropy for nn variables, which is the expected value of their joint surprise:

H(X1,,Xn)=x1,,xnp(x1,,xn)log2p(x1,,xn)H(X_1, \ldots, X_n) = - \sum_{x_1, \ldots, x_n} p(x_1, \ldots, x_n) \log_2 p(x_1, \ldots, x_n)

From basic probability, any joint distribution can be perfectly factored using the chain rule of probability, p(x1,,xn)=i=1np(xixi1,,x1)p(x_1, \dots, x_n) = \prod_{i=1}^n p(x_i \mid x_{i-1}, \dots, x_1). Substitute this into the logarithm:

=x1,,xnp(x1,,xn)log2(i=1np(xixi1,,x1))= - \sum_{x_1, \ldots, x_n} p(x_1, \ldots, x_n) \log_2 \left( \prod_{i=1}^n p(x_i \mid x_{i-1}, \ldots, x_1) \right)

Because the logarithm of a product is the sum of the logarithms, we pull the sum outside:

=x1,,xni=1np(x1,,xn)log2p(xixi1,,x1)= - \sum_{x_1, \ldots, x_n} \sum_{i=1}^n p(x_1, \ldots, x_n) \log_2 p(x_i \mid x_{i-1}, \ldots, x_1)

We swap the order of summations. For each ii-th term, any variable xkx_k with k>ik > i does not appear inside the logarithm, so we can marginalize those future variables out, leaving only the variables up to ii:

=i=1nx1,,xip(x1,,xi)log2p(xixi1,,x1)= - \sum_{i=1}^n \sum_{x_1, \ldots, x_i} p(x_1, \ldots, x_i) \log_2 p(x_i \mid x_{i-1}, \ldots, x_1)

This inner sum is the exact definition of conditional entropy, so it simplifies perfectly to:

=i=1nH(XiXi1,,X1)= \sum_{i=1}^n H(X_i \mid X_{i-1}, \ldots, X_1)


Conditional mutual information

Motivation: Suppose a sender wants to transmit a secret message X{0,1}X \in \{0, 1\} to a receiver. To protect the transmission across an untrusted wire, the sender generates a purely random one-time pad key Z{0,1}Z \in \{0, 1\} and broadcasts the ciphertext Y=XZY = X \oplus Z.

flowchart TD
  X[Secret Plaintext X] --> Y[Ciphertext Y = X ⊕ Z]
  Z[Shared Key Z] --> Y

Without the key, looking at the ciphertext YY tells you absolutely nothing about the message XX (I(X;Y)=0I(X; Y) = 0).

We want to know: Given that we observe the key ZZ, how much information does the ciphertext YY now reveal about the plaintext XX?

ComponentDetail
SignatureI(X;YZ):P(X×Y×Z)[0,)I(X;Y \mid Z) : \mathcal{P}(\mathcal{X} \times \mathcal{Y} \times \mathcal{Z}) \rightarrow [0, \infty)
DomainThe joint probability distribution p(x,y,z)p(x, y, z) across the three variables.
CodomainThe number of bits of information shared between XX and YY within the context of knowing ZZ.

Following the standard definition of mutual information (baseline uncertainty minus remaining uncertainty), we evaluate how observing YY collapses our remaining doubt about XX once ZZ is fixed:

FormulaConditional mutual information

I(X;YZ)=H(XZ)H(XY,Z)I(X; Y \mid Z) = H(X \mid Z) - H(X \mid Y, Z) That is: the remaining uncertainty of XX knowing only the key ZZ, minus the uncertainty of XX knowing both the ciphertext YY and key ZZ.

In the one-time pad:

  • H(XZ)=1 bitH(X \mid Z) = 1\text{ bit} (knowing the key alone leaves XX completely unknown).
  • H(XY,Z)=0 bitsH(X \mid Y, Z) = 0\text{ bits} (knowing key and ciphertext lets you reconstruct X=YZX = Y \oplus Z perfectly).
  • Thus, I(X;YZ)=10=1 bitI(X; Y \mid Z) = 1 - 0 = 1\text{ bit}.

Chain rule for mutual information

Motivation: Instead of one sender, imagine a sequence of nn different sensors (X1XnX_1 \dots X_n) transmitting data to a single central receiver hub YY. We want to calculate the total amount of useful information the entire sensor array successfully shared with the hub.

TheoremChain rule for mutual information

The total mutual information between the sequence and YY is the sum of the individual mutual informations, each conditioned on the sensors that came before it:

I(X1,X2,,Xn;Y)=i=1nI(Xi;YXi1,,X1)I(X_1, X_2, \ldots, X_n ; Y) = \sum_{i=1}^n I(X_i ; Y \mid X_{i-1}, \ldots, X_1)

ProofExpand into entropies, then apply the entropy chain rule twice

Expand I(X1,,Xn;Y)I(X_1, \dots, X_n ; Y) into entropies:

I(X1,,Xn;Y)=H(X1,,Xn)H(X1,,XnY)I(X_1, \ldots, X_n ; Y) = H(X_1, \ldots, X_n) - H(X_1, \ldots, X_n \mid Y)

Apply the entropy chain rule to both terms:

=i=1nH(XiXi1,,X1)i=1nH(XiXi1,,X1,Y)= \sum_{i=1}^n H(X_i \mid X_{i-1}, \ldots, X_1) - \sum_{i=1}^n H(X_i \mid X_{i-1}, \ldots, X_1, Y)

Combine the two sums:

=i=1n[H(XiXi1,,X1)H(XiXi1,,X1,Y)]= \sum_{i=1}^n \Big[ H(X_i \mid X_{i-1}, \ldots, X_1) - H(X_i \mid X_{i-1}, \ldots, X_1, Y) \Big]

The bracketed term is exactly the definition of conditional mutual information:

=i=1nI(Xi;YX1,,Xi1)= \sum_{i=1}^n I(X_i ; Y \mid X_1, \ldots, X_{i-1})

IntuitionNo double-counting correlated sensors

If sensors observe the same physical environment, their raw readings often overlap. A naive sum I(Xi;Y)\sum I(X_i; Y) severely overcounts because multiple sensors report redundant information about the hub YY.

The chain rule accounts for information sequentially where each sensor only gets credit for the fresh, non-redundant insight it provides about YY beyond what previous sensors already disclosed.

2-sensor scenario:

  • Let Y{0,1}Y \in \{0, 1\} be whether a server room is on fire (H(Y)=1 bitH(Y) = 1\text{ bit}, assuming equal prior odds).

  • Sensor X1X_1 is an optical smoke detector that detects the fire perfectly: X1=YX_1 = Y.

  • Sensor X2X_2 is a thermal heat sensor that also detects the fire perfectly: X2=YX_2 = Y.

  1. Sensor 1 alone: I(X1;Y)=H(Y)H(YX1)=10=1 bitI(X_1; Y) = H(Y) - H(Y \mid X_1) = 1 - 0 = 1\text{ bit}.
    Observing X1X_1 completely resolves whether the room is on fire.
  2. Sensor 2 in context: I(X2;YX1)=H(YX1)H(YX1,X2)=00=0 bitsI(X_2; Y \mid X_1) = H(Y \mid X_1) - H(Y \mid X_1, X_2) = 0 - 0 = 0\text{ bits}.
    Because X1X_1 already identified the fire state, X2X_2 provides zero new information about YY, even though X2X_2 by itself has I(X2;Y)=1 bitI(X_2; Y) = 1\text{ bit}.

Applying the chain rule: I(X1,X2;Y)=I(X1;Y)+I(X2;YX1)=1 bit+0 bits=1 bitI(X_1, X_2; Y) = I(X_1; Y) + I(X_2; Y \mid X_1) = 1\text{ bit} + 0\text{ bits} = 1\text{ bit}

The chain rule gives us the true joint information (1 bit1\text{ bit}) by stripping away redundancy, avoiding the naive sum of 1+1=2 bits1 + 1 = 2\text{ bits} of non-existent certainty.


Chain rule for relative entropy

Motivation: Recall our compression engineer from earlier. They built a codebook for the entire sender–receiver system (X,Y)(X,Y) based on a false blueprint q(x,y)q(x,y), when reality was actually p(x,y)p(x,y). The total penalty paid in wasted bits is D(p(x,y)q(x,y))D(p(x,y) \,\Vert\, q(x,y)). We can split this penalty into two distinct physical failures:

  • The penalty from misunderstanding the sender’s language (XX).
  • The penalty from misunderstanding the noise profile of the wire (YXY \mid X).

To measure the latter, we define conditional relative entropy, D(p(yx)q(yx))D\big(p(y \mid x) \,\Vert\, q(y \mid x)\big): the average distance between the true physical noise profile and the assumed noise profile, weighted by how often the sender actually transmits each symbol.

FormulaConditional relative entropy

D(p(yx)q(yx))=xp(x)yp(yx)log2p(yx)q(yx)D\big(p(y \mid x) \,\Vert\, q(y \mid x)\big) = \sum_x p(x) \sum_y p(y \mid x) \log_2 \frac{p(y \mid x)}{q(y \mid x)}

IntuitionPaying for wire modeling errors only where traffic actually flows

If you misjudge the channel noise for a specific input xx, how much does it cost you? That depends on how often you actually transmit xx.

For a fixed transmitted symbol xx, the channel introduces noise according to the true transition distribution p(yx)p(y \mid x), but your receiver codebook expects q(yx)q(y \mid x). The inefficiency for that specific input is the standard KL divergence between the two output distributions:

D(p(YX=x)q(YX=x))=yp(yx)log2p(yx)q(yx)D\big(p(Y \mid X=x) \,\Vert\, q(Y \mid X=x)\big) = \sum_y p(y \mid x) \log_2 \frac{p(y \mid x)}{q(y \mid x)}

To get the overall penalty across the entire wire, take the expected value over reality’s input traffic p(x)p(x):

D(p(yx)q(yx))=EXp(x)[D(p(YX)q(YX))]D\big(p(y \mid x) \,\Vert\, q(y \mid x)\big) = \mathbb{E}_{X \sim p(x)}\Big[ D\big(p(Y \mid X) \,\Vert\, q(Y \mid X)\big) \Big]

TheoremChain rule for relative entropy

The total penalty of the joint system is simply the penalty of the sender plus the penalty of the wire:

D(p(x,y)q(x,y))=D(p(x)q(x))+D(p(yx)q(yx))D\big(p(x,y) \,\Vert\, q(x,y)\big) = D\big(p(x) \,\Vert\, q(x)\big) + D\big(p(y \mid x) \,\Vert\, q(y \mid x)\big)

ProofFactor both blueprints, split the log, marginalize

Expand the joint relative entropy definition:

D(p(x,y)q(x,y))=x,yp(x,y)log2p(x,y)q(x,y)D\big(p(x,y) \,\Vert\, q(x,y)\big) = \sum_{x, y} p(x, y) \log_2 \frac{p(x, y)}{q(x, y)}

Apply the probability chain rule to both reality pp and assumption qq:

=x,yp(x,y)log2p(x)p(yx)q(x)q(yx)= \sum_{x, y} p(x, y) \log_2 \frac{p(x)\,p(y \mid x)}{q(x)\,q(y \mid x)}

Use logarithm rules to split the fraction into the marginal part and the conditional part:

=x,yp(x,y)log2p(x)q(x)+x,yp(x,y)log2p(yx)q(yx)= \sum_{x, y} p(x, y) \log_2 \frac{p(x)}{q(x)} + \sum_{x, y} p(x, y) \log_2 \frac{p(y \mid x)}{q(y \mid x)}

In the first sum, marginalize out yy (since the log term doesn’t depend on yy); keep the second sum as is:

=xp(x)log2p(x)q(x)+x,yp(x,y)log2p(yx)q(yx)= \sum_x p(x) \log_2 \frac{p(x)}{q(x)} + \sum_{x, y} p(x, y) \log_2 \frac{p(y \mid x)}{q(y \mid x)}

These are exactly the definitions of marginal relative entropy and conditional relative entropy:

=D(p(x)q(x))+D(p(yx)q(yx))= D\big(p(x) \,\Vert\, q(x)\big) + D\big(p(y \mid x) \,\Vert\, q(y \mid x)\big)


Summary

QuantityFormulaReads as
Surpriseh(p)=log2ph(p) = -\log_2 pSurprise of one specific symbol.
EntropyH(X)=xp(x)log2p(x)H(X) = -\sum_x p(x) \log_2 p(x)Average bits of surprise per keystroke.
Joint entropyH(X,Y)=x,yp(x,y)log2p(x,y)H(X,Y) = -\sum_{x,y} p(x,y) \log_2 p(x,y)Average surprise of the full across source and wire.
Conditional entropyH(YX)=x,yp(x,y)log2p(yx)H(Y \mid X) = -\sum_{x,y} p(x,y) \log_2 p(y \mid x)Bits of surprise contributed strictly by the wire.
Relative entropyD(pq)=xp(x)log2p(x)q(x)D(p \,\Vert\, q) = \sum_x p(x) \log_2 \frac{p(x)}{q(x)}Wasted bits from coding for qq when reality is pp.
Mutual informationI(X;Y)=x,yp(x,y)log2p(x,y)p(x)p(y)I(X;Y) = \sum_{x,y} p(x,y) \log_2 \frac{p(x,y)}{p(x)p(y)}Useful bits shared per keystroke / distance from a severed wire.
Conditional MII(X;YZ)=H(XZ)H(XY,Z)I(X;Y \mid Z) = H(X \mid Z) - H(X \mid Y,Z)New bits YY gives about XX beyond what ZZ already gave.

Identities:

  • H(X,Y)=H(X)+H(YX)=H(Y)+H(XY)H(X,Y) = H(X) + H(Y \mid X) = H(Y) + H(X \mid Y)
  • H(X,YZ)=H(XZ)+H(YX,Z)H(X,Y \mid Z) = H(X \mid Z) + H(Y \mid X, Z)
  • I(X;Y)=H(X)H(XY)=H(Y)H(YX)=H(X)+H(Y)H(X,Y)I(X;Y) = H(X) - H(X \mid Y) = H(Y) - H(Y \mid X) = H(X) + H(Y) - H(X,Y)
  • I(X;X)=H(X)I(X;X) = H(X)
  • 0H(X)log2X0 \le H(X) \le \log_2 \lvert \mathcal{X} \rvert
  • H(YX)H(XY)H(Y \mid X) \neq H(X \mid Y) and D(pq)D(qp)D(p \,\Vert\, q) \neq D(q \,\Vert\, p) neither is symmetric.

← All notes