Search

Search papers and lessons

Jensen's Inequality

Level 3 · 11 min read

For a convex function, the function of an average is never larger than the average of the function: a bowl-shaped curve always lies below its chords.

On this page

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

TL;DR

Take a random quantity, average it, then apply a convex function. Compare that with applying the function first and averaging afterwards. For a convex function the second answer is never smaller. Jensen's inequality says exactly this: the function of the average is at most the average of the function, and the gap between them measures how much the randomness matters.

Where you'll see it: in proving that the evidence lower bound is a valid bound, in showing that a KL divergence is never negative, in the AM-GM inequality, and in explaining why plugging an average into a nonlinear function gives a biased estimate.

Intuition

A function is convex if its graph is shaped like a bowl: the straight segment joining any two points on the graph lies on or above the graph in between. Take f(x)=x2f(x) = x^2 and pick two points, x=1x = 1 and x=9x = 9. The chord between them runs from height 1 to height 81. The curve between those two points sits below that chord.

Now let XX be a random number that equals 1 or 9, each with probability one half. Two things can be computed:

  • Average first, then apply ff. The average of XX is 5, and f(5)=25f(5) = 25.
  • Apply ff first, then average. The values are f(1)=1f(1) = 1 and f(9)=81f(9) = 81, whose average is 41.

The second number is bigger. That is no accident. Every point of the random variable sits on the curve, and the average of those points lies on the chord, which lies above the curve. Averaging the outputs moves you up to the chord; averaging the inputs and then reading the curve puts you down on the curve.

0246810020406080xf(x)
  • f(x) = x^2
  • chord from (1, 1) to (9, 81)
The two outcomes sit on the curve at heights 1 and 81, so their average height is 41. The mean 5 sits on the curve at height 25, while the average of the two outputs is the point (5, 41) on the chord. The chord is above the curve everywhere between the two points, which is why the average of the outputs is the larger number.

The same picture explains why the idea matters. Whenever a quantity is uncertain and you need some nonlinear function of it, the shortcut of applying the function to the average gives an answer that is too low for a convex function and too high for a concave one.

The math

Convex functions

Definition: Convex function

A function ff defined on an interval is convex if, for every pair of points xx and yy and every weight λ\lambda between 0 and 1,

f(λx+(1−λ)y)≤λf(x)+(1−λ)f(y).f\big(\lambda x + (1-\lambda) y\big) \le \lambda f(x) + (1-\lambda) f(y).

A function is concave when the inequality is reversed. Here λ\lambda is a mixing weight: λ=0.5\lambda = 0.5 picks the midpoint of xx and yy.

The left side is the function at a blend of two inputs. The right side is the same blend of the two outputs, which is the height of the chord. The definition says the curve stays on or below the chord; the ≤\le allows the function to be linear on a stretch. The convex function lesson develops this in full.

Jensen's inequality

Statement. Let XX be a random variable with a finite mean, and let ff be convex. Then

f(E[X])≤E[f(X)].f\big(\mathbb{E}[X]\big) \le \mathbb{E}\big[f(X)\big].

If ff is concave, the inequality flips: f(E[X])≥E[f(X)]f(\mathbb{E}[X]) \ge \mathbb{E}[f(X)].

Proof for a differentiable convex ff. Let μ=E[X]\mu = \mathbb{E}[X]. Draw the tangent line to ff at μ\mu:

ℓ(x)=f(μ)+f′(μ) (x−μ).\ell(x) = f(\mu) + f'(\mu)\,(x - \mu).

Convexity means the curve lies on or above every tangent line, so f(x)≥ℓ(x)f(x) \ge \ell(x) for all xx. Take expectations on both sides. The expectation is monotone, so

E[f(X)]≥E[ℓ(X)].\mathbb{E}[f(X)] \ge \mathbb{E}[\ell(X)].

The right side is linear in XX, so it can be split up:

E[ℓ(X)]=f(μ)+f′(μ) (E[X]−μ)=f(μ)+f′(μ)⋅0=f(μ).\begin{aligned} \mathbb{E}[\ell(X)] &= f(\mu) + f'(\mu)\,\big(\mathbb{E}[X] - \mu\big) \\ &= f(\mu) + f'(\mu)\cdot 0 \\ &= f(\mu). \end{aligned}

Putting the two lines together gives E[f(X)]≥f(μ)\mathbb{E}[f(X)] \ge f(\mu), which is the claim. The tangent line is the key tool: it touches the curve at the mean, and a straight line commutes with averaging. Convex functions that are not differentiable at μ\mu have a supporting line with a slope chosen from the subgradient, and the same argument goes through.

Equality. For a strictly convex ff, equality holds only when XX is constant with probability 1. For a function that is linear on the range of XX, equality also holds. That is why the chord picture is tight only when the two points coincide.

Finite form. With weights p1,…,pnp_1, \dots, p_n that sum to 1 and points x1,…,xnx_1, \dots, x_n,

