Matlab的HMM文档
来源:互联网 发布:网络任务平台有哪些 编辑:程序博客网 时间:2024/06/09 15:52
Introduction to Hidden Markov Models (HMM)
Ahidden Markov model (HMM) is one in which you observe a sequence of emissions, but do not know the sequence of states the model went through to generate the emissions. Analyses of hidden Markov models seek to recover the sequence of states from the observed data.
As an example, consider a Markov model with two states and six possible emissions. The model uses:
A red die, having six sides, labeled 1 through 6.
A green die, having twelve sides, five of which are labeled 2 through 6, while the remaining seven sides are labeled 1.
A weighted red coin, for which the probability of heads is .9 and the probability of tails is .1.
A weighted green coin, for which the probability of heads is .95 and the probability of tails is .05.
The model creates a sequence of numbers from the set {1, 2, 3, 4, 5, 6} with the following rules:
Begin by rolling the red die and writing down the number that comes up, which is the emission.
Toss the red coin and do one of the following:
If the result is heads, roll the red die and write down the result.
If the result is tails, roll the green die and write down the result.
At each subsequent step, you flip the coin that has the same color as the die you rolled in the previous step. If the coin comes up heads, roll the same die as in the previous step. If the coin comes up tails, switch to the other die.
The state diagram for this model has two states, red and green, as shown in the following figure.
You determine the emission from a state by rolling the die with the same color as the state. You determine the transition to the next state by flipping the coin with the same color as the state.
The transition matrix is:
The emissions matrix is:
The model is not hidden because you know the sequence of states from the colors of the coins and dice. Suppose, however, that someone else is generating the emissions without showing you the dice or the coins. All you see is the sequence of emissions. If you start seeing more 1s than other numbers, you might suspect that the model is in the green state, but you cannot be sure because you cannot see the color of the die being rolled.
Hidden Markov models raise the following questions:
Given a sequence of emissions, what is the most likely state path?
Given a sequence of emissions, how can you estimate transition and emission probabilities of the model?
What is the forward probability that the model generates a given sequence?
What is the posterior probability that the model is in a particular state at any point in the sequence?
Back to Top
Analyzing Hidden Markov Models
Generating a Test Sequence
Estimating the State Sequence
Estimating Transition and Emission Matrices
Estimating Posterior State Probabilities
Changing the Initial State Distribution
Statistics Toolbox functions related to hidden Markov models are:
hmmgenerate — Generates a sequence of states and emissions from a Markov model
hmmestimate — Calculates maximum likelihood estimates of transition and emission probabilities from a sequence of emissions and a known sequence of states
hmmtrain — Calculates maximum likelihood estimates of transition and emission probabilities from a sequence of emissions
hmmviterbi — Calculates the most probable state path for a hidden Markov model
hmmdecode — Calculates the posterior state probabilities of a sequence of emissions
This section shows how to use these functions to analyze hidden Markov models.
Generating a Test Sequence
The following commands create the transition and emission matrices for the model described in theIntroduction to Hidden Markov Models (HMM):
TRANS = [.9 .1; .05 .95;];EMIS = [1/6, 1/6, 1/6, 1/6, 1/6, 1/6;...7/12, 1/12, 1/12, 1/12, 1/12, 1/12];
To generate a random sequence of states and emissions from the model, use hmmgenerate:
[seq,states] = hmmgenerate(1000,TRANS,EMIS);
The output seq is the sequence of emissions and the output states is the sequence of states.
hmmgenerate begins in state 1 at step 0, makes the transition to statei1 at step 1, and returns i1 as the first entry instates. To change the initial state, see Changing the Initial State Distribution.
Estimating the State Sequence
Given the transition and emission matrices TRANS and EMIS, the functionhmmviterbi uses the Viterbi algorithm to compute the most likely sequence of states the model would go through to generate a given sequenceseq of emissions:
likelystates = hmmviterbi(seq, TRANS, EMIS);
likelystates is a sequence the same length as seq.
To test the accuracy of hmmviterbi, compute the percentage of the actual sequencestates that agrees with the sequence likelystates.
sum(states==likelystates)/1000ans = 0.8200
In this case, the most likely sequence of states agrees with the random sequence 82% of the time.
Estimating Transition and Emission Matrices
Using hmmestimate
Using hmmtrain
The functions hmmestimate and hmmtrain estimate the transition and emission matrices TRANS andEMIS given a sequence seq of emissions.
Using hmmestimate. The function hmmestimate requires that you know the sequence of states states that the model wentthrough to generate seq.
The following takes the emission and state sequences and returns estimates of the transition and emission matrices:
[TRANS_EST, EMIS_EST] = hmmestimate(seq, states)TRANS_EST =0.8989 0.10110.0585 0.9415EMIS_EST =0.1721 0.1721 0.1749 0.1612 0.1803 0.13930.5836 0.0741 0.0804 0.0789 0.0726 0.1104
You can compare the outputs with the original transition and emission matrices,TRANS and EMIS:
TRANSTRANS =0.9000 0.10000.0500 0.9500EMISEMIS =0.1667 0.1667 0.1667 0.1667 0.1667 0.16670.5833 0.0833 0.0833 0.0833 0.0833 0.0833
Using hmmtrain. If you do not know the sequence of statesstates, but you have initial guesses for TRANS and EMIS, you can still estimateTRANS and EMIS using hmmtrain.
Suppose you have the following initial guesses for TRANS and EMIS.
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.77140.0032 0.9968EMIS_EST2 =0.1436 0.2348 0.1837 0.1963 0.2350 0.00660.4355 0.1089 0.1144 0.1082 0.1109 0.1220
hmmtrain uses an iterative algorithm that alters the matrices TRANS_GUESS andEMIS_GUESS so that at each step the adjusted matrices are more likely to generate the observed sequence,seq. The algorithm halts when the matrices in two successive iterations are within a small tolerance of each other.
If the algorithm fails to reach this tolerance within a maximum number of iterations, whose default value is100, the algorithm halts. In this case, hmmtrain returns the last values ofTRANS_EST and EMIS_EST and issues a warning that the tolerance was not reached.
If the algorithm fails to reach the desired tolerance, increase the default value of the maximum number of iterations with the command:
hmmtrain(seq,TRANS_GUESS,EMIS_GUESS,'maxiterations',maxiter)
where maxiter is the maximum number of steps the algorithm executes.
Change the default value of the tolerance with the command:
hmmtrain(seq, TRANS_GUESS, EMIS_GUESS, 'tolerance', tol)
where tol is the desired value of the tolerance. Increasing the value oftol makes the algorithm halt sooner, but the results are less accurate.
Two factors reduce the reliability of the output matrices of hmmtrain:
The algorithm converges to a local maximum that does not represent the true transition and emission matrices. If you suspect this, use different initial guesses for the matricesTRANS_EST and EMIS_EST.
The sequence seq may be too short to properly train the matrices. If you suspect this, use a longer sequence forseq.
Estimating Posterior State Probabilities
The posterior state probabilities of an emission sequence seq are the conditional probabilities that the model is in a particular state when it generates a symbol inseq, given that seq is emitted. You compute the posterior state probabilities withhmmdecode:
PSTATES = hmmdecode(seq,TRANS,EMIS)
The output PSTATES is an M-by-L matrix, where M is the number of states andL is the length of seq. PSTATES(i,j) is the conditional probability that the model is in statei when it generates the jth symbol of seq, given that seq is emitted.
hmmdecode begins with the model in state 1 at step 0, prior to the first emission.PSTATES(i,1) is the probability that the model is in state i at the following step 1. To change the initial state, seeChanging the Initial State Distribution.
To return the logarithm of the probability of the sequence seq, use the second output argument ofhmmdecode:
[PSTATES,logpseq] = hmmdecode(seq,TRANS,EMIS)
The probability of a sequence tends to 0 as the length of the sequence increases, and the probability of a sufficiently long sequence becomes less than the smallest positive number your computer can represent.hmmdecode returns the logarithm of the probability to avoid this problem.
Changing the Initial State Distribution
By default, Statistics Toolbox hidden Markov model functions begin in state 1. In other words, the distribution of initial states has all of its probability mass concentrated at state 1. To assign a different distribution of probabilities,p = [p1,p2, ..., pM], to the M initial states, do the following:
Create an M+1-by-M+1 augmented transition matrix, of the following form:
where T is the true transition matrix. The first column of containsM+1 zeros. p must sum to 1.
Create an M+1-by-N augmented emission matrix,, that has the following form:
If the transition and emission matrices are TRANS and EMIS, respectively, you create the augmented matrices with the following commands:
TRANS_HAT = [0 p; zeros(size(TRANS,1),1) TRANS];EMIS_HAT = [zeros(1,size(EMIS,2)); EMIS];
Back to Top
- Matlab的HMM文档
- CRF&HMM的Matlab代码
- matlab中hmm的使用示例
- 隐马尔科夫(HMM)的Matlab实现
- HMM MATLAB Toolbox应用
- 【转】Matlab的HMM(隐马尔可夫模型)相关函数介绍
- matlab自带的HMM统计工具箱简介
- HMM隐马尔科夫模型 MATLAB 工具包对各种数据的处理
- HMM matlab代码实现+分析
- 隐马尔可夫模型(HMM)的MATLAB实现——前向后向算法
- 隐马尔可夫模型(HMM)的MATLAB实现——Baum-Welch算法
- 隐马尔可夫模型(HMM)的MATLAB实现——Viterbi算法
- 基于HMM轨迹识别matlab简单说明
- HMM的训练
- HMM的viterbi算法
- HMM模型的理解
- HMM的个人理解
- Matlab最新的官方文档中文翻译
- 在MFC中的windows socket编程要点
- linux modprobe命令参数及用法详解--linux加载模块命令
- ubuntu 使用KindleFire调试Android程序
- 应用索引技术优化SQL 语句(Part 3)
- 自动定义属性页
- Matlab的HMM文档
- Foursquare新推标价$10的企业即时验证服务
- 电脑延迟关机命令Shutdown详解
- Oracle HowTo:如何确定Oracle是32 Bit(位)的还是64 Bit(位)的?
- 修改Titanium的user_agent配置
- ibatis 用HashMap解决Ibatis未知列名和列数的查询结果的resultClass映射
- EBS File Comparison Report Now Available
- MySQL的 Grant命令权限分配
- Customizing Links in an NSTextView