---
title: "期望最大（EM）算法推导"
author: "fuliang"
date: "2012-08-05T19:54:33Z"
tags: ["EM", "期望 最大"]
categories: ["互联网"]
slug: "em"
draft: false
lastmod: "2012-12-21T08:19:12Z"
id: 1621633
url: "https://fuliang.iteye.com/blog/1621633"
---

X是一个随机向量，我们希望找到![](http://latex.codecogs.com/gif.latex?\theta)
使得![](http://latex.codecogs.com/gif.latex?P(X|\theta))取得最大值，这就是关于![](http://latex.codecogs.com/gif.latex?\theta)的最大似然估计。
为了方便估计![](http://latex.codecogs.com/gif.latex?\theta)，我们一般引入log似然函数：
![](http://latex.codecogs.com/gif.latex?L(\theta)=lnP(X|\theta))
EM算法是一个迭代的过程，假设第n次迭代当前![](http://latex.codecogs.com/gif.latex?\theta)的估计是![](http://latex.codecogs.com/gif.latex?\theta_n)。由于我们的目标是最大化![](http://latex.codecogs.com/gif.latex?L(\theta))，我们希望新一轮的更新![](http://latex.codecogs.com/gif.latex?\theta)使得
![](http://latex.codecogs.com/gif.latex?L(\theta)>L(\theta_n)})
等价的，我们希望最大化他们的不同：
![](http://latex.codecogs.com/gif.latex?L(\theta)-L(\theta_n)=lnP(X|\theta)-lnP(x|\theta_n))
现在我们考虑隐变量的问题，隐变量可能是没有观测到的或者缺失的变量，有时为了计算最大似然函数更容易解决也会引入隐变量，因为可以利用EM框架来方便计算。我们假设隐变量用Z来表示，那么
![](http://latex.codecogs.com/gif.latex?P(X|\theta)=\sum_z{P(X|z,\theta)P(z|\theta))
我们重写一下![](http://latex.codecogs.com/gif.latex?L(\theta)-L(\theta_n))得到：
![](http://latex.codecogs.com/gif.latex?L(\theta)-L(\theta_n)=ln(\sum_z{P(X|z,\theta)P(z|\theta)})-lnP(X|\theta_n))
利用Jensen's不定式：
![](http://latex.codecogs.com/gif.latex?ln\sum_{i=1}^n\lambda_ix_i\ge\sum_{i=1}^n\lambda_i{ln(x_i)})
其中常量![](http://latex.codecogs.com/gif.latex?\lambda_i\ge0)并且![](http://latex.codecogs.com/gif.latex?\sum_{i=1}^n\lambda_i=1)
![](http://latex.codecogs.com/gif.latex?L(\theta)-L(\theta_n)=ln(\sum_z{P(X|z,\theta)P(z|\theta)})-lnP(X|\theta_n))
![](http://latex.codecogs.com/gif.latex?=ln(\sum_z{P(X|z,\theta)P(z|\theta)}\frac{P(z|X,\theta_n)}{P(z|X,\theta_n)})-lnP(X|\theta_n))
![](http://latex.codecogs.com/gif.latex?=ln(\sum_z{P(z|X,\theta_n)\frac{P(X|z,\theta)P(z|\theta)}{P(z|X,\theta_n)})-lnP(X|\theta_n))
![](http://latex.codecogs.com/gif.latex?\ge\sum_z{P(z|X,\theta_n)}ln({\frac{P(X|z,\theta)P(z|\theta)}{P(z|X,\theta_n)})-lnP(X|\theta_n))
![](http://latex.codecogs.com/gif.latex?\ge\sum_z{P(z|X,\theta_n)}ln({\frac{P(X|z,\theta)P(z|\theta)}{P(z|X,\theta_n)P(X|\theta_n)}))
![](http://latex.codecogs.com/gif.latex?\doteq\Delta(\theta|\theta_{n}))
其中由于![](http://latex.codecogs.com/gif.latex?\sum_z{P(z|X,\theta_n)=1)
所以有：
![](http://latex.codecogs.com/gif.latex?P(X|\theta_n)=\sum_z{P(z|X,\theta_n)}ln{P(x|\theta_n)})
我们可以写作：
![](http://latex.codecogs.com/gif.latex?L(\theta)\ge{L(\theta_n)+\Delta(\theta|\theta_n)})
为了方便，我们定义：
![](http://latex.codecogs.com/gif.latex?l(\theta|\theta_n)\doteq{L(\theta_n)+\Delta(\theta|\theta_n)})
这样我们得到
![](http://latex.codecogs.com/gif.latex?L(\theta)\ge{l(\theta|\theta_n)})

现在我们得到了似然函数![](http://latex.codecogs.com/gif.latex?L(\theta))的下界![](http://latex.codecogs.com/gif.latex?{l(\theta|\theta_n)})
另外我们观察到：
![](http://latex.codecogs.com/gif.latex?{l(\theta_n|\theta_n)})
![](http://latex.codecogs.com/gif.latex?=L(\theta_n)+\Delta(\theta_n|\theta_n)=L(\theta_n)+\sum_z{P(z|X,\theta_n)ln\frac{P(X|z,\theta_n)P(z|\theta_n)}{P(z|X,\theta_n)P{X|\theta_n)}})
![](http://latex.codecogs.com/gif.latex?=L(\theta_n)+\sum_z{P(z|X,\theta_n)ln\frac{P(X,z|\theta_n)}{P(X,z|\theta_n)}=L(\theta_n)+\sum_z{P(z|X,\theta_n)ln1=L(\theta_n))
所以当![](http://latex.codecogs.com/gif.latex?\theta=\theta_n)时，
![](http://latex.codecogs.com/gif.latex?{l(\theta|\theta_n)}=L(\theta))

所以任何能够增加![](http://latex.codecogs.com/gif.latex?{l(\theta_n|\theta_n)})的![](http://latex.codecogs.com/gif.latex?\theta)都会增加![](http://latex.codecogs.com/gif.latex?L(\theta))
所以EM算法选择最大化![](http://latex.codecogs.com/gif.latex?{l(\theta_n|\theta_n)})

最终我们得到：
![](http://latex.codecogs.com/gif.latex?\theta_{n+1}=argmax_{\theta}\{l(\theta|\theta_n)\})
![](http://latex.codecogs.com/gif.latex?=argmax_\theta\{\L(\theta_n)+\sum_z{P(z|X,\theta_n)}ln({\frac{P(X|z,\theta)P(z|\theta)}{P(z|X,\theta_n)P(X|\theta_n)})\})
去掉相对于![](http://latex.codecogs.com/gif.latex?\theta)的常量得到：
![](http://latex.codecogs.com/gif.latex?=argmax_\theta\{\sum_z{P(z|X,\theta_n)}ln(P(X|z,\theta)P(z|\theta)\})
![](http://latex.codecogs.com/gif.latex?=argmax_\theta\{\sum_z{P(z|X,\theta_n)}ln\frac{P(X,z,\theta)}{p(z,\theta)}\frac{P(z,\theta)}{p(\theta)}\})
![](http://latex.codecogs.com/gif.latex?=argmax_\theta\{\sum_z{P(z|X,\theta_n)}ln(P(X,z|\theta)\})
![](http://latex.codecogs.com/gif.latex?=argmax_\theta\{E_{Z|X,\theta_n}\{lnP(X,z|\theta)}\})

所以EM包含以下迭代步骤：
1、E-step: 得到条件期望![](http://latex.codecogs.com/gif.latex?E_{Z|X,\theta_n}\{lnP(X,z|\theta)})
2、M-step：求解![](http://latex.codecogs.com/gif.latex?\theta)最大化该条件期望