To evaluate the matrix of transition Markov chains in MATLAB with different lengths of a sequence of states

I am trying to build a transition matrix for the Markov chain in MATLAB; I have several different observation sequences (all different lengths), and I need to generate a transition matrix using these.

Building a multi-row Markov matrix transition matrix in Matlab shows me how to build a transition matrix with one observation sequence.

How can I build one using observations of various lengths? One example may be that one sequence is 1,2,3,4 and the other 4,5,6. Is there a way to do this without having to loop through all the sequences and calculations?

+2
source share
2 answers

So, for Markov chains, I assume that you are only interested in state transitions. You can group all state transitions into a single Nx2 matrix and then count the number of times a row appears.

In this example, I use three observations of length 4, 3, and 3. I can use cellfun to group all state transitions in one matrix as follows:

obs = cell(1, 3); obs(1) = {[1 2 3 4]}; obs(2) = {[4 5 6]}; obs(3) = {[3 4 5]}; transitions = cellfun(@(x)([x(1:length(x)-1); x(2:length(x))]), obs, 'UniformOutput', false); alltransitions = cell2mat(transitions)'; 

Which gives me my observed transitions (1->2, 2->3, 3->4 ...) :

 alltransitions = 1 2 2 3 3 4 4 5 5 6 3 4 4 5 

To set up the transition matrix, you can use the advice given here and calculate the rows of all your transitions:

http://www.mathworks.it/matlabcentral/answers/75009-i-ve-a-matrix-of-6x4-and-i-want-to-count-the-rows-how-many-times-it- occur-in-a-matrix

 [uniqueTransitions, ~, i]=unique(alltransitions,'rows','stable'); v=arrayfun(@(x) sum(i==x),1:size(uniqueTransitions,1))'; p = v/sum(v); 

My vector p contains my transition probability, so I can continue and build a sparse matrix

 transitionMatrix = sparse(uniqueTransitions(:,1), uniqueTransitions(:,2), p, 6,6) 

that leads to:

 transitionMatrix = (1,2) 0.1429 (2,3) 0.1429 (3,4) 0.2857 (4,5) 0.2857 (5,6) 0.1429 
+4
source

This might be an easier way to calculate the transition probability matrix (TPM) for a given data sequence (single vector) as follows:

myS = {S1, S2, S1, S3, ...} with as many states as yours;

TPM = hmmestimate (myS, myS);

The hmmestimate function is defined in hidden Markov models in MATLAB.

0
source

Source: https://habr.com/ru/post/1214404/


All Articles