% This function makes the vital decision of whether we'll get to play
% tennis, given the weather conditions. The decision rule is Naive Bayes.
% The example is from the slides 'NaiveBayes.ppt' by Professor Latecki. 

% Made for CIS-2033 homework problem by David (TA).

% Since we have no observation of 'overcast' weather outlook when we didn't
% play tennis, the conditional probability P(overcast|Play = 'no') = 0, and 
% our model is deficient in that it predicts that we never play tennis when
% it's overcast outside. To correct the situation, we use the laplace 
% smoothing remedy from slide 9 of the lecture notes:
%   n_c + m*p 
%   --------- 
%     N + m
% where m is the number of fake (virtual) examples (see slides for what the 
% other params stand for)
function NaiveBayesClass 
m = 1
% m = 0     % with m = 0, we're back to our initial (MLE) estimate
% m = 1000

% laplace-smooth the prior probabilities:
prior_play_yes = (9 + m/2)/(14 + m)
prior_play_no  = (5 + m/2)/(14 + m)

% conditional probabilities - outlook given play
p_overcast_given_yes = (4 + m/3)/(9 + m)
p_overcast_given_no = (0 + m/3)/(5 + m)

% conditional probabilities - temperature given play
p_cool_given_yes = (3 + m/3)/(9 + m)
p_cool_given_no  = (1 + m/3)/(5 + m)

% conditional probabilities - humidity given play
p_high_given_yes = (3 + m/2)/(9 + m)
p_high_given_no  = (4 + m/2)/(5 + m)

% conditional probabilities - wind given play
p_strong_given_yes = (3 + m/2)/(9 + m)
p_strong_given_no  = (3 + m/2)/(5 + m)

% finally, our decision rule:
p_yes = prior_play_yes * p_overcast_given_yes * p_cool_given_yes * p_high_given_yes * p_strong_given_yes
p_no = prior_play_no * p_overcast_given_no * p_cool_given_no * p_high_given_no * p_strong_given_no

if (p_yes > p_no)
    sprintf('   go out and play tennis')
else
    sprintf('   go in and study more probability')
end