function IG = infoGain(y1,y2)
% IG = infoGain(y1,y2)
%
% y1,y2 binary vectors of class labels
%
% Compute information gain, that is, class entropy
% of {y1, y2} minus weighted class entropy of y1 and y2.

if nargin < 2
	error('Too few input parameters.');
end
y1_pos = length(find(y1 > 0));
y1_neg = length(y1) - y1_pos;
y2_pos = length(find(y2 > 0));
y2_neg = length(y2) - y2_pos;
y1     = y1_pos + y1_neg;
y2     = y2_pos + y2_neg;
y_pos  = y1_pos + y2_pos;
y_neg  = y1_neg + y2_neg;
n      = y_pos  + y_neg;

% information gain
IG = entropy(y_pos, y_neg) ...
     - (y1/n * entropy(y1_pos, y1_neg) + y2/n * entropy(y2_pos, y2_neg));

% (information) gain ratio
%IG = IG/entropy(y1, y2);


function H = entropy(n_pos, n_neg)
% entropy

H = 0;
if n_pos <= 0 || n_neg <= 0
	return
end
n = n_pos + n_neg;

 H = -n_pos/n * ld(n_pos/n) - n_neg/n * ld(n_neg/n);


function y = ld(x)
% logarithmus dualis

y = log(x)/log(2);
