Forward–backward algorithm: Difference between revisions

Content deleted Content added
Line 304:
observations = ('normal', 'cold', 'dizzy')
transition_probability = {
start_probability = {'Healthy': 0.6, 'Fever': 0.4}
transition_probability = {
'Healthy' : {'Healthy': 0.69, 'Fever': 0.3, 'E': 0.01},
'Fever' : {'Healthy': 0.4, 'Fever': 0.59, 'E': 0.01},
}
start_probability = {'Healthy': 0.6, 'Fever': 0.4}
 
emission_probability = {
'Healthy' : {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1},
Line 323 ⟶ 321:
L = len(x)
 
fwd = []
f_prev = {}
# Run forward
for il, x_i in enumerate(x):
f_curr fwd = {}[]
f_prev = {}
f_curr = {}
for st in states:
if i == 0:
# Initialize base fwd cases
prev_f_sum = a_0[st]
fwd.append(f_curr)
f_prev = f_curr
 
p_fwd = sum(f_curr[kl]*a[kl][end_st] for k in states)
 
bkw = []
Line 342 ⟶ 339:
# Run bkw
p_bkw = sum(a_0[l1] * e[l][x[01]] * b_curr[l0] for l in states)
 
posterior = {}
Line 365 ⟶ 362:
def example():
return fwd_bkw(observations,
states,)
start_probability,
transition_probability,
emission_probability,
end_state)
for line in example():
print ' '.join(map(str, line))