ObservationType
- Type of Observations handled by the HMM.@PublicationReference(author="Lawrence R. Rabiner", title="A tutorial on hidden Markov models and selected applications in speech recognition", type=Journal, year=1989, publication="Proceedings of the IEEE", pages={257,286}, url="http://www.cs.ubc.ca/~murphyk/Bayes/rabiner.pdf", notes="Rabiner\'s transition matrix is transposed from mine.") public class HiddenMarkovModel<ObservationType> extends MarkovChain implements Distribution<ObservationType>
Modifier and Type | Field and Description |
---|---|
protected java.util.Collection<? extends ComputableDistribution<ObservationType>> |
emissionFunctions
The PDFs that emit symbols from each state.
|
DEFAULT_NUM_STATES, initialProbability, transitionProbability
Constructor and Description |
---|
HiddenMarkovModel()
Default constructor.
|
HiddenMarkovModel(int numStates)
Creates a new instance of ContinuousDensityHiddenMarkovModel
|
HiddenMarkovModel(Vector initialProbability,
Matrix transitionProbability,
java.util.Collection<? extends ComputableDistribution<ObservationType>> emissionFunctions)
Creates a new instance of ContinuousDensityHiddenMarkovModel
|
Modifier and Type | Method and Description |
---|---|
HiddenMarkovModel<ObservationType> |
clone()
This makes public the clone method on the
Object class and
removes the exception that it throws. |
protected java.util.ArrayList<WeightedValue<Vector>> |
computeBackwardProbabilities(java.util.ArrayList<Vector> b,
java.util.ArrayList<WeightedValue<Vector>> alphas)
Computes the backward-probabilities for the given observation likelihoods
and the weights from the alphas.
|
protected WeightedValue<Vector> |
computeBackwardProbabilities(Vector beta,
Vector b,
double weight)
Computes the backward probability recursion.
|
protected java.util.ArrayList<WeightedValue<Vector>> |
computeForwardProbabilities(java.util.ArrayList<Vector> b,
boolean normalize)
Computes the forward probabilities for the given observation likelihood
sequence.
|
protected WeightedValue<Vector> |
computeForwardProbabilities(Vector alpha,
Vector b,
boolean normalize)
Computes the recursive solution to the forward probabilities of the
HMM.
|
protected double |
computeMultipleObservationLogLikelihood(java.util.Collection<? extends java.util.Collection<? extends ObservationType>> sequences)
Computes the log-likelihood of the observation sequences, given the
current HMM's parameterization.
|
protected java.util.ArrayList<Vector> |
computeObservationLikelihoods(java.util.Collection<? extends ObservationType> observations)
Computes the conditionally independent likelihoods
for each state given the observation sequence.
|
Vector |
computeObservationLikelihoods(ObservationType observation)
Computes the conditionally independent likelihoods
for each state given the observation.
|
protected void |
computeObservationLikelihoods(ObservationType observation,
Vector b)
Computes the conditionally independent likelihoods
for each state given the observation.
|
double |
computeObservationLogLikelihood(java.util.Collection<? extends ObservationType> observations)
Computes the log-likelihood of the observation sequence, given the
current HMM's parameterization.
|
double |
computeObservationLogLikelihood(java.util.Collection<? extends ObservationType> observations,
java.util.Collection<java.lang.Integer> states)
Computes the log-likelihood that the given observation sequence
was generated by the given sequence of state indices.
|
protected java.util.ArrayList<Vector> |
computeStateObservationLikelihood(java.util.ArrayList<WeightedValue<Vector>> alphas,
java.util.ArrayList<WeightedValue<Vector>> betas,
double scaleFactor)
Computes the probabilities of the various states over time given the
observation sequence.
|
protected static Vector |
computeStateObservationLikelihood(Vector alpha,
Vector beta,
double scaleFactor)
Computes the probability of the various states at a time instance given
the observation sequence.
|
protected Matrix |
computeTransitions(java.util.ArrayList<WeightedValue<Vector>> alphas,
java.util.ArrayList<WeightedValue<Vector>> betas,
java.util.ArrayList<Vector> b)
Computes the stochastic transition-probability matrix from the
given probabilities.
|
protected static Matrix |
computeTransitions(Vector alphan,
Vector betanp1,
Vector bnp1)
Computes the stochastic transition-probability matrix from the
given probabilities.
|
protected Pair<Vector,int[]> |
computeViterbiRecursion(Vector delta,
Vector bn)
Computes the Viterbi recursion for a given "delta" and "b"
|
static <ObservationType> |
createRandom(java.util.Collection<? extends ProbabilityFunction<ObservationType>> distributions,
java.util.Random random)
Creates a Hidden Markov Model with the given probability function for
each state, but sampling the columns of the transition matrix and the
initial probability distributions from a diffuse Dirichlet.
|
static <ObservationType> |
createRandom(int numStates,
BatchLearner<java.util.Collection<? extends WeightedValue<? extends ObservationType>>,? extends ComputableDistribution<ObservationType>> learner,
java.util.Collection<? extends ObservationType> data,
java.util.Random random)
Creates a Hidden Markov Model with the same PMF/PDF for each state,
but sampling the columns of the transition matrix and the initial
probability distributions from a diffuse Dirichlet.
|
static <ObservationType> |
createRandom(int numStates,
ComputableDistribution<ObservationType> distribution,
java.util.Random random)
Creates a Hidden Markov Model with the same PMF/PDF for each state,
but sampling the columns of the transition matrix and the initial
probability distributions from a diffuse Dirichlet.
|
protected WeightedValue<java.lang.Integer> |
findMostLikelyState(int destinationState,
Vector delta)
Finds the most-likely next state given the previous "delta" in the
Viterbi algorithm.
|
java.util.Collection<? extends ComputableDistribution<ObservationType>> |
getEmissionFunctions()
Getter for emissionFunctions
|
ObservationType |
sample(java.util.Random random)
Draws a single random sample from the distribution.
|
java.util.ArrayList<ObservationType> |
sample(java.util.Random random,
int numSamples)
Draws multiple random samples from the distribution.
|
void |
sampleInto(java.util.Random random,
int sampleCount,
java.util.Collection<? super ObservationType> output)
Draws multiple random samples from the distribution and puts the result
into the given collection.
|
void |
setEmissionFunctions(java.util.Collection<? extends ComputableDistribution<ObservationType>> emissionFunctions)
Setter for emissionFunctions.
|
java.util.ArrayList<Vector> |
stateBeliefs(java.util.Collection<? extends ObservationType> observations)
Computes the probability distribution over all states for each
observation.
|
java.lang.String |
toString() |
java.util.ArrayList<java.lang.Integer> |
viterbi(java.util.Collection<? extends ObservationType> observations)
Viterbi algorithm for decoding the most-likely sequence of states
from the HMMs underlying Markov chain for a given observation sequence.
|
createUniformInitialProbability, createUniformTransitionProbability, getFutureStateDistribution, getInitialProbability, getNumStates, getSteadyStateDistribution, getTransitionProbability, normalize, normalizeTransitionMatrix, normalizeTransitionMatrix, setInitialProbability, setTransitionProbability
protected java.util.Collection<? extends ComputableDistribution<ObservationType>> emissionFunctions
public HiddenMarkovModel()
public HiddenMarkovModel(int numStates)
numStates
- Number of states in the HMM.public HiddenMarkovModel(Vector initialProbability, Matrix transitionProbability, java.util.Collection<? extends ComputableDistribution<ObservationType>> emissionFunctions)
initialProbability
- Initial probability Vector over the states. Each entry must be
nonnegative and the Vector must sum to 1.transitionProbability
- Transition probability matrix. The entry (i,j) is the probability
of transition from state "j" to state "i". As a corollary, all
entries in the Matrix must be nonnegative and the
columns of the Matrix must sum to 1.emissionFunctions
- The PDFs that emit symbols from each state.public static <ObservationType> HiddenMarkovModel<ObservationType> createRandom(int numStates, BatchLearner<java.util.Collection<? extends WeightedValue<? extends ObservationType>>,? extends ComputableDistribution<ObservationType>> learner, java.util.Collection<? extends ObservationType> data, java.util.Random random)
ObservationType
- Type of observations to generate.numStates
- Number of states to createlearner
- Learner to create the distributions for each statedata
- Data from which to make the distributionrandom
- Random number generatorpublic static <ObservationType> HiddenMarkovModel<ObservationType> createRandom(int numStates, ComputableDistribution<ObservationType> distribution, java.util.Random random)
ObservationType
- Type of observations to generate.numStates
- Number of states to createdistribution
- Distribution from which we will assign to each state.random
- Random number generatorpublic static <ObservationType> HiddenMarkovModel<ObservationType> createRandom(java.util.Collection<? extends ProbabilityFunction<ObservationType>> distributions, java.util.Random random)
ObservationType
- Type of observations to generate.distributions
- The distribution for each state. The size of the collection is the
number of states to create.random
- Random number generatorpublic HiddenMarkovModel<ObservationType> clone()
AbstractCloneableSerializable
Object
class and
removes the exception that it throws. Its default behavior is to
automatically create a clone of the exact type of object that the
clone is called on and to copy all primitives but to keep all references,
which means it is a shallow copy.
Extensions of this class may want to override this method (but call
super.clone()
to implement a "smart copy". That is, to target
the most common use case for creating a copy of the object. Because of
the default behavior being a shallow copy, extending classes only need
to handle fields that need to have a deeper copy (or those that need to
be reset). Some of the methods in ObjectUtil
may be helpful in
implementing a custom clone method.
Note: The contract of this method is that you must use
super.clone()
as the basis for your implementation.clone
in interface CloneableSerializable
clone
in class MarkovChain
public double computeObservationLogLikelihood(java.util.Collection<? extends ObservationType> observations)
observations
- Observations to consider.protected double computeMultipleObservationLogLikelihood(java.util.Collection<? extends java.util.Collection<? extends ObservationType>> sequences)
sequences
- Observations sequences to considerpublic double computeObservationLogLikelihood(java.util.Collection<? extends ObservationType> observations, java.util.Collection<java.lang.Integer> states)
observations
- Observations to consider.states
- Indices of states hypothesized to have generated the observation
sequence.public ObservationType sample(java.util.Random random)
Distribution
sample
in interface Distribution<ObservationType>
random
- Random-number generator to use in order to generate random numbers.public java.util.ArrayList<ObservationType> sample(java.util.Random random, int numSamples)
Distribution
sample
in interface Distribution<ObservationType>
random
- Random-number generator to use in order to generate random numbers.numSamples
- Number of samples to draw from the distribution.public void sampleInto(java.util.Random random, int sampleCount, java.util.Collection<? super ObservationType> output)
Distribution
sampleInto
in interface Distribution<ObservationType>
random
- Random number generator to use.sampleCount
- The number of samples to draw. Cannot be negative.output
- The collection to add the samples into.public java.util.Collection<? extends ComputableDistribution<ObservationType>> getEmissionFunctions()
public void setEmissionFunctions(java.util.Collection<? extends ComputableDistribution<ObservationType>> emissionFunctions)
emissionFunctions
- The PDFs that emit symbols from each state.protected WeightedValue<Vector> computeForwardProbabilities(Vector alpha, Vector b, boolean normalize)
alpha
- Previous alpha value.b
- Current observation-emission likelihood.normalize
- True to normalize the alphas, false to leave them unnormalized.protected java.util.ArrayList<WeightedValue<Vector>> computeForwardProbabilities(java.util.ArrayList<Vector> b, boolean normalize)
b
- Observation likelihood sequence.normalize
- True to normalize the alphas, false to leave them unnormalized.public Vector computeObservationLikelihoods(ObservationType observation)
observation
- Observation to considerprotected void computeObservationLikelihoods(ObservationType observation, Vector b)
observation
- Observation to considerb
- Likelihood of each state generating the given observation. This is where
the result of the computation is stored.protected java.util.ArrayList<Vector> computeObservationLikelihoods(java.util.Collection<? extends ObservationType> observations)
observations
- Observation sequence to considerprotected WeightedValue<Vector> computeBackwardProbabilities(Vector beta, Vector b, double weight)
beta
- Beta from the "next" time step.b
- Observation likelihood from the "next" time step.weight
- Weight to use for the current time step.protected java.util.ArrayList<WeightedValue<Vector>> computeBackwardProbabilities(java.util.ArrayList<Vector> b, java.util.ArrayList<WeightedValue<Vector>> alphas)
b
- Observation likelihoods.alphas
- Forward probabilities from which we will use the weights.protected static Vector computeStateObservationLikelihood(Vector alpha, Vector beta, double scaleFactor)
alpha
- Forward probability at time n.beta
- Backward probability at time n.scaleFactor
- Amount to scale the gamma byprotected java.util.ArrayList<Vector> computeStateObservationLikelihood(java.util.ArrayList<WeightedValue<Vector>> alphas, java.util.ArrayList<WeightedValue<Vector>> betas, double scaleFactor)
alphas
- Forward probabilities.betas
- Backward probabilities.scaleFactor
- Amount to scale the gamma byprotected static Matrix computeTransitions(Vector alphan, Vector betanp1, Vector bnp1)
alphan
- Result of the forward pass through the HMM at time nbetanp1
- Result of the backward pass through the HMM at time n+1bnp1
- Conditionally independent likelihoods of each observation at time n+1protected Matrix computeTransitions(java.util.ArrayList<WeightedValue<Vector>> alphas, java.util.ArrayList<WeightedValue<Vector>> betas, java.util.ArrayList<Vector> b)
alphas
- Result of the forward pass through the HMM.betas
- Result of the backward pass through the HMM.b
- Conditionally independent likelihoods of each observation.public java.lang.String toString()
toString
in class MarkovChain
protected WeightedValue<java.lang.Integer> findMostLikelyState(int destinationState, Vector delta)
destinationState
- Destination state index to consider.delta
- Previous value of the "delta".protected Pair<Vector,int[]> computeViterbiRecursion(Vector delta, Vector bn)
delta
- Previous value of the Viterbi recursion.bn
- Current observation likelihood.@PublicationReference(author="Wikipedia", title="Viterbi algorithm", year=2010, type=WebPage, url="http://en.wikipedia.org/wiki/Viterbi_algorithm") public java.util.ArrayList<java.lang.Integer> viterbi(java.util.Collection<? extends ObservationType> observations)
observations
- Observation sequence to considerpublic java.util.ArrayList<Vector> stateBeliefs(java.util.Collection<? extends ObservationType> observations)
observations
-