A simple example of the Viterbi algorithm for HMM

Source: Internet
Author: User

Today I read a Daniel's technical blog about Hmm, and after reading it, I wrote a simple example of the Viterbi algorithm, which was written in Scala and the Java language. Upload it now.

Package COM.TXQ.HMMImport Java.util
Import Scala.collection._
/**
* Hmm Viterbi algorithm, based on the display status chain estimation implicit chain
*@param statesImplicit states
*@param observationsAn explicit states
*@param start_probabilityInitial probability vector
*@param transition_probabilityConversion probability
*@param emission_probabilityEmission probability
*@authorTongxueqiang
* @ 2017/01/21
*/
Class Markov_weather (states:array[any],observations:array[any],start_probability:map[string,double],transition_probability:map[string,map[string,double]],emission_probability:map[string,map[String,double]]) {
/**
*
*@param obs_serObserved explicit state chain
*@return
*/
Def compute_hidden_ser (Obs_ser:util. linkedlist[String]): Util. linkedlist[String] = {
Val result =New util. linkedlist[String] ()Put the results in a linked list
var p = mutable. map[String,double] ()Product of storage conversion probability and output probability
var keys:seq[String] =Null
var values:seq[double] =Null
var max:double =0.0
var label:int =0
Initialize, get an implicit state first
var obs_state = Obs_ser.poll ()
if (obs_state! =NULL) {
For ((k,v) <-start_probability) {
Val A = v * emission_probability (k) (Obs_state)
p + = (k->a)
}
Keys = P.keys.toseq
Values = P.values.toseq
Max = VALUES (0)
for (i <-1 until Values.size) {
if (values (i) > Max) {
Max = VALUES (i)
label = i
}
}
Result.add (keys (label))
Label =0
P.clear ()
}
Start Iteration
Obs_state = Obs_ser.poll ()
while (obs_state! =NULL) {
Val hid_ser = Result.peeklast ()
if (hid_ser! = null) {
For ((k,v) <-transition_probability (Hid_ser)) {
val A = v * emission_probability (k) (Obs_state)
p + = (k->a)
}
Keys = P.keys.toseq
Values = P.values.toseq
Max = values (0)
For (i <- 1 until values.size) {
if (values (i) > Max) {
Max = VALUES (i)
label = i
}
}
Result.add (keys (label))
Label = 0
P.clear ()
Obs_state = Obs_ser.poll ()
}
}
Result
}
}

Test class:

Package COM.TXQ.HMMImport Java.util
/**
*
*/
Object Hmmtest {
def main (args:array[String]): Unit = {
Test Data Set
Val states =Array[any] ("Rainy","Sunny")Implicit states
Val observations =Array[any] ("Walk","Shop","Clean")An explicit states
Val start_probability =Map ("Rainy"0.6,"Sunny"0.4)Initial probability vector
Val transition_probability =Map ("Rainy"Map ("Rainy"0.7,"Sunny"0.3),"Sunny"Map ("Rainy"0.4,"Sunny"0.6))Conversion probability
Emission probability
Val emission_probability =Map ("Rainy"Map ("Walk"0.1,"Shop"0.4,"Clean"0.5),"Sunny"Map ("Walk"0.6,"Shop"0.3,"Clean"0.1))
Val Obs_ser =New util. linkedlist[String] ()Observed set of explicit states
Obs_ser.add ("Walk")
Obs_ser.add ("Shop")
Obs_ser.add ("Clean")
Val Markov:markov_weather =New Markov_weather (states,observations,start_probability,transition_probability,emission_probability)
Val result = Markov.compute_hidden_ser (obs_ser)//Perform calculation
var hidden_state = Result.pollfirst ()
While (hidden_state! = null) {
Print (hidden_state)
if (result.size ()! = 0) {
Print ("-")
}
Hidden_state = Result.pollfirst ()
}
}
}
The results displayed:
Sunny->sunny->rainy
Process finished with exit code 0

A simple example of the Viterbi algorithm for HMM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.