%
% Homepage : http://www.bioinfo.ensmp.fr/~vert/teaching/2008master
%
% Copyright 2008 Jean-Philippe Vert
%
%
%
% Homework 4
%
% SVM
%
% Files required:


% Path to access the functions and the data
addpath ../../code
addpath ../../data/toyclassification/
addpath ../../code/libsvm-mat-2.85-1

% We also need libsvm on the path

% Load the data
load('data.txt')
load('labels.txt')

% Data preparation

% Warning: Labels should be -1/+1 instead of 0/1
y=labels*2-1;

% Warning: data are badly scaled (try plot(std(data)) ). Let's scale them
% to unit standard deviation
[n,p]=size(data);
x = (data - ones(n,1)*mean(data)) ./ (ones(n,1)*std(data));

% Split randomly into a training and test set of equal size
ntrain = 100;
r = randperm(n);
xtrain = x(r(1:ntrain),:);
ytrain = y(r(1:ntrain));
xtest = x(r((ntrain+1):n),:);
ytest = y(r((ntrain+1):n));

kname = {'Linear','Gaussian (gamma = 0.001)','Gaussian (gamma = 0.01)','Gaussian (gamma = 0.1)','Gaussian (gamma = 1)','Gaussian (gamma = 10)'};
libsvmoption = {'-t 0' , '-g 0.001', '-g 0.01',  '-g 0.1' , '-g 1' , '-g 10'};
nk = length(kname); % number of kernels

% Train a SVM
Clist = power(1.5,[-15:25]);
nC = size(Clist,2);
for ik = 1:nk
    accuracy_train = zeros(nC,1);
    accuracy_test = zeros(nC,1);
    for i=1:nC
        opt = [libsvmoption{ik},' -c ',num2str(Clist(i))];
        model = svmtrain(ytrain, xtrain, opt);
        [predict_label, accuracy, dec_values] = svmpredict(ytrain, xtrain, model);
        accuracy_train(i) = accuracy(1);
        [predict_label, accuracy, dec_values] = svmpredict(ytest, xtest, model);
        accuracy_test(i) = accuracy(1);
    end
    % Plot picture
    semilogx(Clist,accuracy_train,'b',Clist,accuracy_test,'r')
    legend('Training accuracy','Test accuracy')
    title(kname{ik})
    xlabel('C')
    ylabel('Accuracy (%)')
    input('Next picture?')
end
