Categories &

Functions List

Function Reference: hmmtrain

statistics: [esttr, estout] = hmmtrain (sequence, transguess, outguess)
statistics: […] = hmmtrain (…, "algorithm", algorithm)
statistics: […] = hmmtrain (…, "symbols", symbols)
statistics: […] = hmmtrain (…, "tolerance", tol)
statistics: […] = hmmtrain (…, "maxiterations", maxiter)
statistics: […] = hmmtrain (…, "pseudotransitions", pseudotransitions)
statistics: […] = hmmtrain (…, "pseudoemissions", pseudoemissions)
statistics: […] = hmmtrain (…, "verbose", vflag)

Estimate the parameters of a hidden Markov model from emitted sequences.

Given one or more observed output sequences and initial guesses for the transition and output probability matrices, hmmtrain finds maximum likelihood estimates of the two matrices using the Baum-Welch algorithm (the default) or Viterbi training. The model assumes that the generation starts in state 1 at step 0 but does not include step 0 in the sequence.

Arguments

  • sequence is a vector of a sequence of given outputs, or, for training from several sequences, a cell array of such vectors or a matrix whose rows are individual sequences. The outputs must be integers ranging from 1 to columns (outguess).
  • transguess is the initial guess for the matrix of transition probabilities. transguess(i, j) is the probability of a transition to state j given state i.
  • outguess is the initial guess for the matrix of output probabilities. outguess(i, j) is the probability of generating output j given state i.

Return values

  • esttr is the estimated matrix of transition probabilities.
  • estout is the estimated matrix of output probabilities.

Name-Value pair arguments

  • "algorithm" selects the training algorithm, either "BaumWelch" (default) or "Viterbi". "BaumWelch" performs the standard forward-backward re-estimation and is recommended for most uses. "Viterbi" performs segmental (hard) re-estimation from the most likely state path of each sequence; it is faster but only approximates the maximum-likelihood estimate.
  • "symbols" specifies the possible outputs. If given, sequence is expected to hold the elements of symbols instead of integers. symbols can be a cell array.
  • "tolerance" is the convergence tolerance (default 1e-6). The algorithm terminates when the change in the log-likelihood and in both estimated matrices falls below tol.
  • "maxiterations" is the maximum number of iterations (default 500). A warning is issued if the algorithm has not converged within this many iterations.
  • "pseudotransitions" and "pseudoemissions" supply pseudo-count matrices for Viterbi training, used to keep transitions or outputs that are very unlikely to occur from collapsing to zero probability.
  • "verbose", when true, prints the log-likelihood and the change in the estimates at each iteration.

Examples

 
 transprob = [0.8, 0.2; 0.4, 0.6];
 outprob = [0.2, 0.4, 0.4; 0.7, 0.2, 0.1];
 sequence = hmmgenerate (100, transprob, outprob);
 transguess = [0.6, 0.4; 0.5, 0.5];
 outguess = [0.3, 0.3, 0.4; 0.5, 0.3, 0.2];
 [esttr, estout] = hmmtrain (sequence, transguess, outguess);

Two results of Viterbi training differ from MATLAB’s, deliberately.

Given several sequences, the counts of every sequence are pooled and normalized once, so a sequence contributes in proportion to its length. MATLAB normalizes each sequence separately and averages the results, which weights an eight-symbol sequence as heavily as a twenty-four-symbol one and is not the maximum likelihood estimate. Its own Baum-Welch pools expected counts, as both algorithms do here.

Given "pseudotransitions" or "pseudoemissions", the pseudo-counts are added to the counted transitions and outputs, once per iteration, before the row is normalized. MATLAB does the same on its first iteration; from its second it adds them to an estimate that has already been normalized, so its iterate mixes counts with probabilities and is no longer a count matrix of any state path.

References

  1. Wendy L. Martinez and Angel R. Martinez. Computational Statistics Handbook with MATLAB. Appendix E, pages 547-557, Chapman & Hall/CRC, 2001.
  2. Lawrence R. Rabiner. A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition. Proceedings of the IEEE, 77(2), pages 257-286, February 1989.

Source Code: hmmtrain

Re-estimate a model with Baum-Welch, starting from rough initial guesses.

 transprob = [0.95, 0.05; 0.10, 0.90];
 outprob = [1/6, 1/6, 1/6, 1/6, 1/6, 1/6; 1/10, 1/10, 1/10, 1/10, 1/10, 1/2];
 sequence = hmmgenerate (1000, transprob, outprob);
 transguess = [0.8, 0.2; 0.2, 0.8];
 outguess = [1/6, 1/6, 1/6, 1/6, 1/6, 1/6; 1/8, 1/8, 1/8, 1/8, 1/8, 3/8];
 [esttr, estout] = hmmtrain (sequence, transguess, outguess)
esttr =

   0.934921   0.065079
   0.199155   0.800845

estout =

   0.145672   0.166482   0.195154   0.165539   0.155432   0.171722
   0.142943   0.054809   0.060430   0.037380   0.092724   0.611714