Class HMMLabeler<T>

java.lang.Object
smile.sequence.HMMLabeler<T>
Type Parameters:
T - the data type of model input objects.
All Implemented Interfaces:
Serializable, SequenceLabeler<T>

public class HMMLabeler<T> extends Object implements SequenceLabeler<T>
First-order Hidden Markov Model sequence labeler.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final HMM
    The HMM model.
    The lambda returns the ordinal numbers of symbols.
  • Constructor Summary

    Constructors
    Constructor
    Description
    HMMLabeler(HMM model, ToIntFunction<T> ordinal)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> HMMLabeler<T>
    fit(T[][] observations, int[][] labels, ToIntFunction<T> ordinal)
    Fits an HMM by maximum likelihood estimation.
    double
    logp(T[] o)
    Returns the logarithm probability of an observation sequence.
    double
    logp(T[] o, int[] s)
    Returns the log joint probability of an observation sequence along a state sequence.
    double
    p(T[] o)
    Returns the probability of an observation sequence.
    double
    p(T[] o, int[] s)
    Returns the joint probability of an observation sequence along a state sequence.
    int[]
    predict(T[] 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(T[][] observations, int iterations)
    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
  • Field Details

    • model

      public final HMM model
      The HMM model.
    • ordinal

      public final ToIntFunction<T> ordinal
      The lambda returns the ordinal numbers of symbols.
  • Constructor Details

    • HMMLabeler

      public HMMLabeler(HMM model, ToIntFunction<T> ordinal)
      Constructor.
      Parameters:
      model - the HMM model.
      ordinal - a lambda returning the ordinal numbers of symbols.
  • Method Details

    • fit

      public static <T> HMMLabeler<T> 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 void update(T[][] 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
    • p

      public double p(T[] o, int[] s)
      Returns the joint probability of an observation sequence along a state sequence.
      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(T[] o, int[] s)
      Returns the log joint probability of an observation sequence along a state sequence.
      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(T[] o)
      Returns the probability of an observation sequence.
      Parameters:
      o - an observation sequence.
      Returns:
      the probability of this sequence.
    • logp

      public double logp(T[] o)
      Returns the logarithm probability of an observation sequence. 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(T[] 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.
      Specified by:
      predict in interface SequenceLabeler<T>
      Parameters:
      o - an observation sequence.
      Returns:
      the most likely state sequence.