# 隐马尔可夫模型估值问题&解码问题计算器

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])


09-09 4809
02-23 1万+
03-23 788
09-21 111
02-21 4万+
01-04 273
07-11 2792
08-30 1183
08-22 573
05-11 1751
09-17 825
12-09 20万+
03-01 5575
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林