Search

Search papers and lessons

Gibbs Variational Principle

Level 4 · 11 min read

The log of an average of exponentials equals the best value of a score minus a KL penalty, and the best distribution is the exponentially tilted one.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Information theory
  • Optimization
  • Sign in to see your progress

TL;DR

Take a log of an average of exponentials, log⁡Eπ[ef]\log \mathbb{E}_\pi[e^{f}]. The Gibbs variational principle says this number is the largest value of a simple objective: the average of the score ff under some distribution QQ, minus the KL divergence from QQ to the prior π\pi. The best QQ is the prior reweighted by efe^{f} and normalized, which is called the tilted or Gibbs distribution.

The result turns a hard calculation, a log of a weighted sum of exponentials, into an optimization over distributions. Any choice of QQ gives a lower bound, and the bound is exact at the best QQ.

Where you'll see it: in statistical mechanics, where the free energy of a system is a log partition function, in variational inference, where the evidence lower bound is this principle applied to a model, and in the Donsker–Varadhan formula for large deviations.

Intuition

A log of a sum that is hard to compute

Suppose you have a prior π\pi over many possible configurations, and each configuration xx has a score f(x)f(x). You want the log of the average of ef(x)e^{f(x)} under π\pi. This number is the log partition function, and it is central in physics and machine learning. It is hard to compute when there are many configurations, because you must sum over all of them.

The Gibbs principle changes the question. Instead of computing the number directly, ask: which distribution QQ over configurations gives the largest value of "average score under QQ, minus the price of moving away from the prior"? The price is the KL divergence DKL(Q ∥ π)D_{\mathrm{KL}}(Q \,\|\, \pi), which is zero when QQ equals π\pi and grows as QQ moves away from it.

The trade-off is the whole idea. Putting all weight on the highest-scoring configuration maximizes the score, but it pays a large KL cost because it is far from the prior. Keeping QQ equal to the prior pays no cost, but it ignores the scores. The best QQ balances the two, and it does so by tilting the prior toward high scores by exactly the amount efe^{f}.

flowchart LR
  P[prior pi] --> T[tilt by e^f]
  F[scores f] --> T
  T --> N[normalize by Z]
  N --> Q[best Q = Gibbs distribution]
  Q --> V[value: log Z]

Why this matters: the objective "score minus KL" is easy to evaluate for any QQ you can sample from. You get a lower bound on the hard quantity for free, and you know exactly when the bound is tight.

The math

Definition

Let π\pi be a probability distribution on a set X\mathcal{X}, and let f:X→Rf : \mathcal{X} \to \mathbb{R} be a score. Define the partition function

Z=Eπ ⁣[ef(X)]=∑xπ(x) ef(x),Z = \mathbb{E}_\pi\!\left[e^{f(X)}\right] = \sum_{x} \pi(x)\, e^{f(x)},

the average of efe^{f} under the prior. The Gibbs distribution, also called the tilted distribution, is

πf(x)=π(x) ef(x)Z.\pi_f(x) = \frac{\pi(x)\, e^{f(x)}}{Z}.

Here π(x)\pi(x) is the prior probability of configuration xx, ef(x)e^{f(x)} is its exponentiated score, and ZZ is the normalizing constant that makes πf\pi_f sum to one.

The principle

For every probability distribution QQ on X\mathcal{X},

EQ[f]−DKL(Q ∥ π)  ≤  log⁡Z,\mathbb{E}_Q[f] - D_{\mathrm{KL}}(Q \,\|\, \pi) \;\le\; \log Z,

and equality holds exactly when Q=πfQ = \pi_f. Written as a supremum,

log⁡Eπ ⁣[ef]=sup⁡Q{EQ[f]−DKL(Q ∥ π)}.\log \mathbb{E}_\pi\!\left[e^{f}\right] = \sup_{Q} \Bigl\{ \mathbb{E}_Q[f] - D_{\mathrm{KL}}(Q \,\|\, \pi) \Bigr\}.

Proof

Take any QQ with q(x)q(x) its probabilities. Expand the KL term and combine it with the expected score:

