Commit eda971ca authored by Mikael Boden's avatar Mikael Boden

HMM_added

parent f9a4551b
......@@ -865,24 +865,30 @@ class HMM():
> states = cpg_hmm.viterbi(X)
> print(states)
"""
X = _terminate(symseq, 1, self.startsym, self.endsym)
X = _terminate(symseq, 1, self.startsym, self.endsym) # put start and end symbols on sequence
# Initialise state scores for each index in X
for state in self.mystates:
# Fill in emission probabilities for each index in X
# Fill in emission probabilities in V for each index of X
# (only the first position is really needed)
V[state] = [self.e[state][x] for x in X]
trace[state] = []
# Next loop through the sequence
for j in range(len(X) - 1):
i = j + 1 # sequence index that we're processing
for tostate in self.mystates:
i = j + 1 # sequence index that we're processing start with 1, not 0
for tostate in self.mystates: # check each state for i = 1, ...
tracemax = 0
beststate = None
beststate = None # the state v with max[Vv(i-1) * t(v,u)]
for fromstate in self.mystates:
# determine the best score propagated forward from previous state
score = V[fromstate][i - 1] * self.a[fromstate][tostate]
if score > tracemax:
beststate = fromstate
tracemax = score
# record the transition that will appear in the traceback
trace[tostate].append(beststate)
# finalise the dynamic programming score for current i in state u
V[tostate][i] = self.e[tostate][X[i]] * tracemax
# finally, assemble the string that describes the most probable path
ret = ''
traced = '$'
for j in range(len(X)):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment