How to find Joint PMF for three vectors using MATLAB

9 views (last 30 days)
I want to find the joint Probability Mass Function of three vectors which are quite large in number; and I need to find joint PMF for three vectors. Following is the code written but I am getting error with it:
% code
clear all; clc
filename = 'Case_I.xlsx';
num=xlsread('Case_I.xlsx'); %Case I Excel file contains data with 3 column vectors
xi = linspace(min(num(:,1)), max(num(:,1)));
yi = linspace(min(num(:,2)), max(num(:,2)));
zi = linspace(min(num(:,3)), max(num(:,3)));
hst = hist3(num,{xi yi zi});
pmf = hst/sum(hst(:));
The error I am getting is:
Error using hist3 (line 119) Bin centers must be specified with a cell array containing two numeric vectors.
Error in CaseIV_JointPMF1 (line 9) hst = hist3(num,{xi yi zi});
Any help in this regard on finding the PMF of three or more vectors will be appreciated. Thanks.

Answers (1)

Hoda Akl
Hoda Akl on 24 Jan 2020
Edited: Hoda Akl on 24 Jan 2020
Hello,
I just faced the same problem and I solved it the following way:
after you have your data in a matrix where the first column is variable 1, second is variable 2, third is variable 3 , which I assume is the matrix here that you call "num" ,
valuesx = sort(unique(num(:,1))); %this gets the unique values of each variable
valuesy = sort(unique(num(:,2)));
valuesz = sort(unique(num(:,3)));
probmat = zeros(length(valuesx),length(valuesy),length(valuesz));
for i=1:length(valuesx)
for j=1:length(valuesy)
for k = 1:length(valuesz)
%the sum gets how many instances those three events occured
%together and then divides by the total number of instances to
%normalize the probability
probmat(i,j,k) = (sum(num(:,1) == valuesx(i) & num(:,2) == valuesy(j) & num(:,3) == valuesz(k)))/size(num,1);
end
end
end
% probmat is your joint distribution
You should check that all values in probmat add up to 1.

Community Treasure Hunt

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

Start Hunting!