% SAR - LEZ SVM
% lezioni prese da http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex7/ex7.html

clear all;
clc;
close all

dire = 'C:\Marco\UNIVR\Didattica\Corsi\Corsi Informatica\Sistemi avanzati per il riconoscimento\Lezioni 2013-14\Lab\Lez2 - Classificatori discriminativi\SVM'
% addpath to the libsvm toolbox
addpath(genpath(dire));

%% ES1
[trainlabels, trainfeatures]    =   libsvmread('.\Dataset\twofeature.txt');
trainfeatures                   =   full(trainfeatures);

% positive = [[1:50]',[ones(50,1)]];
% negative = [[1:50]',[ones(50,1)].*3];
% 
% trainfeatures= [positive;negative];
% trainlabels=[ones(50,1);ones(50,1).*-1];

figure; set(gcf,'name','Scatter dei punti di training');
scatter(trainfeatures(:,1),trainfeatures(:,2),60,trainlabels,'filled'); hold on;

% Training
model = svmtrain(trainlabels, trainfeatures, '-s 0 -t 0 -c 100');

%  The last string argument tells LIBSVM to train using the options
% a. -s 0, SVM classification
% b. -t 0, a linear kernel, because we want a linear decision boundary
% c. -c 1, a cost factor of 1
% You can see all available options by typing "svmtrain" at the Matlab/Octave console. 

supvec = full(model.SVs);
scatter(supvec(:,1),supvec(:,2),200,'Marker','x','MarkerEdgeColor',[0,1,0],'LineWidth',2); 

w = model.SVs' * model.sv_coef;
% model.sv_coef rappresenta la moltiplicazione tra alfa e y
b = model.rho;
if (model.Label(1) == -1)
    w = -w; b = -b;
end
margine = 2/norm(w)
distanza = b/norm(w);
angolo = atan(w(2)/w(1));%in radianti
[px(1),py(1)] = pol2cart(angolo,distanza);
orto = angolo+pi/2;
versore = w./norm(w);
quiver(px(1),py(1),versore(1),versore(2));   
lineLength = 5;
px(2) = px(1) + lineLength * cos(orto);
py(2) = py(1) + lineLength * sin(orto);
px(3) = px(1) - lineLength * cos(orto);
py(3) = py(1) - lineLength * sin(orto);
plot(px,py); axis equal

%% ES2
clear all;
clc;
close all;
[trainlabels, trainfeatures]    =   libsvmread('.\Dataset\email_train-50.txt');
trainfeatures                   =   full(trainfeatures);

% Training
model = svmtrain(trainlabels, trainfeatures, '-s 0 -t 0 -c 1');

[testlabels, testfeatures]    =   libsvmread('.\Dataset\email_test.txt');
testfeatures                   =   full(testfeatures);

[predicted_label, accuracy, dv_pe] = svmpredict(testlabels, testfeatures, model,...
    'libsvm_options');
 
%ROC 
[X,Y,T,AUC] = perfcurve(testlabels,[dv_pe],1);
figure; plot(X,Y) 
 

