how to programme to calculate transition probability matrix?
    7 次查看(过去 30 天)
  
       显示 更早的评论
    
Suppose I have a sequence of states like 1,3,3,1,2,1,4,2,3,1,4,2,4,4,4,3,1,2,5,1. Transition probability matrix calculated by following equation probability=(number of pairs x(t) followed by x(t+1))/(number of pairs x(t) followed by any state). transition probability matrix calculated by manually by me as follows
                 1      3     2    4     5
            1    0     1/5   2/5  2/5    0
            3    3/4   1/4    0    0     0
            2    1/4   1/4    0    1/4   1/4
            4     0    1/5   2/5   2/5   0
            5     1     0     0     0    0
how to programme to obtain above transition probability matrix.
0 个评论
回答(1 个)
  James Tursa
      
      
 2016-5-5
        
      编辑:James Tursa
      
      
 2016-5-5
  
      E.g., brute force:
m = max(x);
n = numel(x);
y = zeros(m,1);
p = zeros(m,m);
for k=1:n-1
    y(x(k)) = y(x(k)) + 1;
    p(x(k),x(k+1)) = p(x(k),x(k+1)) + 1;
end
p = bsxfun(@rdivide,p,y); p(isnan(p)) = 0;
The p matrix will be ordered by natural indexing. E.g., 1, 2, 3, 4, 5 for the above example. It will not be ordered as 1, 3, 2, 4, 5 as you have it above. I.e., the probability of going from state i to state j is p(i,j).
4 个评论
  Patrick Laux
 2018-11-10
				
      编辑:Patrick Laux
 2018-11-10
  
			The brute force code above is giving 5 if you sum up all probabilities ??
Shouldn't it be: p = bsxfun(@rdivide,p,n) ?
However, this, i.e. sum(sum(p)) also gives only 0.95 (not 1). I am confused now.
Maybe: p = bsxfun(@rdivide,p,n-1) ??
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Markov Chain Models 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


