转自著名的“丕子”博客
在统计计算中,最大期望(EM)算法是在概率(probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Latent Variable)。最大期望经常用在机器学习和计算机视觉的数据聚类(Data Clustering) 领域。最大期望算法经过两个步骤交替进行计算,第一步是计算期望(E),利用对隐藏变量的现有估计值,计算其最大似然估计值;第二步是最大化(M),最大 化在 E 步上求得的最大似然值来计算参数的值。M 步上找到的参数估计值被用于下一个 E 步计算中,这个过程不断交替进行。
最大期望值算法由 Arthur Dempster,Nan Laird和Donald Rubin在他们1977年发表的经典论文中提出。他们指出此方法之前其实已经被很多作者"在他们特定的研究领域中多次提出过"。
我们用 表示能够观察到的不完整的变量值,用 表示无法观察到的变量值,这样 和 一起组成了完整的数据。 可能是实际测量丢失的数据,也可能是能够简化问题的隐藏变量,如果它的值能够知道的话。例如,在混合模型(Mixture Model)中,如果“产生”样本的混合元素成分已知的话最大似然公式将变得更加便利(参见下面的例子)。
估计无法观测的数据
让 代表矢量 θ: 定义的参数的全部数据的概率分布(连续情况下)或者概率聚类函数(离散情况下),那么从这个函数就可以得到全部数据的最大似然值,另外,在给定的观察到的数据条件下未知数据的条件分布可以表示为:
EM算法有这么两个步骤E和M:
-
Expectation step: Choose
q to maximize
F:
-
Maximization step: Choose
θ to maximize
F:
举个例子吧:高斯混合
假设 x = (x1,x2,…,xn) 是一个独立的观测样本,来自两个多元d维正态分布的混合, 让z=(z1,z2,…,zn)是潜在变量,确定其中的组成部分,是观测的来源.
即:
- and
where
- and
目标呢就是估计下面这些参数了,包括混合的参数以及高斯的均值很方差:
似然函数:
where 是一个指示函数 ,f 是 一个多元正态分布的概率密度函数. 可以写成指数形式:
下面就进入两个大步骤了:
E-step
给定目前的参数估计 θ(t), Zi 的条件概率分布是由贝叶斯理论得出,高斯之间用参数 τ加权:
- .
因此,E步骤的结果:
M步骤
Q(θ|θ(t))的二次型表示可以使得 最大化θ相对简单. τ, (μ1,Σ1) and (μ2,Σ2) 可以单独的进行最大化.
首先考虑 τ, 有条件τ1 + τ2=1:
和MLE的形式是类似的,二项分布 , 因此:
下一步估计 (μ1,Σ1):
和加权的 MLE就正态分布来说类似
- and
对称的:
- and .
这个例子来自Answers.com的Expectation-maximization algorithm,由于还没有深入体验,心里还说不出一些更通俗易懂的东西来,等研究了并且应用了可能就有所理解和消化。另外,liuxqsmile也做了一些理解和翻译。
============
在网上的源码不多,有一个很好的EM_GM.m,是滑铁卢大学的Patrick P. C. Tsui写的,拿来分享一下:
运行的时候可以如下进行初始化:
12345 | X X(1:200,:) X(201:400,:) X(401:600,:) [W,M,V,L] |
下面是程序源码:
001002003004005006007008009010011012013014015016017018019020021022023024025026027028029030031032033034035036037038039040041042043044045046047048049050051052053054055056057058059060061062063064065066067068069070071072073074075076077078079080081082083084085086087088089090091092093094095096097098099100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 | function
% % % % % % % % % % % % % % % % % % % % % % % % % %%%% if
disp( 'EM_GM must have at least 2 inputs: X,k!/n' ) return elseif
ltol = 0.1; maxiter = 1000; pflag = 0; Init = []; err_X = Verify_X(X); err_k = Verify_k(k); if
return ; end elseif
maxiter = 1000; pflag = 0; Init = []; err_X = Verify_X(X); err_k = Verify_k(k); [ltol,err_ltol] = Verify_ltol(ltol); if
return ; end elseif
pflag = 0; Init = []; err_X = Verify_X(X); err_k = Verify_k(k); [ltol,err_ltol] = Verify_ltol(ltol); [maxiter,err_maxiter] = Verify_maxiter(maxiter); if
return ; end elseif
Init = []; err_X = Verify_X(X); err_k = Verify_k(k); [ltol,err_ltol] = Verify_ltol(ltol); [maxiter,err_maxiter] = Verify_maxiter(maxiter); [pflag,err_pflag] = Verify_pflag(pflag); if
return ; end elseif
err_X = Verify_X(X); err_k = Verify_k(k); [ltol,err_ltol] = Verify_ltol(ltol); [maxiter,err_maxiter] = Verify_maxiter(maxiter); [pflag,err_pflag] = Verify_pflag(pflag); [Init,err_Init]=Verify_Init(Init); if
return ; end else disp( 'EM_GM must have 2 to 6 inputs!' ); return end %%%% t if
[W,M,V] = Init_EM(X,k); L = 0; else W = Init.W; M = Init.M; V = Init.V; end Ln % Initialize log likelihood Lo %%%% niter while
E = Expectation(X,k,W,M,V); % E-step [W,M,V] = Maximization(X,k,E); % M-step Lo = Ln; Ln = Likelihood(X,k,W,M,V); niter = niter + 1; end L %%%% if
[n,d] = size(X); if
disp( 'Can only plot 1 or 2 dimensional applications!/n' ); else Plot_GM(X,k,W,M,V); end elapsed_time = sprintf( 'CPU time used for EM_GM: %5.2fs' ,cputime-t); disp(elapsed_time); disp(sprintf( 'Number of iterations: %d' ,niter-1)); end %%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%% function
[n,d] a S iV for
if
end S(j) = sqrt(det(V(:,:,j))); iV(:,:,j) = inv(V(:,:,j)); end E for
for
dXM = X(i,:)'-M(:,j); pl = exp(-0.5*dXM'*iV(:,:,j)*dXM)/(a*S(j)); E(i,j) = W(j)*pl; end E(i,:) = E(i,:)/sum(E(i,:)); end %%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%% function
[n,d] W V for
% Compute weights for
W(i) = W(i) + E(j,i); M(:,i) = M(:,i) + E(j,i)*X(j,:)'; end M(:,i) = M(:,i)/W(i); end for
for
dXM = X(j,:)'-M(:,i); V(:,:,i) = V(:,:,i) + E(j,i)*dXM*dXM'; end V(:,:,i) = V(:,:,i)/W(i); end W %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% function
% % [n,d] U S L for
iV = inv(V(:,:,i)); L = L + W(i)*(-0.5*n*log(det(2*pi*V(:,:,i))) ... -0.5*(n-1)*(trace(iV*S)+(U-M(:,i))'*iV*(U-M(:,i)))); end %%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%% function
err_X [n,d] if
disp( 'Input data must be n x d!/n' ); return end err_X %%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%% function
err_k if
disp( 'k must be a real integer >= 1!/n' ); return end err_k %%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%% function
err_ltol if
ltol = 0.1; elseif
disp( 'ltol must be a positive real number!' ); return end err_ltol %%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%% function
err_maxiter if
maxiter = 1000; elseif
disp( 'ltol must be a positive real number!' ); return end err_maxiter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function
err_pflag if
pflag = 0; elseif
disp( 'Plot flag must be either 0 or 1!/n' ); return end err_pflag %%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% function
err_Init if
% Do nothing; elseif
[Wd,Wk] = size(Init.W); [Md,Mk] = size(Init.M); [Vd1,Vd2,Vk] = size(Init.V); if
disp( 'k in Init.W(1,k), Init.M(d,k) and Init.V(d,d,k) must equal!/n' ) return end if
disp( 'd in Init.W(1,k), Init.M(d,k) and Init.V(d,d,k) must equal!/n' ) return end else disp( 'Init must be a structure: W(1,k), M(d,k), V(d,d,k) or []!' ); return end err_Init %%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%% function
[n,d] [Ci,C] 'Start' , 'cluster' , ... 'Maxiter' ,100, ... 'EmptyAction' , 'drop' , ... 'Display' , 'off' ); % Ci(nx1) - cluster indeices; C(k,d) - cluster centroid (i.e. mean) while
[Ci,C] = kmeans(X,k, 'Start' , 'cluster' , ... 'Maxiter' ,100, ... 'EmptyAction' , 'drop' , ... 'Display' , 'off' ); end M Vp 'count' ,0, 'X' ,zeros(n,d)),1,k); for
% Separate cluster points Vp(Ci(i)).count = Vp(Ci(i)).count + 1; Vp(Ci(i)).X(Vp(Ci(i)).count,:) = X(i,:); end V for
W(i) = Vp(i).count/n; V(:,:,i) = cov(Vp(i).X(1:Vp(i).count,:)); end %%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%% function
[n,d] if
disp( 'Can only plot 1 or 2 dimensional applications!/n' ); return end S R1 R2 for
% Determine plot range as 4 x standard deviations S(:,i) = sqrt(diag(V(:,:,i))); R1(:,i) = M(:,i)-4*S(:,i); R2(:,i) = M(:,i)+4*S(:,i); end Rmin Rmax R clf, if
Q = zeros(size(R)); for
P = W(i)*normpdf(R,M(:,i),sqrt(V(:,:,i))); Q = Q + P; plot(R,P, 'r-' ); grid on, end plot(R,Q, 'k-' ); xlabel( 'X' ); ylabel( 'Probability density' ); else
plot(X(:,1),X(:,2), 'r.' ); for
Plot_Std_Ellipse(M(:,i),V(:,:,i)); end xlabel( '1^{st} dimension' ); ylabel( '2^{nd} dimension' ); axis([Rmin Rmax Rmin Rmax]) end title( 'Gaussian Mixture estimated by EM' ); %%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%% function
[Ev,D] d if
V(:,:) = ones(d,d)*eps; end iV % P % X-axis projection operator P1 P2 if
Plen = P1(1); else Plen = P2(1); end count step Contour1 Contour2 for
a = iV(2,2); b = x * (iV(1,2)+iV(2,1)); c = (x^2) * iV(1,1) - 1; Root1 = (-b + sqrt(b^2 - 4*a*c))/(2*a); Root2 = (-b - sqrt(b^2 - 4*a*c))/(2*a); if
Contour1(count,:) = [x,Root1] + M'; Contour2(count,:) = [x,Root2] + M'; count = count + 1; end end Contour1 Contour2 plot(M(1),M(2), 'k+' ); plot(Contour1(:,1),Contour1(:,2), 'k-' ); plot(Contour2(:,1),Contour2(:,2), 'k-' ); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |