function G = infogain(data)

% INFOGAIN   Information gain (:= expected reduction in entropy).
%
%  G = INFOGAIN(DATA)
%
%  DATA is a matrix of non-negative numbers indicating frequencies or 
%  probabilities. Each row specifies a level of the target variable T
%  and each column specifies a level of the predictor variable A.
%  INFOGAIN calculates the expected reduction of the entropy of T
%  caused by knowing the value of the predictor A.
%  Note that the function is symmetrical:  infogain(D)==infogain(D')
%
%  Examples: infogain(eye(4)) == 2   % maximally informative predictor
%            infogain(ones(4)) == 0  % totally noninformative predictor
%
%  Typical usage:  infogain(xtab2([stim resp])))
%
%  See also ENTROPY, XTAB2.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2000/3/6 22:00 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

% Garner, W. & Hake, H. (1951). The amount of information in
% absolute judgments. Psychological Review, 58(4), 446-459.
%
% Test case: data = [76 20 0 0; 24 68 20 0; 0 12 68 32; 0 0 12 68]
%            infogain(data) == infogain(data') == 0.96

A_marginals = sum(data) ;
A_probs = A_marginals ./ sum(A_marginals) ;

T_marginals = sum(data')' ;

G = entropy(T_marginals) - sum(A_probs .* entropy(data)) ;

%%  Return G
%%%%% End of INFOGAIN.M
