% scatter plot data
% plot training data, decision boundary, and test on testing data
% output visualizations and both training and testing accuracy
% Fred Park
% COSC 190, Whittier College, Spring Semester 2017

close all; clear all; clc;

data = load('exam_scores_training.txt')'; % load regression data
%data = load('exam_scores.txt')'; % load regression data


y = data(3,:);
M = length(data(1,:));
X = ones(3,M); X(2,:) = data(1,:); X(3,:) = data(2,:); 
x1 = X(2,:); x2 = X(3,:);
pos = find(y==1); % find indices where label = 1
neg = find(y==0); % ditto for 0

%axis range, 10 percent past boundary points
x1m = min(x1); x1M = max(x1);
dx1 = 0.10*(abs(x1M-x1m));
x2m = min(x2); x2M = max(x2);
dx2 = 0.10*(abs(x2M-x2m));
x1ma = x1m-dx1; x1Ma = x1M+dx1;
x2ma = x2m-dx2; x2Ma = x2M+dx2;

% for plotting options
ms = 7; % markersize
xl = 'Exam 1'; %xlabel
yl = 'Exam 2'; %ylabel
ftitle1 = 'Exam Scores: Training';
ftitle2 = 'Exam Scores: Testing';
ftitle3 = 'Exam Scores: Testing and Training'
l1 = 'passed'; %class label
l2 = 'not passed'; %ditto

figure(1);
plot(x1(pos),x2(pos),'r^','MarkerSize',ms,'MarkerFaceColor','r'); hold on;
plot(x1(neg),x2(neg),'bo','MarkerSize',ms,'MarkerFaceColor','b'); hold off;
xlabel(xl,'FontWeight','bold'); 
ylabel(yl,'FontWeight','bold');
title(ftitle1); axis([x1ma x1Ma x2ma x2Ma]); % axis([xmin xmax ymin ymax])
legend(l1, l2);

%%% points on decision boundary line %%%
xdb = [25 98]; ydb = [92 25]; 
%xdb = [20 70]; ydb = [70 30]; 
% xdb = [x_1 x_2]; % ydb = [y_1 y_2];
%
m = (ydb(2)-ydb(1))/(xdb(2)-xdb(1)); % i.e. (y_2-y_1)/(x_2-x_1);
b = ydb(1) - m*xdb(1); % i.e. y_1 - m*x_1
% eqn of line: y - mx - b = 0; 
% equiv to: -b*1 + -m*x + 1*y = 0
% theta = [-b; -m; 1]

theta = [-b; -m; 1]; % parameters for line
db = @(x) (1/theta(3))*(-(theta(2)*x) - theta(1));
xrange_db = (x1ma:0.1:x1Ma);
pause

figure(2);
plot(x1(pos),x2(pos),'r^','MarkerSize',ms,'MarkerFaceColor','r'); hold on;
plot(x1(neg),x2(neg),'bo','MarkerSize',ms,'MarkerFaceColor','b'); hold on;
plot(xrange_db, db(xrange_db), 'g-', 'LineWidth', 2);
xlabel(xl,'FontWeight','bold'); 
ylabel(yl,'FontWeight','bold');
title(ftitle1); axis([x1ma x1Ma x2ma x2Ma]); % axis([xmin xmax ymin ymax])
hold off; hold off;
legend(l1, l2);
pause

p = zeros(M,1); %initialize vect for predicted class label
g = @(z) 1./(1 + exp(-z)); % sigmoid function

