Md. Asif Uddin
    Problem I.13.B02

    The bound, its gap and the price of a Gaussian code

    symbolic▲▲▲

    Exact values where possible; six decimal places otherwise.

    STATEMENT

    Derive the ELBO identity, derive the KL divergence of a Gaussian code from the standard normal and its derivatives, and show that the encoder that maximises the ELBO is the posterior.

    GIVEN

    A model p(z) p(x∣z)p(\vec z)\,p(\vec x\mid\vec z) with evidence p(x)>0p(\vec x)>0, and a distribution q(z∣x)q(\vec z\mid\vec x) that is positive exactly where the posterior is. For the last part, the linear model of I.13.B01.

    FIND

    (a) log⁡p(x)−ELBO⁡(q)\log p(\vec x)-\operatorname{ELBO}(q) as a single divergence. (b) KL⁡(N(μ,s2) ∥ N(0,1))\KL(\mathcal N(\mu,s^2)\,\|\,\mathcal N(0,1)) and its derivatives with respect to μ\mu and log⁡s2\log s^2. (c) The q=N(m,v)q=\mathcal N(m,v) that maximises the ELBO of the linear model, and the ELBO there.

    STRATEGY

    For (a), write log⁡p(x)\log p(\vec x) as an average under qq of something that does not depend on z\vec z. For (b), average the log-ratio of densities. For (c), differentiate the closed-form ELBO.

    SOLUTION

    (a) Bayes’ rule gives p(x)=p(x,z)/p(z∣x)p(\vec x)=p(\vec x,\vec z)/p(\vec z\mid\vec x) for every z\vec z with positive posterior. The left side does not depend on z\vec z, so it equals its average under qq:

    log⁡p(x)  =Eq[log⁡p(x,z)p(z∣x)]  =Eq[log⁡p(x,z)q(z∣x)]  +Eq[log⁡q(z∣x)p(z∣x)].\begin{aligned} &\log p(\vec x)\\ &\;=\E_q\Big[\log\frac{p(\vec x,\vec z)}{p(\vec z\mid\vec x)}\Big]\\ &\;=\E_q\Big[\log\frac{p(\vec x,\vec z)}{q(\vec z\mid\vec x)}\Big]\\ &\;\quad+\E_q\Big[\log\frac{q(\vec z\mid\vec x)}{p(\vec z\mid\vec x)}\Big]. \end{aligned}

    The first average is the ELBO and the second is KL⁡(q(z∣x) ∥ p(z∣x))\KL(q(\vec z\mid\vec x)\,\|\,p(\vec z\mid\vec x)). Splitting p(x,z)=p(x∣z) p(z)p(\vec x,\vec z)=p(\vec x\mid\vec z)\,p(\vec z) in the first gives the form in (I.13.4), with −KL⁡(q ∥ p(z))-\KL(q\,\|\,p(\vec z)) as its second term. Since a KL divergence is non-negative and zero only for equal distributions, the ELBO is a lower bound, tight exactly at the posterior.

    (b) The log-densities are

    log⁡q(z)=−12log⁡2πs2−(z−μ)22s2,log⁡p(z)=−12log⁡2π−z22.\begin{aligned} \log q(z)&=-\tfrac12\log2\pi s^2\\ &\quad-\frac{(z-\mu)^2}{2s^2},\\ \log p(z)&=-\tfrac12\log2\pi-\frac{z^2}{2}. \end{aligned}

    Under qq, (z−μ)2(z-\mu)^2 averages to s2s^2 and z2z^2 to μ2+s2\mu^2+s^2. Therefore

    KL⁡=−12log⁡s2−12+12(μ2+s2)=12(μ2+s2−1−log⁡s2).\begin{aligned} \KL&=-\tfrac12\log s^2-\tfrac12\\ &\quad+\tfrac12(\mu^2+s^2)\\ &=\tfrac12\big(\mu^2+s^2\\ &\qquad-1-\log s^2\big). \end{aligned}

    This is (I.13.5) for one coordinate. With ℓ=log⁡s2\ell=\log s^2, so that s2=eℓs^2=e^\ell,

    ∂KL⁡∂μ=μ,∂KL⁡∂ℓ=12(s2−1).\begin{aligned} \frac{\partial\KL}{\partial\mu}&=\mu,\\[2pt] \frac{\partial\KL}{\partial\ell}&=\tfrac12(s^2-1). \end{aligned}

    Both vanish only at μ=0\mu=0, s2=1s^2=1. For example KL⁡(N(1,0.25) ∥ N(0,1))=12(0.25+1.386294)=0.818147\KL(\mathcal N(1,0.25)\,\|\,\mathcal N(0,1))=\tfrac12(0.25+1.386294)=0.818147.

    (c) From B01, with a=wTxa=\vec w^{\mathsf T}\vec x and r=∥w∥2r=\|\vec w\|^2, the ELBO of N(m,v)\mathcal N(m,v) is, up to a constant,

    −−2am+(m2+v)r2σ2−12(m2+v−1−log⁡v).\begin{aligned} &-\frac{-2am+(m^2+v)r}{2\sigma^2}\\ &\quad-\tfrac12\big(m^2+v-1-\log v\big). \end{aligned}

    Setting the derivative in mm to zero gives a/σ2=m(r/σ2+1)a/\sigma^2=m(r/\sigma^2+1), so m=a/(r+σ2)m=a/(r+\sigma^2). In vv it gives −r/2σ2−12+1/(2v)=0-r/2\sigma^2-\tfrac12+1/(2v)=0, so v=σ2/(r+σ2)v=\sigma^2/(r+\sigma^2). These are the posterior’s mean and variance in (I.13.2). For the point (1,2)(1,2) they are 0.8660250.866025 and 0.250.25, and by (a) the ELBO there equals log⁡p(x)=−3.531024\log p(\vec x)=-3.531024.

    Answer

    (a) log⁡p(x)−ELBO⁡(q)=KL⁡(q(z∣x) ∥ p(z∣x))\log p(\vec x)-\operatorname{ELBO}(q)=\KL(q(\vec z\mid\vec x)\,\|\,p(\vec z\mid\vec x)). (b) 12(μ2+s2−1−log⁡s2)\tfrac12(\mu^2+s^2-1-\log s^2), with derivatives μ\mu and 12(s2−1)\tfrac12(s^2-1). (c) m=a/(r+σ2)m=a/(r+\sigma^2) and v=σ2/(r+σ2)v=\sigma^2/(r+\sigma^2), the exact posterior, where the ELBO is −3.531024-3.531024.

    Check — sanity

    The KL in (b) is zero at the prior and positive elsewhere: for fixed ss it grows with μ2\mu^2, and s2−1−log⁡s2≥0s^2-1-\log s^2\geq0 with equality only at s2=1s^2=1, by log⁡t≤t−1\log t\leq t-1.

    Where this breaks

    If qq is positive somewhere the posterior is zero, the KL in (a) is infinite and the ELBO is minus infinity. If qq is zero somewhere the posterior is not, (a) still holds but qq cannot be the posterior, so the gap never closes. A Gaussian prior and decoder make the posterior positive everywhere, and neither case arises. If the decoder variance also depends on zz, (c) has no closed form.

    Variation

    For a code with kk independent coordinates the KL is a sum of kk copies of (b). At the posterior of the linear model the price is 12log⁡((r+σ2)/σ2)+12(m2+v−1)\tfrac12\log\big((r+\sigma^2)/\sigma^2\big)+\tfrac12(m^2+v-1). At the maximum-likelihood w\vec w the second part averages to zero over the data, and at every corner of the rectangle it is exactly zero, leaving 12log⁡4=log⁡2\tfrac12\log4=\log2.

    Draws on