% Traditional Naive Bayes (independent features assumption) with
% Bernoulli model on each feature (McCallum: "multi-variate
% bernoulli").  Ignores word counts higher than one.
%
% function [w] = nbBinary(x,y,alpha)
% x - examples [n,d]
% y - integer labels (1..l) [n,1]
% alpha - smoothing constant
% w - learned weight vector [d,l]
% 
% Written by Jason Rennie, January 2004
% Last modified: Tue May 24 17:54:02 2005

function [w] = nbBinary(x,y,alpha)
  fn = mfilename;
  if (nargin ~= 3)
    error('%s: 3 arguments required',fn);
  end
  [n,d] = size(x);
  l = max(y);
  x = x>0; % only consider presence/absence
  for i=1:l
    idx = find(y==i);
    tot = length(idx);
    cnt = full(sum(x(idx,:),1))';
    w(:,i) = log((cnt+alpha)./(tot+2*alpha)) - log((tot-cnt+alpha)./(tot+2*alpha));
  end
end
