Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fixed viterbi algorithm (issue #1126) #1129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import random
from collections import defaultdict
from functools import reduce
import numpy as np


# ______________________________________________________________________________
Expand Down Expand Up @@ -687,28 +688,42 @@ def forward_backward(HMM, ev):

def viterbi(HMM, ev):
"""[Equation 15.11]
Viterbi algorithm to find the most likely sequence. Computes the best path,
Viterbi algorithm to find the most likely sequence. Computes the best path and the corresponding probabilities,
given an HMM model and a sequence of observations."""
t = len(ev)
ev = ev.copy()
ev.insert(0, None)

m = [[0.0, 0.0] for _ in range(len(ev) - 1)]

# the recursion is initialized with m1 = forward(P(X0), e1)
m[0] = forward(HMM, HMM.prior, ev[1])
# keep track of maximizing predecessors
backtracking_graph = []

for i in range(1, t):
m[i] = element_wise_product(HMM.sensor_dist(ev[i + 1]),
[max(element_wise_product(HMM.transition_model[0], m[i - 1])),
max(element_wise_product(HMM.transition_model[1], m[i - 1]))])
backtracking_graph.append([np.argmax(element_wise_product(HMM.transition_model[0], m[i - 1])),
np.argmax(element_wise_product(HMM.transition_model[1], m[i - 1]))])

# computed probabilities
ml_probabilities = [0.0] * (len(ev) - 1)
# most likely sequence
ml_path = [True] * (len(ev) - 1)

path = [0.0] * (len(ev) - 1)
# the construction of the most likely sequence starts in the final state with the largest probability,
# and runs backwards; the algorithm needs to store for each xt its best predecessor xt-1
for i in range(t, -1, -1):
path[i - 1] = max(m[i - 1])
# and runs backwards; the algorithm needs to store for each xt its predecessor xt-1 maximizing its probability
i_max = np.argmax(m[-1])

for i in range(t - 1, -1, -1):
ml_probabilities[i] = m[i][i_max]
ml_path[i] = True if i_max == 0 else False
if i > 0:
i_max = backtracking_graph[i - 1][i_max]

return path
return ml_path, ml_probabilities


# _________________________________________________________________________
Expand Down
6 changes: 4 additions & 2 deletions tests/test_probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,12 @@ def test_viterbi():
umbrellaHMM = HiddenMarkovModel(umbrella_transition, umbrella_sensor)

umbrella_evidence = [T, T, F, T, T]
assert rounder(viterbi(umbrellaHMM, umbrella_evidence)) == [0.8182, 0.5155, 0.1237, 0.0334, 0.0210]
assert viterbi(umbrellaHMM, umbrella_evidence)[0] == [T, T, F, T, T]
assert rounder(viterbi(umbrellaHMM, umbrella_evidence)[1]) == [0.8182, 0.5155, 0.1237, 0.0334, 0.0210]

umbrella_evidence = [T, F, T, F, T]
assert rounder(viterbi(umbrellaHMM, umbrella_evidence)) == [0.8182, 0.1964, 0.053, 0.0154, 0.0042]
assert viterbi(umbrellaHMM, umbrella_evidence)[0] == [T, F, F, F, T]
assert rounder(viterbi(umbrellaHMM, umbrella_evidence)[1]) == [0.8182, 0.1964, 0.0275, 0.0154, 0.0042]


def test_fixed_lag_smoothing():
Expand Down