This module implements a Hidden Markov Model featuring methods to obtain the real generation probability and Viterbi approximations as well as methods to initialize and/or reestimate a HMM given a set of generated items with Viterbi re-estimation and linear segmentation.
Implementation itself depends on no additional module.
This project can be used as a NodeJS module:
var hmm = ;var aModel = ;
Initializing with explicit details
A Hidden Markov Model can be initialized giving the explicit list of states (including final state), symbols, initial probabilities, transition probabilities and emission probabilities.
aModel ='1' '2' '3' 'F''F' 'a' 'b' 'c''1': 1'1':'1': 02'2': 05'3': 03'2':'1': 01'3': 09'3':'3': 04'F': 06'1':'b': 03'c': 07'2':'a': 03'b': 06'c': 01'3':'a': 1;
Reestimating an existing Hidden Markov Model
If you have an existing HMM and want to reestimate it with some training samples you can do it with
Optionally you can give an array of optimal paths as second parameter. If you don't give that array of paths the method will compute the optimal paths for each one of given items, so be sure that given items could be generated with the model.
Initializing a new Hidden Markov Model given some training samples
To initialize a new Hidden Markov Model without having a previous model you can use
This method will initialize the model, using linear segmentation to decide which will be the optimal state sequence for each one of the items and after that will reestimate the model.
Getting the probability that a given item is generated by the model
This implementation offers methods to get the real generation probability (using Forward algorithm), the Viterbi approximation and the most probable state sequence.
- Real probability: Given an array of symbols,
generationProbabilitymethod will return the probability that those symbols are generated in that order by the model.
- Viterbi approximation: Given an array of symbols,
viterbiApproximationmethod will return the probability that most probable state sequence generates given symbols in given order.
- Most probable state sequence: Given an array of symbols,
optimalStateSequencewill return the most probable state sequence as an array.
var hmm = ;var milk = ;var something = 'white' 'bottle' ;milk;console;
Printing the model
You can check internal details of the model with
>A 1 2 F1 0 1 02 0 0 1F 0 0 0B white bottle1 1 02 0 1Initial:1: 12: 0F: 0Final: F
You'll need to install development dependencies:
npm install --only=dev
Documentation can be generated running:
npm run-script doc
HTML documentation will be available in
Building from Coffeescript source
Although this module is shipped already built you can build it again from CoffeeScript sources running:
npm run-script prepublish
You can also run the tests and get coverage information. To run the tests on CoffeeScript source just run:
If you want to generate additional coverage information run:
npm run-script test-cov
And then you'll find coverage information in