def viterbi(seq, filenameEmission, filenameTransmission): forwardGraph = graph.createGraph(filenameEmission, filenameTransmission) #print forwardGraph char = seq[1] states = forwardGraph["E"]["E", "States"] statesCompl = states[:] statesCompl.insert(0, "0") statesCompl.append("0") m = matrix.createMatrix(len(states) + 2, len(seq), 0) trace = matrix.createMatrix(len(states) + 2, len(seq), -1) # intialisation part matrix.atualiseValueIntoMatrix(m, 0, 0, 1) # recurrence part # recurrence 1 trace[1][1] = trace[2][1] = 0 for i in range(1, len(m) - 1): m[i][1] = float(forwardGraph["A"]["0", statesCompl[i]]) * float( forwardGraph["E"][statesCompl[i], char]) # recurrence 2 for j in range(2, len(m[0]) - 1): #jump the first 2 collumns char = seq[j] for i in range(1, len(m) - 1): # jump the begin and end states scores = [] for l in range(1, len(m) - 1): #print "L:",statesCompl[l],", K:",statesCompl[i] scores.append( float(m[l][j - 1]) * float(forwardGraph["A"][statesCompl[l], statesCompl[i]]) * float(forwardGraph["E"][statesCompl[i], char])) '''print "partial value: ",scores[-1] print "lines value:", float(m[l][j-1]) print "AKL: ",float(forwardGraph["A"][statesCompl[l],statesCompl[i]]) print "Elc: ", float(forwardGraph["E"][statesCompl[i],char])''' m[i][j] = max(scores) #print "Max: ", scores.index(max(scores))+1 trace[i][j] = scores.index(max(scores)) + 1 #print (m[i][j]) #print "---------------------------------" del (scores) scores = [] for i in range(1, len(m) - 1): scores.append( float(m[i][len(m[0]) - 2]) * float(forwardGraph["A"][statesCompl[i], "0"])) m[len(m) - 1][len(m[0]) - 1] = max(scores) trace[len(m) - 1][len(m[0]) - 1] = scores.index(max(scores)) + 1 #print "__________________________" #matrix.printMatrix(m) #matrix.printMatrix(trace) return trace
def aPosterioriDecoding(seq, fileNameEmis, fileNameTrans): statesGraph = graph.createGraph(fileNameEmis, fileNameTrans) # it's a dictionary f = forward(seq, fileNameEmis, fileNameTrans) b = backward(seq, fileNameEmis, fileNameTrans) seq = seq[1:-1] psm = f[len(f) - 1][len(f[0]) - 1] pi = matrix.createMatrix(len(statesGraph["A"]["A", "States"]), len(seq), 0.) print "F:" matrix.printMatrix(f) print "B:" matrix.printMatrix(b) print "Seq:", seq print "Size of Seq: ", len(seq) for i in range(len(seq)): #pi(i) -> position in the sequence = collunms scores = [] for k in range(1, len(statesGraph["A"][ "A", "States"])): #num of states that produce chars = lines scores.append((f[k][i + 1] * b[k][i + 1]) / psm) pi[0][i] = scores.index(max(scores)) + 1 #states are from 1 to lines-1 pi[1][i] = scores[0] pi[2][i] = scores[1] del (scores) return pi
def backward(seq, filenameEmission, filenameTransmission): backwardGraph = graph.createGraph(filenameEmission, filenameTransmission) #print backwardGraph char = seq[len(seq) - 2] states = backwardGraph["E"]["E", "States"] statesCompl = states[:] statesCompl.insert(0, "0") statesCompl.append("0") seq = seq[:-1] m = matrix.createMatrix(len(states) + 2, len(seq), 0) # intialisation part # the final state for i in range(1, len(m) - 1): m[i][len(m[0]) - 1] = float(backwardGraph["A"][statesCompl[i], "0"]) #matrix.printMatrix(m) # recurrence for j in range(len(m[0]) - 2, 0, -1): #jump the first 2 collumns char = seq[j + 1] #print "char:", char for i in range(1, len(m) - 1): # jump the begin and end states for l in range(len(m) - 2, 0, -1): #print "L:",statesCompl[l],", K:",statesCompl[i] m[i][j] += float(m[l][j + 1]) * float(backwardGraph["A"][ statesCompl[i], statesCompl[l]]) * float( backwardGraph["E"][statesCompl[l], char]) ''' print "partial value: ",float(m[i][j]) print "lines value:", float(m[l][j+1]) print "Alk: ",float(backwardGraph["A"][statesCompl[i],statesCompl[l]]) print "Elc: ", float(backwardGraph["E"][statesCompl[l],char])''' for l in range(1, len(m) - 1): m[0][0] += float(m[l][1]) * float( backwardGraph["A"]["0", statesCompl[l]]) * float( backwardGraph["E"][statesCompl[l], seq[1]]) '''print "psm: ",psm print "lines value:", float(m[l][j+1]) print "Alk: ",float(backwardGraph["A"]["0",statesCompl[l]]) print "Elc: ", float(backwardGraph["E"][statesCompl[l],seq[1]]) print "Char: ", seq[1]''' #matrix.printMatrix(m) return m
def forward(seq, filenameEmission, filenameTransmission): forwardGraph = graph.createGraph(filenameEmission, filenameTransmission) #print forwardGraph char = seq[1] states = forwardGraph["E"]["E", "States"] statesCompl = states[:] statesCompl.insert(0, "0") statesCompl.append("0") statesCompl = ["0", "Y", "N", "0"] print statesCompl m = matrix.createMatrix(len(states) + 2, len(seq), 0) # intialisation part matrix.atualiseValueIntoMatrix(m, 0, 0, 1) # recurrence part # recurrence 1 for i in range(1, len(m) - 1): m[i][1] = float(forwardGraph["A"]["0", statesCompl[i]]) * float( forwardGraph["E"][statesCompl[i], char]) # recurrence 2 for j in range(2, len(m[0]) - 1): #jump the first 2 collumns char = seq[j] for i in range(1, len(m) - 1): # jump the begin and end states for l in range(1, len(m) - 1): #print "L:",statesCompl[l],", K:",statesCompl[i] m[i][j] += float(m[l][j - 1]) * float( forwardGraph["A"][statesCompl[l], statesCompl[i]]) * float( forwardGraph["E"][statesCompl[i], char]) '''print "partial value: ",float(m[i][j]) print "lines value:", float(m[l][j-1]) print "AKL: ",float(forwardGraph["A"][statesCompl[l],statesCompl[i]]) print "Elc: ", float(forwardGraph["E"][statesCompl[i],char])''' for i in range(1, len(m) - 1): m[len(m) - 1][len(m[0]) - 1] += float(m[i][len(m[0]) - 2]) * float( forwardGraph["A"][statesCompl[i], "0"]) matrix.printMatrix(m) return m
upperyearcourses = ['COSC 222', 'MATH 200', 'PHYS 216', 'BIOL 200', 'CHEM 201'] allcourses = loweryearcourses + upperyearcourses #all the majors that we are studying majors = ['COSC', 'MATH', 'PHYS', 'BIOL', 'CHEM'] #the possible decisions and their utilities enjoy_and_do_well = 10 enjoy_and_do_bad = 0 dislike_and_do_well = 0 dislike_and_do_bad = -10 do_not_take_course = 5 #The different lotteries lottery1 = [ enjoy_and_do_well, enjoy_and_do_bad, dislike_and_do_well, dislike_and_do_bad ] lottery2 = [do_not_take_course] #extract the students from the csv file lower_year_students = extract.lower_year upper_year_students = extract.upper_year #create the grade matrices of the students loweryeargrades = matrix.createMatrix(lower_year_students) upperyeargrades = matrix.createMatrix(upper_year_students) #extract the averages of all the students upperyearaverages = [student.average for student in upper_year_students] loweryearaverages = [student.average for student in lower_year_students]