TExcelTAD
Home › Information Theory › The Maximum-Entropy Principle

The Maximum-Entropy Principle

Suppose all you know about a distribution is a few averages: the mean of a die, the average energy of a gas, the expected counts of some features. Infinitely many distributions match. Which one should you use? E. T. Jaynes argued for the one with the largest entropy: it satisfies the constraints and assumes nothing else. This principle explains why the Gaussian, exponential and softmax distributions appear everywhere.

The problem

max⁡P  H(P)subject to∑xP(x)=1,∑xP(x)fk(x)=Fk for each k\max_P \; H(P) \quad \text{subject to} \quad \sum_x P(x) = 1, \quad \sum_x P(x)f_k(x) = F_k \text{ for each } k

Entropy is concave and the constraints are linear, so this is a convex problem with a unique solution (Convex Functions). Setting the gradient of the Lagrangian to zero (The Lagrangian and Multipliers) gives the exponential family form:

P(x)=1Z(λ)exp⁡(∑kλkfk(x))P(x) = \frac{1}{Z(\boldsymbol{\lambda})}\exp\left(\sum_k \lambda_k f_k(x)\right)

The multipliers λk\lambda_k are chosen so the constraints hold.

Jaynes' dice

A die is rolled many times and the average is 4.5 instead of 3.5. What distribution over faces should we assume? Maximum entropy with a mean constraint gives P(k)∝eλkP(k) \propto e^{\lambda k}; we solve for λ\lambda:

python · try it yourself
import numpy as np
from scipy.optimize import brentq

faces = np.arange(1, 7)
def dist(lam):
    w = np.exp(lam * faces); return w / w.sum()
lam = brentq(lambda l: dist(l) @ faces - 4.5, -5, 5)
P = dist(lam)
H = lambda p: float(-np.sum(p * np.log2(p)))
print("lambda =", round(lam, 4))
print("max-ent distribution:", np.round(P, 4), " mean", round(float(P @ faces), 4), " entropy", round(H(P), 4))
alt = np.array([0, 0, 0.25, 0.25, 0.25, 0.25])          # another distribution with mean 4.5
print("an alternative with mean 4.5:", alt, " entropy", round(H(alt[alt > 0]), 4))
lambda = 0.371
max-ent distribution: [0.0544 0.0788 0.1142 0.1654 0.2398 0.3475]  mean 4.5  entropy 2.3279
an alternative with mean 4.5: [0.   0.   0.25 0.25 0.25 0.25]  entropy 2.0

The alternative also has mean 4.5 but claims, without evidence, that 1 and 2 never occur. The maximum-entropy distribution keeps every face possible and tilts smoothly toward the high faces.

Familiar distributions are maximum-entropy

Known Maximum-entropy distribution
nothing (finite set) uniform
mean, on {0,1,2,… }\{0, 1, 2, \dots\} geometric
mean, on [0,∞)[0, \infty) exponential
mean and variance, on R\mathbb{R} Gaussian
expected feature values exponential family (Gibbs distribution)
python · try it yourself
import numpy as np
from scipy.optimize import minimize

# discretized check: on 0..30 with mean 4, maximize entropy numerically
k = np.arange(31)
cons = [{"type": "eq", "fun": lambda p: p.sum() - 1}, {"type": "eq", "fun": lambda p: p @ k - 4}]
negH = lambda p: float(np.sum(p * np.log(np.clip(p, 1e-12, None))))
res = minimize(negH, np.ones(31) / 31, constraints=cons, bounds=[(0, 1)] * 31, method="SLSQP", options={"maxiter": 500})
geo = (1 / 5) * (4 / 5) ** k; geo /= geo.sum()
print("numerical max-ent, first 6:", np.round(res.x[:6], 4))
print("geometric with mean 4:      ", np.round(geo[:6], 4))
numerical max-ent, first 6: [0.1988 0.1593 0.1277 0.1024 0.082  0.0657]
geometric with mean 4:       [0.2002 0.1602 0.1281 0.1025 0.082  0.0656]

