def reEstimation(xi, data):
    symbol = "abcdefghijklmnopqrstuvwxyz "
    emiA = {}
    emiB = {}
    trans = {}
    for c in symbol:
        if c != " ":
            emiA[c] = [0.0, 0]
            emiB[c] = [0.0, 0]
        else:
            emiA["space"] = [0.0, 0]
            emiB["space"] = [0.0, 0]
    for i in range(0, 2):
        demo = [0, 0]
        for j in range(0, 2):
            nume = [0, 0]
            t = 0
            for c in range(len(data)):
                nume = a.addProbability(nume, xi[t][i][j])
                demo = a.addProbability(demo, xi[t][i][j])
                t += 1
            trans[i * 10 + j] = nume
        for j in range(0, 2):
            trans[i * 10 + j] = a.divideProbability(trans[i * 10 + j], demo)
    for j in range(0, 2):
        demo = [0, 0]
        for s in symbol:
            nume = [0, 0]
            for i in range(0, 2):
                t = 0
                for c in range(len(data)):
                    if data[t] == s:
                        nume = a.addProbability(nume, xi[t][i][j])
                        demo = a.addProbability(demo, xi[t][i][j])
                    t += 1
            if j == 0:
                if s != " ":
                    emiA[s] = nume
                else:
                    emiA["space"] = nume
            else:
                if s != " ":
                    emiB[s] = nume
                else:
                    emiB["space"] = nume
        for s in symbol:
            if j == 0:
                if s != " ":
                    emiA[s] = a.divideProbability(emiA[s], demo)
                else:
                    emiA["space"] = a.divideProbability(emiA["space"], demo)
            else:
                if s != " ":
                    emiB[s] = a.divideProbability(emiB[s], demo)
                else:
                    emiB["space"] = a.divideProbability(emiB["space"], demo)
    return (emiA, emiB, trans)
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
Пример #3
0
import avgll as a
import math
def get(tup):
	return math.log(tup[0], 2)+tup[1]*math.log(1e-6, 2)

m = [[0.1, 0], [0.0003, 0], [0.007, 1], [0.224, 5], [0.33, 2], [0.44, 4]]

for i in m:
	for j in m:
		#print str(i)+"\t/\t"+str(j)+"\t=",
		result = a.divideProbability(i, j)
		#check1 = get(i)-get(j)
		#check2 = get(result)
		#if round(check1, 8)==round(check2,8):
		#	print True
		#else:
		#	print i,
		#	print j,
		#	print result
#
#			print check1,
#			print check2
		result = a.getMaxProbability(i, j)
		print str(i)+"\t \t"+str(j)+"\t=",
		print result