Differential Entropy
Real signals such as voltages, audio samples and sensor readings are continuous. Their entropy in the discrete sense is infinite (you need infinitely many bits to pin down a real number exactly), but differences of entropies, such as mutual information, stay finite. The differential entropy is the tool for this.
Definition
For a random variable with density ,
Unlike discrete entropy it can be negative, and it changes under rescaling: .
| Distribution | in bits |
|---|---|
| uniform on | |
| Gaussian with variance | |
| exponential with mean |
import numpy as np
from scipy import stats
for name, dist, formula in [("uniform [0, 0.5]", stats.uniform(0, 0.5), np.log2(0.5)),
("Gaussian sigma=1", stats.norm(0, 1), 0.5 * np.log2(2 * np.pi * np.e)),
("Gaussian sigma=3", stats.norm(0, 3), 0.5 * np.log2(2 * np.pi * np.e * 9)),
("exponential mean 2", stats.expon(scale=2), np.log2(2 * np.e))]:
print(f"{name:20s}: scipy {float(dist.entropy()) / np.log(2):7.4f} bits, formula {formula:7.4f}")uniform [0, 0.5] : scipy -1.0000 bits, formula -1.0000
Gaussian sigma=1 : scipy 2.0471 bits, formula 2.0471
Gaussian sigma=3 : scipy 3.6321 bits, formula 3.6321
exponential mean 2 : scipy 2.4427 bits, formula 2.4427
The uniform distribution on an interval of length 0.5 has negative differential entropy: it is "more concentrated" than the uniform on , which has .
Relation to discrete entropy
Quantize into bins of width . The discrete entropy of the bin index is approximately
So with -bit precision () a continuous variable needs about bits. The term blows up as , but it cancels in differences.
import numpy as np
rng = np.random.default_rng(0)
x = rng.normal(0, 1, 1_000_000)
h = 0.5 * np.log2(2 * np.pi * np.e)
for delta in [0.5, 0.1, 0.01]:
_, counts = np.unique(np.floor(x / delta), return_counts=True)
q = counts / counts.sum()
print(f"delta = {delta}: H(quantized) = {-np.sum(q * np.log2(q)):.4f}, h - log2(delta) = {h - np.log2(delta):.4f}")delta = 0.5: H(quantized) = 3.0628, h - log2(delta) = 3.0471
delta = 0.1: H(quantized) = 5.3705, h - log2(delta) = 5.3690
delta = 0.01: H(quantized) = 8.6913, h - log2(delta) = 8.6910
The Gaussian maximizes entropy
Among all densities with variance , the Gaussian has the largest differential entropy. The proof is Gibbs' inequality again (Jensen's and Gibbs' Inequalities): for any density with that variance and the Gaussian , . This is why Gaussian noise is the worst case for communication (The Gaussian Channel) and a natural default model (The Maximum-Entropy Principle).
import numpy as np
from scipy import stats
# all with variance 1
dists = {"Gaussian": stats.norm(0, 1), "uniform": stats.uniform(-np.sqrt(3), 2 * np.sqrt(3)),
"Laplace": stats.laplace(0, 1 / np.sqrt(2)), "logistic": stats.logistic(0, np.sqrt(3) / np.pi)}
for name, d in dists.items():
print(f"{name:9s}: variance {float(d.var()):.3f}, h = {float(d.entropy()) / np.log(2):.4f} bits")Gaussian : variance 1.000, h = 2.0471 bits
uniform : variance 1.000, h = 1.7925 bits
Laplace : variance 1.000, h = 1.9427 bits
logistic : variance 1.000, h = 2.0264 bits
Mutual information stays meaningful
For continuous and , is non-negative, invariant under rescaling either variable, and the limit of the discrete mutual information of finer and finer quantizations. For a Gaussian signal plus independent Gaussian noise:
import numpy as np
rng = np.random.default_rng(1)
sx, sn = 2.0, 1.0
x = rng.normal(0, sx, 2_000_000); y = x + rng.normal(0, sn, x.size)
for delta in [0.5, 0.2]:
bx, by = np.floor(x / delta).astype(int), np.floor(y / delta).astype(int)
pairs, c = np.unique(np.stack([bx, by]), axis=1, return_counts=True)
pxy = c / c.sum()
_, cx = np.unique(bx, return_counts=True); _, cy = np.unique(by, return_counts=True)
H = lambda q: -np.sum(q * np.log2(q))
print(f"delta = {delta}: quantized I = {H(cx / cx.sum()) + H(cy / cy.sum()) - H(pxy):.4f}")
print(f"formula: {0.5 * np.log2(1 + sx**2 / sn**2):.4f}")delta = 0.5: quantized I = 1.1336
delta = 0.2: quantized I = 1.1570
formula: 1.1610
In software
scipy.stats.<dist>.entropy()returns differential entropy in nats.- For multivariate Gaussians, ; use
np.linalg.slogdetfor numerical stability. - Estimating or from samples is hard in high dimensions; nearest-neighbour estimators (Kozachenko-Leonenko, KSG) are used by
sklearn.feature_selection.mutual_info_regression.
Exercises
Exercise 1: Scaling
If bits, what is ?
Answer
3.5 bits.
Solution
.
Exercise 2: Negative entropy
For which widths is the uniform distribution on negative in differential entropy?
Answer
.
Solution
exactly when . Negative values are fine: differential entropy is not a number of bits by itself.
Exercise 3: Multivariate Gaussian (code)
Compute in bits for a 2-D Gaussian with covariance and compare with two independent unit Gaussians.
Answer
About 3.357 bits versus 4.094 bits.
Solution
import numpy as np
S = np.array([[1.0, 0.8], [0.8, 1.0]])
h = lambda S: 0.5 * (len(S) * np.log2(2 * np.pi * np.e) + np.linalg.slogdet(S)[1] / np.log(2))
print(round(float(h(S)), 4), round(float(h(np.eye(2))), 4))3.3572 4.0942
The difference, 0.737 bits, is the mutual information between the two coordinates, .
Test yourself
Differential entropy can be…
- negative
- only positive
- only an integer
- only infinite
Among densities with a given variance, the largest differential entropy belongs to…
- the Gaussian
- the uniform
- the Laplace
- the exponential
Quantizing a continuous variable with bin width Delta gives a discrete entropy of about…