def getXi(data, hmm): alpha = a.forward(data, hmm) beta = a.backward(data, hmm) #likelihood=beta[0][0] xi = [[[0,0],[0,0]]] beta.append([[1, 0], [1,0]]) for t in range(len(data)): xi.append([[0,0],[0,0]]) t = 0 for h in range(len(data)): t+=1 for i in range(0, 2): for j in range(0, 2): ptrans = a.getTransitionProbability(i, j, hmm)[0] if t<len(data)-1: pemit = a.getEmissionProbability(j, data[t+1], hmm)[0] else: pemit = 1 #temp = a.multiplyProbability(alpha[t][i], ptrans) #temp = a.multiplyProbability(temp, pemit) #temp = a.multiplyProbability(temp, beta[t+1][j]) #xi[t][i][j]=a.divideProbability(temp, likelihood) #print str(i)+" "+str(j) #print xi #print xi[t][i][j] xi[t][i][j]=alpha[t][i][0]*ptrans*pemit*beta[t+1][j][0] # xi[t][i][j]=temp return xi
def getXi(data, hmm): alpha = a.forward(data, hmm) beta = a.backward(data, hmm) likelihood = beta[0][0] xi = [[[[0, 0], [0, 0]], [[0, 0], [0, 0]]]] for t in range(len(data) - 1): xi.append([[[0, 0], [0, 0]], [[0, 0], [0, 0]]]) data = "#" + data t = 0 for h in range(len(data) - 1): for i in range(0, 2): for j in range(0, 2): ptrans = a.getTransitionProbability(i, j, hmm) pemit = a.getEmissionProbability(j, data[t + 1], hmm) temp = a.multiplyProbability(alpha[t][i], ptrans) temp = a.multiplyProbability(temp, pemit) temp = a.multiplyProbability(temp, beta[t + 1][j]) xi[t][i][j] = a.divideProbability(temp, likelihood) t += 1 return xi