% A short demonstration of SVM on toy examples
% 
% Supporting material for the Master course "Kernel Methods in Computational Biology", Spring 2005
% Jean-Philippe.Vert@mines.org
%
% This script requires the Spider package, freely available at:
% http://www.kyb.tuebingen.mpg.de/bs/people/spider/
%
% It borrows examples from the recommended "SVM practical session" performed at the
% 2002 Machine learning summer school in Tuebigen by Jason Weston, Arthur Gretton
% and Andre Elisseeff, available at:
% http://www.kyb.tuebingen.mpg.de/bs/people/weston/svmpractical/



% Initialization
cd spider/   % Go to the spider directory that you should have downloaded and untarred
use_spider   % Initialize spider



% Make two overlapping Gaussian classes
randn('seed',1)
     m=20; c=1; s=1; % 20 points in each class, generated by Gaussian centered in (-d,-d) and (d,d) with width s. 

% Generate the points
x1=[randn(m,1)*s-c randn(m,1)*s-c]
x2=[randn(m,1)*s+c randn(m,1)*s+c]
plot(x1(:,1),x1(:,2),'+');hold on;plot(x2(:,1),x2(:,2),'o');hold off;
d=data([x1;x2],[ones(m,1) ; -ones(m,1)]);

%train a SVM with varying C
a=svm;
a.child = kernel('linear');
for C=10.^[-3:1:5]
    a.C = C;
    [r,a]=train(a,d);
    clf; %clear figure
    plot(a);
    input('next?')
end

% Use RBF kernel with varying sigma
a.C=1;
for sigma =10.^(.2*[-5:5])
    a.child = kernel('rbf',sigma);
    [r,a]=train(a,d);
    clf; %clear figure
    plot(a);
    input('next?')
end

% Use RBF with varying C
sigma=1;
a.child = kernel('rbf',sigma);
for C=10.^[-2:1:5]
    a.C = C;
    [r,a]=train(a,d);
    clf; %clear figure
    plot(a);
    input('next?')
end

% Same with more overlapping datasets
s=2;
x1=[randn(m,1)*s-c randn(m,1)*s-c]
x2=[randn(m,1)*s+c randn(m,1)*s+c]
clf;
plot(x1(:,1),x1(:,2),'+');hold on;plot(x2(:,1),x2(:,2),'o')
d=data([x1;x2],[ones(m,1) ; -ones(m,1)]);
input('next')
cvres=[];
for C=10.^(0.5*[-4:10])
    a.C = C;
    [r,a]=train(a,d);
    clf; %clear figure
    plot(a);
    
    s=cv(a);
    [r,s] = train(s,d);
    tmp = get_mean(r);
    tmp=tmp.Y;
    res=[res; C , tmp(1)];
    input('next?')
end

