%==============================================================================
%    mut_info.m
%    Copyright (C) 2007  Denise M. Wolf, dmwolf@lbl.gov
%
%    This program is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see <http://www.gnu.org/licenses/>.
%===============================================================================
function  [HA,HB,HAB,IAB]=mut_info(ClustA,ClustB)
%------------------------------------------------------------------------------
%
% [HA,HB,HAB,IAB] = mut_info(ClustA,ClustB) calculates the mutual
% information between two clusterings of the same data
%
% Input: 
%   ClustA, "optimal" clustering vector for A (each entry an integer)
%   ClustB, "optimal" clustering vector for B (each entry an integer)
% Output:
%   HA, entropy of A in bits
%   HB, entropy of B in bits
%   HAB, entropy of the vector (A,B)in bits
%   IAB, mutual information of A and B in bits
%
%------------------------------------------------------------------------------

no_clustersA=max(ClustA);
no_clustersB=max(ClustB);


%H=-sum(log2(pi)pi)  = informational entropy 

HA=0;

for i=1:no_clustersA   
  size_clustiA(i)=length(find(ClustA==i));
  piA(i)= size_clustiA(i)/length(ClustA);
  HA=HA-log2(piA(i))*piA(i);
end

HB=0;

for i=1:no_clustersB   
  size_clustiB(i)=length(find(ClustB==i));
  piB(i)= size_clustiB(i)/length(ClustB);
  HB=HB-log2(piB(i))*piB(i);
end


%%%I(X,Y)=H(X)+H(Y)-H(X,Y)= mutual information of X,Y
%%H(X,Y)=sum_x(sum_y(pij*log2(pij))= entropy of vector (X,Y)

PAB=zeros(no_clustersA,no_clustersB); %PAB is the probability distribution of A,B
HAB=0;
for j=1:no_clustersB
    for i=1:no_clustersA 
        PAB(i,j)=length(intersect(find((ClustA==i)),find((ClustB==j))))/length(ClustA);
        if PAB(i,j)>0
           HAB=HAB-PAB(i,j)*log2(PAB(i,j));
        end
    end
end


IAB=HA+HB-HAB;

