Practice / Probability for ML
Jensen's inequality and convexity
Before you start
One inequality sits under more of machine learning than any other: for a convex function, the function of an average is at most the average of the function. It is why the variance is nonnegative, why the KL divergence is nonnegative, why the evidence lower bound is a lower bound, why log-sum-exp is a smooth maximum, and why an ensemble's average prediction loses no more than its members do on average. These ten problems build it from the second-derivative test and the tangent line, prove the finite version from the definition alone, and then spend it: on the AM–GM inequality, on the moment generating function of a Gaussian, on the ELBO and the importance-weighted bound that tightens it, on the softmax Hessian, on the convexity of the logistic and hinge losses, on the entropy ceiling, and on the ambiguity decomposition. The five mistakes at the end are the ones that produce a true-looking inequality that is false: composing convex functions, applying Jensen to a sum instead of an average, replacing the mean of a reciprocal by the reciprocal of the mean, calling a flat direction strictly convex, and reading convexity off a single point.
- A function on an interval is convex if for all and : the chord between two points of the graph lies on or above the graph. It is strictly convex if the inequality is strict whenever and , and concave if is convex. The same definition applies to on with vectors .
- Jensen's inequality: for a convex and a random variable with finite mean, ; for concave the inequality reverses. For a finite distribution with weights , , it reads . The difference is the Jensen gap.
- is the natural logarithm. is the logistic sigmoid with , and the softplus is , with (the activation-functions page).
- For , is the log-sum-exp and has ; is the all-ones vector and the diagonal matrix with on its diagonal. A symmetric matrix is positive semidefinite (PSD) if for every , and a twice-differentiable on is convex exactly when its Hessian is PSD everywhere (the gradients-and-Hessians page).
- and are distributions over outcomes with ; the entropy is and (the entropy page). For a latent-variable model, is the joint, the evidence, a distribution the model chooses, and the importance weight.
Builds on: Entropy, cross-entropy and KL divergence, Variance, covariance and correlation
Problems
- ·
Using the second derivative, decide which of , , and are convex and which are concave on .
- ··
Let be twice differentiable with on an interval. Show that the tangent line lies below the graph, , and deduce Jensen's inequality for any random variable taking values in the interval with finite mean.
- ··
Without derivatives, show from the definition of convexity that for weights with , by induction on . Why does this version apply to , which has no second derivative?
- ··
From Jensen's inequality for , prove the weighted AM–GM inequality for and weights , . Evaluate both sides for with equal weights and for with equal weights.
- ··
Show that each of the following is a Jensen gap and compute it: (a) for any with finite variance; (b) for , by first showing ; (c) for equal to or with probability each.
- ···
For a latent-variable model with and , show that , the evidence lower bound. Then, for independent draws , show that : averaging the weights inside the log tightens the bound.
- ···
For , show that , that , and that is PSD, so is convex.
- ··
Show from the definition that (a) if is convex then is convex, and (b) if are convex then is convex. Use (a) to show that the logistic loss is convex in for fixed and , and compute its Hessian. Use (b) for the hinge loss .
- ··
Use Jensen's inequality to show that (a) for every distribution over outcomes, with equality for the uniform distribution, and (b) .
- ···
An ensemble of models makes predictions for a target , and the ensemble predicts the average . (a) Show that for any loss that is convex in , . (b) For the squared loss, show the exact identity .
Answers
- , and are convex on (second derivatives , , ); and are concave (second derivatives and )
- , and
- ; for : ; for :
- (a) the gap is ; (b) and the gap is ; (c) , gap
- , the right-hand side being the ELBO
- ; ; is PSD, so is convex
- and are convex when , are; is convex with , ; the hinge loss is a maximum of two affine functions, hence convex
- with equality for the uniform distribution;
- for convex ; for the squared loss,
Worked solutions
Problem 1
Using the second derivative, decide which of , , and are convex and which are concave on .
- .Differentiate twice; the exponential is its own derivative and is positive everywhere.
- .The derivative of is , and the derivative of is .
- .Product rule for the first derivative: .
- .Power rule twice; the second derivative is negative for every .
- , and are convex on (second derivatives , , ); and are concave (second derivatives and )A nonnegative second derivative on an interval means the slope never decreases, so the graph bends upwards and every chord lies above it; Problem 2 makes that precise through the tangent line. The same test in several variables asks whether the Hessian is PSD (Problem 7). Concavity of is the fact behind Problems 4, 6 and 9, and convexity of is the fact behind the entropy being concave.
Problem 2
Let be twice differentiable with on an interval. Show that the tangent line lies below the graph, , and deduce Jensen's inequality for any random variable taking values in the interval with finite mean.
- for some between and .Taylor's theorem with the Lagrange remainder at order one (the Taylor-series page).
- , so . on the interval and a square is nonnegative.
- Put and : for every value of .Step 2 holds for every pair of points in the interval, and lies in the interval because it is an average of values in it.
- .Expectation preserves inequalities and is linear; and are constants, and .
- , and The whole of Jensen is the supporting line at the mean: the graph sits above its tangent there, and the tangent's expectation is exactly because the linear term averages to zero. The argument never used what the distribution of is, so it covers sums, integrals and anything in between. If strictly, step 1's remainder is zero only when , so the gap is zero only when with probability one: equality in Jensen for a strictly convex means is constant, which is what pins the ELBO's equality case to the posterior in Problem 6.
Problem 3
Without derivatives, show from the definition of convexity that for weights with , by induction on . Why does this version apply to , which has no second derivative?
- : and both sides are . : with .The two-point case is the definition of convexity with .
- Suppose the claim holds for points, and take points with . Write with .Factor out of the first terms; the new weights are nonnegative and sum to , so is a convex combination of . If there is nothing to prove.
- .The two-point case with applied to the points and .
- , so .The induction hypothesis on the points with the new weights; multiply through by .
- Steps 3 and 4 together. Only the chord definition was used, so the result holds for every convex function, differentiable or not. is convex because is the triangle inequality, so for any finite distribution, and the same chord argument gives for vectors. The induction fails if the weights do not sum to one, which is Mistake 2.
Problem 4
From Jensen's inequality for , prove the weighted AM–GM inequality for and weights , . Evaluate both sides for with equal weights and for with equal weights.
- is concave on , so .Problem 1 for the concavity; Problem 3's finite Jensen with the inequality reversed for a concave function.
- . and a sum of logs is the log of the product.
- , so . is increasing, so it preserves the inequality.
- , : .The geometric mean is and the arithmetic mean is .
- , : ..
- ; for : ; for : Equality needs the all equal, because is strictly concave (Problem 2's equality case). With and equal weights the inequality is , which is rearranged. The same step from the mean of logs to the log of the mean is the one taken, in the other direction, in Problem 6.
Problem 5
Show that each of the following is a Jensen gap and compute it: (a) for any with finite variance; (b) for , by first showing ; (c) for equal to or with probability each.
- (a) is convex, so , and the gap is .; the variance page's .
- (b) Write with : .A Gaussian is a shifted and scaled standard normal; is a constant.
- .Complete the square: ; the remaining integral is the density of , which integrates to .
- , and the gap is . is convex (Problem 1) and ; for .
- (c) is convex on since ; and .Each value has probability ; .
- (a) the gap is ; (b) and the gap is ; (c) , gap Three faces of one inequality. (a) says the variance is a Jensen gap, and Problem 7 reuses it to prove a Hessian is PSD. (b) is the moment generating function of a Gaussian; it is why the mean of a log-normal variable is , not , and why exponentiating a predicted log-price under-estimates the expected price by the factor . (c) is the harmonic-mean gap: the expected reciprocal exceeds the reciprocal of the expectation, which is Mistake 3 when it is forgotten.
Problem 6
For a latent-variable model with and , show that , the evidence lower bound. Then, for independent draws , show that : averaging the weights inside the log tightens the bound.
- .The cancels; summing the joint over gives the marginal.
- .Jensen for the concave (Problem 1), with the inequality reversed as for every concave function.
- Let . Then , so .Each has mean by step 1 and the mean of an average is the average of the means; then Jensen again on the random variable .
- for every draw.Problem 3's finite Jensen for the concave with equal weights : the log of an average is at least the average of the logs.
- Taking expectations in step 4: .Expectation preserves inequalities; the are identically distributed, so each term has the same mean.
- , the right-hand side being the ELBOThe ELBO is the case. Equality in step 2 needs constant under , which by Problem 2's equality case means ; the ELBO page's Problem 2 computes the gap as . The -sample bound is tighter because is less spread than a single (its variance is ), so its Jensen gap is smaller; as , and the bound closes, whatever is. This is the importance-weighted bound, and the check verifies both inequalities on a model small enough to enumerate every pair of draws.
Problem 7
For , show that , that , and that is PSD, so is convex.
- Let . Then .The sum contains the term and all terms are positive; each of the terms is at most .
- .Take logs of step 1, which preserves the order; .
- .Chain rule: the derivative of is , and only the -th term of the sum depends on .
- , so .The softmax Jacobian from the softmax page; the Hessian is the Jacobian of the gradient.
- , where takes the value with probability .Expand the quadratic form; is a probability vector, so the two sums are the second moment and the squared mean of under , and their difference is a variance, which is Problem 5(a)'s Jensen gap.
- ; ; is PSD, so is convexLog-sum-exp is a smooth maximum, never below the true maximum and never more than above it, which is why cross-entropy with softmax, , is a convex function of the logits: a convex function minus a linear one. The PSD proof is Jensen's gap in disguise, and the bound of step 2 is tight at both ends: gives and gives . The Hessian is only semidefinite: gives , which Mistake 4 overlooks.
Problem 8
Show from the definition that (a) if is convex then is convex, and (b) if are convex then is convex. Use (a) to show that the logistic loss is convex in for fixed and , and compute its Hessian. Use (b) for the hinge loss .
- (a) .An affine map sends a convex combination of points to the same convex combination of their images: .
- .Convexity of at the points and .
- (b) for every , so the maximum over of the left side, , obeys the same bound.Convexity of each , then pointwise; a bound that holds for every holds for the largest.
- with , and , so is convex and is composed with the affine map : convex by (a). (the activation-functions page); makes the product positive; (a) with () and .
- and .Chain rule through : , and in the second derivative. is PSD since , in agreement with (a).
- with and , both affine, hence convex; so is convex by (b).An affine function satisfies the chord inequality with equality.
- and are convex when , are; is convex with , ; the hinge loss is a maximum of two affine functions, hence convexThese two rules, with nonnegative sums (a sum of convex functions is convex because the chord inequalities add), build almost every convex loss in machine learning from a few one-dimensional pieces: the logistic loss is softplus after an affine map, the hinge loss and ReLU are maxima of affine maps, the squared loss is after an affine map, and a mean over a dataset is a nonnegative sum. What the rules do not include is composition of two convex functions (Mistake 1) or products.
Problem 9
Use Jensen's inequality to show that (a) for every distribution over outcomes, with equality for the uniform distribution, and (b) .
- (a) , where is an outcome drawn from .; a -weighted sum is an expectation under .
- .Jensen for the concave ; the expectation of is a sum of ones.
- For : .Direct evaluation; is the constant , so there is no Jensen gap.
- (b) .Flip the fraction and pull out the sign: .
- .Jensen for the concave , multiplied by , which flips to ; the cancel and sums to one.
- with equality for the uniform distribution; Both are Jensen on with a cleverly chosen random variable, and , whose expectations under are known constants, and . Equality in (b) needs constant, and a constant ratio between two distributions is , so : Gibbs' inequality, which the entropy page proves instead from , the tangent line of at , which is Problem 2's supporting line at the point where sits.
Problem 10
An ensemble of models makes predictions for a target , and the ensemble predicts the average . (a) Show that for any loss that is convex in , . (b) For the squared loss, show the exact identity .
- (a) .Problem 3's finite Jensen with equal weights , applied to with held fixed.
- (b) , so .Add and subtract , then expand the square.
- .Average step 2 over ; does not depend on .
- , so the cross term vanishes.The deviations from a mean sum to zero.
- for convex ; for the squared loss, The ensemble's error is the members' average error minus the members' spread, the ambiguity decomposition: averaging can only help, and it helps most when the members disagree. (b) is Problem 5(a) applied across models, with the spread term the variance of the predictions, so the Jensen gap for the squared loss is exactly that variance. For the log loss with probabilities, (a) says the averaged probability scores at least as well on average as its members; the inequality is strict unless all members agree. This is the inequality behind bagging and model averaging, and it says nothing about any single member, which may be better than the ensemble; the bias–variance page quantifies how much averaging reduces variance.
Where this goes wrong
1. Composing two convex functions
Convexity survives affine maps inside and nonnegative sums and maxima outside, and the natural next guess is that it survives any composition.
- and are both convexRight so far: both have positive second derivatives.
- “A convex function of a convex function is convex.”The rule that causes the mistake: Problem 8(a) allows an affine inner function, not a convex one.
- is convexIts second derivative is , negative on , and it has two minima at with a local maximum at : the chord from to runs along while the graph rises to between them. Composition preserves convexity only when the outer function is also nondecreasing, which is not on ; that is why in Problem 8 is safe (the inner map is affine) and why is not convex (sigmoid is neither convex nor monotone-convex), so the squared error of a logistic unit is not a convex function of .
2. Jensen applied to a sum instead of an average
The finite inequality says of a combination is at most the combination of , and the weights are easy to forget.
- for , Right so far: Problem 3.
- “The are just positive weights, so any positive weights will do.”The habit that causes the mistake: Problem 3's induction needs at every stage; with and the rescaled , the normalisation is what makes a point the chord inequality can be applied to.
- for convex With and : . For the true statement is : the inequality holds only after the sum is rewritten as a multiple of an average. The fix is the same in every case: pull out the total weight, apply Jensen to the normalised combination, and keep the factor. in Problem 7 is the one place a sum appears without weights, and there the inequality that survives is the bound , not a Jensen inequality.
3. The expected reciprocal taken to be the reciprocal of the expectation
The mean of a ratio comes up constantly: a mean rate, a mean waiting time, a mean of importance weights, and the shortcut takes expectations inside.
- for with equal probabilityRight so far: Problem 5(c).
- “Expectation is linear, so it passes through .”The habit that causes the mistake: linearity passes expectation through , and is not linear.
- (Problem 5), and the direction is forced by Jensen: is convex on , so always, with equality only for a constant . The same error in the other direction treats as , which would make the ELBO equal to for every (Problem 6), and treats as , which drops the of Problem 5(b). The estimators page on importance sampling meets the first form again in the self-normalised estimator, whose denominator is a mean of weights.
4. Calling log-sum-exp strictly convex
The Hessian has positive diagonal entries, and the quadratic form is a variance, which feels positive.
- Right so far: Problem 7, step 5.
- “A variance is positive, so the Hessian is positive definite and is strictly convex.”The shortcut that causes the mistake: a variance is zero when the variable is constant.
- , so is strictly convex and cross-entropy with softmax has a unique minimising logit vector gives constant and , so is in the Hessian's null space at every : along the function is linear, , and the softmax does not change at all (the softmax page's shift invariance). Cross-entropy is constant along , so every minimiser comes with a whole line of them, and in a linear model the Hessian in is singular for the same reason. The positive-definite-matrices page has the general test: PSD with a null vector is not PD, however positive the diagonal looks.
5. Checking the second derivative at one point
The second-derivative test for a minimum is local, and the same test is reached for when convexity is wanted.
- has , and Right so far: a correct second derivative.
- “, so is convex.”The habit that causes the mistake: Problem 1's test asks for on the whole interval, and Problem 2's tangent-line argument needs at an unknown point between and .
- is convex, so for every : the graph is a double well with minima at and a local maximum at . Take with probability each: , , but , so the claimed inequality reads . One positive second derivative certifies a local minimum at a critical point, not convexity; convexity is a statement about every chord, and it fails as soon as is negative anywhere between two points. The same error in several variables accepts a PSD Hessian at the current iterate as proof that the loss is convex, which for any neural network with a hidden layer it is not.
Print this set: jensens-inequality-and-convexity.pdf (problems, answers, and worked solutions on separate pages).