-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHmmCell.py
More file actions
95 lines (64 loc) · 1.41 KB
/
HmmCell.py
File metadata and controls
95 lines (64 loc) · 1.41 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
## Functions ##
def addMeasure(measure, count):
measurements.append(measure)
quantity.append(count)
# Map measurement to matrix
def getObs(measure):
if measure == 'occ':
return B_o
elif measure == 'free':
return B_f
elif measure == 'no':
return B_n
else:
raise ValueError('Unspecified measurement was used')
def getTransition():
return np.matrix( [[ 1 - p_fo, p_fo],
[ p_of, 1 - p_of]])
def updateParam(measure):
calcGamma(measure)
calcPhi(measure)
updateA
calcQ()
def calcGamma(measure):
for l in range(len(A_)):
for h in range(len(A_)):
gamma[l][h] = A_[l][h]
def calcPhi(measure, gamma):
def calcQ():
## Measurement inputs ##
measurements = []
quantity = []
addMeasure( 'occ', 10)
addMeasure( 'free', 10)
addMeasure( 'no', 100)
addMeasure( 'occ', 0)
## Constatns ##
B = np.matrix([[ 0.8, 0.2, 0.5],
[ 0.2, 0.8, 0.5]])
B_n = np.matrix([[ 0.5, 0],
[ 0, 0.5]])
B_o = np.matrix([[ 0.8, 0],
[ 0, 0.2]])
B_f = np.matrix([[ 0.2, 0],
[ 0, 0.8]])
## Initialization ##
gamma = []
phi = []
A_ = [[ 0.8, 0.2],
[ 0.1, 0.9]]
B_ = [[ 0.7, 0.2, 0.1],
[ 0.2, 0.7, 0.1]]
Q_ = [ 0.5, 0.5]
Q = np.matrix([0.5, 0.5])
p_of = 0.8
p_fo = 0.1
for i in range(len(measurements)):
for j in range(quantity[i]):
Q = Q * getTransition() * getObs(measurements[i])
Q = Q / Q.sum(axis = 1)
print Q
print A_
print B_
print Q_