VKVlad KulikovPublic / lectures
Information Theory for ML4 / 16

Module 4. Jensen's inequality: when averaging meets nonlinearity

Try each exercise first. Expand worked answers or project guidance when needed; their open state is shared between RU/EN.

Download course files (.zip) 33.9 KB

The archive preserves the folder structure of nine Python solution files shared by RU/EN.

Exercise 1. See the Jensen gap

Let XX take values 00 and 22, each with probability 1/21/2.

  1. For ϕ(x)=x2\phi(x)=x^2, compute ϕ(EX)\phi(\mathbb E X) and Eϕ(X)\mathbb E\phi(X).

  2. Show that their difference equals Var⁡(X)\operatorname{Var}(X).

  3. Repeat the calculation for ϕ(x)=ex\phi(x)=e^x.

  4. Explain why both inequalities are strict.

Show answer and solution

Let

P(X=0)=P(X=2)=12.P(X=0)=P(X=2)=\frac12.

Then

EX=1.\mathbb E X=1.

1. The square

For ϕ(x)=x2\phi(x)=x^2,

ϕ(EX)=12=1,\phi(\mathbb E X)=1^2=1,

while

Eϕ(X)=12⋅02+12⋅22=2.\mathbb E\phi(X) = \frac12\cdot0^2+\frac12\cdot2^2 =2.

Hence

E[X2]−(EX)2=2−1=1.\mathbb E[X^2]-(\mathbb E X)^2=2-1=1.

2. Relation to variance

By definition,

Var⁡(X)=E[X2]−(EX)2=1.\operatorname{Var}(X) = \mathbb E[X^2]-(\mathbb E X)^2 =1.

Thus the Jensen gap for x2x^2 is exactly the variance.

3. The exponential

For ϕ(x)=ex\phi(x)=e^x,

ϕ(EX)=e≈2.71828,\phi(\mathbb E X)=e\approx2.71828,

whereas

E[eX]=1+e22≈4.19453.\mathbb E[e^X] = \frac{1+e^2}{2} \approx4.19453.

The gap is approximately

4.19453−2.71828≈1.47625.4.19453-2.71828 \approx1.47625.

4. Why both inequalities are strict

Both x2x^2 on R\mathbb R and exe^x are strictly convex. Since XX is not almost surely constant, equality in Jensen is impossible.

Exercise 2. Two ways to build an ensemble

Two models predict

q1=(0.7,0.2,0.1),q2=(0.2,0.3,0.5),q_1=(0.7,0.2,0.1), \qquad q_2=(0.2,0.3,0.5),

and the observed class is the first one.

  1. Compute the average NLL of the members.

  2. Compute the NLL of the arithmetic probability mixture.

  3. Construct the normalized geometric mean and compute its NLL.

  4. Verify both Jensen inequalities.

  5. Explain why the result does not imply higher top-1 accuracy on a dataset.

Show answer and solution

We have

q1=(0.7,0.2,0.1),q2=(0.2,0.3,0.5),q_1=(0.7,0.2,0.1), \qquad q_2=(0.2,0.3,0.5),

equal weights, and target class y=1y=1.

1. Average member NLL

L1=−ln⁡0.7≈0.356675,L_1=-\ln0.7\approx0.356675, L2=−ln⁡0.2≈1.609438.L_2=-\ln0.2\approx1.609438.

Therefore

Lˉ=L1+L22≈0.983056.\bar L = \frac{L_1+L_2}{2} \approx0.983056.

2. Arithmetic probability mixture

qmix=q1+q22=(0.45,0.25,0.30).q_{\mathrm{mix}} = \frac{q_1+q_2}{2} = (0.45,0.25,0.30).

For the target class,

Lmix=−ln⁡0.45≈0.798508,L_{\mathrm{mix}} = -\ln0.45 \approx0.798508,

so

0.798508<0.983056.0.798508<0.983056.

3. Normalized geometric mean

The unnormalized weights are

rk=q1(k)q2(k).r_k=\sqrt{q_1(k)q_2(k)}.

Thus

r=(0.14,0.06,0.05)≈(0.374166,0.244949,0.223607).r = (\sqrt{0.14},\sqrt{0.06},\sqrt{0.05}) \approx (0.374166,0.244949,0.223607).

After normalization,

qgeom≈(0.443997,0.290664,0.265339).q_{\mathrm{geom}} \approx (0.443997,0.290664,0.265339).

Its NLL is

Lgeom=−ln⁡0.443997≈0.811938,L_{\mathrm{geom}} = -\ln0.443997 \approx0.811938,

