With base-2 logarithms, surprisal is measured in bits; with natural logarithms, in nats.
Example 1 (Counting bits with fair coins) One toss of a fair coin has \(2\) equally likely outcomes, and one binary digit is enough to record which one happened. Two tosses have \(4\) outcomes, and two binary digits record them. In general, \(k\) tosses have \(n = 2^k\) equally likely outcomes, and \(k = \log_2 n\) binary digits record them.
Each of those \(n\) outcomes has probability \(p = 1/n\), so
\[
\begin{aligned}
k
&= \log_2 n \\
&= \log_2 \frac{1}{p} \\
&= -\log_2 p,
\end{aligned}
\]
Remark 1 (Unlikely outcomes carry more information). Surprisal is large when an outcome was unlikely, and it falls to \(0\) as the outcome becomes certain. Correctly predicting heads on one toss of a fair coin is worth \(1\) bit; correctly predicting ten heads in a row is worth \(10\) bits. MacKay (2003, sec. 2.4) calls \(s(x)\) in bits the Shannon information content of the outcome \(x\).
Remark 2 (Entropy is expected surprisal). The entropy of a discrete random variable \(X\) is the expectation of its surprisal:
So \(H(X)\) is the average number of bits (with base-2 logarithms) needed to record an outcome of \(X\) when each outcome \(x\) costs \(s(x)\) bits. The source coding theorem, on a later page of these notes, makes that average precise: no uniquely decodable code does better.
2 Joint and conditional entropy
Definition 2 (Joint entropy) Let \(X\) and \(Y\) be discrete random variables with joint probability mass function \(p(x, y) \stackrel{\text{def}}{=}\operatorname{P}(X=x, Y=y)\). Their joint entropy is the entropy of the pair \((X, Y)\):
where the sum runs over the pairs with \(p(x, y) > 0\).
Definition 3 (Entropy given one value) For \(X\) and \(Y\) as in Definition 2, write \(p(y \mid x) \stackrel{\text{def}}{=}\operatorname{P}(Y=y \mid X=x)\). The entropy of \(Y\) given \(X=x\), for a value \(x\) with \(\operatorname{P}(X=x) > 0\), is
where the sum runs over the values \(y\) with \(p(y \mid x) > 0\).
Definition 4 (Conditional entropy) For \(X\) and \(Y\) as in Definition 2, write \(p(x) \stackrel{\text{def}}{=}\operatorname{P}(X=x)\). The conditional entropy of \(Y\) given \(X\) averages the entropy of \(Y\) given each value of \(X\) over the distribution of \(X\):
The fourth line groups the terms by \(x\) and uses \(p(x, y) = p(x) \, p(y \mid x)\) in the second sum; the fifth uses \(\sum_{y} p(x, y) = p(x)\) and the definition of \(H(Y \mid X=x)\).
3 Mutual information
Definition 5 (Mutual information) The mutual information between discrete random variables \(X\) and \(Y\) with finite entropies is the reduction in the entropy of \(Y\) from learning \(X\):
Subtracting \(H(X \mid Y) + H(Y \mid X)\) from the outer two expressions gives
\[H(X) - H(X \mid Y) = H(Y) - H(Y \mid X).\]
The right side is \(I(X; Y)\) by Definition 5, and the left side is \(I(Y; X)\) by the same definition.
Remark 3 (Mutual information as a divergence). Cover and Thomas (2006, sec. 2.3) define mutual information as the Kullback–Leibler divergence of the joint distribution of \((X, Y)\) from the product of its marginals, and show that this equals \(H(Y) - H(Y \mid X)\)(Cover and Thomas 2006, sec. 2.4). Since a Kullback–Leibler divergence is never negative, and is \(0\) only when the two distributions are equal (Cover and Thomas 2006, sec. 2.6), \(I(X; Y) \ge 0\), with equality exactly when \(X\) and \(Y\) are independent. Learning \(X\) never raises the entropy of \(Y\) on average.
Definition 6 (Information gain) A decision tree splits the points at a node into branches. The information gain of the split is the entropy of the class labels at the node, minus the entropy of the labels in each branch, weighted by the share of the node’s points that branch holds (Quinlan 1986).
Remark 4 (Information gain is mutual information). Draw one point uniformly at random from the node of Definition 6, and let \(Y\) be its class and \(X\) the branch it falls into. Then the entropy at the node is \(H(Y)\), the weighted entropy left in the branches is \(H(Y \mid X)\), and the information gain is the mutual information\(I(X; Y)\).
Exercise 1 (Choosing between two splits) A node holds \(20\) points, \(10\) red and \(10\) green. Two candidate splits each send the points into two branches:
split
branch
red
green
A
yes
\(4\)
\(0\)
no
\(6\)
\(10\)
B
yes
\(5\)
\(10\)
no
\(5\)
\(0\)
Draw one point uniformly at random from the node, let \(Y\) be its color, and let \(X\) be the branch it falls into (Remark 4). The information gain of each split (Definition 6) is then \(I(X; Y)\). Use base-2 logarithms throughout.
Compute \(H(Y)\).
For each split, compute \(H(Y \mid X = x)\) for both branches, and then \(H(Y \mid X)\).
Compute the information gain \(I(X; Y)\) for each split. Which split has the larger gain?
By Definition 5, the gain is \(H(Y) - H(Y \mid X)\):
split A: \(1 - 0.764 = 0.236\) bits
split B: \(1 - 0.689 = 0.311\) bits
Split B has the larger gain. Both splits isolate a branch of one color, so the gain depends on the other, mixed branch. B’s mixed branch, \(5\) red against \(10\) green, is more lopsided than A’s \(6\) against \(10\), so it has less entropy, and it also holds fewer of the points, \(15\) against \(16\), so its entropy gets less weight.
References
Cover, Thomas M., and Joy A. Thomas. 2006. Elements of Information Theory. 2nd ed. Wiley-Interscience. https://doi.org/10.1002/047174882X.
MacKay, David J. C. 2003. Information Theory, Inference, and Learning Algorithms. Cambridge University Press.
---title: "Entropy and mutual information"format: html: default revealjs: output-file: entropy-slides.html pdf: output-file: entropy-handout.pdf docx: output-file: entropy.docx---{{< include latex-macros/macros.qmd >}}This page builds on the definitions of[entropy, cross-entropy and Kullback--Leibler divergence](https://morrison-lab.github.io/pds/expectation.html#sec-entropy)in *Probability for Data Science*.## Surprisal {#sec-surprisal}<!-- No slidebreak here: the heading shares its slide with the def-surprisal div. -->{{< include ids/_subfiles/_def-surprisal.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exm-surprisal-coins.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-surprisal-meaning.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-entropy-expected-surprisal.qmd >}}## Joint and conditional entropy {#sec-conditional-entropy}<!-- No slidebreak here: the heading shares its slide with the def-joint-entropy div. -->{{< include ids/_subfiles/_def-joint-entropy.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_def-entropy-given-value.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_def-conditional-entropy.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_thm-entropy-chain-rule.qmd >}}## Mutual information {#sec-mutual-information}<!-- No slidebreak here: the heading shares its slide with the def-mutual-information div. -->{{< include ids/_subfiles/_def-mutual-information.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_thm-mutual-information-symmetric.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-mutual-information-kl.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_def-information-gain.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-information-gain.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exr-information-gain.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_sol-information-gain.qmd >}}## References {.unnumbered}::: {#refs}:::