% Matlab code for LfD1 tutorial sheet for week 5.
% Tutor: Yuval Marom 1/11/02
% based on David Barber's code in the example given in
% the "Naive Bayes" lecture notes.
% Note however that here the  data are in the rows (ie 
% each row is a datapoint), and the  attributes are in the 
% columns (ie each column represents a different attribute), which
% is the opposite to the construction in the example in the lecture notes.

xP=[1 0 1 1 1 0 1 1; % Politics
0 0 0 1 0 0 1 1;
1 0 0 1 1 0 1 0;
0 1 0 0 1 1 0 1;
0 0 0 1 1 0 1 1;
0 0 0 1 1 0 0 1];

xS=[1 1 0 0 0 0 0 0; % Sport
0 0 1 0 0 0 0 0;
1 1 0 1 0 0 0 0;
1 1 0 1 0 0 0 1;
1 1 0 1 1 0 0 0;
0 0 0 1 0 1 0 0;
1 1 1 1 1 0 1 0];

%test point
x = [1 0 0 1 1 1 1 0];

%ML estimates of p(x=1|class)
thetaP = mean(xP)
thetaS = mean(xS)

%ML class priors
pP = size(xP,1)/(size(xP,1)+size(xS,1))
pS = 1-pP

% baysian numerators: prob(x|xP)*pP and prob(x|xS)*pS
% prob(x|xP) calculated using the naive bayes method where
% the attributes (columns) in xP are assumed independent so
% we can multiply their probabilities together. 
% similarly for prob(x|xS).
yP = prod(thetaP.^x.*(1-thetaP).^(1-x)) * pP
yS = prod(thetaS.^x.*(1-thetaS).^(1-x)) * pS      

% so prob(xP|x) is calcualted using the Bayes rule:
yP / (yP+yS)            

% should get 0.8306
