Calculating probability matrices of a table for a discrete time Markov chain

2 views (last 30 days)
Hello, I need help creating the probability matrices for a discrete time Markov chain from a table. The table is (5448x144) where the 144 columns correspond to 10 min intervals.
My data in each cell is one of three states (value in the cell ranges from 1-3), I am trying to create a probability matirx P for each time interval to express the probability that the state will transistion to a new state or remain the same.
How can I check the probability of a state changing from one column to the next using MatLab functions, and loop this for each column? I have given a small example of the table below:
For example the section below would have the probability matrix in transition from wher_28 to wher_29 as follows;
P=[0.875 0.000 0.125;
1.000 0.000 0.000;
0.000 0.000 1.000;]
Thanks,
Michael

Accepted Answer

Siddharth Solanki
Siddharth Solanki on 13 Jul 2021
The below code calculates the 143 required transition probability matrices. In the code below I have used matrix ‘data’ as the input. You may refer this link for converting a table to matrix.
data = randi([1,3],[5448,144]); %Matrix representing data
transition_matrices = zeros([3,3,143]); %Initializing all the 143 transition prob matrices
for c=1:143
for r=1:5448
%Updating the probability matix based on this column and next
%column's values
% 1 2 3 (Next Column)
%(This col) 1
% 2
% 3
% Each cell is a transition probability value
dim1val = data(r,c);
dim2val = data(r,c+1);
transition_matrices(dim1val,dim2val,c)= transition_matrices(dim1val,dim2val,c)+1;
end
%Normalizing the summed values to find probability
transition_matrices(:,:,c)= transition_matrices(:,:,c)/5448;
end
Additionally you can vectorize the code if required.
  1 Comment
Michael Wells
Michael Wells on 13 Jul 2021
Thank you for the support, I have now managed to get the transition matrices.
I have amended the above code, as I needed to find the probability that each individual state will change/ remain a state
wheras the above gave the probability of each state change out of all state changes (not just the probability against the intial state)
the amended code is below which give a probaility matrix where each row adds to 1 i.e 100%.
>> transition_matrices = zeros([3,3,143]); %Initializing all the 143 transition prob matrices
for c=1:143
for r=1:5448
%Updating the probability matix based on this column and next
%column's values
% 1 2 3 (Next Column)
%(This col) 1
% 2
% 3
% Each cell is a transition probability value
dim1val = data(r,c);
dim2val = data(r,c+1);
transition_matrices(dim1val,dim2val,c)= transition_matrices(dim1val,dim2val,c)+1;
end
%Normalizing the summed values to find probability
Column = data(:,c);
[GC] = groupcounts(Column);
transition_matrices(:,:,c)= [transition_matrices(1,:,c)/GC(1);
transition_matrices(2,:,c)/GC(2);
transition_matrices(3,:,c)/GC(3)];
end

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!