EM算法例子 matlab

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dahaiya/article/details/85059585

EM算法

EM算法 实例讲解

%{
实现的是一个AB硬币的例子
thetaA和thetaB表示硬币A和B正面向上的概率
A的计算结果是在每一轮投掷时,选择的是硬币A的概率
%}
clear
clc
data = [[5 5];[9 1];[8 2];[4 6];[7 3]];
m_step = 10;
PA = zeros(1,5);
PB = zeros(1,5);
A = zeros(5,1);
thetaA_start = 0.5;
thetaB_start = 0.4;
resultA = zeros(1,m_step);
resultB = zeros(1,m_step);
thetaA = thetaA_start;
thetaB = thetaB_start;

for iteration = 1:m_step
    for k = 1:length(data)
        onedata = data(k,:);
        PA(k) = thetaA^onedata(1)*(1-thetaA)^onedata(2);
        PB(k) = thetaB^onedata(1)*(1-thetaB)^onedata(2);
        A(k) = PA(k)/(PA(k)+PB(k));
    end
    AH = sum(A.*data(:,1));
    AT = sum(A.*data(:,2));
    thetaA = AH/(AH+AT);
    resultA(iteration) = thetaA;
    
    B = 1-A;
    BH = sum(B.*data(:,1));
    BT = sum(B.*data(:,2));
    thetaB = BH/(BH+BT);
    resultB(iteration) = thetaB;
end
plot(1:m_step,resultA)
hold on
plot(1:m_step,resultB)

A
resultA(m_step)
resultB(m_step)

结果图
如何感性地理解EM算法?

展开阅读全文
博主设置当前文章不允许评论。

没有更多推荐了,返回首页