p_fwd = 0.0
for(k in self.states):
p_fwd += f_previous[k] * self.transition_probability[k][self.end_state]
{'probability': p_fwd, 'sequence': forward}
The forward algorithm will go through each state at each observation and multiply
them together to get a forward probability of how the state works in this given con‐
text. Next, we need to define the backward algorithm, which is:
class ForwardBackward:
# __init__
# forward
def backward():
backward = []
b_prev = {}
for(i in xrange(len(self.observations), 0, -1)):
b_curr = {}
for(state in self.states):
if i == 0:
b_curr[state] = self.transition_probability[state][self.end_state]
else: ...