フィルターのクリア

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 Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by