當前位置 主頁 > 網站技術 > 代碼類 > 最大化 縮小

    Python實現隱馬爾可夫模型的前向后向算法的示例代碼

    欄目:代碼類 時間:2019-12-31 15:09

    本篇文章對隱馬爾可夫模型的前向和后向算法進行了Python實現,并且每種算法都給出了循環和遞歸兩種方式的實現。

    前向算法Python實現

    循環方式

    import numpy as np
    def hmm_forward(Q, V, A, B, pi, T, O, p):
      """
      :param Q: 狀態集合
      :param V: 觀測集合
      :param A: 狀態轉移概率矩陣
      :param B: 觀測概率矩陣
      :param pi: 初始概率分布
      :param T: 觀測序列和狀態序列的長度
      :param O: 觀測序列
      :param p: 存儲各個狀態的前向概率的列表,初始為空
      """
      for t in range(T):
        # 計算初值
        if t == 0:
          for i in range(len(Q)):
            p.append(pi[i] * B[i, V[O[0]]])
        # 初值計算完畢后,進行下一時刻的遞推運算
        else:
          alpha_t_ = 0
          alpha_t_t = []
          for i in range(len(Q)):
            for j in range(len(Q)):
              alpha_t_ += p[j] * A[j, i]
            alpha_t_t.append(alpha_t_ * B[i, V[O[t]]])
            alpha_t_ = 0
          p = alpha_t_t
      return sum(p)
    # 《統計學習方法》書上例10.2
    Q = [1, 2, 3]
    V = {'紅':0, '白':1}
    A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]])
    B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]])
    pi = [0.2, 0.4, 0.4]
    T = 3
    O = ['紅', '白', '紅']
    p = []
    print(hmm_forward(Q, V, A, B, pi, T, O, p)) # 0.130218

    遞歸方式

    import numpy as np
    def hmm_forward_(Q, V, A, B, pi, T, O, p, T_final):
      """
      :param T_final:遞歸的終止條件
      """
      if T == 0:
        for i in range(len(Q)):
          p.append(pi[i] * B[i, V[O[0]]])
      else:
        alpha_t_ = 0
        alpha_t_t = []
        for i in range(len(Q)):
          for j in range(len(Q)):
            alpha_t_ += p[j] * A[j, i]
          alpha_t_t.append(alpha_t_ * B[i, V[O[T]]])
          alpha_t_ = 0
        p = alpha_t_t
      if T >= T_final:
        return sum(p)
      return hmm_forward_(Q, V, A, B, pi, T+1, O, p, T_final)
    
    Q = [1, 2, 3]
    V = {'紅':0, '白':1}
    A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]])
    B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]])
    pi = [0.2, 0.4, 0.4]
    T = 0
    O = ['紅', '白', '紅']
    p = []
    T_final = 2 # T的長度是3,T的取值是(0時刻, 1時刻, 2時刻)
    print(hmm_forward_(Q, V, A, B, pi, T, O, p, T_final))

    后向算法Python實現

    循環方式

    import numpy as np
    def hmm_backward(Q, V, A, B, pi, T, O, beta_t, T_final):
      for t in range(T, -1, -1):
        if t == T_final:
          beta_t = beta_t
        else:
          beta_t_ = 0
          beta_t_t = []
          for i in range(len(Q)):
            for j in range(len(Q)):
              beta_t_ += A[i, j] * B[j, V[O[t + 1]]] * beta_t[j]
            beta_t_t.append(beta_t_)
            beta_t_ = 0
          beta_t = beta_t_t
        if t == 0:
          p=[]
          for i in range(len(Q)):
            p.append(pi[i] * B[i, V[O[0]]] * beta_t[i])
          beta_t = p
      return sum(beta_t)
    # 《統計學習方法》課后題10.1
    Q = [1, 2, 3]
    V = {'紅':0, '白':1}
    A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]])
    B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]])
    pi = [0.2, 0.4, 0.4]
    T = 3
    O = ['紅', '白', '紅', '白']
    beta_t = [1, 1, 1]
    T_final = 3
    print(hmm_backward_(Q, V, A, B, pi, T, O, beta_t, T_final)) # 0.06009
    
    
    
    下一篇:沒有了
青海十一选五开奖数据