hidden markov models (hmm) -凯发k8网页登录

main content

hidden markov models (hmm)

introduction to hidden markov models (hmm)

a hidden 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, as shown in the following figure.

state diagram for the markov model with two states

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:

t=[0.90.050.10.95]

the emissions matrix is:

e=[161616161616712112112112112112]

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?

analyzing hidden markov models

statistics and machine learning toolbox™ functions related to hidden markov models are:

  • — generates a sequence of states and emissions from a markov model

  • — calculates maximum likelihood estimates of transition and emission probabilities from a sequence of emissions and a known sequence of states

  • — calculates maximum likelihood estimates of transition and emission probabilities from a sequence of emissions

  • — calculates the most probable state path for a hidden markov model

  • — 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 the introduction 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 :

[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 state i1 at step 1, and returns i1 as the first entry in states. 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 function uses the viterbi algorithm to compute the most likely sequence of states the model would go through to generate a given sequence seq 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 sequence states that agrees with the sequence likelystates.

sum(states==likelystates)/1000
ans =
   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

the functions and estimate the transition and emission matrices trans and emis given a sequence seq of emissions.

using hmmestimate.  the function requires that you know the sequence of states states that the model went through 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.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

you can compare the outputs with the original transition and emission matrices, trans and emis:

trans
trans =
0.9000    0.1000
0.0500    0.9500
emis
emis =
0.1667    0.1667    0.1667    0.1667    0.1667    0.1667
0.5833    0.0833    0.0833    0.0833    0.0833    0.0833

using hmmtrain.  if you do not know the sequence of states states, but you have initial guesses for trans and emis, you can still estimate trans and emis using .

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.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

hmmtrain uses an iterative algorithm that alters the matrices trans_guess and emis_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 is 100, the algorithm halts. in this case, hmmtrain returns the last values of trans_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 of tol 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 matrices trans_est and emis_est.

  • the sequence seq may be too short to properly train the matrices. if you suspect this, use a longer sequence for seq.

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 in seq, given that seq is emitted. you compute the posterior state probabilities with :

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 the conditional probability that the model is in state i 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, see changing the initial state distribution.

to return the logarithm of the probability of the sequence seq, use the second output argument of hmmdecode:

[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 and machine learning 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:

  1. create an m 1-by-m 1 augmented transition matrix, t^ of the following form:

    t^=[0p0t]

    where t is the true transition matrix. the first column of t^ contains m 1 zeros. p must sum to 1.

  2. create an m 1-by-n augmented emission matrix, e^, that has the following form:

    e^=[0e]

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

see also

| | | |

related topics

    网站地图