% Machine Learning: Perceptron 

clc %clear console and return cursor to home
format long

% dancing with the stars data (Catherine M. Math 354)
x1 = [8 7 5 4 4 3 1 2]'; % scores from 1-10 for dance 1
x2 = [10 6 5 7 9 6 3 10]'; % scores from 1-10 for dance 2
y = [1 1 0 0 1 0 0 1]'; % 1 = went on to next round, 0 = did not

x = [x1'; x2'];

c0 = find(y==0); c1 = find(y==1); 
plot(x1(c0),x2(c0),'bx','MarkerSize',10,'LineWidth', 2); hold on;
plot(x1(c1),x2(c1),'ro','MarkerSize',10,'LineWidth', 2); hold off;
axis([0 10 0 10])

g = @(z) 1./(1+exp(-z));
H = @(z,yi) (g(z) - yi)*(1-g(z))*g(z); 

delta = 0.1;
theta1 = -0.5; theta2 = 1; b = 0;
theta = [theta1; theta2];
xp = linspace(0,10,10);

figure(2)
f = @(x,theta1,theta2,b) (-1/theta2)*(b + theta1*x);
plot(x1(c0),x2(c0),'bx','MarkerSize',10,'LineWidth', 3); hold on;
plot(x1(c1),x2(c1),'ro','MarkerSize',10,'LineWidth', 3); hold on;
plot(xp,f(xp,theta1,theta2,b),'g-','LineWidth',2); hold off; hold off;
axis([0 10 0 10])
sprintf('press any key to start the process')
pause
outer_max = 6500;

for j =1:outer_max 
    r = randperm(length(x1));
    for i = 1:length(x1)
        theta1_old = theta1;
        theta2_old = theta2;
        b_old = b;
        
        % stoch grad descent
        theta1 = theta1 - delta*( H(theta'*x(:,r(i)) + b, y(r(i)))*x1(r(i)));
        theta2 = theta2 - delta*( H(theta'*x(:,r(i)) + b, y(r(i)))*x2(r(i)));
        b = b - delta*( H(theta'*x(:,r(i)) + b, y(r(i))));
        
    end
    theta = [theta1; theta2];
    %plot curve every 10 outer iterations
    if rem(j,50) == 0
        figure(2)
        f = @(x,theta1,theta2,b) (-1/theta2)*(b + theta1*x);
        plot(x1(c0),x2(c0),'bx','MarkerSize',10,'LineWidth', 3); hold on;
        plot(x1(c1),x2(c1),'ro','MarkerSize',10,'LineWidth', 3); hold on;
        plot(xp,f(xp,theta1,theta2,b),'g-','LineWidth',2); hold off; hold off;
        axis([0 10 0 10])
        pause(0.1)
        j
    end
end

figure(3)
f = @(x,theta1,theta2,b) (-1/theta2)*(b + theta1*x);
plot(x1(c0),x2(c0),'bx','MarkerSize',10,'LineWidth', 3); hold on;
plot(x1(c1),x2(c1),'ro','MarkerSize',10,'LineWidth', 3); hold on;
plot(3,3,'md','MarkerSize',10,'LineWidth', 3); hold on;
plot(xp,f(xp,theta1,theta2,b),'g-','LineWidth',2); hold off; hold off; hold off;
axis([0 10 0 10])

    
sprintf('theta1 = %f', theta1)
sprintf('theta2 = %f', theta2)
sprintf('b = %f', b)





