Hidden Markov model examples

This notebook assumes that the definitions in "markov_definitions.nb" have already been loaded. All starred (*) sections require the sample HMM and sample observation sequences file imported in the first section (you will need to download those files from the web and adjust the path in the first section to your system).

Import examples (ReadHMM, ReadObservationSequences)

Set directory

SetDirectory["/Users/nechyba/www/eel6825/course_materials/t8.markov_\
systems/sample_data"];

Read sample HMM file

Import HMM model

model  =  ReadHMM["sample_hmm.hmm"];

A matrix

model[[1]]  //  TableForm

B matrix

model[[2]]  //  TableForm

π vector

model[[3]]  //  TableForm

Read sample observation sequence file

Import observation sequences

obs  = ReadObservationSequences["sample_data.sym"];

Display observation sequences

TableForm[obs,  TableSpacing -> 1]

Visualization examples (DrawHMM, DrawObservationSequence)*

Define observable styles

styles = {Red, Green, Blue, Yellow, Cyan, Magenta, Orange, 
          Black};

Visualize sample hidden Markov model

DrawHMM[model[[1]], model[[2]], HMMStyle -> Sequential, 
        AngularOffset -> 46 Degree,  BendAngle -> 20  Degree,  
        SelfCircleSize -> 1/7, TextOffset -> 1,  SelfTextOffset -> 1, 
        StateText -> False,  MarkovText -> False, 
        ObservableStyle -> styles];

Visualize the sample observation sequences

First observation sequence

DrawObservationSequence[obs[[1]],  styles,  
        ImageSize -> 400];

Eleventh observation sequence

DrawObservationSequence[obs[[1]],  styles,  
        ImageSize -> 400];

All observation sequences

DrawObservationSequence[#,  styles,  
            ImageSize -> 400] &  /@  obs;

Evaluation example (LogProb, NormProb)*

Log probability of single observation sequence

LogProb[obs[[1]], FloorHMM[model]]

Log probability of multiple observation sequences

lprobs = LogProb[#, FloorHMM[model]] &  /@  obs

Plus  @@ lprobs

Normalized (w/respect to observation sequence length) probability of single observation sequence

NormProb[obs[[1]], FloorHMM[model]]

10^(LogProb[obs[[1]], FloorHMM[model]]/
          Length[obs[[1]]])

Decoding example (DecodeHMM)*

Visualize sample hidden Markov model

DrawHMM[model[[1]], model[[2]], HMMStyle -> Sequential, 
        AngularOffset -> 46 Degree,  BendAngle -> 20  Degree,  
        SelfCircleSize -> 1/7, TextOffset -> 1,  SelfTextOffset -> 1, 
        StateText -> False,  MarkovText -> False, 
        ObservableStyle -> styles];

Visualize sample observation sequence

DrawObservationSequence[obs[[1]], styles, 
        ImageSize -> 400];

Decode HMM (Viterbi algorithm)

qtst = DecodeHMM[obs[[1]],  FloorHMM[model]]

Visualize most likely state sequence

ListPlot[qtst,  PlotJoined -> True,  Frame -> True, 
        PlotStyle -> {Thickness[ .01], Red}];

Training example (TrainHMM)*

Define observable styles

styles = {Red, Green, Blue, Yellow, Cyan, Magenta, Orange, 
          Black};

Initial model

initial = RandomHMM[3, 8, 1];

Visualize initial model

DrawHMM[initial[[1]], initial[[2]], 
        HMMStyle -> Sequential, AngularOffset -> 46 Degree,  
        BendAngle -> 20  Degree,  SelfCircleSize -> 1/7, 
        TextOffset -> 1,  SelfTextOffset -> 1, 
        StateText -> False,  MarkovText -> False, 
        ObservableStyle -> styles];

Training on sample observation sequences (slow)

training = TrainHMM[obs, initial,  30];
final =  training  // Last;

Visualize final model

DrawHMM[final[[1]], final[[2]], HMMStyle -> Sequential, 
        AngularOffset -> 46 Degree,  BendAngle -> 20  Degree,  
        SelfCircleSize -> 1/7, TextOffset -> 1,  SelfTextOffset -> 1, 
        StateText -> False,  MarkovText -> False, 
        ObservableStyle -> styles];

Compare log probabilities of initial and final models

initialProb  =  
      Plus  @@  (LogProb[#,  initial] & /@ obs)

finalProb = 
      Plus  @@  (LogProb[#,  final] & /@ obs)

Generate observation sequence from HMM (GenerateObservationSequence)*

Visualize sample hidden Markov model

DrawHMM[model[[1]], model[[2]], HMMStyle -> Sequential, 
        AngularOffset -> 46 Degree,  BendAngle -> 20  Degree,  
        SelfCircleSize -> 1/7, TextOffset -> 1,  SelfTextOffset -> 1, 
        StateText -> False,  MarkovText -> False, 
        ObservableStyle -> styles];

Generate observation sequence from HMM

ogen = GenerateObservationSequence[model, 25]

Visualize generated observation sequence

DrawObservationSequence[ogen, styles, 
        ImageSize -> 400];

HMM utility functions (RandomHMM, FloorHMM)

Define observable styles

styles = {Red, Green, Blue, Yellow, Cyan, Magenta, Orange, 
          Black};

Generate random HMM (left-to-right structure)

nStates = 3;
nObservables  =  5;
structure  =  1;

model2 = RandomHMM[nStates, nObservables, structure];

DrawHMM[model2[[1]], model2[[2]], 
        HMMStyle -> Sequential, AngularOffset -> 46 Degree,  
        BendAngle -> 20  Degree,  SelfCircleSize -> 1/7, 
        TextOffset -> 1,  SelfTextOffset -> 1, 
        StateText -> False,  MarkovText -> False, 
        ObservableStyle -> styles];

Generate random HMM (fully connected)

nStates = 5;
nObservables  =  5;
structure  =  0;

model2 = RandomHMM[nStates, nObservables, structure];

DrawHMM[model2[[1]], model2[[2]], HMMStyle -> Circular, 
        AngularOffset -> 46 Degree,  BendAngle -> 20  Degree,  
        SelfCircleSize -> 1/7, TextOffset -> 1,  SelfTextOffset -> 1, 
        StateText -> False,  MarkovText -> False, 
        ObservableStyle -> styles];

Flooring an HMM

Generate random model and floor it

model3 = RandomHMM[3, 4, 1];
model4 = FloorHMM[model3,  {1, 1, 1},  10^-4];

Compare A matrices

model3[[1]]  //  TableForm

model4[[1]]  //  TableForm