# (C) May 2009, Mathieu Blondel import sys sys.path.append('/home/mathieu/Desktop/sources/jahmm-0.6.1.jar') from be.ac.ulg.montefiore.run.jahmm import ObservationReal from be.ac.ulg.montefiore.run.jahmm import OpdfGaussianMixture from be.ac.ulg.montefiore.run.jahmm import Hmm from be.ac.ulg.montefiore.run.jahmm.toolbox import MarkovGenerator from be.ac.ulg.montefiore.run.jahmm.learn import BaumWelchLearner from java.util import ArrayList def reals_to_observations(reals): arr = [ObservationReal(r) for r in reals] return ArrayList(arr) # We have 2 states and each observation has 1 dimension. means1 = [100.0, 150.0] variances1 = [15.0, 30.0] weights1 = [0.5, 0.5] opdf1 = OpdfGaussianMixture(means1, variances1, weights1) means2 = [200.0, 300.0] variances2 = [20.0, 40.0] weights2 = [0.6, 0.4] opdf2 = OpdfGaussianMixture(means2, variances2, weights2) pi = [0.6, 0.4] a = [[0.7, 0.3], [0.4, 0.6]] b = ArrayList([opdf1, opdf2]) hmm = Hmm(pi, a, b) # We generate fake data with the Markov generator. # In the real world, we would use real training data. # Don't forget that HMM are *generative* models ;) obs_set = ArrayList() generator = MarkovGenerator(hmm) from random import randint for i in range(100): # we generate 100 sequences of 20 + k observations where k is variable # to show that sequences can be of variable length obs_set.add(generator.observationSequence(20 + randint(0, 10))) # We train our HMM with our fake data print "HMM, before training" print str(hmm) + "\n" learner = BaumWelchLearner() hmm = learner.learn(hmm, obs_set) # Note that it shouldn't change that much since we trained the HMM # with observations generated from itself print "HMM, after training" print str(hmm) + "\n" # # Now let's find the log likelihood of a sample sequence obs = [85.0, 150.0, 245.0] print "Log likelihood for ", obs print hmm.lnProbability(reals_to_observations(obs)) print "Viterbi sequence" seq = hmm.mostLikelyStateSequence(reals_to_observations(obs)) print list(seq)