Transition probability matrix for markov chain
12 views (last 30 days)
Show older comments
Hi there
I have time, speed and acceleration data for a car in three columns. I'm trying to generate a 2 dimensional transition probability matrix of velocity and acceleration.
The concept is given a particular speed and acceleration I would like to know the next most likely (probable) speed and acceleration.
I have some code below, but cannot fully understand it. Will it generate a 2D transition matrix or a 4D transition matrix?
Thank you
%%First bin data into categories
speedBinN = 5;
aceelBinN = 5;
speed = binit( data(:,2), linspace(min(data(:,2)),max(data(:,2)),speedBinN) ); % bin them into categories
accel = binit( data(:,3), linspace(min(data(:,3)),max(data(:,3)),aceelBinN) );
%%count up transitions
transCountMat = zeros(speedBinN,aceelBinN,speedBinN,aceelBinN);
for ii = 1:size(data,1)-1
transCountMat( speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) = transCountMat( speed(ii),accel(ii),speed(ii+1),accel(ii+1) ) + 1;
end
%%calculate probabilities
sumOverPossibleDestinations = sum( sum(transCountMat, 4), 3);
transMat = bsxfun( @rdivide, transCountMat, sumOverPossibleDestinations );
%%User Interactive stuff
IM = imagesc(squeeze(transMat(1,1,:,:)));
colorbar
set(IM,'ButtonDownFcn',@bdFcn)
set(gca,'ydir','normal')
ylabel speed
xlabel accel
hold on
p = plot(1,1,'w');
updateIndicator(1,1)
0 Comments
Answers (6)
Walter Roberson
on 2 Sep 2011
Looks to me like it will generate a 2D output for transMat. The count matrix is 4 dimensional, but it is summed twice, which reduces that to 2 dimensions.
Looks to me like binit() is just the second output of histc(). With the linspace nature of the bins, that operation could probably be made more efficient than even histc(). Also the transcount loop could probably be replaced with a single accumarray call.
0 Comments
John
on 3 Sep 2011
2 Comments
Walter Roberson
on 3 Sep 2011
There is no "val" in the code you show, so I am unsure what you are asking about?
When MATLAB displays a multidimensional matrix (4 dimensions in this case), it displays a "page" at a time, where a "page" is the first 2 dimensions. What format would you like the 4 dimensional matrix printed out in?
Walter Roberson
on 3 Sep 2011
Hmmm, looking again, it appears that transMat will be 4 dimensional, not the 2 dimensional that I thought.
Harini pushparaj
on 27 Dec 2017
@john..HI can i get your sample data file ..I maybe useful for my work ..I don't understand what is binit is ..it will be great if you could help me..Thank you!!
3 Comments
Harini pushparaj
on 8 Jan 2018
if i use binit ..i am getting error saying undefined binit and if i use histc..there is no error ..how come?
Walter Roberson
on 8 Jan 2018
You would need to store the above two lines in binit.m in your code directory.
shahab anjum
on 2 Mar 2020
please help me too if i have 1000x286 matrix how can i calculate the transistion and emission probabilites of that matrix plz
help
12 Comments
Walter Roberson
on 13 Mar 2020
Edited: Walter Roberson
on 28 Nov 2020
The way you processed your data originally is not compatible with the possibility that your array is an image.
There appear to be a number of techniques available for HMM processing of images for different purposes.
Attilio Pittelli
on 28 Nov 2020
you spoke about a list, but in the previous example he got the velocity and acceleration matrix. In my case i've got a double matrix of those inputs: a duration and a sequence of values. I'm trying to code it out but i'm still getting a transition probability matrix of 0 or 1 and the most of the outputs are 0 and it's not working. Do you have any suggestion? I'm really stucked at this point
Thank you
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!