# HMM算法

## HMM算法

1. Generating Patterns
Generating Patterns包括determination pattern和non- determiniation pattern 两种 ，在non-determiniation中，我们假设当下的状态仅与前k个状态有关，这种假设下得到的模型也就是K阶马尔科夫模型。当k=1时，得到1阶马尔科夫模型。
一阶马氏模型有以下三个要素：

• 状态
• PI向量：系统初始化每个状态的概率
• 状态转移矩阵（该矩阵在系统生命周期内保持不变）
2. Hidden Patterns
观察的状态->(隐藏的状态）->结果
引入：Hidden Markov Models

PI：初始向量
A：转移矩阵
B：观测矩阵（或者混淆矩阵）：包含了观察的状态与隐藏状态的关系

input：Markov Model λ=(A,B,π)$\lambda=(A,B,\pi)$，观察序列O$O$
output：观测序列概率P(O|λ)$P(O|\lambda)$

α1(i)=πibi(o1)$\alpha_1(i)=\pi_ib_i(o_1)$
αt+1(i)=[Nj=1αt(j)aji]bi(ot+1)$\alpha_{t+1}(i)=[\sum_{j=1}^{N}\alpha_t(j)a_{ji}]b_i(o_{t+1})$
βT(i)=1$\beta_T(i)=1$
βt(i)=Nj+1aijbj(ot+1)βt+1(j)$\beta_t(i)=\sum_{j+1}^{N}a_{ij}b_j(o_{t+1})\beta_{t+1}(j)$
P(O|λ)=Ni=1Nj=1αt(i)aijbj(ot+1)βt+1(j)$P(O|\lambda)=\sum_{i=1}^{N}\sum_{j=1}^{N}\alpha_t(i)a_{ij}b_j(o_{t+1})\beta_{t+1}(j)$

αt(i)=P(o1,o2,...ot,it=qi|λ)

βt(i)=P(ot+1,ot+2,...,oT|it=qi,λ

input：Markov Model λ=(A,B,π)$\lambda=(A,B,\pi)$，观察序列O$O$
output：最优路径I$I^*$

STEP1：
δ1(i)=πibi(o1)$\delta_1(i)=\pi_ib_i(o_1)$
ψ1(i)=0$\psi_1(i)=0$
STEP2：
δt(i)=max1jN[δt1aji]bi(ot)$\delta_t(i)=max_{1\leqslant j\leqslant N}[\delta_{t-1}a_{ji}]b_i(o_t)$
ψt(i)=argmax[δt1(j)aji]$\psi_t(i)=arg max[\delta_{t-1}(j)a_{ji}]$
STEP3：
P=maxδT(i)$P^*=max\delta_T(i)$
iT=argmax[δT(i)]$i_{T}^{*}=argmax[\delta_T(i)]$
STEP4：最优路径回溯
it=ψt+1(tt+1)$i_t^*=\psi_{t+1}(t_{t+1}^*)$

%forward algotithm
clc
%===============data===================
A=[0.5,0.2,0.3;0.3,0.5,0.2;0.2,0.3,0.5];
B=[0.5,0.5;0.4,0.6;0.7,0.3];
PI=[0.2,0.4,0.4];
Q=[1,2,3];
T=4;%[~,T]=size(O)
O=[1,2,1,2];
%======forward algorithm method========
alpha=[];
%calculate initial value
[~,num]=size(Q);
for i=1:num
alpha(i)=PI(i)*B(i,O(1));
end
alpha
%recursive
for k=2:T
alpha=recursive(alpha,A,B,O(k),num)
end
%
prob=0;
for i=1:num
prob=prob+alpha(i);
end
prob
%======back algorithm method==============
beta=ones(1,num)
for k=0:T-2
beta=recursive1(beta,A,B,O(T-k),num);
end
prob=0;
for i=1:num
prob=prob+PI(i)*B(i,O(1))*beta(i);
end
prob
%==========viterbi==========================
viter=zeros(T,num);
for i=1:num
viter(1,i)=PI(i)*B(i,O(1));
end
viter
%recursive
flag=2;
path=zeros(T,num);
for i=1:num
path(1,i)=i;
end
for k=2:T
[viter(k,:),path(flag,:)]=viterbi(viter(k-1,:),A,B,O(k),num)
flag=flag+1;
end

$前向算法$

function [outputarray]=recursive(inputarray,A,B,position,num)
outputarray=[];
for i=1:num
sum=0;
for j=1:num
sum=sum+inputarray(j)*A(j,i);
end
outputarray(i)=B(i,position)*sum;
end

$后向算法$

function [outputarray]=recursive1(inputarray,A,B,position,num)
outputarray=[];
for i=1:num
sum=0;
for j=1:num
sum=sum+A(i,j)*B(j,position)*inputarray(j);
end
outputarray(i)=sum;
end


$维特比译码$

function [outputarray,path]=viterbi(inputarray,A,B,position,num)
outputarray=[];
path=[];
for i=1:num
for j=1:num
temp(j)=inputarray(j)*A(j,i);
end
outputarray(i)=B(i,position)*max(temp);
path(i)=find(temp==max(temp));
end

alpha =

0.1000    0.1600    0.2800


alpha =

0.0770    0.1104    0.0606


alpha =

0.0419    0.0355    0.0528


alpha =

0.0211    0.0252    0.0138


prob =

0.0601


prob =

0.0601


viter =

0.1000    0.1600    0.2800
0.0280    0.0504    0.0420
0.0076    0.0101    0.0147
0.0019    0.0030    0.0022


path =

 1     2     3
3     3     3
2     2     3
1     2     3


• 本文已收录于以下专栏：

举报原因： 您举报文章：HMM算法 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)