Module 4. Jensen's inequality: when averaging meets nonlinearity
How to read this module. Sections 4.1–4.5 form the main route. A single theorem will run through three familiar ML constructions: ensembles, the ELBO, and the contraction of KL under coarse-graining. The mathematical deepening on the Jensen gap and Bregman divergence can wait for a second pass. To move on to Module 5, it is enough to understand the direction of the inequality, its equality conditions, and two working consequences: ensemble guarantees and the log-sum inequality.
4.1. Average first, or evaluate the loss first?
In machine learning, an average rarely appears alone. It is usually followed or preceded by a nonlinearity: a logarithm, an exponential, a square, softmax, or logsumexp. Once a nonlinear map enters, the order of operations matters.
Consider the shortest possible example. Two models assign the observed class probabilities
Their average NLL is
If we average the probabilities first, we obtain , followed by
Neither original model has improved. We merely changed the order of two operations:
The difference comes from the curvature of . The same pattern reappears in more substantial settings:
- an ensemble mixes predictions before receiving a logarithmic score;
- a latent-variable model sums or integrates over hidden explanations before taking a logarithm;
- a data transformation merges states before we compare the resulting distributions by KL;
- logit averaging passes through the convex
logsumexpfunction.
Each case asks the same question:
Jensen's inequality is a compass for situations in which we try to move an average through a nonlinearity. It tells us the direction of the change; the Jensen gap tells us the price of the swap.
Unless stated otherwise, this module uses natural logarithms. Changing the base only multiplies logarithmic quantities by a positive constant and therefore does not change any inequality direction.
4.2. Jensen's inequality: curvature sets the direction
From a chord to a random variable
Let be convex. A function is convex if, for all and ,
Geometrically, the chord joining two points on the graph lies above the graph. For a concave function, the inequality reverses.
The weighted finite form is
Passing from a finite mixture to a random variable gives Jensen's familiar expectation form. If the required expectations exist and takes values in , then
For concave ,

Four functions will keep returning throughout the course:
| Function | Curvature | What it gives us |
|---|---|---|
| , | strictly convex | a guarantee for probability mixtures |
| , | strictly concave | the ELBO and other lower bounds |
| convex | a guarantee for logit averaging | |
| , | convex | the log-sum inequality and convexity of KL |
The square gives the most familiar special case:
or, equivalently,
A short proof worth seeing once
Assume that is differentiable and convex, and let
A tangent hyperplane to a convex function is a global lower bound:
Take expectations:
The linear term vanishes precisely because the tangent is taken at the mean . That is the entire mechanism. In the general nondifferentiable case, a supporting subgradient plays the same role; the differentiable version is enough for the ML applications below.
When does the inequality become an equality?
If is strictly convex on the convex hull of the essential support of , then
is possible only when is almost surely constant. For a finite mixture with positive weights, all arguments that actually participate in the mixture must coincide.
For a function that is convex but not strictly convex, nontrivial equality cases are possible—for example, when the support lies entirely in a region on which the function is affine.
This is not merely a technical footnote. It answers a practical question: when does averaging actually change anything? If the models provide the same relevant argument, Jensen creates no gain; if their arguments differ and the function bends between them, the gap is nonzero.
Mathematical deepening: the gap, curvature, and Bregman divergence
For convex , define the Jensen gap
If is scalar, concentrated near , and is twice differentiable, a Taylor expansion gives the local approximation
In several dimensions, the corresponding expression is
This gives a useful engineering intuition: averaging has an effect only when there is both dispersion in the arguments and curvature in the relevant directions. The slogan “more diverse models always make a better ensemble” is therefore too crude. What matters is diversity in the predictions along directions to which the loss is curved.
For differentiable convex , define the Bregman divergence
Once again the linear term disappears after averaging:
For , the right-hand side is . Thus the Jensen gap can be read as an average curvature-aware displacement from a random argument to its mean. This connection is useful, but not required for the main route.
4.3. Ensembles: an exact guarantee for log loss
Suppose models produce distributions
with weights summing to one. There are at least two natural ways to build an ensemble, and they define different predictive models.
Mixing probabilities
Define the arithmetic mixture
For the observed label , the function is strictly convex, so
This is stronger than an average test-set statement: it holds for every individual example. Averaging it over a dataset preserves the inequality.
Equality holds when all positively weighted models assign the observed class the same probability. Their full distributions may still differ: per-example NLL only sees the coordinate of the observed class.
The formula compares the mixture with the average member, not with the best member. Nor does it automatically promise higher top-1 accuracy, better calibration under every distribution, or robustness to dataset shift. That is not a weakness of the result; it is its exact scope: for fixed predictions, a probability mixture is no worse than the average member under log loss.
Averaging logits
Let
and average logits rather than probabilities:
The cross-entropy loss for class is
logsumexp is convex, and subtracting a linear term preserves convexity. Hence
This ensemble is also no worse than the average member under NLL. Its distribution, however, is not the arithmetic mixture. From the softmax definition,
Thus logit averaging produces a normalized geometric mean, also known as a logarithmic opinion pool.
The distinction has a useful qualitative interpretation.
- An arithmetic mixture tolerates disagreement: one member assigning a high probability can keep a class alive.
- A geometric mixture demands more agreement: a very small probability from one member suppresses the class more strongly.
For finite logits, equality in the logit inequality occurs, in particular, when all models define the same softmax distribution—that is, when their logits differ only by additive constants shared across classes.
Neither method wins universally