again below the average member NLL:

0.811938<0.983056.0.811938<0.983056.

4. What has been verified

For probability averaging, Jensen is applied to −log⁡-\log:

−log⁡q1(y)+q2(y)2≤−log⁡q1(y)−log⁡q2(y)2.-\log\frac{q_1(y)+q_2(y)}2 \le \frac{-\log q_1(y)-\log q_2(y)}2.

For logit averaging, we use convexity of softmax cross-entropy in the logits.

5. Why dataset accuracy does not follow

The inequalities concern NLL of the observed class. Accuracy depends only on whether the argmax equals the label, while NLL uses the entire assigned probability. An ensemble may improve NLL without changing accuracy, and it may move some argmax decisions in either direction.

Exercise 3. The ELBO in a Gaussian model

Let

z∼N(0,1),x∣z∼N(z,1),z\sim\mathcal N(0,1), \qquad x\mid z\sim\mathcal N(z,1),

and suppose x=1x=1 is observed.

  1. Find the marginal distribution p(x)p(x).

  2. Find the exact posterior p(z∣x)p(z\mid x).

  3. For q(z∣x)=N(0,1)q(z\mid x)=\mathcal N(0,1), compute

    DKL(q(z∣x)∥p(z∣x)).D_{\mathrm{KL}}(q(z\mid x)\|p(z\mid x)).
  4. Use the exact gap to obtain the ELBO.

  5. State which choice of qq makes the bound tight.

Show answer and solution

The model is

z∼N(0,1),x∣z∼N(z,1).z\sim\mathcal N(0,1), \qquad x\mid z\sim\mathcal N(z,1).

1. Marginal distribution

Write

x=z+ε,ε∼N(0,1),x=z+\varepsilon, \qquad \varepsilon\sim\mathcal N(0,1),

with independent zz and ε\varepsilon. Hence

p(x)=N(0,2).p(x)=\mathcal N(0,2).

At x=1x=1,

log⁡p(1)=−12log⁡(4π)−14≈−1.515512.\log p(1) = -\frac12\log(4\pi)-\frac14 \approx-1.515512.

2. Exact posterior

The posterior precision is

1+1=2,1+1=2,

so its variance is 1/21/2. Its mean is

1⋅x1+1=12.\frac{1\cdot x}{1+1} = \frac12.

Therefore

p(z∣x=1)=N(12,12).p(z\mid x=1) = \mathcal N\left(\frac12,\frac12\right).

3. KL for q=N(0,1)q=\mathcal N(0,1)

For univariate Gaussians,

DKL(N(μq,σq2)∥N(μp,σp2))=12[σq2σp2+(μp−μq)2σp2−1+ln⁡σp2σq2].D_{\mathrm{KL}} \bigl( \mathcal N(\mu_q,\sigma_q^2) \| \mathcal N(\mu_p,\sigma_p^2) \bigr) = \frac12 \left[ \frac{\sigma_q^2}{\sigma_p^2} + \frac{(\mu_p-\mu_q)^2}{\sigma_p^2} -1 + \ln\frac{\sigma_p^2}{\sigma_q^2} \right].

Substituting

μq=0,σq2=1,μp=12,σp2=12\mu_q=0,\quad \sigma_q^2=1,\quad \mu_p=\frac12,\quad \sigma_p^2=\frac12

gives

DKL(q∥p)=12[2+(1/2)21/2−1+ln⁡12]=12[32−ln⁡2]≈0.403426.\begin{aligned} D_{\mathrm{KL}}(q\|p) &= \frac12 \left[ 2+\frac{(1/2)^2}{1/2}-1+\ln\frac12 \right]\\ &= \frac12 \left[ \frac32-\ln2 \right]\\ &\approx0.403426. \end{aligned}

4. ELBO

Using the exact gap,

log⁡p(x)−ELBO⁡(x)=DKL(q∥p(z∣x)).\log p(x)-\operatorname{ELBO}(x) = D_{\mathrm{KL}}(q\|p(z\mid x)).

Therefore

ELBO⁡(1)=−1.515512−0.403426≈−1.918939.\operatorname{ELBO}(1) = -1.515512-0.403426 \approx-1.918939.

5. Tightness

Choose

q(z∣x)=p(z∣x)=N(12,12).q(z\mid x) = p(z\mid x) = \mathcal N\left(\frac12,\frac12\right).

Then the KL gap is zero.

Exercise 4. Log-sum and coarse-graining

For

P=(0.4,0.3,0.2,0.1),Q=(0.1,0.2,0.3,0.4),P=(0.4,0.3,0.2,0.1), \qquad Q=(0.1,0.2,0.3,0.4),

