From eda971ca78e983bbeabde23ceb91cd3e1fb99140 Mon Sep 17 00:00:00 2001
From: Mikael Boden
Date: Thu, 23 Aug 2018 06:51:01 +1000
Subject: [PATCH] HMM_added
---
prob.py | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/prob.py b/prob.py
index cf4a360..52b15a3 100644
--- a/prob.py
+++ b/prob.py
@@ -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)):
--
2.18.1