Consider two examples with equal weights and class observed.
| Example | Mean member NLL | Probability mixture | Mean logits |
|---|---|---|---|
| , | |||
| , |
The arithmetic mixture wins in the first example; the geometric mixture wins in the second. Both satisfy their Jensen guarantee, but there is no universal ordering between them.
Implementation detail: these are different lines of code
If the models already return logits, compute the probability mixture stably in log space:
import torch
import torch.nn.functional as F
# logits: [num_models, num_classes]
# weights: [num_models], positive and summing to one
# target: scalar class index
log_probs = F.log_softmax(logits, dim=-1)
log_weights = weights.log()
# Arithmetic mixture of probabilities, computed stably.
log_q_mix = torch.logsumexp(
log_probs + log_weights[:, None], dim=0
)
loss_probability_mix = -log_q_mix[target]
# Averaging logits gives a normalized geometric pool.
mean_logits = (weights[:, None] * logits).sum(dim=0)
loss_logit_mix = F.cross_entropy(
mean_logits[None, :],
torch.tensor([target], device=logits.device),
)
Averaging logits is not a numerically convenient substitute for averaging probabilities. It is a different operation and may produce a substantially different prediction.
4.4. The ELBO: the logarithm sits outside hidden explanations
In a latent-variable model, an observation can have many hidden explanations . The marginal likelihood adds them up:
Training requires
but the integral is often intractable. The familiar structural problem has returned: the logarithm is applied after summing over hidden states.
Introduce a variational distribution that is positive wherever . Then
The logarithm is concave, so Jensen reverses the inequality:
The right-hand side is the evidence lower bound, or ELBO:
Here Jensen does more than certify the existence of a bound. It turns a difficult log integral into an expectation that can be estimated with samples from and optimized with stochastic gradients.
The gap is known exactly
Using
we obtain
Therefore

Jensen's equality condition now receives a probabilistic interpretation. The importance weight
must be constant under . This happens exactly when
almost everywhere. In that case and the bound is tight.
A small discrete example
For a fixed , suppose
Then
Take the crude approximation
We have
and
The gap is
which is exactly
The value is not a penalty inserted by hand. It is the exact price of using instead of the true posterior.
The form familiar from VAEs
If the joint model factorizes as
then the ELBO becomes
The first term is often called the reconstruction term, although mathematically it is an expected conditional log likelihood and its exact form depends on the decoder. The second compares the approximate posterior with the prior. It follows from the probabilistic decomposition; it is not an external regularizer attached to the objective after the fact.
The positive result is already strong:
- maximizing the ELBO raises a computable lower bound on ;
- the tightness of the bound is determined by the KL divergence to the true posterior;
- a richer variational family can reduce the best achievable variational gap.
The boundary is equally important. A higher ELBO does not guarantee a global optimum, a useful latent representation for every downstream task, or an easy optimization problem. IWAE, the amortization gap, and posterior collapse belong in Module 9, where the full VAE setting will be available.
4.5. The log-sum inequality: what is lost when states are merged
Suppose and differ over four states, after which we merge those states into two groups. We have discarded detail. Can the KL distinguishability of the distributions increase?
No. The algebraic mechanism behind this fact is the log-sum inequality.
Statement
Let , with
Use the conventions
Then
If and the left-hand side is finite, equality holds exactly when the normalized vectors agree:
for all relevant . Equivalently, the ratios are constant.
Why this is the same nonnegativity of KL
Normalize the sequences:
Then
The log-sum inequality is not a separate trick to memorize next to KL. It is KL nonnegativity after the right normalization.
It can also be derived directly from Jensen's inequality applied to the convex function , with weights . The two proofs expose the same structure from different sides.
Joint convexity of KL: mixing hides the component index
Let be pairs of distributions with weights . For each outcome, apply log-sum to
and then sum over . This gives
This is the joint convexity of KL.
The formula has a useful experimental reading. First choose an index , then draw an observation from either or . While is visible, the average distinguishability is the right-hand side. Hide , leaving only the mixtures, and distinguishability cannot increase. Mixing removed information about the component that produced the observation.
With fixed, we obtain
which is the distribution-level version of the probability-ensemble guarantee.
Coarse-graining cannot increase KL
Let the fine-grained state be replaced by a group label
For each group , apply log-sum to the values and over the fiber . Summing over groups yields