merge the outcomes into groups {1,2}\{1,2\} and {3,4}\{3,4\}.

  1. Compute D(P∥Q)D(P\|Q).

  2. Compute the KL divergence between the grouped distributions.

  3. Show how the inequality follows from two applications of log-sum.

  4. State the equality condition in terms of the conditional distributions within each group.

Show answer and solution

1. KL before grouping

D(P∥Q)=0.4ln⁡0.40.1+0.3ln⁡0.30.2+0.2ln⁡0.20.3+0.1ln⁡0.10.4≈0.456435 nats.\begin{aligned} D(P\|Q) &= 0.4\ln\frac{0.4}{0.1} + 0.3\ln\frac{0.3}{0.2}\\ &\quad+ 0.2\ln\frac{0.2}{0.3} + 0.1\ln\frac{0.1}{0.4}\\ &\approx0.456435\ \text{nats}. \end{aligned}

2. KL after grouping

The grouped distributions are

Pˉ=(0.7,0.3),Qˉ=(0.3,0.7).\bar P=(0.7,0.3), \qquad \bar Q=(0.3,0.7).

Thus

D(Pˉ∥Qˉ)=0.7ln⁡0.70.3+0.3ln⁡0.30.7≈0.338919 nats.\begin{aligned} D(\bar P\|\bar Q) &= 0.7\ln\frac{0.7}{0.3} + 0.3\ln\frac{0.3}{0.7}\\ &\approx0.338919\ \text{nats}. \end{aligned}

Hence

D(Pˉ∥Qˉ)<D(P∥Q).D(\bar P\|\bar Q) < D(P\|Q).

3. Two log-sum applications

For the first group,

0.4ln⁡0.40.1+0.3ln⁡0.30.2≥0.7ln⁡0.70.3.0.4\ln\frac{0.4}{0.1} + 0.3\ln\frac{0.3}{0.2} \ge 0.7\ln\frac{0.7}{0.3}.

For the second,

0.2ln⁡0.20.3+0.1ln⁡0.10.4≥0.3ln⁡0.30.7.0.2\ln\frac{0.2}{0.3} + 0.1\ln\frac{0.1}{0.4} \ge 0.3\ln\frac{0.3}{0.7}.

Adding the inequalities proves the result.

4. Equality condition

Within each group, the conditional distributions under PP and QQ must match:

P(X=i∣X∈G)=Q(X=i∣X∈G)P(X=i\mid X\in G) = Q(X=i\mid X\in G)

for every relevant i∈Gi\in G.

Thus, once the group is known, the finer state must provide no additional ability to distinguish PP from QQ.

Bonus. The Jensen gap as a Bregman divergence

Prove that, for differentiable convex ϕ\phi,

Eϕ(X)−ϕ(EX)=EDϕ(X,EX).\mathbb E\phi(X)-\phi(\mathbb E X) = \mathbb E D_\phi(X,\mathbb E X).

Check the identity for ϕ(x)=x2/2\phi(x)=x^2/2.

Show answer and solution

Let

m=EX.m=\mathbb E X.

By definition,

Dϕ(X,m)=ϕ(X)−ϕ(m)−∇ϕ(m)⊤(X−m).D_\phi(X,m) = \phi(X)-\phi(m)-\nabla\phi(m)^\top(X-m).

Taking expectations,

EDϕ(X,m)=Eϕ(X)−ϕ(m)−∇ϕ(m)⊤(EX−m)=Eϕ(X)−ϕ(m).\begin{aligned} \mathbb E D_\phi(X,m) &= \mathbb E\phi(X)-\phi(m) -\nabla\phi(m)^\top(\mathbb E X-m)\\ &= \mathbb E\phi(X)-\phi(m). \end{aligned}

Since m=EXm=\mathbb E X,

EDϕ(X,EX)=Eϕ(X)−ϕ(EX).\mathbb E D_\phi(X,\mathbb E X) = \mathbb E\phi(X)-\phi(\mathbb E X).

For ϕ(x)=x2/2\phi(x)=x^2/2,

Dϕ(x,m)=x22−m22−m(x−m)=12(x−m)2.D_\phi(x,m) = \frac{x^2}{2}-\frac{m^2}{2}-m(x-m) = \frac12(x-m)^2.

Therefore

EDϕ(X,EX)=12Var⁡(X),\mathbb E D_\phi(X,\mathbb E X) = \frac12\operatorname{Var}(X),

which matches the Jensen gap for x2/2x^2/2.