public class HMM
extends java.lang.Object
implements java.io.Serializable
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.
Constructor and Description |
---|
HMM(double[] pi,
smile.math.matrix.Matrix a,
smile.math.matrix.Matrix b)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static HMM |
fit(int[][] observations,
int[][] labels)
Fits an HMM by maximum likelihood estimation.
|
static <T> HMM |
fit(T[][] observations,
int[][] labels,
java.util.function.ToIntFunction<T> ordinal)
Fits an HMM by maximum likelihood estimation.
|
double[] |
getInitialStateProbabilities()
Returns the initial state probabilities.
|
smile.math.matrix.Matrix |
getStateTransitionProbabilities()
Returns the state transition probabilities.
|
smile.math.matrix.Matrix |
getSymbolEmissionProbabilities()
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) . |
java.lang.String |
toString() |
void |
update(int[][] observations,
int iterations)
Updates the HMM by the Baum-Welch algorithm.
|
<T> void |
update(T[][] observations,
int iterations,
java.util.function.ToIntFunction<T> ordinal)
Updates the HMM by the Baum-Welch algorithm.
|
public HMM(double[] pi, smile.math.matrix.Matrix a, smile.math.matrix.Matrix b)
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).public double[] getInitialStateProbabilities()
public smile.math.matrix.Matrix getStateTransitionProbabilities()
public smile.math.matrix.Matrix getSymbolEmissionProbabilities()
public double p(int[] o, int[] s)
o
- an observation sequence.s
- a state sequence.public double logp(int[] o, int[] s)
o
- an observation sequence.s
- a state sequence.public double p(int[] o)
o
- an observation sequence.public double logp(int[] o)
o
- an observation sequence.public int[] predict(int[] o)
P(I | O, HMM)
. In the calculation, we may get ties. In this
case, one of them is chosen randomly.o
- an observation sequence.public static HMM fit(int[][] observations, int[][] labels)
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.public static <T> HMM fit(T[][] observations, int[][] labels, java.util.function.ToIntFunction<T> ordinal)
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.public <T> void update(T[][] observations, int iterations, java.util.function.ToIntFunction<T> ordinal)
observations
- the training observation sequences.iterations
- the number of iterations to execute.ordinal
- a lambda returning the ordinal numbers of symbols.public void update(int[][] observations, int iterations)
observations
- the training observation sequences.iterations
- the number of iterations to execute.public java.lang.String toString()
toString
in class java.lang.Object