前后向概率计算HMMs——机器学习作业

题目:

 代码:

import numpy as np


def fore_algorithm(A, B, p_i, o, T, N):
    # 设初值,alpha_1(i) = pt_(i)b_i(o(i))
    alpha = np.zeros((T, N))
    for i in range(N):
        h = o[0]
        alpha[0][i] = p_i[i] * B[i][h]
    # 递推
    for t in range(T - 1):
        h = o[t + 1]
        for i in range(N):
            a = 0
            for j in range(N):
                a += (alpha[t][j] * A[j][i])
            alpha[t + 1][i] = a * B[i][h]
    # 终止
    P = 0
    for i in range(N):
        P += alpha[T - 1][i]
    return P, alpha


def back_algorithm(A, B, p_i, o, T, N):
    # 设置初值,beta_t(i)=1
    beta = np.ones((T, N))
    # 递推
    for t in range(T - 1):
        t = T - t - 2
        h = o[t + 1]
        h = int(h)

        for i in range(N):
            beta[t][i] = 0
            for j in range(N):
                beta[t][i] += A[i][j] * B[j][h] * beta[t + 1][j]
    # 终止
    P = 0
    for i in range(N):
        h = o[0]
        h = int(h)
        P += p_i[i] * B[i][h] * beta[0][i]
    return P, beta


if __name__ == "__main__":
    T = 8
    N = 3
    A = [[0.5, 0.1, 0.4], [0.3, 0.5, 0.2], [0.2, 0.2, 0.6]]
    B = [[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]]
    pi = [0.2, 0.3, 0.5]
    O = ['红', '白', '红', '红', '白', '红', '白', '白']
    o = np.zeros(T, np.int)
    for i in range(T):
        if O[i] == '白':
            o[i] = 1
        else:
            o[i] = 0
    PF, alpha = fore_algorithm(A, B, pi, o, T, N)
    PB, beta = back_algorithm(A, B, pi, o, T, N)
    print("PF:", PF, "PB:", PB)
    # P(i_4=q_3|O,\lambda) = alpah_4(3)* beta_4(3)
    P = alpha[4 - 1][3 - 1] * beta[4 - 1][3 - 1]
    print("前向后向概率计算可得 P(i4=q3|O,lambda)=", P / PF)

最终结果:

代码说明:

前向算法:


def fore_algorithm(A, B, p_i, o, T, N):
    # 设初值,alpha_1(i) = pt_(i)b_i(o(i))
    alpha = np.zeros((T, N))
    for i in range(N):
        h = o[0]
        alpha[0][i] = p_i[i] * B[i][h]
    # 递推
    for t in range(T - 1):
        h = o[t + 1]
        for i in range(N):
            a = 0
            for j in range(N):
                a += (alpha[t][j] * A[j][i])
            alpha[t + 1][i] = a * B[i][h]
    # 终止
    P = 0
    for i in range(N):
        P += alpha[T - 1][i]
    return P, alpha

后向算法:

def back_algorithm(A, B, p_i, o, T, N):
    # 设置初值,beta_t(i)=1
    beta = np.ones((T, N))
    # 递推
    for t in range(T - 1):
        t = T - t - 2
        h = o[t + 1]
        h = int(h)

        for i in range(N):
            beta[t][i] = 0
            for j in range(N):
                beta[t][i] += A[i][j] * B[j][h] * beta[t + 1][j]
    # 终止
    P = 0
    for i in range(N):
        h = o[0]
        h = int(h)
        P += p_i[i] * B[i][h] * beta[0][i]
    return P, beta

前后向概率计算:

参考代码及文章:(48条消息) 统计学习方法 第十章习题答案_Brielleqqqqqqjie的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值