Prepare the following exercises for discussion in class on Thursday, November 24.
Alice believes that the weather of a day depends only on the weather of the previous day. There are three states, “Rainy”, “Sunny” and “Foggy”, but she cannot observe them directly. On each day, there is a certain chance that Bob will perform one of the following activities, depending on the weather: "walk" or "read". Bob tells Alice about his activities.
observations = ['walk', 'read', 'walk'] start_probability = {'Rainy': 0.5, 'Sunny': 0.4, 'Foggy': 0.1} transition_probability = { 'Rainy' : {'Rainy': 0.6, 'Sunny': 0.2, 'Foggy': 0.2}, 'Sunny' : {'Rainy': 0.05, 'Sunny': 0.8, 'Foggy': 0.15}, 'Foggy' : {'Rainy': 0.3, 'Sunny': 0.2, 'Foggy': 0.5}, } emission_probability = { 'Rainy' : {'walk': 0.2, 'read': 0.8}, 'Sunny' : {'walk': 0.9, 'read': 0.1}, 'Foggy' : {'walk': 0.7, 'read': 0.3}, } |
Derive manually the computations for answering the following questions.
Check the correctness of your answer to the second and third questions running on the appropriate data the Forward and the Viterbi algorithm implemented below.
aFirst = array([0.5,0.4,0.1]) a = array([[.6,.2,.2],[.05,.8,.15],[.3,.2,.5]]) ## trans b = array([[.2,.8],[.9,.1],[.7,.3]]) ## emit obs = array([0,1,0]) def Forward(start_p,trans_p,emit_p,obs): nStates = shape(b)[0] T = shape(obs)[0] alpha = zeros((nStates,T)) alpha[:,0] = start_p*emit_p[:,obs[0]] for t in range(1,T): for s in range(nStates): ## elapse time alpha[s,t] = sum(alpha[:,t-1] * trans_p[:,s]) ## observe alpha[s,t] = alpha[s,t] * emit_p[s,obs[t]] return alpha def ViterbiSimple(start_p,trans_p,emit_p,obs): nStates = shape(b)[0] T = shape(obs)[0] path = zeros(T) m = zeros((nStates,T)) m[:,0] = start_p * emit_p[:,obs[0]] path[0] = argmax(m[:,0]) for t in range(1,T): for s in range(nStates): m[s,t] = max( emit_p[s,obs[t]] * trans_p[:,s] * m[:,t-1] ) path[t] = argmax(m[:,t]) print m print "Path: ", path #print m[path[T-1]] return path,m path,m = ViterbiSimple(aFirst,a,b,obs) print [states[int(x)] for x in path] |
Solution of 14.14.
|
|
The last line is isomorphic to the problem with n− 1 variables instead of n; the work done on the first step is a constant independent of n, hence (by induction on n, if you want to be formal) the total work is O(n).