EQ[f]−DKL(Q ∥ π)=∑xq(x)(f(x)−log⁡q(x)π(x)).\mathbb{E}_Q[f] - D_{\mathrm{KL}}(Q \,\|\, \pi) = \sum_x q(x)\Bigl(f(x) - \log \frac{q(x)}{\pi(x)}\Bigr).

Now compare with πf\pi_f. Its probabilities satisfy log⁡πf(x)=log⁡π(x)+f(x)−log⁡Z\log \pi_f(x) = \log \pi(x) + f(x) - \log Z, so f(x)=log⁡πf(x)−log⁡π(x)+log⁡Zf(x) = \log \pi_f(x) - \log \pi(x) + \log Z. Substitute this into the sum. Each term becomes q(x)(log⁡Z−log⁡q(x)πf(x))q(x)\bigl(\log Z - \log \frac{q(x)}{\pi_f(x)}\bigr). Summing gives

EQ[f]−DKL(Q ∥ π)=log⁡Z−DKL(Q ∥ πf).\mathbb{E}_Q[f] - D_{\mathrm{KL}}(Q \,\|\, \pi) = \log Z - D_{\mathrm{KL}}(Q \,\|\, \pi_f).

The KL divergence between two distributions is never negative and is zero only when they match, so the right-hand side is at most log⁡Z\log Z. Equality holds exactly when Q=πfQ = \pi_f. This is the whole proof, and it uses only the fact that KL is non-negative. The non-negativity itself comes from Jensen's inequality, as shown in the KL divergence lesson.

Free-energy form

In statistical mechanics the configurations are the NN states of a physical system, the score is f=−E/(kBT)f = -E/(k_B T) for an energy EE at temperature TT, and the prior is uniform, π(x)=1/N\pi(x) = 1/N. Then πf\pi_f is the Boltzmann distribution. Let Zphys=∑xe−E(x)/(kBT)Z_{\mathrm{phys}} = \sum_x e^{-E(x)/(k_B T)} be the usual partition function, so that Z=Zphys/NZ = Z_{\mathrm{phys}}/N. For a uniform prior, DKL(Q ∥ π)=log⁡N−S(Q)D_{\mathrm{KL}}(Q \,\|\, \pi) = \log N - S(Q), where S(Q)=−∑xq(x)log⁡q(x)S(Q) = -\sum_x q(x)\log q(x) is the Shannon entropy of QQ in nats. The log⁡N\log N terms cancel and the principle reads

−kBTlog⁡Zphys=min⁡Q{EQ[E]−kBT S(Q)} .-k_B T \log Z_{\mathrm{phys}} = \min_{Q}\bigl\{ \mathbb{E}_Q[E] - k_B T\,S(Q) \bigr\}\,.

The left-hand side is the free energy, and the right-hand side is the minimum of energy minus temperature times entropy over all distributions. Nature settles into the distribution that minimizes this trade-off between low energy and high entropy.

Connection to the convex conjugate

The map f↦log⁡Eπ[ef]f \mapsto \log \mathbb{E}_\pi[e^{f}] is convex in ff. The supremum above says that it is the Legendre–Fenchel transform (convex conjugate) of the convex function Q↦DKL(Q ∥ π)Q \mapsto D_{\mathrm{KL}}(Q \,\|\, \pi), evaluated at ff. That is why the principle is also called a duality. The dual variable is the distribution QQ, and the penalty is the KL divergence.

Worked examples

Example
Two outcomes, done by hand

