Continuous Emission Hidden Markov Model for sequence classification

Embed Size (px)

DESCRIPTION

A implementation of Continuous Emission Hidden Markov Model for sequence classification using C++,Eigen for OpenVision Machine Learning Package https://github.com/pi19404/OpenVision

Citation preview

  • ContinuousEmission Hidden

    Markov Models forSequence

    ClassificationPi19404

    March 13, 2014

  • Contents

    Contents

    Hidden Markov Models with continuous emission 3

    0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30.2 Continuous Emission . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    0.2.1 Gaussian Mixture Models . . . . . . . . . . . . . . . . . . . . 50.2.2 Forward Algorithm . . . . . . . . . . . . . . . . . . . . . . . . 5

    0.3 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    2 | 8

  • Hidden Markov Models with continuous emission

    Hidden Markov Models withcontinuous emission

    0.1 IntroductionIn this article we will look at hidden Markov models with continuousemission and its application in classification of D dimensional sequen-tial data . The emission probabilities are modeled using Gaussianmixture model.The implementation is developed as part of OpenVision-Vision and Machine learning Library.

    Let us assume that observed process Yn

    ; n 2 N is vector valuedin a euclidean space O Rd and Xn

    ; n 2 N are the hidden states.

    The pair of processes Xn

    and Yn

    is called a continuous hiddenMarkov model.

    0.2 Continuous Emission

    In the case of discrete hidden Markov model we had consideredthe emission to be discrete random variance and specified aemission matrix.

    In the case of continuous hidden Markov model the observedrandom variables are continuous in nature.Let the continuousrandom variable be defined in a D dimensional Euclidean space.Y = y

    1

    ; : : : ; y

    D

    is the observed random variable.

    At each time step the system generates a hidden state Xt

    ac-cording to a a state to state transition matrix Ti;j

    .

    Once xt

    has been generated the system generates a hidden clus-ter yt

    according to the state to emission probability distribution.

    Gaussian mixture models are used to model a multi-modal densityfunction in Euclidean space.

    Each hidden state is associated with emission probability distri-bution.

    3 | 8

  • Hidden Markov Models with continuous emission

    The emission probability distribution are specified in terms ofconditional distribution conditioned on the hidden state.

    The HMM can be viewed as a parametric model whose parame-ters vary with the state of the underlying hidden Markov chain.

    Let N be number of hidden states ,X = x1

    ; : : : ; x

    n

    ; n 2 N

    Let M be the number of mixture components

    P

    Y

    (yjx

    i

    ) =

    X

    k

    = 1

    M

    a

    i;k

    (

    i;k

    ;

    i;k

    )

    i;k

    is a vector of dimension D

    i;k

    is a matrix of size DxD

    Given we have a observation Y we need to determine the prob-ability that observation is emitted by the hidden state X that isgiven by the modeled conditional distribution.

    This distribution is used to model the emission probability.

    Thus the continuous emission hidden Markov model is specifiedby transition matrix,initial probability matrix and emission prob-ability distribution specified by Gaussian mixture model.

    MatrixXf _transition;

    MatrixXf _initial;

    vector _emission;

    int _nstates;

    int _nobs;

    Let M be the number of hidden states,possible discrete value therandom variable Xn

    can take.

    Let = (;A;B) represent hidden Markov model

    With a continuous HMM B corresponds to a family of paramet-ric distributions py

    () where 2 Rp and B = (1

    ; : : : ;

    M

    ).

    For each hidden state we have a associated parametric distribu-tion.

    P

    Y

    (y;

    i

    ) =

    X

    k

    a

    i;k

    (

    i;k

    ;

    i;k

    )

    Another interpretation of above representation is,that hiddenstate i consists of hidden subsets k 2 N

    4 | 8

  • Hidden Markov Models with continuous emission

    And each of these subsets k can generate the observed symbolY with probabilities ai;k

    and probabilities that observed symbol isgenerate from specific hidden state is given by (i;k

    ;

    i;k

    ).Thustotal probability can be specified as a product ai;k

    (

    i;k

    ;

    i;k

    ),and av-erage probability that observed symbol is generate by the hiddenstate i is given by the mixture model.

    Again the task is the present article is sequence classification.

    For the implementation we require implementation of GaussianMixture model and then see the modifications required in theforward/backward algorithm for continuous emission models.

    0.2.1 Gaussian Mixture Models

    The mixture models are specified by number of mixture compo-nents and parameters for each components k 2 M and parame-ter of mixture components (i;k

    ;

    i;k

    )

    We can consider this to be a soft-clustering process where acontinuous random vector xn

    is assigned to clusters specifiedwith the mixture models with some probability.

    The clusters can be considers as hidden discrete emission whichagain emits a continuous random vector which is truly beingobserved.

    Thus we can consider than we have a another hidden latentvariable which is the discrete emission specified by the cluter.

    The Class GaussianMixture which can be found in OpenVisionLibrary which encapsulates Gaussian Mixture model is used tomodel the emission probabilities

    0.2.2 Forward Algorithm

    The goal in the forward algorithm is to compute the probabilityP (y

    1

    ; : : : ; y

    n

    )

    this was computed recursively using

    (z

    n

    ) = P (x

    1

    ; : : : ; x

    n

    ; z

    n

    )(z

    n

    ) =

    X

    z

    n1

    (Z

    n1

    )P (x

    n

    jz

    n

    )P (z

    n

    jz

    n1

    )

    The quantity P (xn

    jz

    n

    ) was take from emission matrix in case ofdiscrete hidden Markov model.

    5 | 8

  • Hidden Markov Models with continuous emission

    In case of continuous HMM this is required to be computedfrom the parametric distribution P (xn

    = xjz

    n

    = z

    i

    ) =

    P

    k

    a

    i;k

    (

    i;k

    ;

    i;k

    )

    Remaining computation remain the same,just instead of a lookuptable in case of discrete distribution we have to perform com-putation according to model of parametric distribution in caseof continuous observation variable.

    To verify the code the scikit-learn -a ptyhon machine learningpackage was used consider the following hidden Markov modelfor sequence classification.

    #number of hidden states

    _nstates=2;

    #number of emissions per state

    _nmix=2;

    #length of the feature vector

    ndim=3;

    #transition probability

    model.transmat_=[[0.9,0.1],[0.1,0.9]];

    #inital state probability

    model.startprob=[0.5,0.5];

    #multivariate Gaussian for emission 1

    a1=GMM(n_components=_nmix,covariance_type='full');

    #the mean vector

    a1.means_=np.array([[ 0.08513302,-0.02109036,-0.54525889],

    [-0.02646832,0.01030935,0.55921764]]);

    #the covariance vector

    a1.covars_=np.array([[ [1.6045371 ,0,0],[0, 0.95491166,0],[0,0, 0.76062784]],

    [ [0.63467475,0,0],[0, 1.03482635,0],[0,0, 0.53010913]]]);

    #the mixture weights

    a1.weights_=[0.48161747,0.51838253];

    #multivariate Gaussian for emission 2

    a2=GMM(n_components=_nmix,covariance_type='full');

    a2.means_=np.array([[ 0.06263125, -0.01238598, -0.38820614],

    [ 0.3781768 , -0.29539352, 0.84720285]]);

    a2.covars_=np.array([[ [0.66081899, 0,0],[0,1.3027587,0],[0,0, 0.68465955]],

    [ [1.23561827,0,0],[ 0, 0.55178899,0],[ 0,0,0.80040462]]]);

    a2.weights_=[ 0.74733838, 0.25266162];

    model.gmms_[0]=a1;

    model.gmms_[1]=a2;

    #input feature vector sequence

    x=[[0.3,0.4,0.5],[0.1,0.2,0.3],[0.4,0.4,0.6]];

    6 | 8

  • Hidden Markov Models with continuous emission

    #predicting the probability that sequence belongs to model

    print model.score(x)

    The libconfig++ library is used to load the parameters of theHMM from the configuration files.

    A sample code for testing the Continuous emission HMM isshown below

    int hmm_test1(int argc,char **argv)

    {

    Config cfg;

    // Read the file. If there is an error, report it and exit.

    try

    {

    cfg.readFile(argv[1]);

    }

    catch(const FileIOException &fioex)

    {

    std::cerr

  • Hidden Markov Models with continuous emission

    float l=hmm1.likelyhood(sequence);

    cerr