码迷,mamicode.com
首页 > 其他好文 > 详细

【转】Matlab的HMM(隐马尔可夫模型)相关函数介绍

时间:2015-05-19 19:12:48      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:hmm   隐马尔科夫模型   matlab   statistics toolbox   

声明:本文主要介绍Matlab2011b中 Statistics Toolbox工具箱里与隐马尔科夫模型相关的函数及其用法(请勿与其它HMM工具箱混淆)。本文的主要内容来自Matlab 2011b的帮助文档,为作者自学笔记。水平有限,笔记粗糙,本着“交流探讨,知识分享”的宗旨,希望对HMM感兴趣的同学有些许帮助,欢迎指教,共同进步。

变量说明:

设有M个状态,N个符号Markov模型。

TRANS:对应状态转移矩阵,大小为M*M,表示各状态相互转换的概率,TRANS(i,j)表示从状态i转换到状态j的概率。

EMIS:对应符号生成矩阵,又叫混淆矩阵,观察符号概率分布。EMIS(i,j)代表在状态i时,产生符号j的概率。

 

函数介绍:

hmmgenerate— Generates a sequence of states and emissions from a Markov model

从一个马尔科夫模型产生状态序列和输出序列,该序列具有模型所表达的随机性特征。

A random sequence seq of emission symbols

A random sequence states of states

用法:

[seq,states] = hmmgenerate(len,TRANS,EMIS)

hmmgenerate(...,‘Symbols‘,SYMBOLS)
hmmgenerate(...,‘Statenames‘,STATENAMES)

示例:

 trans = [0.95,0.05;    0.10,0.90];

 emis= [1/6 1/6 1/6 1/6 1/6 1/6;

   1/10 1/10 1/10 1/10 1/10 1/2];

 

[seq,states] = hmmgenerate(100,trans,emis)

[seq,states] =hmmgenerate(100,trans,emis,...

   ‘Symbols‘,{‘one‘,‘two‘,‘three‘,‘four‘,‘five‘,‘six‘},...

‘Statenames‘,{‘fair‘;‘loaded‘})

 

估计状态序列:Estimatingthe State Sequence

hmmviterbi— Calculates the most probable state path for a hidden Markov model

Given the transition and emission matricesTRANS and EMIS, the function hmmviterbiuses the Viterbi algorithm to compute the most likely sequence of states themodel would go through to generate a given sequence seq of emissions:

给定状态转移矩阵TRANS和混淆矩阵EMIS,hmmviterbi使用Viterbi算法计算该模型最相似的状态序列。

用法:

STATES = hmmviterbi(seq,TRANS,EMIS)
hmmviterbi(...,‘Symbols‘,SYMBOLS)
hmmviterbi(...,‘Statenames‘,STATENAMES)

示例:

likelystates = hmmviterbi(seq, TRANS,EMIS);

 

trans = [0.95,0.05;

            0.10,0.90];

emis = [1/6 1/6 1/6 1/6 1/6 1/6;

            1/10 1/10 1/10 1/10 1/10 1/2];

[seq,states] = hmmgenerate(100,trans,emis);

estimatedStates =hmmviterbi(seq,trans,emis);

[seq,states] =hmmgenerate(100,trans,emis,...

               ‘Statenames‘,{‘fair‘;‘loaded‘});

estimatesStates = hmmviterbi(seq,trans,emis,...

              ‘Statenames‘,{‘fair‘;‘loaded‘});

 

hmmestimatehmmtrain用于通过给定的输出序列估计转移矩阵TRANS和混淆矩阵EMIS。

hmmestimate— Calculates maximum likelihood estimates of transition and emissionprobabilities from a sequence of emissions and a known sequence of states

通过一个输出序列和已知的状态序列,计算转移概率和输出概率的最大似然估计。

用法:

