Creating a matrix of marginal distributions from a matrix of joint distributions

조회 수: 11 (최근 30일)
alpedhuez
alpedhuez 2018년 6월 2일
답변: Prajit T R 2018년 6월 12일
I would like to create a matrix of marginal distributions from a matrix of joint distributions.
As a specific example, suppose
A=[0 0 a b; 0.1 0 c d; 0.1 0.1 e f; 0.2 0 g h; 0.2 0.1 i j;0.2 0.2 k l]
I want to create the following matrix B such that
- the first column of B collects unique elements of the entries in the first column and the second column of A (assume that entries in the first column of A are the same with entries in the second column of A). So the first column of B is
B(:,1)=[0 0.1 0.2]
- the second column of B: for each entry of the first column of B, find rows of A where the first entry of A is equal to that entry of B, and then take averages of the third entries of A (marginal distribution) Thus it will be
B(:,2)=[a; (c+e)/2; (g+i+k)/3]
- the third column of B: for each entry of the second column of B, find row of A where the second entry of A is equal to that entry of B, and then take average of the fourth entries of A. Thus it will be
B(:,3)=[(b+d+h)/3; (f+j)/2; l]
Please advise the steps. Thank you.
  댓글 수: 1
Kaninika Pant
Kaninika Pant 2018년 6월 5일
A is your joint distribution matrix? If that is the case then the rows must represent one random variable (lets say X) and the columns must represent the other random variable ( let Y). Then one element (eg (3,4) ) should have the probability of X=3 , Y=4. Then the marginal distribution of X is simply a column matrix where each row represents the probability of X=row_value with no conditions on Y. The first row will be the sum of all values in the first row of A...and so on. But what are you trying to do? What do the first and second columns of A represent? And why average?

댓글을 달려면 로그인하십시오.

답변 (1개)

Prajit T R
Prajit T R 2018년 6월 12일
Hi
The following code will help you perform the steps you need:
a=1;b=2;c=3;d=4;e=5;f=6;g=7;h=8;i=9;j=10;k=11;l=12;
A=[0 0 a b; 0.1 0 c d; 0.1 0.1 e f; 0.2 0 g h; 0.2 0.1 i j;0.2 0.2 k l];
T=A';
B_1=unique(T(1,:));
B_2=zeros(1,length(B_1));
B_3=zeros(1,length(B_1));
for i=1:length(B_1)
idx=find(T(1,:)==B_1(i));
temp=T(3,:);
B_2(i)=sum(temp(idx))/length(idx);
end
for i=1:length(B_1)
idx=find(T(2,:)==B_1(i));
temp=T(4,:);
B_3(i)=sum(temp(idx))/length(idx);
end
B=[B_1;B_2;B_3];
B=B'
Just change the values of a,b,c...l as per your requirement.
Hope this helps.
Prajit

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by