from scipy import stats
import numpy as np
observations = np.array([[1, 0, 0, 0, 1, 1, 0, 1, 0, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 1, 0, 1, 1],
[1, 0, 1, 0, 0, 0, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 1, 1, 1, 0, 1]])
def em(observations, prior, tol=1e-6, iterations=10):
import math
iteration = 0
while iteration < iterations:
print "iter %s" % iteration
new_prior = em_single(prior, observations)
delta_change = np.abs(prior[0] - new_prior[0])
if delta_change < tol:
break
else:
prior = new_prior
iteration += 1
print '\n'
return [new_prior, iteration]
def em_single(priors, observations):
"""
EM
Arguments
---------
priors : [theta_A, theta_B]