[TRANS,EMIS] = hmmestimate(seq,states)
hmmestimate(...,‘Symbols‘,SYMBOLS)
hmmestimate(...,‘Statenames‘,STATENAMES)
hmmestimate(...,‘Pseudoemissions‘,PSEUDOE)
hmmestimate(...,‘Pseudotransitions‘,PSEUDOTR)

 

示例:

通过已知的输出序列和状态序列估计出转移状态和混淆矩阵。

[TRANS_EST, EMIS_EST] = hmmestimate(seq,states)

 

TRANS_EST =

0.8989   0.1011

0.0585   0.9415

 

EMIS_EST =

0.1721   0.1721    0.1749    0.1612   0.1803    0.1393

0.5836   0.0741    0.0804    0.0789   0.0726    0.1104

 

 

hmmtrain— Calculates maximum likelihood estimates of transition and emissionprobabilities from a sequence of emissions.在知道输出序列,不知道状态转移序列,但是对转移矩阵和混淆矩阵有个初始猜测的情况下,可以使用hmmtrain估计转移状态和混淆矩阵,改函数可以选择使用BaumWelch或者Viterbi算法,通过迭代的方式进行估计,可以设置迭代次数Maxiterations和精度Tolerance。

 

用法:

[ESTTR,ESTEMIT] =hmmtrain(seq,TRGUESS,EMITGUESS)
hmmtrain(...,‘Algorithm‘,algorithm)
hmmtrain(...,‘Symbols‘,SYMBOLS)
hmmtrain(...,‘Tolerance‘,tol)
hmmtrain(...,‘Maxiterations‘,maxiter)
hmmtrain(...,‘Verbose‘,true)
hmmtrain(...,‘Pseudoemissions‘,PSEUDOE)
hmmtrain(...,‘Pseudotransitions‘,PSEUDOTR)

 

示例:

TRANS_GUESS = [.85 .15; .1 .9];

EMIS_GUESS = [.17 .16 .17 .16 .17 .17;.6.08 .08 .08 .08 08];

You estimate TRANS and EMIS as follows:

[TRANS_EST2, EMIS_EST2] = hmmtrain(seq,TRANS_GUESS, EMIS_GUESS)

 

TRANS_EST2 =

0.2286   0.7714

0.0032   0.9968

 

EMIS_EST2 =

0.1436   0.2348    0.1837    0.1963   0.2350    0.0066

0.4355   0.1089    0.1144    0.1082   0.1109    0.1220

 

 

hmmdecode— Calculates the posterior state probabilities of a sequence of emissions

用于估计后验状态概率。对于一个输出序列seq的后验状态概率,是指这个模型在确定的状态下产生seq中一个符号的条件概率。

PSTATES = hmmdecode(seq,TRANS,EMIS)

The output PSTATES is an M-by-L matrix,where M is the number of states and L is the length of seq. PSTATES(i,j) is theconditional probability that the model is in state i when it generates the jthsymbol of seq, given that seq is emitted.

PSTATES是一个M×L的矩阵,M是状态的个数,L是输出序列seq的长度。PSTATES(i,j)是在状态i时,产生seq的第j个符号的条件概率。

 

细心的读者可能会发现,上面都没有使用到Markov模型的初始状态概率矩阵。实际上,如果指定初始状态的概率,则上述函数默认从第一个状态开始,即初始状态为第一个状态的概率是1。但是statisticstoolbox也提供了修改初始状态概率矩阵的方法。

 

设实际的状态转移矩阵和混淆矩阵分别是TRANS和EMIS,初始状态的概率分布为p=[p1,p2,p3,...,pm],则可以通过以下方式重新设置初始状态。

 

TRANS_HAT = [0 p; zeros(size(TRANS,1),1)TRANS];

EMIS_HAT = [zeros(1,size(EMIS,2)); EMIS];

 

即:

技术分享

【转】Matlab的HMM(隐马尔可夫模型)相关函数介绍

标签:hmm   隐马尔科夫模型   matlab   statistics toolbox   

原文地址:http://blog.csdn.net/badboy_1990/article/details/45846485

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!