隐马尔可夫模型估值问题&解码问题计算器
程序员文章站
2022-04-21 19:13:45
模式识别课上,老师没完没了的让算马尔科夫链,为了加快写作业(摸鱼 )的效率,在python上写了两个函数来帮助计算。这里贴贴记录一下。呆码import numpy as npimport mathT=4 #序列长度为Tvt=[1,3,2,0]#序列状态vn=5 #发生事件的个数c=4 #隐状态个数A=[[ 1, 0, 0, 0], [0.2,0.3,0.1,0.4], [0.2,0.5,0.2,0.1], [0.8,0.1, 0,0.1]]B=[[1,...
模式识别课上,老师没完没了的让算马尔科夫链,为了加快写作业(摸鱼 )的效率,在python上写了两个函数来帮助计算。这里贴贴记录一下。
呆码
import numpy as np
import math
T=4 #序列长度为T
vt=[1,3,2,0]#序列状态
vn=5 #发生事件的个数
c=4 #隐状态个数
A=[[ 1, 0, 0, 0],
[0.2,0.3,0.1,0.4],
[0.2,0.5,0.2,0.1],
[0.8,0.1, 0,0.1]]
B=[[1, 0, 0, 0, 0],
[0,0.3,0.4,0.1,0.2],
[0,0.1,0.1,0.7,0.1],
[0,0.5,0.2,0.1,0.2]]
C=[[0 for i in range(T+1)] for j in range(c)]
C[1][0]=1 #假设初始状态是w1
for j in range(1,len(C[0])):
for i in range(len(C)):
for k in range(len(C)):
C[i][j]+=C[k][j-1]*A[k][i]*B[i][vt[j-1]]
print("估值问题")
for i in range(len(C)):
print(C[i])
C=[[0 for i in range(T+1)] for j in range(c)]
C[1][0]=1 #假设初始状态是w1
for j in range(1,len(C[0])):
for i in range(len(C)):
for k in range(len(C)):
if C[i][j]==0:
C[i][j]=C[k][j-1]*A[k][i]*B[i][vt[j-1]]
elif C[k][j-1]*A[k][i]*B[i][vt[j-1]]>C[i][j]:
C[i][j]=C[k][j-1]*A[k][i]*B[i][vt[j-1]]
print("解码问题")
for i in range(len(C)):
print(C[i])
运行效果:
估值Example:
解码Example:
程序计算结果:
本文地址:https://blog.csdn.net/weixin_45435206/article/details/109953395