For
we have
Merge the first two states and the last two states:
Now
Distinguishability decreased because we can no longer see how probability mass is arranged inside each group.
The equality condition is particularly informative. KL is preserved exactly when the likelihood ratio
is constant within each group. Equivalently, whenever the group masses are positive,
Once the group is known, the fine-grained state provides no additional evidence for distinguishing from . This is the exact algebraic form of the claim that a compression has lost nothing relevant to this distinction. The same pattern will later lead to the Data Processing Inequality and sufficient statistics.
For an arbitrary stochastic channel, the proof has the same shape: apply log-sum to the terms and . In Module 5 this construction becomes the basis of data processing for mutual information.
4.6. One theorem, four working mechanisms
It is useful to collect the route in one table.
| Where the average appears | Nonlinearity | Jensen's consequence |
|---|---|---|
| model probabilities | mixture NLL is no worse than mean NLL | |
| model logits | logsumexp | mean-logit NLL is no worse than mean NLL |
| hidden explanations | the ELBO lower bound | |
| terms in a sum or states being merged | log-sum, convexity of KL, and contraction of distinguishability |
The important skill is not merely recognizing the word “Jensen” inside another proof. It is asking a transferable question:
What exactly is being averaged, which function is applied after the average, and what is the curvature of that function on the relevant domain?
This question prevents two opposite mistakes. The first is swapping operations as though nonlinearity were linear. The second is seeing convexity and drawing a much broader conclusion about the entire system. Jensen compares specific quantities in a fixed setup; generalization, optimization, and behavior under distribution shift require separate arguments.
The same machinery will return in richer forms:
- in Module 5, log-sum becomes data processing for KL and mutual information;
- in Module 8, equality conditions reappear alongside sufficient statistics and exponential families;
- in Module 9, the ELBO enters VAEs, the Information Bottleneck, and rate–distortion;
- in Module 11, convexity produces variational representations of divergences;
- in Module 12, KL appears as a complexity term in PAC-Bayes.
4.7. What to carry into ML practice
- For convex , and the sign reverses for concave .
- The gap comes from both dispersion in the arguments and curvature of the function. Averaging identical predictions changes nothing.
- Probability mixtures and mean logits define different ensembles. Both beat the average member under NLL, but neither dominates the other universally.
- The ELBO is Jensen applied directly to the log marginal likelihood; its exact gap is .
- Log-sum is a working form of KL nonnegativity. It explains joint convexity and why merging states cannot increase distinguishability.
- Convexity gives an exact local guarantee about specified quantities. It does not replace checks of optimization, generalization, calibration, or target-distribution performance.
4.9. References and primary sources
- Boyd & Vandenberghe, Convex Optimization — convex functions, supporting hyperplanes,
logsumexp, and Jensen's inequality. - Cover & Thomas, Elements of Information Theory — the log-sum inequality, joint convexity of KL, and data processing.
- Kingma & Welling, Auto-Encoding Variational Bayes — the variational lower bound and amortized inference for models with continuous latent variables.
- Lakshminarayanan, Pritzel & Blundell, Simple and Scalable Predictive Uncertainty Estimation Using Deep Ensembles — practical context for averaging probabilistic predictions.
- Bregman, The relaxation method of finding the common point of convex sets and its application to the solution of problems in convex programming — the original Bregman-divergence construction.