Class HMM

java.lang.Object
smile.sequence.HMM
All Implemented Interfaces:
Serializable

public class HMM extends Object implements Serializable
First-order Hidden Markov Model. A hidden Markov model (HMM) is a statistical Markov model in which the system being modeled is assumed to be a Markov process with unobserved (hidden) states. An HMM can be considered as the simplest dynamic Bayesian network.

In a regular Markov model, the state is directly visible to the observer, and therefore the state transition probabilities are the only parameters. In a hidden Markov model, the state is not directly visible, but output, dependent on the state, is visible. Each state has a probability distribution over the possible output tokens. Therefore the sequence of tokens generated by an HMM gives some information about the sequence of states.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    HMM(double[] pi, Matrix a, Matrix b)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static HMM
    fit(int[][] observations, int[][] labels)
    Fits an HMM by maximum likelihood estimation.
    static <T> HMM
    fit(T[][] observations, int[][] labels, ToIntFunction<T> ordinal)
    Fits an HMM by maximum likelihood estimation.
    double[]
    Returns the initial state probabilities.
    Returns the state transition probabilities.
    Returns the symbol emission probabilities.
    double
    logp(int[] o)
    Returns the logarithm probability of an observation sequence given this HMM.
    double
    logp(int[] o, int[] s)
    Returns the log joint probability of an observation sequence along a state sequence given this HMM.
    double
    p(int[] o)
    Returns the probability of an observation sequence given this HMM.
    double
    p(int[] o, int[] s)
    Returns the joint probability of an observation sequence along a state sequence given this HMM.
    int[]
    predict(int[] o)
    Returns the most likely state sequence given the observation sequence by the Viterbi algorithm, which maximizes the probability of P(I | O, HMM).
     
    void
    update(int[][] observations, int iterations)
    Updates the HMM by the Baum-Welch algorithm.
    <T> void
    update(T[][] observations, int iterations, ToIntFunction<T> ordinal)
    Updates the HMM by the Baum-Welch algorithm.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • HMM

      public HMM(double[] pi, Matrix a, Matrix b)
      Constructor.
      Parameters:
      pi - the initial state probabilities.
      a - the state transition probabilities, of which a[i][j] is P(s_j | s_i);
      b - the symbol emission probabilities, of which b[i][j] is P(o_j | s_i).
  • Method Details

    • getInitialStateProbabilities

      public double[] getInitialStateProbabilities()
      Returns the initial state probabilities.
      Returns:
      the initial state probabilities.
    • getStateTransitionProbabilities

      public Matrix getStateTransitionProbabilities()
      Returns the state transition probabilities.
      Returns:
      the state transition probabilities.
    • getSymbolEmissionProbabilities

      public Matrix getSymbolEmissionProbabilities()
      Returns the symbol emission probabilities.
      Returns:
      the symbol emission probabilities.
    • p

      public double p(int[] o, int[] s)
      Returns the joint probability of an observation sequence along a state sequence given this HMM.
      Parameters:
      o - an observation sequence.
      s - a state sequence.
      Returns:
      the joint probability P(o, s | H) given the model H.
    • logp

      public double logp(int[] o, int[] s)
      Returns the log joint probability of an observation sequence along a state sequence given this HMM.
      Parameters:
      o - an observation sequence.
      s - a state sequence.
      Returns:
      the log joint probability P(o, s | H) given the model H.
    • p

      public double p(int[] o)
      Returns the probability of an observation sequence given this HMM.
      Parameters:
      o - an observation sequence.
      Returns:
      the probability of this sequence.
    • logp

      public double logp(int[] o)
      Returns the logarithm probability of an observation sequence given this HMM. A scaling procedure is used in order to avoid underflow when computing the probability of long sequences.
      Parameters:
      o - an observation sequence.
      Returns:
      the log probability of this sequence.
    • predict

      public int[] predict(int[] o)
      Returns the most likely state sequence given the observation sequence by the Viterbi algorithm, which maximizes the probability of P(I | O, HMM). In the calculation, we may get ties. In this case, one of them is chosen randomly.
      Parameters:
      o - an observation sequence.
      Returns:
      the most likely state sequence.
    • fit

      public static HMM fit(int[][] observations, int[][] labels)
      Fits an HMM by maximum likelihood estimation.
      Parameters:
      observations - the observation sequences, of which symbols take values in [0, n), where n is the number of unique symbols.
      labels - the state labels of observations, of which states take values in [0, p), where p is the number of hidden states.
      Returns:
      the model.
    • fit

      public static <T> HMM fit(T[][] observations, int[][] labels, ToIntFunction<T> ordinal)
      Fits an HMM by maximum likelihood estimation.
      Type Parameters:
      T - the data type of observations.
      Parameters:
      observations - the observation sequences.
      labels - the state labels of observations, of which states take values in [0, p), where p is the number of hidden states.
      ordinal - a lambda returning the ordinal numbers of symbols.
      Returns:
      the model.
    • update

      public <T> void update(T[][] observations, int iterations, ToIntFunction<T> ordinal)
      Updates the HMM by the Baum-Welch algorithm.
      Type Parameters:
      T - the data type of observations.
      Parameters:
      observations - the training observation sequences.
      iterations - the number of iterations to execute.
      ordinal - a lambda returning the ordinal numbers of symbols.
    • update

      public void update(int[][] observations, int iterations)
      Updates the HMM by the Baum-Welch algorithm.
      Parameters:
      observations - the training observation sequences.
      iterations - the number of iterations to execute.
    • toString

      public String toString()
      Overrides:
      toString in class Object