Search code examples
matlabhidden-markov-models

MATLAB's hmmtrain assumes initial transition from state 1


The MATLAB statistics toolbox function hmmtrain.m appears to assume that the model is initially in state 1 before the training sequence. Is there any way to turn off this "feature"? An example:

>> y = [ 3 3 1 2 3 ];
>> H = eye( 3 );
>> T = ones(3)/3;
>> [ T, H ] = hmmtrain( y, T, H )

T =

         0    0.5000    0.5000
         0         0    1.0000
    0.5000         0    0.5000


H =

     1     0     0
     0     1     0
     0     0     1

The training set includes no transitions from 1 to 3. Why is T(1,3) non-zero?!


Solution

  • I wrote the following wrapper function for hmmtrain that creates a special state 1 that is only used for the initial state of the model. From the outside caller's point of view it doesn't exist and you only get statistics of the transitions between the symbols of your training sequence; it learns nothing regarding the initial state of the system.

      % MYHMMTRAIN - Wrapper on HMMTRAIN that removes "initial state" effects
    
      function [ T, H ] = myhmmtrain( y, T, H, varargin )
    
      % hmmtrain assumes the system always starts in state 1, 
      % so we create a "state 1" that isn't used for anything else
      N = size(T,1);
      T = [ 0           ones(1,N)/N; ...
            zeros(N,1)  T ];
      M = size(H,2);
      H = [ zeros(1,M); H ];
    
      % train
      [ T, H ] = hmmtrain( y, T, H, varargin{:} );
    
      % remove false state 1
      T = T(2:end,2:end);
      H = H(2:end,:);
    
      end