function mi = mutinf(X,yt)
% computes the mutual information between
%   a feature and the target (both binary)
%  mi: mutual information
%  X:  the feature (vector or matrix)
%  y:  the target (vector)

Ny = size(yt,1);
N1 = sum(yt);

Py = N1/Ny;

Hy = (Py * log2(Py)) + ((1-Py) * log2(1-Py));

for k=1:size(X,2)
  Xk = X(:,k);
  
  % -- Pyjp = P(y , phi)
  % -- Pycp = P(y | phi)
  for y=1:2
      for p=1:2
          Pyjp(y,p) = getjpr(y-1,p-1,yt,Xk);
          Pycp(y,p) = getcpr(y-1,p-1,yt,Xk);
      end;
  end;

% boo = [Pyjp [k;k]]
  
  Hycp = 0;
  for y=1:2
      for p=1:2
          if(Pycp(y,p) ~= 0)
              Hycp = Hycp + Pyjp(y,p) * log2(Pycp(y,p));
          end;
      end;
  end;

  mi(k,1) = -1 * ( Hy - Hycp );
  %mi(k,2) = Hy;
  %mi(k,3) = Hycp;
  
end;
  
%-----------------------------------------------
function prob = getjpr(i1,i2,v1,v2)
% -- get a joint probability
Iv1 = find(v1==i1);
match = find(v2(Iv1)==i2);

prob = size(match,1) / size(v1,1);


%-----------------------------------------------
function prob = getcpr(i1,i2,v1,v2)
% -- get a conditional probability
Iv2 = find(v2==i2);
if isempty(Iv2)
    prob = 1;
else
    match = find(v1(Iv2)==i1);
    if isempty(match)
        prob = 0;
    else
        prob = size(match,1) / size(Iv2,1);
    end;
end;