EM算法:从极大似然估计导出EM算法(还算通俗易懂)

本文深入探讨了EM算法的理论基础,详细解析了从极大似然估计到EM算法的推导过程,通过实例说明了如何处理含有隐变量的概率模型参数估计问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前看了《统计学习方法》,吴恩达老师的cs229讲义,一起看感觉很昏(如果要看建议选择其中一个,《统计学习方法》里面基本很少会写到yiy_iyi而都是用 YYY只用了极大化没有写出我们熟悉的似然函数?!,cs229有视频,但我喜欢看书),网上看了点博客大部分好像也是来自cs229讲义。这里结合七月里面一个博士讲的与自己的理解来写出从极大似然估计推出EM算法好了。

1、隐变量与概率模型参数

EM算法:概率模型有时既含有观测变量,又含有隐变量。如果概率模型的变量都是观测变量,那么给定数据,就可以直接使用极大似然估计来求得参数或者贝叶斯估计模型参数。但是如果变量中有因变量,那么这些方法就不行了,这个时候就可以使用EM算法,EM算法就是含有隐变量的概率模型参数的极大似然估计方法。

下面给出《统计学习方法》中的例子

在这里插入图片描述

观测结果 1,1,0,1,0,0,1,0,1,1{1,1,0,1,0,0,1,0,1,1}1,1,0,1,0,0,1,0,1,1我们用变量 YYY表示,叫做显变量,这里取值是0或1

而掷的硬币A的结果我们是不知道的,我们用变量 ZZZ表示,叫做隐变量

2、EM算法推导

π,p,q\pi,p,qπpq则是模型参数,现在我们要求这三个参数。由于是改了模型,我们知道观测结果来求参数,自然想到使用极大似然估计。根据极大似然估计定义,概率分布 P(Y=yi)=pθ(yi;)P(Y=y_i)=p_\theta(y_i;)P(Y=yi)=pθ(yi;),其中θ\thetaθ为模型参数

先回顾下概率公式 (推导会用到):
p(y)=∑zp(z)p(y∣z)=∑zp(y,z)p(y)=\sum\limits_{z}p(z)p(y|z)=\sum\limits_{z}p(y,z)p(y)=zp(z)p(yz)=zp(y,z) 全概率公式和贝叶斯公式
∑zp(z∣y)=1\sum\limits_{z}p(z|y)=1zp(zy)=1

写出极大似然函数

L(θ)=∏i=1npθ(yi)=∏i=1n∑zpθ(yi,z)=∏i=1n∑zpθ(z)pθ(yi∣z)L(\theta)=\prod\limits_{i=1}^{n}p_\theta(y_i)\\=\prod\limits_{i=1}^{n}\sum\limits_{z}p_\theta(y_i,z)\\=\prod\limits_{i=1}^{n}\sum\limits_{z}p_\theta(z)p_\theta(y_i|z)L(θ)=i=1npθ(yi)=i=1nzpθ(yi,z)=i=1nzpθ(z)pθ(yiz)

写出对数形式

l(θ)=lnL(θ)=ln∏i=1n∑zpθ(z)pθ(yi∣z)=∑i=1nln[∑zpθ(z)pθ(yi∣z)]l(\theta)=lnL(\theta)=ln\prod\limits_{i=1}^{n}\sum\limits_{z}p_\theta(z)p_\theta(y_i|z)=\sum\limits_{i=1}^{n}ln[\sum\limits_{z}p_\theta(z)p_\theta(y_i|z)]l(θ)=lnL(θ)=lni=1nzpθ(z)pθ(yiz)=i=1nln[zpθ(z)pθ(yiz)]

通常到这里就要对参数求导θ\thetaθ求导从而得到似然函数的极大值,但是这里由于对数里面存在求和,这种情况是难以求解的。这种情况下,通常的做法是使用迭代逐步去毕竟最优解,而EM算法就是这样一种迭代算法,假设第nnn次迭代求出的参数为 θn\theta_nθn,我们希望下一次迭代得到的参数满足 l(θn+1)>l(θn)l(\theta_{n+1})>l(\theta_{n})l(θn+1)>l(θn)

