# How to create a probability table and derive probability results

7 views (last 30 days)
simcc on 8 Nov 2011
I have, say, two probability variables: xval=[1 2] with probability xp=[0.5 0.5] and yval=[1 2] with probability yp=[0.75 0.25]
The probability table of these two, I can calculate using: [X,Y]=ndgrid(xp,yp); jpdf=X./Y; which gives me the joint probabilities with the values [1 2; 2 4].
I can calculate the values of the probabilities using: j=find(xp>0)'*find(yp>0);
But now I want to convert this, in as little code as possible, to an array which holds the combined probability values. i.e.: xyval=[1 2 3 4] with probability xyp=[0.375 0.5 0 0.125]
The 2nd term in xyp is constructed from 0.375+0.125, because there are two values of '2' in the probability table.
There are ways to do this which are unnecessarily long, but I'm looking for something in a few lines at most. Any idea's? Thanks, sim

Jonathan on 8 Nov 2011
Hi sim,
If I understand correctly, then 'jpdf=X./Y;' should use element-wise multiplication like 'jpdf=X.*Y;'. Also, calculating 'j' with 'j=find(xp>0)'*find(yp>0);' will only work if 'xval' and 'yval' have the values 1 through n, for xval, and 1 through m, for yval. Using 'j=xval(xp>0)'*yval(yp>0)' is a more general approach.
The result you desire can be computed using the following code.
xyval = unique(j(:));
[~, index] = ismember(j(:), xyval);
xyp = accumarray(index, jpdf(:));
I hope this helps.
~Jonathan

simcc on 9 Nov 2011
This does the trick, thanks.
I was looking in the same direction, but was using accumarry incorrectly.