clear all
close all
clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% joint pmf %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----------------------------------------------------------')
disp('joint pmfs')
Pj(1,:)=[1/8 1/16 1/32 1/32];
Pj(2,:)=[1/16 1/8 1/32 1/32];
Pj(3,:)=[1/16 1/16 1/16 1/16];
Pj(4,:)=[1/4 0 0 0];
Pj
sum(sum(Pj))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% marginal pmfs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----------------------------------------------------------')
disp('marginal pmfs')
Px=sum(Pj)
Py=sum(Pj')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% conditional pmfs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----------------------------------------------------------')
disp('conditional pmfs')
for i=1:4
    Pc_y_x(:,i)=Pj(:,i)/Px(i)
end
sum(Pc_y_x)
%%%%%%%%%%%%%%%%
for i=1:4
    Pc_x_y(i,:)=Pj(i,:)/Py(i)
end
sum(Pc_x_y,2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% joint entropy %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----------------------------------------------------------')
disp('----------------------------------------------------------')
disp('ENTROPIES')
aux=-Pj.*log2(Pj)
aux(4,2)=0;
aux(4,3)=0;
aux(4,4)=0;
aux
disp('joint entropy')
Hxy=sum(sum(aux))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----------------------------------------------------------')
disp('marginal entropies')
Hx=sum(-Px.*log2(Px))
Hy=sum(-Py.*log2(Py))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----------------------------------------------------------')
disp('conditional entropies')
Hc_y_x=Hxy-Hx
Hc_x_y=Hxy-Hy
disp('conditional entropies - other way')
aux2=-Pc_y_x.*log2(Pc_y_x)
aux2(4,2)=0;
aux2(4,3)=0;
aux2(4,4)=0;
aux2
Hc_y_given_x_with_a_spec_value=sum(aux2);
disp('------------')
Hc_y_x_v2=sum(Px.*Hc_y_given_x_with_a_spec_value)
%for i=1:4
%    Hc_y_given_x_with_a_spec_value(i)=sum(-Pc_y_x(i,:).*log2(Pc_y_x(i,:)))
% end
disp('------------')
aux3=-Pc_x_y.*log2(Pc_x_y)
aux3(4,2)=0;
aux3(4,3)=0;
aux3(4,4)=0;
aux3
Hc_x_given_y_with_a_spec_value=sum(aux3,2);
Hc_x_given_y_with_a_spec_value=Hc_x_given_y_with_a_spec_value'
disp('------------')
Hc_x_y_v2=sum(Py.*Hc_x_given_y_with_a_spec_value)
disp('------------')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('----------------------------------------------------------')
disp('mutual information')
Ixy=Hx-Hc_x_y
Ixy=Hy-Hc_y_x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% graphical representation
plot([0 Hxy],[10 10],'r-','LineWidth',5)
hold on
plot([0 Hx],[9 9],'b-','LineWidth',5)
plot([Hx Hx+Hc_y_x],[9 9],'m-','LineWidth',5)
%%%%%
plot([0 Hy],[8 8],'g-','LineWidth',5)
plot([Hy Hy+Hc_x_y],[8 8],'c-','LineWidth',5)
%%%%%
plot([0 Hc_y_x],[7 7],'m-','LineWidth',5)
plot([Hc_y_x Hc_y_x+Ixy],[7 7],'y-','LineWidth',5)
plot([Hc_y_x+Ixy Hc_y_x+Ixy+Hc_x_y],[7 7],'c-','LineWidth',5)
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot([Hxy Hxy],[0 10.5],'k--','LineWidth',5)
set(gca,'Ytick',[])
set(gca,'Fontsize',20)
set(gca,'FontWeight','bold')
text(1.6,10.2,'H(X,Y)','Fontsize',20)
text(0.9,9.2,'H(X)','Fontsize',20)
text(2.4,9.2,'H(Y|X)','Fontsize',20)
text(0.9,8.2,'H(Y)','Fontsize',20)
text(2.4,8.2,'H(X|Y)','Fontsize',20)
%%%%%%
text(0.7,7.2,'H(Y|X)','Fontsize',20)
text(1.6,7.2,'I(X,Y)','Fontsize',20)
text(2.4,7.2,'H(X|Y)','Fontsize',20)
axis([0 Hxy 6.5 10.5])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


