# Is there a matlab function that calculates joint probability for more than 2 random variables ("histc" for more than 3 rvs)?

16 views (last 30 days)
tsan toso on 16 Oct 2013
Commented: tsan toso on 18 Oct 2013
Hi I would like to ask if there is a function that works like 'histc' for as much as 10 random variables. For short illustration:
if there are 3 random variables with 2 states each. The various combinations of the 3 random variables would give 2*2*2=8 states. And the function would return the counts (obversations) for a given dataset of the the 3 rvs, very much like the 'histc' function.
Does matlab have a built in function like that or is there a not too complicated way of calculating it?
Thanks a bunch

Jonathan LeSage on 16 Oct 2013
Edited: Jonathan LeSage on 16 Oct 2013
If you are dealing with discrete random variables, you really only need to use the accumarray function.
Here is an example for three random variables with two possible states:
% Generate three discrete random variables (uncorrelated)
numRealizations = 100;
numStates = 2;
x = randi(numStates,numRealizations,1);
y = randi(numStates,numRealizations,1);
z = randi(numStates,numRealizations,1);
% Count the number of (x,y,z) values in each possible state
rvCount = accumarray([x y z],1);
To include additional random variables, you can just append more values to the matrix input to accumarray ([x y z t], etc.). Counting the values shouldn't be too much of a problem, however, visualization is another problem entirely!
Hope this helps to get you started and good luck!
##### 3 CommentsShow 1 older commentHide 1 older comment
Jonathan LeSage on 17 Oct 2013
The only difference when dealing with continuous random variables is that we need to figure out which "bin" each point belongs too. Once you have found the proper bin for each point, the procedure that I outlined above remains unchanged. If the edges vector is 1:1, you could just use ceil, floor, or round. For example, if your values lie on the continuous domain between 1 and 2, values above or equal to 1.5 are in bin #2 and those below are in bin #1.
With only two states, you could define a custom threshold using logical indexing. Here is a simple example:
% Determining the bins of the x-data (two-state case)
threshold = 1.5;
xBinLocations = x;
xBinLocations(x >= threshold) = 2;
xBinLocations(x <= threshold) = 1;
The value of xBinLocations with return a vector which contains the discrete locations of each element of the continuous x-data vector. You could repeat a similar process for the y and z data, and then use the accumarray function as before!
tsan toso on 18 Oct 2013