Skip to content

Commit 2ca5658

Browse files
author
Hamid Gasmi
committed
#184 is completed
1 parent cfa2c15 commit 2ca5658

File tree

1 file changed

+17
-2
lines changed
  • 06-dynamic-programming-applications-in-machine-learning-and-genomics/3-hidden-markov-models-for-sequence-alignment-1

1 file changed

+17
-2
lines changed

06-dynamic-programming-applications-in-machine-learning-and-genomics/3-hidden-markov-models-for-sequence-alignment-1/outcome_likelihood.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import sys
22

3-
def outcome_likelihood(x,sigma,states,transition,emission):
3+
def outcome_likelihood(x, sigma, states, transition, emission):
44

5-
return 0
5+
# The transitions from the initial state occur with equal probability: Prob(P0 = Si) = 1/|states| * Pr(x = x0 | P0)
6+
M = [ [ emission[ states[s] ][ x[0] ]/len(states) if p == 0 else 0 for p in range(len(x))] for s in range(len(states)) ]
7+
8+
for p in range(len(x) - 1):
9+
next_x = x[p + 1]
10+
11+
for s in range(len(states)):
12+
M[s][p + 1] = M[0][p] * transition[ states[0] ][ states[s] ] * emission[ states[s] ][ next_x ]
13+
14+
for s in range(1, len(states)):
15+
for next_s in range(len(states)):
16+
M[next_s][p + 1] += M[s][p] * transition[ states[s] ][ states[next_s] ] * emission[ states[next_s] ][ next_x ]
17+
prob_x = 0
18+
for s in range(len(states)):
19+
prob_x += M[s][len(x) - 1]
20+
return prob_x
621

722
if __name__ == "__main__":
823
x = sys.stdin.readline().strip()

0 commit comments

Comments
 (0)