-
Notifications
You must be signed in to change notification settings - Fork 1
/
20180315-hmm.py
83 lines (66 loc) · 2.07 KB
/
20180315-hmm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# ==========================================================================
# Tool for grading HMM
# --------------------------------------------------------------------------
#
# ==========================================================================
states = ('R', 'G', 'Y')
observations = ('Bright', 'Dark')
start_probability = {'R':0.3, 'G':0.4, 'Y':0.3}
#start_probability = {'R':0.5, 'G':0.5, 'Y':0}
transition_probability = {
'R': {'R':0.8,'G':0.1, 'Y':0.1},
'G': {'R':0.0,'G':0.3, 'Y':0.7},
'Y': {'R':0.2,'G':0.5, 'Y':0.3}
}
emission_probability = {
'R' : {'Bright':0.25, 'Dark':0.75},
'G' : {'Bright':0.5, 'Dark':0.5},
'Y' : {'Bright':0.8, 'Dark':0.2}
}
def print_dptable(prior, post):
print(" ",end="")
for i in range(len(prior)):
print("%7d" % i,end="")
print()
print("Prior")
for y in prior[0].keys():
print("%6s: " % y,end="")
for t in range(len(prior)):
print("%7s" % ("%.4f" % prior[t][y]),end="")
print()
print("Post")
for y in post[0].keys():
print("%6s: " % y,end="")
for t in range(len(post)):
print("%7s" % ("%.4f" % post[t][y]),end="")
print()
def hmm(observations, states, start_p, trans_p, emit_p):
post = [{}]
prior = [{}]
for x in states:
prior[0][x] = start_p[x]
post[0][x] = start_p[x]
t = 1
for ob in observations:
post.append({})
prior.append({})
for x in states:
prior[t][x] = 0
post[t][x] = 0
for x in states:
for y in states:
prior[t][y] += post[t-1][x] * trans_p[x][y]
#prior[1]={'R':0.3, 'G':0.4, 'Y':0.3}
#prior[1]={'R':0.31, 'G':0.35, 'Y':0.33}
divider = 0
for x in states:
divider += prior[t][x] * emit_p[x][ob]
for x in states:
post[t][x] = prior[t][x] * emit_p[x][ob] / divider
t = t+1
print_dptable(prior, post)
hmm(observations,
states,
start_probability,
transition_probability,
emission_probability)