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
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:
The multipliers 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 ; we solve for :
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 | geometric |
| mean, on | exponential |
| mean and variance, on | Gaussian |
| expected feature values | exponential family (Gibbs distribution) |
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 : 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).
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 bits.
Solution
All are absent, so 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
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: down to .
Solution
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…
- Gaussian
- uniform
- exponential
- Laplace
Maximum-entropy distributions under expectation constraints have the form…
- uniform on the support
- a mixture of Gaussians
Multinomial logistic regression is also called…
- a maximum-entropy classifier
- a Huffman classifier
- a minimum-description classifier
- a typical-set classifier