hmm
index
/s/chopin/a/grad/hamiltom/hmm.py

Python module for creating, training and applying hidden
Markov models to discrete or continuous observations.
:Author:
  - `Michael Hamilton, hamiltom@cs.colostate.edu`
Theoretical concepts obtained from Rabiner, 1989.

 
Modules
       
copy
numpy.linalg
numpy
pylab
numpy.random
time

 
Classes
       
HMM
HMM_Classifier

 
class HMM
    Creates and maintains a hidden Markov model.  This version assumes the every state can be
reached DIRECTLY from any other state (ergodic).  This, of course, excludes the start state.
Hence the state transition matrix, A, must be N X N .  The observable symbol probability
distributions are represented by an N X M matrix where M is the number of observation
symbols.  
 
              |a_11 a_12 ... a_1N|                   |b_11 b_12 ... b_1M|   
              |a_21 a_22 ... a_2N|                   |b_21 b_22 ... b_2M|
          A = | .    .        .  |               B = | .    .        .  |
              | .         .   .  |                   | .         .   .  |
              |a_N1 a_N2 ... a_NN|                   |b_N1 b_N2 ... b_NM|
      
       a_ij = P(q_t = S_j|q_t-1 = S_i)       b_ik = P(v_k at t|q_t = S_i)
    where q_t is state at time t and v_k is k_th symbol of observation sequence
 
   If the observable symbols are real numbers, then B is given by a list of
   N 2-tuples ( mus, sigma )_i, where mu and sigma parameterize a normal
   distribution for the observable value at state i.  If the observable values
   are univariate, then mus is simply the mean and sigma is the standard deviation.
   For multivariate values, mus is a 1 X D numpy array and sigma is the covariance,
   D X D, where D is the dimensionality of the values.
   
           B = [ ( mus, sigma )_1, ( mus, sigma )_2, ... , ( mus, sigma )_N ]
 
  Methods defined here:
__init__(self, n_states=1, **args)
:Keywords:
  - `n_states` - number of hidden states
  - `V` - list of all observable symbols
  - `A` - transition matrix
  - `B` - observable symbol probability distribution
  - `D` - dimensionality of continuous observations
  - `F` - Fixed emission probabilities for the given state ( dict: i -> numpy.array( [n_states] ),
          where i is the state to hold fixed.
__repr__(self)

 
class HMM_Classifier
    A binary hmm classifier that utilizes two hmms: one corresponding
to the positive activity and one corresponding to the negative
activity.
 
  Methods defined here:
__init__(self, **args)
:Keywords:
  - `neg_hmm` - hmm corresponding to negative activity
  - `pos_hmm` - hmm corresponding to positive activity
add_neg_hmm(self, neg_hmm)
Add the hmm corresponding to negative
activity.  Replaces current negative hmm, if it exists.
add_pos_hmm(self, pos_hmm)
Add the hmm corresponding to positive
activity.  Replaces current positive hmm, if it exists.
classify(self, sample)
Classification is performed by calculating the
log odds for the positive activity.  Since the hmms
return a log-likelihood (due to scaling)
of the corresponding activity, the difference of
the two log-likelihoods is the log odds.

 
Functions
       
backward(hmm, Obs, c=None)
Calculate the probability of a partial observation sequence
from t+1 to T, given some state t.
Obs: observation sequence
hmm: model
c: the scaling coefficients from forward algorithm
returns: B_t(i)
baum_welch(hmm, Obs_seqs, **args)
EM algorithm to update Pi, A, and B for the HMM
:Parameters:
  - `hmm` - hmm model to train
  - `Obs_seqs` - list of observation sequences to train over
:Return:
  a trained hmm
 
:Keywords:
  - `epochs` - number of iterations to perform EM, default is 20
  - `val_set` - validation data set, not required but recommended to prevent over-fitting
  - `updatePi` - flag to update initial state probabilities
  - `updateMean` - flag to update means of continuous observations, default is True
  - `updateVar` - flag to update variance of continuous obervations, default is True
  - `updateA` - flag to update transition probabilities, default is True
  - `updateB` - flag to update observation emission probabilites for discrete types, default is True
  - `scaling` - flag to scale probabilities (log scale), default is True
  - `graph` - flag to plot log-likelihoods of the training epochs, default is False
  - `normUpdate` - flag to use 1 / -(normed log-likelihood) contribution for each observation
                   sequence when updating model parameters, default if False
  - `verbose` - flag to print training times and log likelihoods for each training epoch, default is false
dishonest_casino_test(graph=True)
forward(hmm, Obs, scaling=True)
Calculate the probability of an observation sequence, Obs,
given the model, P(Obs|hmm).
Obs: observation sequence
hmm: model
returns: P(Obs|hmm)
norm_df(X, mu=0, sigma=1.0)
Returns the normal density function centered at mu
with width sigma for a value X.  X is a N X D matrix
where N is the number of samples and D is the
dimensionality of the data.
symbol_index(hmm, Obs)
Converts an obeservation symbol sequence into a sequence
of indices for accessing distribution matrices.
viterbi(hmm, Obs, scaling=True)
Calculate P(Q|Obs, hmm) and yield the state sequence Q* that
maximizes this probability. 
Obs: observation sequence
hmm: model