贝叶斯推断中的边际似然-变分推断(公式推导)

【投稿赢 iPhone 17】「我的第一个开源项目」故事征集:用代码换C位出道! 10w+人浏览 1.7k人参与

贝叶斯推断中的边际似然-变分推断

边际似然的背景

在贝叶斯框架下,我们有:

p(θ∣D)=p(D∣θ)p(θ)p(D)(1)p(\theta|\mathcal{D})=\frac{p(\mathcal{D}|\theta)p(\theta)}{p(\mathcal{D})}\qquad{(1)}p(θD)=p(D)p(Dθ)p(θ)(1)

其中分母:

p(D)=∫p(D∣θ)p(θ)dθp(\mathcal{D})=\int p(\mathcal{D}|\theta)p(\theta)d\thetap(D)=p(Dθ)p(θ)dθ

是边际似然,反应模型对数据的解释能力。但是这个积分通常是高维的、没有解析形式,因此我们需要:

  • 采样近似(如MCMC)
  • 优化近似(如Variational Inference)

变分推断基本思想

我们可以使用一个可计算的分布q(θ)q(\theta)q(θ)来近似后验分布p(θ∣D)p(\theta|\mathcal{D})p(θD)

我们希望q(θ)q(\theta)q(θ)尽量接近真实后验,也就是最小化KL散度:

KL(q(θ)∣∣p(θ∣D))=∫q(θ)log⁡q(θ)p(θ∣D)dθ\mathrm{KL}(q(\theta)||p(\theta|\mathcal{D}))=\int q(\theta)\log\frac{q(\theta)}{p(\theta|\mathcal{D})}d\thetaKL(q(θ)∣∣p(θD))=q(θ)logp(θD)q(θ)dθ

由于p(D)p(\mathcal{D})p(D)是常数(与θ\thetaθ无关),我们无法直接最小化它,但是可以通过下式进行变形:

log⁡p(D)=L(q)+KL(q(θ)∣∣p(θ∣D))(2)\log p(\mathcal{D})=\mathcal{L}(q)+\mathrm{KL}(q(\theta)||p(\theta|\mathcal{D}))\quad\quad{(2)}logp(D)=L(q)+KL(q(θ)∣∣p(θD))(2)

因此:

L(q)=∫q(θ)log⁡p(D,θ)q(θ)dθ\mathcal{L}(q)=\int q(\theta)\log\frac{p(\mathcal{D},\theta)}{q(\theta)}d\thetaL(q)=q(θ)logq(θ)p(D,θ)dθ是证据下限(Evidence Lower Bound, ELBO)。因此最大化ELBO⇔\Leftrightarrow最小化KL散度。

推导过程

我们对贝叶斯公式(1)两边取对数并取期望(期望分布为q(θ)q(\theta)q(θ)):

Eq[log⁡p(θ∣D)]=Eq[log⁡p(D,θ)]−log⁡p(D)E_q[\log p(\theta|\mathcal{D})]=E_q[\log p(\mathcal{D},\theta)]-\log p(\mathcal{D})Eq[logp(θD)]=Eq[logp(D,θ)]logp(D)

log⁡p(D)=log⁡∫p(D,θ)dθ\log p(\mathcal{D})=\log\int p(\mathcal{D},\theta)d\thetalogp(D)=logp(D,θ)dθ

引入q(θ)q(\theta)q(θ)

log⁡p(D)=log⁡∫q(θ)p(D,θ)q(θ)dθ\log p(\mathcal{D})=\log\int q(\theta)\frac{p(\mathcal{D},\theta)}{q(\theta)}d\thetalogp(D)=logq(θ)q(θ)p(D,θ)dθ

利用Jensen不等式:

log⁡p(D)≥∫q(θ)log⁡p(D,θ)q(θ)dθ=L(q)\log p(\mathcal{D})\geq\int q(\theta)\log\frac{p(\mathcal{D},\theta)}{q(\theta)}d\theta=\mathcal{L}(q)logp(D)q(θ)logq(θ)p(D,θ)dθ=L(q)

KL散度的定义为:

KL(q(θ)∥p(θ∣D))=∫q(θ)log⁡q(θ)p(θ∣D)dθ(3)\mathrm{KL}(q(\theta)\|p(\theta|\mathcal{D}))=\int q(\theta)\log\frac{q(\theta)}{p(\theta|\mathcal{D})}d\theta\qquad{(3)}KL(q(θ)p(θD))=q(θ)logp(θD)q(θ)dθ(3)

