版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37334135/article/details/85305067
之前看了《统计学习方法》,吴恩达老师的cs229讲义,一起看感觉很昏(如果要看建议选择其中一个,《统计学习方法》里面基本很少会写到
yi而都是用
Y只用了极大化没有写出我们熟悉的似然函数?!,cs229有视频,但我喜欢看书),网上看了点博客大部分好像也是来自cs229讲义。这里结合七月里面一个博士讲的与自己的理解来写出从极大似然估计推出EM算法好了。
1、隐变量与概率模型参数
EM算法:概率模型有时既含有观测变量,又含有隐变量。如果概率模型的变量都是观测变量,那么给定数据,就可以直接使用极大似然估计来求得参数或者贝叶斯估计模型参数。但是如果变量中有因变量,那么这些方法就不行了,这个时候就可以使用EM算法,EM算法就是含有隐变量的概率模型参数的极大似然估计方法。
下面给出《统计学习方法》中的例子
观测结果
1,1,0,1,0,0,1,0,1,1我们用变量
Y表示,叫做显变量,这里取值是0或1
而掷的硬币A的结果我们是不知道的,我们用变量
Z表示,叫做隐变量
2、EM算法推导
而
π,p,q则是模型参数,现在我们要求这三个参数。由于是改了模型,我们知道观测结果来求参数,自然想到使用极大似然估计。根据极大似然估计定义,概率分布
P(Y=yi)=pθ(yi;),其中
θ为模型参数
先回顾下概率公式 (推导会用到):
p(y)=z∑p(z)p(y∣z)=z∑p(y,z) 全概率公式和贝叶斯公式
z∑p(z∣y)=1
写出极大似然函数
L(θ)=i=1∏npθ(yi)=i=1∏nz∑pθ(yi,z)=i=1∏nz∑pθ(z)pθ(yi∣z)
写出对数形式
l(θ)=lnL(θ)=lni=1∏nz∑pθ(z)pθ(yi∣z)=i=1∑nln[z∑pθ(z)pθ(yi∣z)]
通常到这里就要对参数求导
θ求导从而得到似然函数的极大值,但是这里由于对数里面存在求和,这种情况是难以求解的。这种情况下,通常的做法是使用迭代逐步去毕竟最优解,而EM算法就是这样一种迭代算法,假设第
n次迭代求出的参数为
θn,我们希望下一次迭代得到的参数满足
l(θn+1)>l(θn)
l(θ)−l(θn)=i=1∑n(lnz∑pθ(z)pθ(yi∣z)−lnpθn(yi))
对
pθ(z)pθ(yi∣z) 进行乘一项除一项得到
pθn(z∣yi)pθn(z∣yi)pθ(z)pθ(yi∣z)
l(θ)−l(θn)=i=1∑n(lnz∑pθn(z∣yi)pθn(z∣yi)pθ(z)pθ(yi∣z)−lnpθn(yi))
由于
z∑p(z∣y)=1,所以
lnpθn(yi)=z∑lnpθn(yi)pθn(z∣yi)
l(θ)−l(θn)=i=1∑n(lnz∑pθn(z∣yi)pθn(z∣yi)pθ(z)pθ(yi∣z)−z∑lnpθn(yi)pθn(z∣yi))
下面介绍下琴生不等式(Jensen)
看下面的图,曲线对应的是一个凹函数
对于凸函数,上面的符号改成小于等于即可,可以自己画图。
lnz∑pθn(z∣yi)pθn(z∣yi)pθ(z)pθ(yi∣z)≥z∑pθn(z∣yi)lnpθn(z∣yi)pθ(z)pθ(yi∣z)
=>l(θ)−l(θn)≥i=1∑n(z∑pθn(z∣yi)lnpθn(z∣yi)pθ(z)pθ(yi∣z)−z∑lnpθn(yi)pθn(z∣yi))=i=1∑nz∑(pθn(z∣yi)lnpθn(yi)pθn(z∣yi)pθ(z)pθ(yi∣z))
最后我们得到
l(θ)≥l(θn)+i=1∑nz∑(pθn(z∣yi)lnpθn(yi)pθn(z∣yi)pθ(z)pθ(yi∣z))
记
B(θ∣θn)=l(θn)+i=1∑nz∑(pθn(z∣yi)lnpθn(yi)pθn(z∣yi)pθ(z)pθ(yi∣z)),那么
l(θ)≥B(θ∣θn)
称
B(θ∣θn)为
l(θ)的下边界函数,可以注意到:
B(θn∣θn)=l(θn)+i=1∑nz∑(pθn(z∣yi)lnpθn(yi)pθn(z∣yi)pθn(z)pθn(yi∣z))=l(θn)
只要我们最大化
B(θ∣θn)那么
l(θ)也就可以尽可能大。再来看
B(θ∣θn),我们要求他的极大值
B(θ∣θn)=l(θn)+i=1∑nz∑(pθn(z∣yi)lnpθn(yi)pθn(z∣yi)pθ(z)pθ(yi∣z))=l(θn)+i=1∑nz∑pθn(z∣yi)lnpθ(z)pθ(yi∣z)−i=1∑nz∑pθn(z∣yi)lnpθn(yi)pθn(z∣yi)
去掉常数项,得到仅与
θ有关的项
i=1∑nz∑pθn(z∣yi)lnpθ(z)pθ(yi∣z)=i=1∑nz∑pθn(z∣yi)lnpθ(yi,z),在这里称
Q(θ∣θn)=i=1∑nz∑pθn(z∣yi)lnpθ(yi,z)为
Q函数(《统计学习方法》中没有直接写最外层的求和,所以写的是
z∑pθn(z∣yi)lnpθ(yi,z)) 以后进行求导即可。这样我们就得到了第
n+1步的参数值
θn+1
θn+1=argθmaxQ(θ∣θn)
第
n+2步,
n+3步,一直这样往下求,直到收敛,比如
∣∣θn+1−θn∣∣<ε。
下面给出《统计学习方法》中的直观解释:
3、EM算法过程
到这里我们引出EM算法步骤:
输入:观测数据
Y,联合分布
P(Y,Z;θ),条件分布
P(Z∣Y;θ)
输出:模型参数
θ
(1)、选择参数的初始值
θ0开始进行迭代;
(2)、E(Expection)步:记第n次迭代参数为
θn,那么计算
n+1次的E步
Q(θ∣θn)=i=1∑nz∑pθn(z∣yi)lnpθ(yi,z)
(3)、M(Maximization)步:求使得
Q(θ∣θn)最大化的
θn+1,即确定第
n+1次的模型参数
θn+1=argθmaxQ(θ∣θn)
(4)、重复(2),(3)直到收敛。
注意:初值选的不一样,结果可能不一样,EM算法对初值是敏感的。