take sum log of probability matrix

1 view (last 30 days)
Jay Hanuman
Jay Hanuman on 10 Nov 2016
Answered: Guillaume on 10 Nov 2016
I have data
m= 1 8 3 3 3 8 4 1 8 4 1 6 7 10 10 8 6 8 6 8 6 3 3 8 3 9......2000
with sequence length 2000. with this data I made transition probability matrix p
1 2 3 4 5 6 7 8 9 10
1 0.0781 0.1317 0.0489 0.0652 0.1222 0.0713 0.1371 0.2077 0.0339 0.1039
2 0.0681 0.1482 0.1012 0.0669 0.0319 0.0783 0.0892 0.1163 0.1398 0.1602
3 0.0421 0.1672 0.1125 0.0798 0.0679 0.0805 0.0773 0.1898 0.0974 0.0855
4 0.1421 0.1099 0.0465 0.0661 0.0357 0.0420 0.1528 0.1698 0.1939 0.0411
5 0.1809 0.1485 0.0477 0.0486 0.0837 0.0594 0.1206 0.1773 0.0774 0.0558
6 0.0545 0.0881 0.1186 0.0401 0.1154 0.1202 0.2588 0.0777 0.0897 0.0369
7 0.1935 0.0494 0.0445 0.1698 0.0327 0.1044 0.0661 0.1079 0.0445 0.1872
8 0.0612 0.0564 0.1830 0.0858 0.0828 0.2106 0.0390 0.0318 0.0828 0.1668
9 0.2055 0.0522 0.3804 0.0449 0.0353 0.0449 0.0955 0.0385 0.0425 0.0602
10 0.0781 0.2253 0.0525 0.1133 0.1824 0.0456 0.0394 0.0871 0.0961 0.0802
and initial probability matrix r
1 2 3 4 5 6 7 8 9 10
0.1052 0.1186 0.1136 0.0799 0.0794 0.0892 0.1026 0.1191 0.0890 0.1034
now I want to select data with window size 10, so 10 elements of m i.e. 1 8 3 3 3 8 4 1 8 4 are selected. I want to take sum of log of transition probability and initial probability of corresponding selected elements of m. i.e. 1 8 3 3 3 8 4 1 8 4 are selected so
log(r1)+log(r3)+log(r4)+log(r8)+log(p11)+log(p13)+log(14)+log(18)+log(31)+log(33)+log(34)+log(38)+log(41)+log(43)+log(44)+log(48)+log(81)+log(83)+log(84)+log(88)
in above r1,r3,r4,r8 are initial probability and p11,p13... are transition probability. so above result would be one single value, now same above thing I want to do with sliding window by 1 position to next i.e. skipping previous element and adding next element so selected elements of m will be 8 3 3 3 8 4 1 8 4 1. how to do this for sliding window upto end. each window operation give one value so window operation value should be accumulate in one variable. how to do this.

Answers (1)

Guillaume
Guillaume on 10 Nov 2016
This function will calculate the probability for a window:
function lp = logprobability(window, p, r)
uv = unique(window); %get unique values in window
[row, col] = ndgrid(uv); %generate all combinations of unique values
lp = sum(log(r(uv))) + sum(log(p(sub2ind(size(p), row(:), col(:)))));
end
To apply that to consecutive windows of size 10 of m:
windowsize = 10;
assert(mod(numel(m) / windowsize), 1) == 0, 'number of elements of m not a multiple of window size')
lps = cellfun(@(w) logprobability(w, p, r), mat2cell(m, 1, ones(1, numel(m)/windowsize) * windowsize));

Community Treasure Hunt

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

Start Hunting!