l(θ)−l(θn)=∑i=1n(ln∑zpθ(z)pθ(yi∣z)−lnpθn(yi))l(\theta)-l(\theta_n)=\sum\limits_{i=1}^{n}(ln\sum\limits_{z}p_\theta(z)p_\theta(y_i|z)-ln^{p_{\theta_n}(y_i))}l(θ)l(θn)=i=1n(lnzpθ(z)pθ(yiz)lnpθn(yi))

pθ(z)pθ(yi∣z)p_\theta(z)p_\theta(y_i|z)pθ(z)pθ(yiz) 进行乘一项除一项得到pθn(z∣yi)pθ(z)pθ(yi∣z)pθn(z∣yi)p_{\theta_n}(z|y_i)\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(z|y_i)}pθn(zyi)pθn(zyi)pθ(z)pθ(yiz)

l(θ)−l(θn)=∑i=1n(ln∑zpθn(z∣yi)pθ(z)pθ(yi∣z)pθn(z∣yi)−lnpθn(yi))l(\theta)-l(\theta_n)=\sum\limits_{i=1}^{n}(ln\sum\limits_{z}p_{\theta_n}(z|y_i)\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(z|y_i)}-ln^{p_{\theta_n}(y_i)})l(θ)l(θn)=i=1n(lnzpθn(zyi)pθn(zyi)pθ(z)pθ(yiz)lnpθn(yi))

由于∑zp(z∣y)=1\sum\limits_{z}p(z|y)=1zp(zy)=1,所以lnpθn(yi)=∑zlnpθn(yi)pθn(z∣yi)ln^{p_{\theta_n}(y_i)}=\sum\limits_{z}ln^{p_{\theta_n}(y_i)}p_{\theta_n}(z|y_i)lnpθn(yi)=zlnpθn(yi)pθn(zyi)

l(θ)−l(θn)=∑i=1n(ln∑zpθn(z∣yi)pθ(z)pθ(yi∣z)pθn(z∣yi)−∑zlnpθn(yi)pθn(z∣yi))l(\theta)-l(\theta_n)=\sum\limits_{i=1}^{n}(ln\sum\limits_{z}p_{\theta_n}(z|y_i)\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(z|y_i)}-\sum\limits_{z}ln^{p_{\theta_n}(y_i)}p_{\theta_n}(z|y_i))l(θ)l(θn)=i=1n(lnzpθn(zyi)pθn(zyi)pθ(z)pθ(yiz)zlnpθn(yi)pθn(zyi))

下面介绍下琴生不等式(Jensen)

看下面的图,曲线对应的是一个凹函数
在这里插入图片描述

对于凸函数,上面的符号改成小于等于即可,可以自己画图。

ln∑zpθn(z∣yi)pθ(z)pθ(yi∣z)pθn(z∣yi)≥∑zpθn(z∣yi)lnpθ(z)pθ(yi∣z)pθn(z∣yi)ln\sum\limits_{z}p_{\theta_n}(z|y_i)\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(z|y_i)}\geq \sum\limits_{z}p_{\theta_n}(z|y_i)ln^{\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(z|y_i)}}lnzpθn(zyi)pθn(zyi)pθ(z)pθ(yiz)zpθn(zyi)lnpθn(zyi)pθ(z)pθ(yiz)

