How to train a hidden markov model with constrained probabilities (or missing links between hidden states)?

345 views Asked by At

I have a hidden Markov model (HMM) with 3 hidden states and 2 discrete emission symbols. I know that the probability of transitioning from state 2 to state 3 is 0 (i.e. there is no direct link from S2 to S3). What is the best way of fitting the parameters (implementing the constraint) of this model given an observed sequence of symbols?

Can this be done in python's hmmlearn?

1

There are 1 answers

0
Jon Nordby On

This turned out to be quite easy in hmmlearn. Below is a code example that illustrates the approach.

class ConstrainedGaussianHMM(hmmlearn.hmm.GaussianHMM):
    def _do_mstep(self, stats):
        
        # do the standard HMM learning step
        super()._do_mstep(stats)
                
        # NOTE: the mapping of state indices to the data is nondeterministic
        # so you should find a heuristic to identify the correct ones
        s2 = 1, s3 = 2

        # manipulate the transition matrix as you see fit
        self.transmat_[s2,s3] = 0.0

Complete code example can be found in https://github.com/jonnor/machinehearing/blob/d557001e697f01ac5d7498e5cad00363bd8205a2/handson/constrained-hmm/ConstrainedHMM.ipynb