MoonSay /Tutorials/Machine Learning Interview #8
Contents
  1. 01Introduction
  2. 02Surprise
  3. 03Entropy
  4. 04Cross-entropy
  5. 05KL divergence
  6. 06Information gain and mutual information
  7. 07Why training minimizes cross-entropy
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References

03Entropy: the average surprise

3.1Definition

A single outcome has a surprise; a whole forecast, before you see the outcome, has an average surprise. Weight each outcome’s surprise by how often it happens:

Definition 1 (Entropy). The entropy of a discrete distribution p is

H(p)=\sum_x\;\;p(x)\log_2\frac{1}{p(x)}=\E_{x\sim p}\Bigl[\log_2\frac1{p(x)}\Bigr]. \tag{2}

It is the average surprise of a local, someone who knows the true odds (Murphy, 2022, §6.1.1). Figure 2(a,b) draws it as an area: each outcome is a bar whose width is its probability and whose height is its surprise, and the total area is the entropy.

Worked example 2 (Entropy of July weather). Cupertino: H=0.0032\times8.28+0.9968\times0.0047=0.027+0.005=0.031 bits per day. The rare rainy day is very surprising but almost never happens, and the common dry day happens but surprises no one, so the forecast carries almost no information. Miami: H=0.558\times0.84+0.442\times1.18=0.470+0.521=0.99 bits per day, almost one coin flip. Entropy measures unpredictability.

(a, b) Entropy as an area: each outcome is a bar of width p and height \log_2(1/p), and the area is the average height. Cupertino’s rain bar is 8.28 bits tall but only 1/310 wide (drawn wider so that it is visible). (c) The binary entropy H(\theta)=\theta\log_2\frac1\theta+(1-\theta)\log_2\frac1{1-\theta} for a chance of rain \theta: zero at 0 and 1, largest (1 bit) at \theta=\frac12. Data: NOAA climate normals; computed for this tutorial.
Figure 2. (a, b) Entropy as an area: each outcome is a bar of width p and height \log_2(1/p), and the area is the average height. Cupertino’s rain bar is 8.28 bits tall but only 1/310 wide (drawn wider so that it is visible). (c) The binary entropy H(\theta)=\theta\log_2\frac1\theta+(1-\theta)\log_2\frac1{1-\theta} for a chance of rain \theta: zero at 0 and 1, largest (1 bit) at \theta=\frac12. Data: NOAA climate normals; computed for this tutorial.
Try itThe entropy of a forecast

Move the chance of rain. Each outcome is a bar whose width is its probability and whose height is its surprise; the entropy, equation (2), is the total area, the average height.

3.2Properties

  • Zero when you already know. H(p)\ge0, with H=0 exactly when one outcome has probability 1.
  • Largest when outcomes are equally likely. For two outcomes with probabilities \theta and 1-\theta, \frac{dH}{d\theta}=\log_2\frac{1-\theta}{\theta}, which is zero at \theta=\frac12, where H=1 bit (Figure 2c). For K outcomes, H(p)\le\log_2K, with equality only for the uniform distribution; we prove this in Section 5 (Murphy, 2022, Corollary 6.2.1).
  • Adds over independent parts. If x and y are independent, every joint surprise is a sum, so H(x,y)=H(x)+H(y). Thirty-one independent July days in Miami carry 31\times0.99=30.7 bits.
  • Depends only on the probabilities, not on what the outcomes are called or their numerical values.

3.3Entropy is the shortest message

Shannon’s source coding theorem gives entropy an operational meaning: to report outcomes drawn from p, no code can use fewer than H(p) bits per outcome on average, and by coding long blocks of outcomes together one gets as close to H(p) as one likes (Shannon, 1948), (Cover and Thomas, 2006, ch. 5). A good code gives an outcome a codeword about \log_2(1/p) bits long, short for common outcomes and long for rare ones, which is what Morse code does by hand and Huffman coding does optimally. Miami’s weather service cannot report a July in fewer than about 30.7 bits on average; Cupertino’s needs only 31\times0.031=0.97 bits for the whole month, since it almost always has to say just “dry again”.

Remark 1 (Continuous outcomes). For a density p(x) the sum becomes an integral, and the entropy becomes the differential entropy

h(p)=\int p(x)\ln\frac1{p(x)}\,dx.

It can be negative: a Gaussian has h=\frac12\ln(2\pi e\sigma^2) nats, which is negative when \sigma is small (Murphy, 2022, §6.1.6). Among all densities with a given variance, the Gaussian has the largest differential entropy (Bishop, 2006, §1.6). The KL divergence below keeps all of its properties for densities.

Pause and think. The sum of two fair dice takes 11 values, 2 to 12. Is its entropy \log_2 11=3.46 bits?

Show the answer

No. The sums are not equally likely (7 has probability 6/36, 2 only 1/36), so the entropy is smaller: 3.27 bits. Only the uniform distribution reaches \log_2K.