-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples.py
60 lines (53 loc) · 1.54 KB
/
examples.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
from phmm import PHMM, PHMM_d
"""
Some example uses of the PHMM and PHMM_d classes. Note that it might take some
finessing to find optimal starting values for the HMM parameters.
"""
def phmm_test():
# Parameters
theta = [[0.5, 0.5],
[0.5, 0.5]]
delta = [1.0, 0.0]
lambdas = [1.0, 5.4]
# Random parameters
theta_2 = [[0.3, 0.7],
[0.7, 0.3]]
delta_2 = [1.0, 0.0]
lambdas_2 = [0.5, 6.7]
# Initialization
# Initialization
h_1 = PHMM(delta, theta, lambdas)
seqs, states = zip(*[h_1.gen_seq() for _ in range(20)])
h_2 = PHMM(delta_2, theta_2, lambdas_2)
h_2.baum_welch(seqs)
print(h_2.transition_matrix())
print(h_2.lambdas)
v_2 = list(map(h_2.viterbi, seqs))
print(list(zip(states[0], v_2[0])))
print(h_2.log_likelihood(seqs))
def phmm_d_test():
# Parameters
theta = [[0.5, 0.5],
[0.5, 0.5]]
delta = [1.0, 0.0]
lambdas = [[1.0, 5.4]] * 10
# Random parameters
theta_2 = [[0.3, 0.7],
[0.7, 0.3]]
delta_2 = [1.0, 0.0]
lambdas_2 = [[0.5, 6.7]] * 10
# Initialization
h_1 = PHMM_d(delta, theta, lambdas)
seqs, states = zip(*[h_1.gen_seq(i) for i in range(10)])
h_2 = PHMM_d(delta_2, theta_2, lambdas_2)
h_2.baum_welch(seqs)
print(h_2.transition_matrix())
print(h_2.lambdas)
v_2 = list(map(h_2.viterbi, range(len(seqs)), seqs))
print(list(zip(states[0], v_2[0])))
print(h_2.log_likelihood(seqs))
def main():
phmm_test()
# phmm_d_test()
if __name__ == "__main__":
main()