-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathviterbi.nim
81 lines (71 loc) · 2.69 KB
/
viterbi.nim
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
## Viterbi
{.push raises: [].}
type
HiddenMarkovModel[S] = ref object
states: seq[S]
startProbability: seq[float] # Sum of all elements must be 1
transitionProbability: seq[seq[float]] # Sum of all elements in each row must be 1
emissionProbability: seq[seq[float]] # Sum of all elements in each row must be 1
func viterbi*[S, O](hmm: HiddenMarkovModel[S], observations: seq[O]): seq[S] =
var
probabilities = newSeq[seq[float]](len(observations))
backpointers = newSeq[seq[int]](len(observations))
# Initialization
for i in 0 ..< len(observations):
probabilities[i] = newSeq[float](len(hmm.states))
backpointers[i] = newSeq[int](len(hmm.states))
for state in 0 ..< len(hmm.states):
probabilities[0][state] = hmm.startProbability[state] *
hmm.emissionProbability[state][observations[0].ord]
backpointers[0][state] = 0
# Forward Pass - Derive the probabilities
for nObs in 1 ..< len(observations):
var
obs = observations[nObs].ord
for state in 0 ..< len(hmm.states):
# Compute the argmax for probability of the current state
var
maxProb = -1.0
maxProbState = 0
for priorState in 0 ..< len(hmm.states):
var
prob = probabilities[nObs - 1][priorState] *
hmm.transitionProbability[priorState][state] *
hmm.emissionProbability[state][obs]
if prob > maxProb:
maxProb = prob
maxProbState = priorState
# Update probabilities and backpointers
probabilities[nObs][state] = maxProb
backpointers[nObs][state] = maxProbState
# Final observation
var
maxProb = -1.0
maxProbState = 0
for state in 0 ..< len(hmm.states):
if probabilities[len(observations) - 1][state] > maxProb:
maxProb = probabilities[len(observations) - 1][state]
maxProbState = state
result = newSeq[S](len(observations))
result[^1] = hmm.states[maxProbState]
# Backward Pass - Derive the states from the probabilities
for i in 1 ..< len(observations):
result[^(i+1)] = hmm.states[backpointers[^i][maxProbState]]
maxProbState = backpointers[^i][maxProbState]
when isMainModule:
import std/unittest
suite "Viterbi algorithm":
test "Example from Wikipedia":
type
States = enum
Healthy, Fever
Observations = enum
Normal, Cold, Dizzy
var
hmm = HiddenMarkovModel[States]()
observations = @[Normal, Cold, Dizzy]
hmm.states = @[Healthy, Fever]
hmm.startProbability = @[0.6, 0.4]
hmm.transitionProbability = @[@[0.7, 0.3], @[0.4, 0.6]]
hmm.emissionProbability = @[@[0.5, 0.4, 0.1], @[0.1, 0.3, 0.6]]
check viterbi(hmm, observations) == @[Healthy, Healthy, Fever]