clear all
close all
clc
addpath(genpath('cvx/'))

%% gen linearly separable data
N=10;
x=(rand(2,N)-0.5)*2;
phi=rand*2*pi;
y=sign([cos(phi) sin(phi)]*x);
%y=2*(round(rand(1,N))-0.5);

figure(1)
plot(x(1,y==1),x(2,y==1),'o','MarkerEdgeColor','r','MarkerFaceColor','w','Markersize',18,'LineWidth',2)
hold on
plot(x(1,y==-1),x(2,y==-1),'x','MarkerEdgeColor','k','MarkerFaceColor','k','Markersize',18,'LineWidth',2)


cvx_begin
variables w(2,1) b(1,1)
minimize (0.5*w'*w)
  subject to
   y.*(w'*x+b*ones(1,N))-ones(1,N)>=0
cvx_end
 

yi = interp1([0 -b/w(1)],[-b/w(2) 0],[-1 1],'linear','extrap')
plot([-1 1],yi,'--k','linewidth',3)
axis([-1 1 -1 1])



%% gen data
N=20;
close
%x=(rand(2,N)-0.5)*2;
%phi=rand*2*pi;
%y=sign([cos(phi) sin(phi)]*x);
%y=2*(round(rand(1,N))-0.5);
sig=0.5;
x=[sig*randn(2,N/2)-0.5*ones(2,N/2) sig*randn(2,N/2)+0.5*ones(2,N/2)];
y=[ones(1,N/2) -ones(1,N/2)];

figure(1)
plot(x(1,y==1),x(2,y==1),'o','MarkerEdgeColor','r','MarkerFaceColor','w','Markersize',18,'LineWidth',2)
hold on
plot(x(1,y==-1),x(2,y==-1),'x','MarkerEdgeColor','k','MarkerFaceColor','k','Markersize',18,'LineWidth',2)

C=10000;
cvx_begin
variables w(2,1) b(1,1) zeta(1,N)
minimize (0.5*w'*w+C*ones(1,N)*zeta')
  subject to
   y.*(w'*x+b*ones(1,N))-ones(1,N)+zeta >= 0
   zeta >= 0
cvx_end
 

yi = interp1([0 -b/w(1)],[-b/w(2) 0],[-1 1],'linear','extrap')
plot([-1 1],yi,'--k','linewidth',3)
axis([-1 1 -1 1])
zeta

%% gen data
N=10;
x=(rand(2,N)-0.5)*2;
phi=rand*2*pi;
%y=sign([cos(phi) sin(phi)]*x);
y=2*(round(rand(1,N))-0.5);
l=0.5;
k=@(x1,x2) exp(-sum((x1*ones(1,size(x2,2))-x2).^2)/l)'

figure(1)
plot(x(1,y==1),x(2,y==1),'o','MarkerEdgeColor','r','MarkerFaceColor','w','Markersize',18,'LineWidth',2)
hold on
plot(x(1,y==-1),x(2,y==-1),'x','MarkerEdgeColor','k','MarkerFaceColor','k','Markersize',18,'LineWidth',2)

K=zeros(N,N);
for t=1:N
    for s=t:N
  K(t,s)=k(x(:,t),x(:,s));
K(s,t)=K(t,s);
    end
end

cvx_begin
variables a(N,1)
minimize ( 1/2*(a.*y')'*K*(a.*y') - ones(1,N)*a)
  subject to
   ones(1,N)*(a.*y') == 0
   a >= 0
cvx_end

ind=find(a>0.01);
ind

 

wphi = @(xstar) ones(1,N)*(a.*y'.*k(xstar,x)) 
b=0;
for i=1:length(ind)
b=b+1/y(ind(i))-wphi(x(:,ind(i)));
end
b=b/length(ind);

ystar = @(xstar) wphi(xstar)+b 
[X1,X2] = meshgrid(-1:.1:1, -1:.1:1);
for s=1:length(X1)
for t=1:length(X2)
Z(s,t)=ystar([X1(s,t) ;X2(s,t)]);
end
end
contour(X1,X2,Z,[0 0],'k','linewidth',2)
 