for i = 1:M
    p(i) = g(theta'*X(:,i)) > 1/2; % calculate predictions based on decision boundary
end

% Compute accuracy on our training set
fprintf('----------------------------------------------------------------\n');
fprintf('Training Accuracy: %.2f %%\n', (sum(p == y')/M)*100 );
fprintf('----------------------------------------------------------------\n');

%test predictor
te1 = [1; 85; 50];
p1 = theta'*te1; % testing value above or below line
fprintf('g(theta''*te1) = %.16f \n',g(p1));
pp1 = g(p1)>1/2;  % 1 if > 0 else 0
fprintf('The model predicts that \n example (%.1f,%.1f) is in class %.0f \n',te1(2),te1(3),pp1);
fprintf('----------------------------------------------------------------\n');
te2 = [1; 35; 50];
p2 = theta'*te2; %ditto
fprintf('g(theta''*te2) = %.16f \n',g(p2));
pp2 = (g(p2) > 1/2); %ditto
fprintf('The model predicts that \n example (%.1f,%.1f) is in class %.0f \n',te2(2),te2(3),pp2);


figure(3);
plot(x1(pos),x2(pos),'r^','MarkerSize',ms,'MarkerFaceColor','r'); hold on;
plot(x1(neg),x2(neg),'bo','MarkerSize',ms,'MarkerFaceColor','b'); hold on;
plot(te1(2),te1(3),'mo','MarkerSize',15,'MarkerFaceColor','m'); hold on;
plot(te2(2),te2(3),'co','MarkerSize',15,'MarkerFaceColor','c'); hold on;
xlabel(xl,'FontWeight','bold'); 
ylabel(yl,'FontWeight','bold');
title(ftitle3);  axis([x1ma x1Ma x2ma x2Ma]); % axis([xmin xmax ymin ymax])
plot(xrange_db, db(xrange_db), 'g-', 'LineWidth',2); hold on;
hold off; hold off; hold off;
legend(l1, l2,'passed (testing)', 'not passed (testing)');
pause

tx = theta'*X;
gtx = g(tx);

ptx = find(g(theta'*X) > 1/2);
ntx = find(g(theta'*X) < 1/2);
mtx = min(tx);
Mtx = max(tx);
xax = [mtx-5:0.5:Mtx+5];

figure(4);
plot(tx(pos),tx(pos),'r^','MarkerSize',ms,...
    'LineWidth',1, 'MarkerFaceColor','r'); hold on;
plot(tx(neg),tx(neg),'bo','MarkerSize',ms,...
    'LineWidth',1,'MarkerFaceColor','b'); hold on;
plot(xax,0*xax,'k-','LineWidth',2);
hold off; hold off;
xlabel('\theta^{T}x','FontSize',15,'FontWeight','bold'); 
ylabel('\theta^{T}x','FontSize',15,'FontWeight','bold');
title('Linear Separation')
pause

figure(5);
plot(tx(pos),gtx(pos),'r^','MarkerSize',ms,...
    'LineWidth',1, 'MarkerFaceColor','r'); hold on;
plot(tx(neg),gtx(neg),'bo','MarkerSize',ms,...
    'LineWidth',1,'MarkerFaceColor','b'); hold on;
plot(xax,g(xax),'k-','LineWidth',2);
hold off; hold off;
xlabel('\theta^{T}x','FontSize',15,'FontWeight','bold'); 
ylabel('g(\theta^{T}x)','FontSize',15,'FontWeight','bold');
title('Sigmoid Separation')

pause


%%%%%%%%%%%%%%%%%%%%%%%%%% testing phase %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%data = load('exam_scores_testing.txt')'; % load regression testing data
data = load('exam_scores.txt')'; % load regression testing data


%store training data for visualization
x1t = x1; x2t = x2;
post = pos; negt = neg;

y = data(3,:);
m = length(data(1,:));
X = ones(3,m); X(2,:) = data(1,:); X(3,:) = data(2,:); 
x1 = X(2,:); x2 = X(3,:);
pos = find(y==1); % find indices where label = 1
neg = find(y==0); % ditto for 0

%adjust axis to testing set. 
%uncomment if you desire this
%axis range, 10 percent past boundary points
% x1m = min(x1); x1M = max(x1);
% dx1 = 0.10*(abs(x1M-x1m));
% x2m = min(x2); x2M = max(x2);
% dx2 = 0.10*(abs(x2M-x2m));
% x1ma = x1m-dx1; x1Ma = x1M+dx1;
% x2ma = x2m-dx2; x2Ma = x2M+dx2;

% for plotting options
ms = 7; % markersize
xl = 'Exam 1'; %xlabel
yl = 'Exam 2'; %ylabel
ftitle = 'Exam Scores';
l1 = 'passed'; %class label
l2 = 'not passed'; %ditto

figure(6);
plot(x1(pos),x2(pos),'m^','MarkerSize',ms,'MarkerFaceColor','m'); hold on;
plot(x1(neg),x2(neg),'co','MarkerSize',ms,'MarkerFaceColor','c'); hold off;
xlabel(xl,'FontWeight','bold'); 
ylabel(yl,'FontWeight','bold');
title(ftitle2); axis([x1ma x1Ma x2ma x2Ma]); % axis([xmin xmax ymin ymax])
legend(l1, l2);
pause;


figure(7);
plot(x1(pos),x2(pos),'m^','MarkerSize',ms,'MarkerFaceColor','m'); hold on;
plot(x1(neg),x2(neg),'co','MarkerSize',ms,'MarkerFaceColor','c'); hold on;
plot(xrange_db, db(xrange_db), 'g-', 'LineWidth',2);
xlabel(xl,'FontWeight','bold'); 
ylabel(yl,'FontWeight','bold');
title(ftitle2); axis([x1ma x1Ma x2ma x2Ma]); % axis([xmin xmax ymin ymax])
hold off; 
hold off;
legend(l1, l2);
pause;

p = zeros(M,1);

g = @(z) 1./(1 + exp(-z));

for i = 1:M
    p(i) = g(theta'*X(:,i)) > 1/2; % calculate predictions based on decision boundary
end


% Compute accuracy on our testing set
fprintf('----------------------------------------------------------------\n');
fprintf('Testing Accuracy: %.2f %%\n', (sum(p == y')/M)*100 );
fprintf('----------------------------------------------------------------\n');

figure(8);
plot(x1(pos),x2(pos),'m^','MarkerSize',ms,'MarkerFaceColor','m'); hold on;
plot(x1(neg),x2(neg),'co','MarkerSize',ms,'MarkerFaceColor','c'); hold on;
plot(x1t(post),x2t(post),'r^','MarkerSize',ms,'MarkerFaceColor','r'); hold on;
plot(x1t(negt),x2t(negt),'bo','MarkerSize',ms,'MarkerFaceColor','b'); hold on;
plot(xrange_db, db(xrange_db), 'g-', 'LineWidth',2);
xlabel(xl,'FontWeight','bold'); 
ylabel(yl,'FontWeight','bold');
title(ftitle3); axis([x1ma x1Ma x2ma x2Ma]); % axis([xmin xmax ymin ymax])
hold off; hold off; hold off; hold off;
legend(l1, l2, 'passed (training)', 'not passed (training)');





    
