function h = h2nats(p, q) %H2NATS binary (cross-)entropy in nats for a Bernoulli distribution % % H2NATS is applied elementwise to probabilities p in [0,1] % % h2nats(p, q) = -p.*log(q) - (1-p).*log(1-q) % If p is omitted, q = p. % % However, unlike the above straight-forward implementation: % - when 0*log(0) is evaluated, the limit of 0 is returned. % - q probabilities outside [0,1] give errors rather than complex numbers % % See also: H2BITS % Iain Murray, November 2007, September 2008 if nargin < 2 z = (p == 0) | (p == 1); p1 = 1-p; h = -p.*reallog(p) - p1.*reallog(p1); h(z) = 0; else assert(isequal(size(p), size(q))); h = zeros(size(p)); m = (p ~= 0); h(m) = -p(m).*reallog(q(m)); m = (p ~= 1); h(m) = h(m) - (1-p(m)).*reallog(1-q(m)); end