Skip to content

Commit

Permalink
04commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuoyuan yao committed Jan 30, 2020
1 parent aa78a0f commit 8eb87e3
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions 04-HMM/hmm.py
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)

0 comments on commit 8eb87e3

Please sign in to comment.