With base-2 logarithms, surprisal is also called the Shannon information content of the outcome \(x\)(MacKay 2003, sec. 2.4).
Definition 2 (Bit (shannon)) A bit, also called a shannon (symbol Sh) (International Electrotechnical Commission 2008), is the unit of information when the logarithm in surprisal, and in every quantity built from it, has base \(2\).
One bit is the surprisal of an outcome with probability \(1/2\): for an outcome \(x\) of a discrete random variable \(X\),
In words, a bit is the surprisal of the outcome of one binary trial whose two outcomes are equally likely, such as one toss of a fair coin.
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}
\]
Example 2 (One fair coin toss in nats) By Example 1 with \(k = 1\), one toss of a fair coin landing heads has a surprisal of \(1\)bit. In nats, the same outcome has a surprisal of
So an outcome with a surprisal of \(1\)nat has a surprisal of \(1 / \ln 2 \approx 1.443\)bits, and the \(\ln 2 \approx 0.693\)nats of Example 2 are exactly \(\ln 2 / \ln 2 = 1\)bit.
A bit as a unit of information is not the same thing as a bit as a binary digit. The name shannon refers only to the unit of information, so it avoids this ambiguity. A binary digit whose two values are equally likely has a surprisal of exactly \(1\) bit, whichever value it takes. A binary digit that is \(0\) nine times in ten does not: the value \(0\) has a surprisal of \(-\log_2 0.9 \approx 0.152\) bits, and the value \(1\) has a surprisal of \(-\log_2 0.1 \approx 3.322\) bits.
Remark 2 (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.
Remark 3 (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.
So each face of the fair die has rarity\({\mathbb{r}\mathopen{}\left(x\right)\mathclose{}} = 6\): one roll in six.
Suppose the die is loaded so that a six has probability \(1/2\). Then \({\mathbb{r}\mathopen{}\left(6\right)\mathclose{}} = 2\). If the other five faces share the rest equally, each has \({\mathbb{r}\mathopen{}\left(x\right)\mathclose{}} = 10\). The more probable an outcome, the lower its rarity.
Remark 4 (Source of the name). Leinster writes that, for a community whose \(i\)th species has relative abundance \(p_i\), “\(1/p_i\) is a measure of its rarity or specialness” (Leinster 2021, sec. 2.4, p. 52). The name “rarity” for the reciprocal of a probability is borrowed from there.
Proof. \[
\begin{aligned}
{\mathbb{s}\mathopen{}\left(x\right)\mathclose{}}
&= -{\operatorname{log}\mathopen{}\left\{\mathbb{P}(X=x)\right\}\mathclose{}} && \text{(definition of surprisal)} \\
&= {\operatorname{log}\mathopen{}\left\{\frac{1}{\mathbb{P}(X=x)}\right\}\mathclose{}} && \text{(logarithm of a reciprocal)} \\
&= {\operatorname{log}\mathopen{}\left\{{\mathbb{r}\mathopen{}\left(x\right)\mathclose{}}\right\}\mathclose{}}. && \text{(definition of rarity)}
\end{aligned}
\]
Theorem 2 (Surprisal of a continuous value is the logarithm of the rarity) Let \(X\) be a continuous random variable with density\(\mathbb{p}\), and let \(x\) be a value with \(\mathbb{p}(x) > 0\). Then the surprisal of \(x\) is the logarithm of its rarity:
Proof. \[
\begin{aligned}
{\mathbb{s}\mathopen{}\left(x\right)\mathclose{}}
&= -{\operatorname{log}\mathopen{}\left\{\mathbb{p}(x)\right\}\mathclose{}} && \text{(definition of surprisal)} \\
&= {\operatorname{log}\mathopen{}\left\{\frac{1}{\mathbb{p}(x)}\right\}\mathclose{}} && \text{(logarithm of a reciprocal)} \\
&= {\operatorname{log}\mathopen{}\left\{{\mathbb{r}\mathopen{}\left(x\right)\mathclose{}}\right\}\mathclose{}}. && \text{(definition of rarity)}
\end{aligned}
\]
Corollary 1 (Surprisal of a discrete outcome is nonnegative) Let \(X\) be a discrete random variable, and let \(x\) be an outcome with \(\mathbb{P}(X=x) > 0\). Then the rarity of \(x\) is at least \(1\), with equality exactly when \(\mathbb{P}(X=x) = 1\), and the surprisal satisfies
Proof. Since \(0 < \mathbb{P}(X=x) \le 1\), the rarity\({\mathbb{r}\mathopen{}\left(x\right)\mathclose{}} = 1 / \mathbb{P}(X=x)\) is at least \(1\), with equality exactly when \(\mathbb{P}(X=x) = 1\). The logarithm is increasing, and \({\operatorname{log}\mathopen{}\left\{1\right\}\mathclose{}} = 0\), so by Theorem 1, \({\mathbb{s}\mathopen{}\left(x\right)\mathclose{}} = {\operatorname{log}\mathopen{}\left\{{\mathbb{r}\mathopen{}\left(x\right)\mathclose{}}\right\}\mathclose{}} \ge 0\).
Example 4 (Surprisal and rarity of an outcome with probability one quarter) Take an outcome with probability\(1/4\):
Remark 6 (Values of the probability, the rarity and the surprisal). For a discrete random variable, the mass \(\mathbb{P}(X=x)\) of an outcome with a mass above \(0\), its rarity\({\mathbb{r}\mathopen{}\left(x\right)\mathclose{}}\) and its surprisal\({\mathbb{s}\mathopen{}\left(x\right)\mathclose{}}\) take the values in the middle column. For a continuous random variable, the density\(\mathbb{p}(x)\) takes the place of the mass. At a value \(x\) with a density above \(0\), the rarity \({\mathbb{r}\mathopen{}\left(x\right)\mathclose{}}\) is the reciprocal of \(\mathbb{p}(x)\), and the surprisal \({\mathbb{s}\mathopen{}\left(x\right)\mathclose{}}\) is the surprisal of a continuous value. Their values are in the right column.
Mass. A mass of \(1\) gives the smallest rarity, \(1\), and the smallest surprisal, \(0\). No mass exceeds \(1\), the probability of a certain outcome. So no mass gives a rarity below \(1\) or a surprisal below \(0\).
Density. A density can be arbitrarily large, as in the uniform example, so the rarity can fall below \(1\) and the surprisal can fall below \(0\).
Zero. A mass or density of \(0\) gives a rarity and a surprisal of \(\infty\).
Units. A mass has no units. A density has the units of \(1/x\), so its rarity has the units of \(x\). Rescaling \(x\) by a nonzero \(c\) shifts its surprisal by \({\operatorname{log}\mathopen{}\left\{\mathopen{}\left|c\right|\mathclose{}\right\}\mathclose{}}\).
Remark 7 (Differential entropy differs from entropy). A density value is not a probability, so the surprisal of a continuous value and the entropy of a continuous random variable (the differential entropy) behave differently from their discrete counterparts:
Sign. A density can exceed \(1\), so the surprisal \({\mathbb{s}\mathopen{}\left(x\right)\mathclose{}}\) and the differential entropy can be negative, as in the uniform example(Cover and Thomas 2006, sec. 8.1). The entropy of a discrete random variable is never negative.
Units. Rescaling changes the differential entropy: \({\mathbb{H}\mathopen{}\left(aX\right)\mathclose{}} = {\mathbb{H}\mathopen{}\left(X\right)\mathclose{}} + \log |a|\) for a constant \(a \ne 0\)(Cover and Thomas 2006, Theorem 8.6.4). Measuring the same quantity in metres rather than centimetres therefore changes it. Adding a constant does not: \({\mathbb{H}\mathopen{}\left(X + c\right)\mathclose{}} = {\mathbb{H}\mathopen{}\left(X\right)\mathclose{}}\)(Cover and Thomas 2006, Theorem 8.6.3).
Not a limit of entropy. Round a continuous \(X\) to bins of width \(\Delta\), which gives a discrete random variable \(X^\Delta\). Then \({\mathbb{H}\mathopen{}\left(X^\Delta\right)\mathclose{}} + \log \Delta \to {\mathbb{H}\mathopen{}\left(X\right)\mathclose{}}\) as \(\Delta \to 0\)(Cover and Thomas 2006, Theorem 8.3.1). The entropy \({\mathbb{H}\mathopen{}\left(X^\Delta\right)\mathclose{}}\) itself grows without bound as \(\Delta \to 0\), so the differential entropy is not its limit.
Definition 5 (Collision probability) The collision probability of a discrete random variable\(X\) is the chance that two independent copies \(X\) and \(X'\) of \(X\) take the same value:
Theorem 3 (The expected probability is the collision probability) Let \(X\) be a discrete random variable, and let \(X'\) be an independent copy of \(X\). Then
with equality exactly when \(\mathbb{P}(X)\) is constant. The left side is \({\mathbb{H}\mathopen{}\left(X\right)\mathclose{}}\). The right side is \(H_2(X)\) by its definition. The function \(\mathbb{P}(X)\) is constant exactly when every value that \(X\) takes with positive probability has that same probability.
Example 5 (Collision entropy and entropy of a three-valued random variable)
Let \(X\) take three values with probabilities \(1/2\), \(1/4\) and \(1/4\). The code below computes the expected probability, the collision entropy and the entropy, with base-2 logarithms.
By Theorem 3, the expected probability \({\mathbb{E}\mathopen{}\left[\mathbb{P}(X)\right]\mathclose{}} = 0.375\) is the collision probability. The collision entropy is \(H_2(X) = 1.415\)bits. The entropy is \({\mathbb{H}\mathopen{}\left(X\right)\mathclose{}} = 1.5\) bits. As Theorem 4 states, \(H_2(X) \le {\mathbb{H}\mathopen{}\left(X\right)\mathclose{}}\), and the two differ because the three values are not equally likely.
Remark 9 (Surprisal and the Fisher information). When the probabilities depend on a parameter \(\theta\), the surprisal of an outcome \(x\) is \(-\log \mathbb{p}(x \mid \theta)\), which is minus the log-likelihood:
The expected squared score is the expected information, also called the Fisher information. It is also the expected second derivative of the surprisal, when the outcome \(X\) is drawn from the model at the true parameter value \(\theta_0\), and the score is evaluated at that same outcome \(X\):
Definition 7 (Joint entropy) Let \(X\) and \(Y\) be discrete random variables with joint probability mass function \(p(x, y) \stackrel{\text{def}}{=}\mathbb{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 8 (Entropy given one value) For \(X\) and \(Y\) as in Definition 7, write \(p(y \mid x) \stackrel{\text{def}}{=}\mathbb{P}(Y=y \mid X=x)\). The entropy of \(Y\) given \(X=x\), for a value \(x\) with \(\mathbb{P}(X=x) > 0\), is
where the sum runs over the values \(y\) with \(p(y \mid x) > 0\).
Definition 9 (Conditional entropy) For \(X\) and \(Y\) as in Definition 7, write \(p(x) \stackrel{\text{def}}{=}\mathbb{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 10 (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 10, and the left side is \(I(Y; X)\) by the same definition.
Remark 10 (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 11 (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 11 (Information gain is mutual information). Draw one point uniformly at random from the node of Definition 11, 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 11). The information gain of each split (Definition 11) 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 10, 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.
International Electrotechnical Commission. 2008. Quantities and Units — Part 13: Information Science and Technology. IEC 80000-13:2008. International Electrotechnical Commission.
---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/_def-bit.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exm-surprisal-coins.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_def-nat.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exm-coin-nats.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-bits-nats.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-surprisal-meaning.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-entropy-expected-surprisal.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_def-rarity.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exm-rarity-die.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-rarity-source.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_thm-surprisal-log-rarity.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_thm-surprisal-log-rarity-continuous.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_cor-surprisal-discrete-nonnegative.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exm-surprisal-rarity-four.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-surprisal-log-rarity.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-rarity-ranges.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-differential-entropy-vs-entropy.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_def-collision-probability.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_def-renyi-entropy.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_thm-expected-probability-collision.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_thm-collision-entropy-at-most-entropy.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_exm-collision-entropy-three-values.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-collision-entropy.qmd >}}{{< slidebreak >}}{{< include ids/_subfiles/_rem-surprisal-fisher-information.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}:::