KL(q(θ)∥p(θ∣D))=∫q(θ)(log⁡q(θ)−log⁡p(θ∣D))dθ=∫q(θ)log⁡q(θ)dθ⏟Eq[log⁡q(θ)]−∫q(θ)log⁡p(θ∣D)dθ⏟Eq[log⁡p(θ∣D)](4)\mathrm{KL}(q(\theta)\|p(\theta|\mathcal{D}))=\int q(\theta)\left(\log q(\theta)-\log p(\theta\mid\mathcal{D})\right)d\theta=\underbrace{\int q(\theta)\log q(\theta)d\theta}_{E_q[\log q(\theta)]}-\underbrace{\int q(\theta)\log p(\theta\mid\mathcal{D})d\theta}_{E_q[\log p(\theta|\mathcal{D})]}\qquad{(4)}KL(q(θ)p(θD))=q(θ)(logq(θ)logp(θD))dθ=Eq[logq(θ)]q(θ)logq(θ)dθEq[logp(θD)]q(θ)logp(θD)dθ(4)

由公式(1)可以得到log⁡p(θ∣D)=log⁡p(D,θ)−log⁡p(D)\log p(\theta\mid\mathcal{D})=\log p(\mathcal{D},\theta)-\log p(\mathcal{D})logp(θD)=logp(D,θ)logp(D)。将其带入公式(4)的第二项:

Eq[log⁡p(θ∣D)]=∫q(θ)(log⁡p(D,θ)−log⁡p(D))dθ.E_q[\log p(\theta\mid\mathcal{D})]=\int q(\theta)\left(\log p(\mathcal{D},\theta)-\log p(\mathcal{D})\right)d\theta.Eq[logp(θD)]=q(θ)(logp(D,θ)logp(D))dθ.

因为log⁡p(D)\log p(\mathcal{D})logp(D)θ\thetaθ无关,且∫q(θ)dθ=1\int q(\theta)d\theta=1q(θ)dθ=1,得到:

Eq[log⁡p(θ∣D)]=Eq[log⁡p(D,θ)]−log⁡p(D)(5)E_q[\log p(\theta\mid\mathcal{D})]=E_q[\log p(\mathcal{D},\theta)]-\log p(\mathcal{D})\qquad{(5)}Eq[logp(θD)]=Eq[logp(D,θ)]logp(D)(5)

将公式(5)代入KL散度(公式(4))的展开式,整理得到:

KL(q∥p)=log⁡p(D)−(Eq[log⁡p(D,θ)]−Eq[log⁡q(θ)])(6)\mathrm{KL}(q\|p)=\log p(\mathcal{D})-\left(E_q[\log p(\mathcal{D},\theta)]-E_q[\log q(\theta)]\right)\qquad{(6)}KL(qp)=logp(D)(Eq[logp(D,θ)]Eq[logq(θ)])(6)

定义ELBO并最终写出恒等式

定义证据下界(ELBO)为:

L(q):=Eq[log⁡p(D,θ)]−Eq[log⁡q(θ)].\mathcal{L}(q):=E_q[\log p(\mathcal{D},\theta)]-E_q[\log q(\theta)].L(q):=Eq[logp(D,θ)]Eq[logq(θ)].

于是公式(6)可以写成:

KL(q(θ)∥p(θ∣D))=log⁡p(D)−L(q)\mathrm{KL}(q(\theta)\|p(\theta|\mathcal{D}))=\log p(\mathcal{D})-\mathcal{L}(q)KL(q(θ)p(θD))=logp(D)L(q)

也就是:

log⁡p(D)=L(q)+KL(q(θ)∥p(θ∣D))\log p(\mathcal{D})=\mathcal{L}(q)+\mathrm{KL}(q(\theta)\|p(\theta|\mathcal{D}))logp(D)=L(q)+KL(q(θ)p(θD))

这个公式意味着:

  • log⁡p(D)\log p(\mathcal{D})logp(D):我们想要最大化的模型证据(边际似然)
  • L(q)\mathcal{L}(q)L(q):我们能优化的下界(ELBO)
  • KL(q∣∣p)KL(q||p)KL(q∣∣p):永远非负。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值