=>l(θ)−l(θn)≥∑i=1n(∑zpθn(z∣yi)lnpθ(z)pθ(yi∣z)pθn(z∣yi)−∑zlnpθn(yi)pθn(z∣yi))=∑i=1n∑z(pθn(z∣yi)lnpθ(z)pθ(yi∣z)pθn(yi)pθn(z∣yi))=>l(\theta)-l(\theta_n)\geq \sum\limits_{i=1}^{n}(\sum\limits_{z}p_{\theta_n}(z|y_i)ln^{\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(z|y_i)}}-\sum\limits_{z}ln^{p_{\theta_n}(y_i)}p_{\theta_n}(z|y_i))= \sum\limits_{i=1}^{n} \sum\limits_{z}(p_{\theta_n}(z|y_i)ln^{{\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(y_i)p_{\theta_n}(z|y_i)}}})=>l(θ)l(θn)i=1n(zpθn(zyi)lnpθn(zyi)pθ(z)pθ(yiz)zlnpθn(yi)pθn(zyi))=i=1nz(pθn(zyi)lnpθn(yi)pθn(zyi)pθ(z)pθ(yiz))

最后我们得到

l(θ)≥l(θn)+∑i=1n∑z(pθn(z∣yi)lnpθ(z)pθ(yi∣z)pθn(yi)pθn(z∣yi))l(\theta)\geq l(\theta_n)+ \sum\limits_{i=1}^{n} \sum\limits_{z}(p_{\theta_n}(z|y_i)ln^{{\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(y_i)p_{\theta_n}(z|y_i)}}})l(θ)l(θn)+i=1nz(pθn(zyi)lnpθn(yi)pθn(zyi)pθ(z)pθ(yiz))

B(θ∣θn)=l(θn)+∑i=1n∑z(pθn(z∣yi)lnpθ(z)pθ(yi∣z)pθn(yi)pθn(z∣yi))B(\theta|\theta_n)= l(\theta_n)+ \sum\limits_{i=1}^{n} \sum\limits_{z}(p_{\theta_n}(z|y_i)ln^{{\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(y_i)p_{\theta_n}(z|y_i)}}})B(θθn)=l(θn)+i=1nz(pθn(zyi)lnpθn(yi)pθn(zyi)pθ(z)pθ(yiz)),那么l(θ)≥B(θ∣θn)l(\theta)\geq B(\theta|\theta_n)l(θ)B(θθn)

B(θ∣θn)B(\theta|\theta_n)B(θθn)l(θ)的下边界函数l(\theta)的下边界函数l(θ),可以注意到:
B(θn∣θn)=l(θn)+∑i=1n∑z(pθn(z∣yi)lnpθn(z)pθn(yi∣z)pθn(yi)pθn(z∣yi))=l(θn)B(\theta_n|\theta_n)=l(\theta_n)+ \sum\limits_{i=1}^{n} \sum\limits_{z}(p_{\theta_n}(z|y_i)ln^{{\frac{p_{\theta_n}(z)p_{\theta_n}(y_i|z)}{p_{\theta_n}(y_i)p_{\theta_n}(z|y_i)}}})=l(\theta_n)B(θnθn)=l(θn)+i=1nz(pθn(zyi)lnpθn(yi)pθn(zyi)pθn(z)pθn(yiz))=l(θn)

只要我们最大化B(θ∣θn)B(\theta|\theta_n)B(θθn)那么l(θ)l(\theta)l(θ)也就可以尽可能大。再来看B(θ∣θn)B(\theta|\theta_n)B(θθn),我们要求他的极大值

B(θ∣θn)=l(θn)+∑i=1n∑z(pθn(z∣yi)lnpθ(z)pθ(yi∣z)pθn(yi)pθn(z∣yi))=l(θn)+∑i=1n∑zpθn(z∣yi)lnpθ(z)pθ(yi∣z)−∑i=1n∑zpθn(z∣yi)lnpθn(yi)pθn(z∣yi)B(\theta|\theta_n)= l(\theta_n)+ \sum\limits_{i=1}^{n} \sum\limits_{z}(p_{\theta_n}(z|y_i)ln^{\frac{p_\theta(z)p_\theta(y_i|z)}{p_{\theta_n}(y_i)p_{\theta_n}(z|y_i)}})\\= l(\theta_n)+ \sum\limits_{i=1}^{n} \sum\limits_{z}p_{\theta_n}(z|y_i)ln^{p_\theta(z)p_\theta(y_i|z)}-\sum\limits_{i=1}^{n} \sum\limits_{z}p_{\theta_n}(z|y_i)ln^{p_{\theta_n}(y_i)p_{\theta_n}(z|y_i)}B(θθn)=l(θn)+i=1nz(pθn(zyi)lnpθn(yi)pθn(zyi)pθ(z)pθ(yiz))=l(θn)+i=1nzpθn(zyi)lnpθ(z)pθ(yiz)i=1nzpθn(zyi)lnpθn(yi)pθn(zyi)

