TExcelTAD
Home › Information Theory › Differential Entropy

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 p(x)p(x),

h(X)=−∫p(x)log⁡2p(x) dxh(X) = -\int p(x)\log_2 p(x)\,dx

Unlike discrete entropy it can be negative, and it changes under rescaling: h(aX)=h(X)+log⁡2∣a∣h(aX) = h(X) + \log_2 |a|.

Distribution h(X)h(X) in bits
uniform on [0,a][0, a] log⁡2a\log_2 a
Gaussian with variance σ2\sigma^2 12log⁡2(2πeσ2)\frac{1}{2}\log_2(2\pi e\sigma^2)
exponential with mean μ\mu log⁡2(eμ)\log_2(e\mu)
python · try it yourself
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 [0,1][0, 1], which has h=0h = 0.

Relation to discrete entropy

Quantize XX into bins of width Δ\Delta. The discrete entropy of the bin index is approximately

H(XΔ)≈h(X)−log⁡2ΔH(X_\Delta) \approx h(X) - \log_2 \Delta

So with nn-bit precision (Δ=2−n\Delta = 2^{-n}) a continuous variable needs about h(X)+nh(X) + n bits. The −log⁡2Δ-\log_2 \Delta term blows up as Δ→0\Delta \to 0, but it cancels in differences.

python · try it yourself
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 σ2\sigma^2, the Gaussian has the largest differential entropy. The proof is Gibbs' inequality again (Jensen's and Gibbs' Inequalities): for any density pp with that variance and the Gaussian gg, 0≤DKL(p∥g)=hg−hp0 \le D_{\text{KL}}(p \parallel g) = h_g - h_p. This is why Gaussian noise is the worst case for communication (The Gaussian Channel) and a natural default model (The Maximum-Entropy Principle).

python · try it yourself
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 XX and YY, I(X;Y)=h(Y)−h(Y∣X)I(X; Y) = h(Y) - h(Y \mid X) 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:

I(X;X+N)=12log⁡2(1+σX2σN2)I(X; X + N) = \frac{1}{2}\log_2\left(1 + \frac{\sigma_X^2}{\sigma_N^2}\right)
python · try it yourself
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, h=12log⁡2((2πe)ddet⁡Σ)h = \frac{1}{2}\log_2((2\pi e)^d \det \Sigma); use np.linalg.slogdet for numerical stability.
  • Estimating hh or II 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 h(X)=1.5h(X) = 1.5 bits, what is h(4X)h(4X)?

Answer

3.5 bits.

Solution

h(aX)=h(X)+log⁡2∣a∣=1.5+2h(aX) = h(X) + \log_2 |a| = 1.5 + 2.

Exercise 2: Negative entropy

For which widths aa is the uniform distribution on [0,a][0, a] negative in differential entropy?

Answer

a<1a < 1.

Solution

h=log⁡2a<0h = \log_2 a < 0 exactly when a<1a < 1. Negative values are fine: differential entropy is not a number of bits by itself.

Exercise 3: Multivariate Gaussian (code)

Compute hh in bits for a 2-D Gaussian with covariance [10.80.81]\begin{bmatrix} 1 & 0.8 \\ 0.8 & 1 \end{bmatrix} and compare with two independent unit Gaussians.

Answer

About 3.357 bits versus 4.094 bits.

Solution
python · try it yourself
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, −12log⁡2(1−0.82)-\frac{1}{2}\log_2(1 - 0.8^2).

Test yourself

Differential entropy can be…

  1. negative
  2. only positive
  3. only an integer
  4. only infinite

Among densities with a given variance, the largest differential entropy belongs to…

  1. the Gaussian
  2. the uniform
  3. the Laplace
  4. the exponential

Quantizing a continuous variable with bin width Delta gives a discrete entropy of about…

  1. h(X)−log⁡2Δh(X) - \log_2 \Delta
  2. h(X)h(X)
  3. h(X)+Δh(X) + \Delta
  4. log⁡2Δ\log_2 \Delta