% bayes_clasifier.m
% Bayes Classifier for Guassian Distributed Data Samples
% Dr. W. David Pan, EE 604, UAH

clear all;
close all;
M = 100;   % number of samples generated for each class
data = zeros(2*M, 2);
label = zeros(2*M, 1);

% Generate data entries for Class 1
m1 = [3,3];
c1 = [2,1;1,2];
rng default  % For reproducibility
r1 = mvnrnd(m1,c1,M);
data(1:M,:) = r1;
label(1:M) = 1;

% Generate data entries for Class 2
m2 = [9,9];
c2 = [2,1;1,2];
rng default  % For reproducibility
r2 = mvnrnd(m2,c2,100);
group2 = ones(100, 1);
data(M+1:2*M,:) = r2;
label(M+1:2*M) = 2;

% Plot the data samples of the two classes
gscatter(data(:,1),data(:,2),label,'rb','ox')

% means are column vector in the book
m1 = m1';
m2 = m2';

% Decision function coefficients for Class 1
U = inv(c1)*m1
const1 = -(1/2)*m1' *inv(c1) * m1

% Decision function coefficients for Class 2
V = inv(c2)*m2
const2 = -(1/2)*m2' *inv(c2) * m2

% Calculate the coefficients for the decision boundary line
% Straight line equation: ax + by + c = 0
a = V(1) - U(1)
b = V(2) - U(2)
c = const2 - const1

% Plot the decision boundary
y = min(data(:,2)): 0.01: max(data(:,2));
x = (-b/a)*y - c/a;
hold on;
plot(x, y, 'g');