去掉常数项,得到仅与 θ\thetaθ有关的项 ∑i=1n∑zpθn(z∣yi)lnpθ(z)pθ(yi∣z)=∑i=1n∑zpθn(z∣yi)lnpθ(yi,z)\sum\limits_{i=1}^{n} \sum\limits_{z}p_{\theta_n}(z|y_i)ln^{p_\theta(z)p_\theta(y_i|z)}=\sum\limits_{i=1}^{n} \sum\limits_{z}p_{\theta_n}(z|y_i)ln^{p_\theta(y_i,z)}i=1nzpθn(zyi)lnpθ(z)pθ(yiz)=i=1nzpθn(zyi)lnpθ(yi,z),在这里称Q(θ∣θn)=∑i=1n∑zpθn(z∣yi)lnpθ(yi,z)Q(\theta|\theta_n)=\sum\limits_{i=1}^{n} \sum\limits_{z}p_{\theta_n}(z|y_i)ln^{p_\theta(y_i,z)}Q(θθn)=i=1nzpθn(zyi)lnpθ(yi,z)QQQ函数(《统计学习方法》中没有直接写最外层的求和,所以写的是∑zpθn(z∣yi)lnpθ(yi,z)\sum\limits_{z}p_{\theta_n}(z|y_i)ln^{p_\theta(y_i,z)}zpθn(zyi)lnpθ(yi,z)) 以后进行求导即可。这样我们就得到了第n+1n+1n+1步的参数值 θn+1\theta_{n+1}θn+1

θn+1=arg⁡max⁡θQ(θ∣θn)\theta_{n+1}=\arg\max\limits_{\theta}Q(\theta|\theta_n)θn+1=argθmaxQ(θθn)

n+2n+2n+2步,n+3n+3n+3步,一直这样往下求,直到收敛,比如 ∣∣θn+1−θn∣∣&lt;ε||\theta_{n+1}-\theta_n||&lt;\varepsilonθn+1θn<ε

下面给出《统计学习方法》中的直观解释:

在这里插入图片描述

3、EM算法过程

到这里我们引出EM算法步骤

输入:观测数据 YYY,联合分布 P(Y,Z;θ)P(Y,Z;\theta)P(Y,Z;θ),条件分布 P(Z∣Y;θ)P(Z|Y;\theta)P(ZY;θ)
输出:模型参数 θ\thetaθ

(1)、选择参数的初始值 θ0\theta_0θ0开始进行迭代;
(2)、E(Expection)步:记第n次迭代参数为θn\theta_nθn,那么计算 n+1n+1n+1次的E步

Q(θ∣θn)=∑i=1n∑zpθn(z∣yi)lnpθ(yi,z)Q(\theta|\theta_n)=\sum\limits_{i=1}^{n} \sum\limits_{z}p_{\theta_n}(z|y_i)ln^{p_\theta(y_i,z)}Q(θθn)=i=1nzpθn(zyi)lnpθ(yi,z)

(3)、M(Maximization)步:求使得 Q(θ∣θn)Q(\theta|\theta_n)Q(θθn)最大化的 θn+1\theta_{n+1}θn+1,即确定第n+1n+1n+1次的模型参数

θn+1=arg⁡max⁡θQ(θ∣θn)\theta_{n+1}=\arg\max\limits_{\theta}Q(\theta|\theta_n)θn+1=argθmaxQ(θθn)

(4)、重复(2),(3)直到收敛。

注意:初值选的不一样,结果可能不一样,EM算法对初值是敏感的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值