Package QSTK :: Package qstklearn :: Module hmm :: Class HMMLearner
[hide private]
[frames] | no frames]

Class HMMLearner

source code


A class for modeling and learning HMMs.

This class conveniently wraps the module level functions. Class objects hold 6
data members:
- num_states                    number of hidden states in the HMM
- num_symbols                   number of possible symbols in the observation 
                                sequence
- precision                     precision of the numpy.array elements (defaults to
                                longdouble)
- prior                         The prior probability of starting in each state
                                (Nx1 array)
- transition_matrix             The probability of transitioning between each state
                                (NxN matrix)
- emission_matrix               The probability of each symbol in each state
                                (NxO matrix)
You can set the 3 matrix parameters as you wish, but make sure the shape of
the arrays matches num_states and num_symbols, as these are used internally

Typical usage of this class is to create an HMM with a set number of states
and external symbols, train the HMM using addEvidence(...), and then use
the sequenceProb(...) method to see how well a specific sequence matches
the trained HMM.

Instance Methods [hide private]
 
__init__(self, num_states, num_symbols, init_type='uniform', precision=<type 'numpy.float128'>)
Creates a new HMMLearner object with the given number of internal states, and external symbols.
source code
 
reset(self, init_type='uniform')
Resets the 3 arrays using the given initialization method.
source code
 
sequenceProb(self, newData)
Returns the probability that this HMM generated the given sequence.
source code
 
addEvidence(self, newData, iterations=1, epsilon=0.0)
Updates this HMMs parameters given a new set of observed sequences using the Baum-Welch algorithm.
source code
Method Details [hide private]

__init__(self, num_states, num_symbols, init_type='uniform', precision=<type 'numpy.float128'>)
(Constructor)

source code 

Creates a new HMMLearner object with the given number of internal states, and external symbols.

calls self.reset(init_type=init_type)

reset(self, init_type='uniform')

source code 

Resets the 3 arrays using the given initialization method.

Wipes out the old arrays. You can use this method to change the shape
of the arrays by first changing num_states and/or num_symbols, and then
calling this method.

Currently supported initialization methods:
uniform         prior, transition, and emission probabilities are all 
                        uniform (default)

sequenceProb(self, newData)

source code 

Returns the probability that this HMM generated the given sequence.

Uses the forward-backward algorithm. If given an array of sequences, returns a 1D array of probabilities.

addEvidence(self, newData, iterations=1, epsilon=0.0)

source code 

Updates this HMMs parameters given a new set of observed sequences using the Baum-Welch algorithm.

newData can either be a single (1D) array of observed symbols, or a 2D matrix, each row of which is a seperate sequence. The Baum-Welch update is repeated 'iterations' times, or until the sum absolute change in each matrix is less than the given epsilon. If given multiple sequences, each sequence is used to update the parameters in order, and the sum absolute change is calculated once after all the sequences are processed.