Let X={a,b}\mathcal{X} = \{a, b\} with a uniform prior π(a)=π(b)=12\pi(a) = \pi(b) = \tfrac{1}{2}. Let the scores be f(a)=0f(a) = 0 and f(b)=ln⁡3f(b) = \ln 3, so ef=(1,3)e^{f} = (1, 3).

  1. The partition function is Z=12⋅1+12⋅3=2Z = \tfrac{1}{2}\cdot 1 + \tfrac{1}{2}\cdot 3 = 2, so log⁡Z=ln⁡2≈0.6931\log Z = \ln 2 \approx 0.6931.
  2. The tilted distribution is πf(a)=12⋅1/2=0.25\pi_f(a) = \tfrac{1}{2}\cdot 1 / 2 = 0.25 and πf(b)=12⋅3/2=0.75\pi_f(b) = \tfrac{1}{2}\cdot 3 / 2 = 0.75.
  3. Evaluate the objective at Q=πfQ = \pi_f. Its expected score is 0.75ln⁡3≈0.82400.75 \ln 3 \approx 0.8240. Its KL divergence from the prior is 0.25ln⁡(0.25/0.5)+0.75ln⁡(0.75/0.5)≈−0.1733+0.3041=0.13080.25\ln(0.25/0.5) + 0.75\ln(0.75/0.5) \approx -0.1733 + 0.3041 = 0.1308. The objective is 0.8240−0.1308≈0.69320.8240 - 0.1308 \approx 0.6932, which matches ln⁡2\ln 2.
  4. Try two other distributions to see that they do worse. Q=(0.5,0.5)Q = (0.5, 0.5) gives expected score 0.54930.5493 and KL 00, for 0.54930.5493. Q=(0,1)Q = (0, 1) gives expected score 1.09861.0986 and KL ln⁡2≈0.6931\ln 2 \approx 0.6931, for 0.40550.4055. Both are below 0.69310.6931.

Interpretation: the best distribution puts three times as much weight on bb as on aa (0.75 against 0.25), whereas the prior put equal weight on both. That factor is exactly ef(b)−f(a)=eln⁡3=3e^{f(b)-f(a)} = e^{\ln 3} = 3. Any other choice of QQ either loses score or pays more KL than it gains.

The plot shows the objective as a function of q=Q(b)q = Q(b), with Q=(1−q,q)Q = (1 - q, q). It peaks at q=3/4q = 3/4, where the value is ln⁡2\ln 2.

0.20.40.60.800.20.40.60.8x = Q(b)objective (nats)
  • E_Q[f] - KL(Q || pi)
The Gibbs objective for two outcomes with a uniform prior and scores 0 and ln 3. The maximum is ln 2 at q = 3/4, the tilted distribution.
Example
Three configurations with scores 1, 2 and 3

Let X={1,2,3}\mathcal{X} = \{1, 2, 3\} with a uniform prior, and scores f=(1,2,3)f = (1, 2, 3). This is the same calculation as a softmax over three logits.

  1. Compute the exponentials: e1≈2.7183e^{1} \approx 2.7183, e2≈7.3891e^{2} \approx 7.3891, e3≈20.0855e^{3} \approx 20.0855.
  2. The partition function is Z=13(2.7183+7.3891+20.0855)=13(30.1928)≈10.0643Z = \tfrac{1}{3}(2.7183 + 7.3891 + 20.0855) = \tfrac{1}{3}(30.1928) \approx 10.0643.
  3. The log is log⁡Z≈2.3090\log Z \approx 2.3090.
  4. The tilted distribution is the softmax of the scores: πf=(0.0900,  0.2447,  0.6652)\pi_f = (0.0900,\; 0.2447,\; 0.6652). These sum to 11.
  5. Evaluate the objective at πf\pi_f. The expected score is 0.0900⋅1+0.2447⋅2+0.6652⋅3≈2.57520.0900 \cdot 1 + 0.2447 \cdot 2 + 0.6652 \cdot 3 \approx 2.5752. The KL from the uniform prior is ∑xq(x)ln⁡(3q(x))≈0.2662\sum_x q(x)\ln(3q(x)) \approx 0.2662. The objective is 2.5752−0.2662≈2.30902.5752 - 0.2662 \approx 2.3090, which matches log⁡Z\log Z.

Interpretation: the highest-scoring configuration gets the most weight, but not all of it, because putting all weight there would cost too much KL. Here log⁡Z≈2.309\log Z \approx 2.309 sits just below the largest score, 33. The gap, about 0.690.69, is the price of the prior's spread. In general max⁡f−log⁡∣X∣≤log⁡Z≤max⁡f\max f - \log|\mathcal{X}| \le \log Z \le \max f, so with three states the gap is at most log⁡3≈1.10\log 3 \approx 1.10.

Note that log⁡Z\log Z is computed with the log-sum-exp trick in practice, subtracting the maximum score before exponentiating, so large scores do not overflow.

Tradeoffs and failure modes

