Forward algorithm written by ruby

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.