Package nltk_lite :: Package tag :: Module hmm :: Class HiddenMarkovModel
[hide private]
[frames] | no frames]

Class HiddenMarkovModel

source code

object --+
         |
        HiddenMarkovModel

Hidden Markov model class, a generative model for labelling sequence data. These models define the joint probability of a sequence of symbols and their labels (state transitions) as the product of the starting state probability, the probability of each state transition, and the probability of each observation being generated from each state. This is described in more detail in the module documentation.

This implementation is based on the HMM description in Chapter 8, Huang, Acero and Hon, Spoken Language Processing.

Instance Methods [hide private]
 
__init__(self, symbols, states, transitions, outputs, priors)
Creates a hidden markov model parametised by the the states, transition probabilities, output probabilities and priors.
source code
float
probability(self, sequence)
Returns the probability of the given symbol sequence.
source code
float
log_probability(self, sequence)
Returns the log-probability of the given symbol sequence.
source code
list
tag(self, unlabeled_sequence)
Tags the sequence with the highest probability state sequence.
source code
float
_output_logprob(self, state, symbol)
Returns: the log probability of the symbol being observed in the given state
source code
 
_create_cache(self) source code
sequence of any
best_path(self, unlabeled_sequence)
Returns the state sequence of the optimal (most probable) path through the HMM.
source code
sequence of any
best_path_simple(self, unlabeled_sequence)
Returns the state sequence of the optimal (most probable) path through the HMM.
source code
list
random_sample(self, rng, length)
Randomly sample the HMM to generate a sentence of a given length.
source code
 
_sample_probdist(self, probdist, p, samples) source code
 
entropy(self, unlabeled_sequence)
Returns the entropy over labellings of the given sequence.
source code
 
point_entropy(self, unlabeled_sequence)
Returns the pointwise entropy over the possible states at each position in the chain, given the observation sequence.
source code
 
_exhaustive_entropy(self, unlabeled_sequence) source code
 
_exhaustive_point_entropy(self, unlabeled_sequence) source code
array
_forward_probability(self, unlabeled_sequence)
Return the forward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states.
source code
array
_backward_probability(self, unlabeled_sequence)
Return the backward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states.
source code
 
__repr__(self)
repr(x)
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, symbols, states, transitions, outputs, priors)
(Constructor)

source code 

Creates a hidden markov model parametised by the the states, transition probabilities, output probabilities and priors.

Parameters:
  • symbols ((seq) of any) - the set of output symbols (alphabet)
  • states (seq of any) - a set of states representing state space
  • transitions (ConditionalProbDistI) - transition probabilities; Pr(s_i | s_j) is the probability of transition from state i given the model is in state_j
  • outputs (ConditionalProbDistI) - output probabilities; Pr(o_k | s_i) is the probability of emitting symbol k when entering state i
  • priors (ProbDistI) - initial state distribution; Pr(s_i) is the probability of starting in state i
Overrides: object.__init__

probability(self, sequence)

source code 

Returns the probability of the given symbol sequence. If the sequence is labelled, then returns the joint probability of the symbol, state sequence. Otherwise, uses the forward algorithm to find the probability over all label sequences.

Parameters:
  • sequence (Token) - the sequence of symbols which must contain the TEXT property, and optionally the TAG property
Returns: float
the probability of the sequence

log_probability(self, sequence)

source code 

Returns the log-probability of the given symbol sequence. If the sequence is labelled, then returns the joint log-probability of the symbol, state sequence. Otherwise, uses the forward algorithm to find the log-probability over all label sequences.

Parameters:
  • sequence (Token) - the sequence of symbols which must contain the TEXT property, and optionally the TAG property
Returns: float
the log-probability of the sequence

tag(self, unlabeled_sequence)

source code 

Tags the sequence with the highest probability state sequence. This uses the best_path method to find the Viterbi path.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: list
a labelled sequence of symbols

_output_logprob(self, state, symbol)

source code 
Returns: float
the log probability of the symbol being observed in the given state

best_path(self, unlabeled_sequence)

source code 

Returns the state sequence of the optimal (most probable) path through the HMM. Uses the Viterbi algorithm to calculate this part by dynamic programming.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: sequence of any
the state sequence

best_path_simple(self, unlabeled_sequence)

source code 

Returns the state sequence of the optimal (most probable) path through the HMM. Uses the Viterbi algorithm to calculate this part by dynamic programming. This uses a simple, direct method, and is included for teaching purposes.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: sequence of any
the state sequence

random_sample(self, rng, length)

source code 

Randomly sample the HMM to generate a sentence of a given length. This samples the prior distribution then the observation distribution and transition distribution for each subsequent observation and state. This will mostly generate unintelligible garbage, but can provide some amusement.

Parameters:
  • rng (Random (or any object with a random() method)) - random number generator
  • length (int) - desired output length
Returns: list
the randomly created state/observation sequence, generated according to the HMM's probability distributions. The SUBTOKENS have TEXT and TAG properties containing the observation and state respectively.

entropy(self, unlabeled_sequence)

source code 

Returns the entropy over labellings of the given sequence. This is
given by:

H(O) = - sum_S Pr(S | O) log Pr(S | O)

where the summation ranges over all state sequences, S. Let M{Z =
Pr(O) = sum_S Pr(S, O)} where the summation ranges over all state
sequences and O is the observation sequence. As such the entropy can
be re-expressed as:

H = - sum_S Pr(S | O) log [ Pr(S, O) / Z ]
  = log Z - sum_S Pr(S | O) log Pr(S, 0)
  = log Z - sum_S Pr(S | O) [ log Pr(S_0) + sum_t Pr(S_t | S_{t-1})
                                          + sum_t Pr(O_t | S_t) ]

The order of summation for the log terms can be flipped, allowing
dynamic programming to be used to calculate the entropy. Specifically,
we use the forward and backward probabilities (alpha, beta) giving:

H = log Z - sum_s0 alpha_0(s0) beta_0(s0) / Z * log Pr(s0)
          + sum_t,si,sj alpha_t(si) Pr(sj | si) Pr(O_t+1 | sj) beta_t(sj)
                        / Z * log Pr(sj | si)
          + sum_t,st alpha_t(st) beta_t(st) / Z * log Pr(O_t | st)

This simply uses alpha and beta to find the probabilities of partial
sequences, constrained to include the given state(s) at some point in
time.

_forward_probability(self, unlabeled_sequence)

source code 

Return the forward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states. Each entry (t, s) gives the probability of being in state s at time t after observing the partial symbol sequence up to and including t.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: array
the forward log probability matrix

_backward_probability(self, unlabeled_sequence)

source code 

Return the backward probability matrix, a T by N array of log-probabilities, where T is the length of the sequence and N is the number of states. Each entry (t, s) gives the probability of being in state s at time t after observing the partial symbol sequence from t .. T.

Parameters:
  • unlabeled_sequence (list) - the sequence of unlabeled symbols
Returns: array
the backward log probability matrix

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)