function theta = NBest(phis,labels,p,n)
% THETA = NBEST(X,Y,P,N)
% 
%   estimates the parameters of Naive Bayes generative model, based on
%   documents represented by binary features in rows of X.
%   The labels (1 or 2) are given on Y.
%   P and N are hyperparameters (of the beta distribution for the prior).
%   Default values for P and N are 0.01 and 10, respectively.
%   The estimated parameters are returned in THETA, i.e.
%   THETA(y,k) is the estimated Prob(phi_k = 1 | y )

% (c) Greg Shakhnarovich MIT AI Lab  2002


if (nargin < 3)
  p = 0.01;
end
if (nargin < 4)
  n = 10;
end

% Nky(k) = # of docs. that contain words{k} 
for y=1:2
  Nky = sum(phis(find(labels==y),:),1);
  
  % # of documents in class
  Ny = sum(labels==y);

  theta(y,:) = (p*n + Nky)/(Ny+n);
end

