How to programme to calculate transition probability matrix?

조회 수: 4 (최근 30일)
Ramesh Kasotiya
Ramesh Kasotiya 2020년 7월 7일
답변: emami.m 2021년 1월 23일
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
The following code (by James Tursa) gives the answer
%%%%%
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;
%%%%
How to programme for transition probability matrix if x have 2D vectors or 3D vectors or N dimensional vectors. For example take 2D vectors matrix
x=[0 0;
1 0;
2 1;
2 1;
2 1;
1 1;
1 1;
1 1;
1 1;
1 0;
2 1;
2 1;
2 1;
3 2;
2 2;
2 2;
2 2;
3 2;
3 2;
4 2;
4 2;
4 2;
3 2;
3 2;
3 2]

답변 (1개)

emami.m
emami.m 2021년 1월 23일
I guess you should define unique combinations as states so you can map each pair of x to a state, then adopt your code to the new set of states.
[Categories,freq,new_seq] = unique(string(num2str(x)));
Now you can use new_seq as x in your code.

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by