f(∑ipixi)≤∑ipi f(xi).f\Big(\sum_{i} p_i x_i\Big) \le \sum_{i} p_i\, f(x_i).

This is the same statement with XX taking the value xix_i with probability pip_i.

Two standard consequences

Variance is never negative. Take f(x)=x2f(x) = x^2, which is convex. Jensen gives (E[X])2≤E[X2](\mathbb{E}[X])^2 \le \mathbb{E}[X^2]. Subtracting the left side from the right gives

Var(X)=E[X2]−(E[X])2≥0.\mathrm{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 \ge 0.

So variance can never be negative, and it is zero only when XX is constant.

Arithmetic mean versus geometric mean. Take f=log⁡f = \log, which is concave, so the inequality flips. For positive numbers a1,…,ana_1, \dots, a_n with equal weights 1/n1/n,

log⁡(1n∑iai)≥1n∑ilog⁡ai.\log\Big(\frac{1}{n}\sum_i a_i\Big) \ge \frac{1}{n}\sum_i \log a_i.

Exponentiating both sides gives the AM-GM inequality: the arithmetic mean is at least the geometric mean.

Use in the evidence lower bound

Variational inference needs a lower bound on log⁡p(x)\log p(x) that can be optimised. Introduce any distribution q(z)q(z) over a hidden variable zz and write log⁡p(x)=log⁡Eq ⁣[p(x,z)q(z)]\log p(x) = \log \mathbb{E}_{q}\!\left[\frac{p(x,z)}{q(z)}\right]. Since log⁡\log is concave, Jensen gives

log⁡p(x)≥Eq ⁣[log⁡p(x,z)q(z)].\log p(x) \ge \mathbb{E}_{q}\!\left[\log \frac{p(x,z)}{q(z)}\right].

The right side is the evidence lower bound. Concavity of log⁡\log is used only in this one step, and it is why the bound holds for every choice of qq (with q(z)>0q(z) \gt 0 wherever p(x,z)>0p(x, z) \gt 0).

Worked examples

Example
The two-point variable again

Let XX equal 2 or 6, each with probability one half, and let f(x)=x2f(x) = x^2.

  1. Average first: E[X]=(2+6)/2=4\mathbb{E}[X] = (2 + 6)/2 = 4, so f(E[X])=16f(\mathbb{E}[X]) = 16.
  2. Apply ff first: f(2)=4f(2) = 4 and f(6)=36f(6) = 36, so E[f(X)]=(4+36)/2=20\mathbb{E}[f(X)] = (4 + 36)/2 = 20.
  3. Check the inequality: 16≤2016 \le 20. The gap is 4.
  4. The gap equals the variance. The values are 2 and 6, each 2 away from the mean 4, so Var(X)=22=4\mathrm{Var}(X) = 2^2 = 4.
  5. Interpretation: the more the variable spreads out around its mean, the larger the gap between averaging before and after the convex function.
Example
An exponential with two outcomes

Let XX equal 0 or 2 with probability one half each, and let f(x)=exf(x) = e^x, which is convex.

  1. E[X]=(0+2)/2=1\mathbb{E}[X] = (0 + 2)/2 = 1, so f(E[X])=e1≈2.718f(\mathbb{E}[X]) = e^1 \approx 2.718.
  2. f(0)=1f(0) = 1 and f(2)=e2≈7.389f(2) = e^2 \approx 7.389, so E[f(X)]=(1+7.389)/2=4.194\mathbb{E}[f(X)] = (1 + 7.389)/2 = 4.194.
  3. Check: 2.718≤4.1942.718 \le 4.194. Holds.
  4. Practical reading: a model that predicts eE[X]e^{\mathbb{E}[X]} for a quantity that is really eXe^X with uncertain XX will systematically underestimate the average value.
Example
AM-GM from Jensen with log

Take the three numbers 1, 4 and 16, each with weight 1/31/3, and the concave function log⁡\log.

  1. Arithmetic mean: (1+4+16)/3=7(1 + 4 + 16)/3 = 7.
  2. Mean of the logs: (log⁡1+log⁡4+log⁡16)/3=(0+1.3863+2.7726)/3=1.3863(\log 1 + \log 4 + \log 16)/3 = (0 + 1.3863 + 2.7726)/3 = 1.3863, which is log⁡4\log 4.
  3. Jensen with a concave function says log⁡7≥1.3863\log 7 \ge 1.3863. Indeed log⁡7≈1.9459\log 7 \approx 1.9459.
  4. Exponentiate: the geometric mean is e1.3863=4e^{1.3863} = 4 and the arithmetic mean is 7. So 7≥47 \ge 4, as AM-GM promises.

Tradeoffs and failure modes

Jensen is the right tool when you need to compare a function of an average with an average of a function, and you know the function's curvature. It gives a bound with no distributional assumptions beyond a finite mean.

The common mistakes are all about the direction of the inequality:

SituationWhat goes wrongBetter choice
Plugging the sample mean into a convex function to estimate E[f(X)]\mathbb{E}[f(X)]The estimate is biased low, since f(xˉ)≤E[f(X)]f(\bar{x}) \le \mathbb{E}[f(X)]Average ff over the samples
Reversing the inequality for a concave functionThe bound points the wrong way and the proof failsCheck the sign of f′′f'' first
Using Jensen on a function that is neither convex nor concaveNo inequality holds in generalBound the function on its range, or use a Taylor expansion
Expecting equality in practiceEquality needs a constant variable or a linear functionUse the gap as a measure of uncertainty

Some practical warnings:

  • Jensen gives a bound, not the size of the gap. It says which way the error goes, but a tight estimate of E[f(X)]\mathbb{E}[f(X)] usually needs a second-order term such as 12f′′(μ) Var(X)\tfrac{1}{2} f''(\mu)\,\mathrm{Var}(X).
  • Check that the expectation exists. If XX has heavy tails and E[X]\mathbb{E}[X] is infinite, the statement is meaningless.
  • Convexity is a property of ff, not of the data. Jensen says nothing about the distribution of XX beyond its mean, so the gap can be large or tiny for the same ff.

Alternatives include exact computation when the distribution is known, Monte Carlo averaging of ff over samples, and second-order expansions when the variance is small.

History

Johan Jensen, a Danish mathematician who worked mainly as an engineer at the Copenhagen telephone company, published the general statement in 1906 in Acta Mathematica, in a paper on convex functions and inequalities between mean values. Special cases were known long before: the arithmetic-geometric mean inequality was known in special cases long before, and inequalities of the same flavour appeared in the work of Hölder and others in the nineteenth century.

What Jensen did was isolate the principle. A single statement about convex functions and averages covers many earlier results at once, and the proof uses nothing but the definition of convexity and the linearity of averaging. That generality is why it caught on. It became a standard tool in analysis, then in probability, and later in statistics and information theory, where Gibbs' inequality and the non-negativity of KL divergence both follow from it.

Its modern prominence in machine learning comes from variational methods. The step that turns a log of an expectation into an expectation of a log is an application of Jensen, and it is the reason the evidence lower bound can be optimised by gradient methods.

Check yourself

QuestionA function ff is convex. What can you say about f(E[X])f(\mathbb{E}[X]) compared with E[f(X)]\mathbb{E}[f(X)], and what happens if ff is concave?Show answer

For convex ff, f(E[X])≤E[f(X)]f(\mathbb{E}[X]) \le \mathbb{E}[f(X)]: the function of the average is at most the average of the function. For concave ff the inequality reverses. The intuition is that a convex curve lies below its chords, so averaging after applying ff lands above the curve.

QuestionLet XX equal 3 or 7 with probability one half each, and let f(x)=x2f(x) = x^2. Compute f(E[X])f(\mathbb{E}[X]) and E[f(X)]\mathbb{E}[f(X)], and check the gap against the variance.Show answer

E[X]=5\mathbb{E}[X] = 5, so f(E[X])=25f(\mathbb{E}[X]) = 25. The outcomes give f(3)=9f(3) = 9 and f(7)=49f(7) = 49, so E[f(X)]=(9+49)/2=29\mathbb{E}[f(X)] = (9 + 49)/2 = 29. The gap is 29−25=429 - 25 = 4. The variance is E[X2]−(E[X])2=29−25=4\mathbb{E}[X^2] - (\mathbb{E}[X])^2 = 29 - 25 = 4, so the gap is exactly the variance, as it is for f(x)=x2f(x) = x^2 in general.

QuestionA colleague estimates the expected value of 1/X1/X by computing 1/xˉ1/\bar{x} from the sample mean xˉ\bar{x}. Is that estimate too high or too low, and why?Show answer

Too low. The function f(x)=1/xf(x) = 1/x is convex on positive numbers, so Jensen gives 1/E[X]≤E[1/X]1/\mathbb{E}[X] \le \mathbb{E}[1/X]. Plugging the sample mean into 1/x1/x therefore underestimates E[1/X]\mathbb{E}[1/X] on average. A better estimate averages 1/xi1/x_i over the samples directly.

QuestionYou are fitting a model whose loss is a log of a sum over latent variables, and you want a tractable lower bound on the log likelihood. Explain where Jensen's inequality enters and why the bound is valid for any choice of the auxiliary distribution.Show answer

Writing the log of a sum as the log of an expectation under qq makes the function log⁡\log act on an average. Because log⁡\log is concave, Jensen says the log of the average is at least the average of the log. That gives a lower bound that depends on qq only through the expectation. Since the inequality holds for every qq, any choice gives a valid bound, and you then choose qq to make the bound as tight as possible.

Sources