function yout = nbpredict(theta,py,X,y)
% predict using a naive bayes model
% -----------------------------------------
% theta: the prediction parameters
% Xte:   the data to predict on
% yte:   the ground truth of the data

[r c] = size(X);

for i=1:r
    for j=1:c
        if(X(i,j) > 0)
            pthetak0(i,j) = theta(1,j);
            pthetak1(i,j) = theta(2,j);
        else
            pthetak0(i,j) = (1 - theta(1,j));
            pthetak1(i,j) = (1 - theta(2,j));
       end;
    end;
end;

ptheta0 = prod(pthetak0,2);
ptheta1 = prod(pthetak1,2);

% -- yprob is the probability that it's a 1
% -- (i.e., simply use ptheta1(i) & py(2))

for i=1:r
    yprob(i,1) = (ptheta1(i) * py(2)) / ...
          ((ptheta0(i) * py(1)) + ...
           (ptheta1(i) * py(2)));
end;

for i=1:size(yprob) 
    if(yprob(i) > .5) 
        ypred(i,1) = 1; 
    else 
        ypred(i,1) = 0;
    end;
end;

if(nargin<3)
    ep = yprob';
else
    yout = [yprob ypred y abs(ypred-y)];
%    yout = sum(abs(yerr)) / size(yerr,1);
end;