How to programme to calculate transition probability matrix?
5 views (last 30 days)
Show older comments
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]
0 Comments
Answers (1)
emami.m
on 23 Jan 2021
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!