Maximum entropy and logistic regression

A classifier that matches the training averages of feature-times-class indicators and is otherwise as uncertain as possible has the form P(y∣x)∝exp⁡(wyTx)P(y \mid \mathbf{x}) \propto \exp(\mathbf{w}_y^T\mathbf{x}): softmax regression. Maximizing its likelihood is the dual of the maximum-entropy problem, so multinomial logistic regression is also called a maximum-entropy classifier. The same form appears as the Boltzmann distribution in physics and as softmax with temperature in language models (Sampling and Temperature).

python · try it yourself
import numpy as np
from sklearn.linear_model import LogisticRegression

rng = np.random.default_rng(0)
X = rng.normal(size=(500, 2)); y = (X @ [1.5, -1.0] + 0.5 * rng.normal(size=500) > 0).astype(int)
clf = LogisticRegression(C=1e6, max_iter=1000).fit(X, y)
p = clf.predict_proba(X)[:, 1]
print("model average of p * x:", np.round((p[:, None] * X).mean(axis=0), 4))
print("data average of y * x: ", np.round((y[:, None] * X).mean(axis=0), 4))
print("model mean of p:", round(float(p.mean()), 4), "  data mean of y:", round(float(y.mean()), 4))
model average of p * x: [ 0.2788 -0.2025]
data average of y * x:  [ 0.2789 -0.2024]
model mean of p: 0.4741   data mean of y: 0.474

At the maximum-likelihood solution the model reproduces exactly the feature averages it was trained on, which are the constraints of the dual maximum-entropy problem.

In software

  • Natural language processing used "MaxEnt" models (logistic regression with many indicator features) for tagging and parsing before neural networks.
  • Species distribution modelling (the Maxent software) and image texture synthesis also fit maximum-entropy models matched to observed statistics.
  • Fitting is convex: iterative scaling or any gradient method for logistic regression works.

Exercises

Exercise 1: No constraints

What is the maximum-entropy distribution on 10 outcomes with no constraint except normalization?

Answer

Uniform, with entropy log⁡210≈3.32\log_2 10 \approx 3.32 bits.

Solution

All λk\lambda_k are absent, so P(x)=1ZP(x) = \frac{1}{Z} is constant.

Exercise 2: Fair die

If the die's average is 3.5, what does the maximum-entropy principle give?

Answer

The uniform distribution.

Solution

λ=0\lambda = 0 satisfies the constraint, because the uniform distribution already has mean 3.5.

Exercise 3: Mean 2.5 (code)

Find the maximum-entropy die distribution with mean 2.5.

Answer

It tilts toward low faces: P(1)≈0.347P(1) \approx 0.347 down to P(6)≈0.054P(6) \approx 0.054.

Solution
python · try it yourself
import numpy as np
from scipy.optimize import brentq

faces = np.arange(1, 7)
dist = lambda l: np.exp(l * faces) / np.exp(l * faces).sum()
lam = brentq(lambda l: dist(l) @ faces - 2.5, -5, 5)
print(round(lam, 4), np.round(dist(lam), 4))
-0.371 [0.3475 0.2398 0.1654 0.1142 0.0788 0.0544]

Test yourself

The maximum-entropy distribution with a known mean and variance on the real line is…

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

Maximum-entropy distributions under expectation constraints have the form…

  1. 1Zexp⁡(∑kλkfk(x))\frac{1}{Z}\exp(\sum_k \lambda_k f_k(x))
  2. ∑kλkfk(x)\sum_k \lambda_k f_k(x)
  3. uniform on the support
  4. a mixture of Gaussians

Multinomial logistic regression is also called…

  1. a maximum-entropy classifier
  2. a Huffman classifier
  3. a minimum-description classifier
  4. a typical-set classifier