-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhuoyuan yao
committed
Jan 30, 2020
1 parent
aa78a0f
commit 8eb87e3
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Author: Kaituo Xu, Fan Yu | ||
|
||
def forward_algorithm(O, HMM_model): | ||
"""HMM Forward Algorithm. | ||
Args: | ||
O: (o1, o2, ..., oT), observations | ||
HMM_model: (pi, A, B), (init state prob, transition prob, emitting prob) | ||
Return: | ||
prob: the probability of HMM_model generating O. | ||
""" | ||
pi, A, B = HMM_model | ||
T = len(O) | ||
N = len(pi) | ||
prob = 0.0 | ||
# Begin Assignment | ||
|
||
# Put Your Code Here | ||
|
||
# End Assignment | ||
return prob | ||
|
||
|
||
def backward_algorithm(O, HMM_model): | ||
"""HMM Backward Algorithm. | ||
Args: | ||
O: (o1, o2, ..., oT), observations | ||
HMM_model: (pi, A, B), (init state prob, transition prob, emitting prob) | ||
Return: | ||
prob: the probability of HMM_model generating O. | ||
""" | ||
pi, A, B = HMM_model | ||
T = len(O) | ||
N = len(pi) | ||
prob = 0.0 | ||
# Begin Assignment | ||
|
||
# Put Your Code Here | ||
|
||
# End Assignment | ||
return prob | ||
|
||
|
||
def Viterbi_algorithm(O, HMM_model): | ||
"""Viterbi decoding. | ||
Args: | ||
O: (o1, o2, ..., oT), observations | ||
HMM_model: (pi, A, B), (init state prob, transition prob, emitting prob) | ||
Returns: | ||
best_prob: the probability of the best state sequence | ||
best_path: the best state sequence | ||
""" | ||
pi, A, B = HMM_model | ||
T = len(O) | ||
N = len(pi) | ||
best_prob, best_path = 0.0, [] | ||
# Begin Assignment | ||
|
||
# Put Your Code Here | ||
|
||
# End Assignment | ||
return best_prob, best_path | ||
|
||
|
||
if __name__ == "__main__": | ||
color2id = { "RED": 0, "WHITE": 1 } | ||
# model parameters | ||
pi = [0.2, 0.4, 0.4] | ||
A = [[0.5, 0.2, 0.3], | ||
[0.3, 0.5, 0.2], | ||
[0.2, 0.3, 0.5]] | ||
B = [[0.5, 0.5], | ||
[0.4, 0.6], | ||
[0.7, 0.3]] | ||
# input | ||
observations = (0, 1, 0) | ||
HMM_model = (pi, A, B) | ||
# process | ||
observ_prob_forward = forward_algorithm(observations, HMM_model) | ||
print(observ_prob_forward) | ||
|
||
observ_prob_backward = backward_algorithm(observations, HMM_model) | ||
print(observ_prob_backward) | ||
|
||
best_prob, best_path = Viterbi_algorithm(observations, HMM_model) | ||
print(best_prob, best_path) |