Hidden Markov Model has three problems:
1) estimation problem: given an observation sequence o = o1o2... OT and model u = (a, B, π), how to quickly calculate the probability of observing the sequence o under the given model u, that is, P (o | U)
2) sequence problem: Given the observation sequence o = o1o2... OT and model u = (a, B, π), how to quickly and effectively select the "Optimal" state sequence q = q1q2... qt, so that the state sequence is "best interpreted" to observe the sequence?
3) Training problem or parameter estimation: given an observation sequence o = o1o2... ot, how can we obtain the model parameter value based on the maximum likelihood estimation? That is, how to adjust the parameters of Model U = (a, B, π) to make P (o | u) The largest?
Question 1 is a decoding problem. forward algorithms can solve this problem (Forward procedure) and are also an application of dynamic programming.
For more information about the forward algorithm, see the following article.
Http://www.cnblogs.com/tornadomeet/archive/2012/03/24/2415583.html
Ruby code
class FowardAlgorithm def initialize(pi, trans_pro, emit_pro) @pi = pi @trans_pro = trans_pro @emit_pro = emit_pro end def alpha1(state, ob) @pi[state] * @emit_pro[state][ob] end def alpha(t, state) # t starts from 0, states starts from 9 if t.equal? 0 alpha1(state, @ob[0]) else sum = ([email protected]_pro.size).inject(0.0) { |sum, i| sum += alpha(t-1, i) * @trans_pro[i][state] } sum * @emit_pro[state][@ob[t]] end end def p_rerial_n ob n = ob.size n -= 1 @ob = ob # time & states res = 0.0 [email protected]_pro.size.times { |i| #print "time #{n}, state #{i}" tmp = alpha n, i res += tmp } res endenddef test pi = [0.2, 0.4, 0.4] trans_pro = [ [0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5] ] emit_pro = [ [0.5, 0.5], [0.4, 0.6], [0.7, 0.3] ] f = FowardAlgorithm.new pi, trans_pro, emit_pro puts f.p_rerial_n([0, 1, 0, 1])endif $PROGRAM_NAME == __FILE__ testend