我们有两个硬币,分别具有正面Theta1和Theta2的概率。以下数据为我们提供了经过10次翻转和5次迭代后出现的正面的数量:'x1 = 5,x2 = 9,x3 = 8,x4 = 4,x5 = 7'
从上面的数据考虑,第一枚硬币的概率为p
,第二枚硬币的概率为1-p
。连续迭代(r)
和(r+1)
之后EM算法的终止条件为
$(p^(r+1) -p^(r))^2 + ( theta1^(r+1) - theta1^(r) )^2 +
(theta2^(r+1)-theta2^(r))^2<=10^(-10)$
例如,我编写了这段代码,但是只需要1次迭代即可收敛。
EMflipcoin = function(p,n,theta1,theta2,x) {
likelihood = function(p,x,theta,n){
p * dbinom(x, n, theta) / (p * dbinom(x,n,theta) +
(1 - p) * (dbinom(x,n,1-theta)))}
M = function(p,x,theta,n){pi.est = likelihood(p,x,theta,n)
mean(pi.est)}
tol=10^(-10)
maxiter=1000
diff=1
iter=0
while(diff>tol & iter<maxiter){
## E-step:
d1 = likelihood(p=p,x=x,theta=theta1,n=n)
d2 = likelihood(p=p,x=x,theta=theta2,n=n)
p1 = likelihood(p=p,x=x,theta=p,n=n)
## M-step:
m1 = M(p,x,theta1,n)
m2 = M(p,x,theta2,n)
p.new = M(p,x,p,n)
## calculate diff to check convergence
diff= (mean(p1)-p.new)^2+(mean(d1)-m1)^2+ (mean(d2)-m2)^2
d1=m1; d2=m2;
iter=iter+1;
cat("Iter", iter,
", p=", p.new,
": theta1=", m1,
", theta2=",m2,
", diff=", diff, "\n")}
}
x=c(8,5,9,4,7)
EMflipcoin(p=0.5,n=10,theta1=0.6,theta2=0.4,x=x)