The principle is exact, but using it in practice requires choices.

SituationWhat goes wrongBetter choice
The configuration space is huge, so ZZ cannot be summedThe exact log partition function is out of reachRestrict QQ to a simpler family and maximize the bound, which is the variational approach
The family of QQ does not contain the tilted distributionThe bound is strictly below log⁡Z\log Z, and the gap is not visible from the bound aloneA richer family, or a sampling method such as Monte Carlo that targets πf\pi_f directly
The prior π\pi is badly chosenThe bound is loose or the tilt is distorted, since KL is measured against the wrong referencePick a prior that already covers the plausible configurations
Scores are largeDirect exponentiation overflowsLog-sum-exp with the maximum subtracted first
Scores are noisy or estimatedThe optimal QQ fits the noise, and the bound overstates the valueRegularize QQ or average over several score estimates

A common mistake is to read the bound as an estimate of log⁡Z\log Z from either side. Any QQ gives a lower bound, never an upper bound. An upper bound needs a different argument, such as the simple bound log⁡Z≤max⁡f\log Z \le \max f, which holds because the prior's weights sum to one.

History

The formula comes from Josiah Willard Gibbs's work on statistical mechanics in the late 19th century. In his 1902 book on the subject he described how a system at equilibrium is a distribution over states that balances low energy against entropy. The equilibrium distribution is the exponential tilt of the prior, and the free energy is the log of the normalizing constant. Gibbs's framework is the origin of the name.

The variational form as a supremum over distributions, with KL as the penalty, was developed in probability theory in the 1970s. Monroe Donsker and S. R. S. Varadhan used it to study large deviations of Markov processes, and it is now called the Donsker–Varadhan formula. In machine learning, the same identity underlies the evidence lower bound used in variational inference, which became popular in the 1990s and 2000s as a way to fit probabilistic models that have intractable normalizing constants. These dates are approximate, and the names are the main contributors rather than a full history.

Check yourself

QuestionWhy is the KL penalty needed? What would go wrong if the objective were only the expected score under QQ?Show answer

Without the penalty, the supremum of EQ[f]\mathbb{E}_Q[f] is achieved by putting all weight on the highest-scoring configuration, which gives max⁡f\max f. That is an upper bound on log⁡Z\log Z that is usually loose, and it ignores the prior entirely. The KL term measures how far QQ has moved from the prior, and it is what makes the optimum the exponentially tilted distribution, with value exactly log⁡Z\log Z.

QuestionWith a uniform prior over two states and scores f(a)=0f(a) = 0, f(b)=ln⁡4f(b) = \ln 4, what is log⁡Z\log Z?Show answer

ef=(1,4)e^{f} = (1, 4), so Z=12(1+4)=2.5Z = \tfrac{1}{2}(1 + 4) = 2.5. Therefore log⁡Z=ln⁡2.5≈0.9163\log Z = \ln 2.5 \approx 0.9163. The tilted distribution is (1/5,4/5)(1/5, 4/5), which is (0.2,0.8)(0.2, 0.8).

QuestionWhy does the Gibbs variational principle give a lower bound for any QQ, and when is that bound tight?Show answer

For any QQ, the objective EQ[f]−DKL(Q ∥ π)\mathbb{E}_Q[f] - D_{\mathrm{KL}}(Q \,\|\, \pi) equals log⁡Z−DKL(Q ∥ πf)\log Z - D_{\mathrm{KL}}(Q \,\|\, \pi_f). KL is non-negative, so the objective is at most log⁡Z\log Z, which means it is a lower bound. The bound is tight exactly when QQ equals the tilted distribution πf\pi_f, because then the KL term is zero.

QuestionIn a variational model, a family of distributions QQ can only be a Gaussian with a fixed variance, and the true tilted distribution is skewed. What happens to the bound, and why?Show answer

The bound is still valid, since it holds for every QQ, but it cannot reach log⁡Z\log Z because the tilted distribution is not in the family. The best Gaussian in the family gives a value strictly below log⁡Z\log Z, and the gap is the KL divergence from that Gaussian to the true tilted distribution. Enlarging the family, for example by allowing skew or by learning the variance, shrinks the gap.

Sources