%% SVM vs Bayes optimal decision boundary

%% Initialization
clear all; close all; clc

%% Create Data
%  The first two columns contain the point coordinates
%  and the third column contains the label.

meanP = 6;
meanN = 2;
x1P = (randn(1,100) + meanP)';
x2P = (randn(1,100) + meanP)';
x1N = (randn(1,100) + meanN)';
x2N = (randn(1,100) + meanN)';
X = [x1P x2P; x1N x2N];
y = [ones(100,1); zeros(100,1)];

% Randomise to break symmetry
r = randperm(size(X, 1));
X = X(r, :);
y = y(r);

%  Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);

% Add intercept term to x and X_test
%X = [ones(m, 1) X];

plotData(X, y)
hold on;

% SVM
C = 100;
model = svmTrain(X, y, C, @linearKernel, 1e-3, 20);
myVisualizeBoundaryLinear(X, y, model, 1);

C = 1;
model = svmTrain(X, y, C, @linearKernel, 1e-3, 20);
myVisualizeBoundaryLinear(X, y, model, 2);

% Bayes optimal
thetaBO(1) = -0.5 * [meanP - meanN; meanP - meanN]' * [meanP + meanN; meanP + meanN];
thetaBO(2) = meanP - meanN;
thetaBO(3) = meanP - meanN;
plotMyDecisionBoundary(thetaBO, X, y, 'Bayes Optimal', 'k');

% Labels and Legend
legend('Positive', 'Negative', 'SVM (C=100)', 'SVM (C=1)', 'Bayes Optimal')
xlabel('Feature 1')
ylabel('Feature 2')
title('Support Vector Machine')
hold off;

