
clear all; pack;

optimize = .1;

% generate sample data
a=1;
b=.1;
n=0;
step = 0;

for t=-4*pi:0.1:4*pi

     step = step + 1;
    
     % positive class
     x=a * cos(t) * exp(b*t) + randn*n;
     y=a * sin(t) * exp(b*t) + randn*n;
 
     z(step,1) = x;
     z(step,2) = y;
     c(step,1) = 1;
     
     step = step + 1;

     %negative class
     x=a * cos(t+pi) * exp(b*t) + randn*n;
     y=a * sin(t+pi) * exp(b*t) + randn*n;
  
     z(step,1) = x;
     z(step,2) = y;
     c(step,1) = -1;

end;

% ensure range is [-1,+1]
z=rescale(z);

% randomly sort
w = cat(2,rand(size(c,1),1),c,z);
w = sortrows(w,1);
c = w(:,2);
z = w(:,3:size(w,2));

% optimize parameters
if optimize > 0
    [ suggest best_acc best_sv ] = sa_svc ( 1, z, c, [-3:8], [-3:8], 0.001, 10, 100, 1, optimize, 1, .5, 1 );
else
    suggest = [ 3.7082 4.5447];     % 99.01% 10-fold CV acc, 424/504 sv
    % estimate accuracy
    params = sprintf ( '-s 0 -t 2 -g %.8f -c %.8f -e %.8f -v 10', exp(suggest(1)), exp(suggest(2)), 0.001 );
    best_acc = svmtrain ( c, z, params );
    params = sprintf ( '-s 0 -t 2 -g %.8f -c %.8f -e %.8f', exp(suggest(1)), exp(suggest(2)), 0.001 );
    model = svmtrain ( c, z, params );
    best_sv = sum(model.nSV);
end;

% train a new model at these params with all data points
params = sprintf ( '-s 0 -t 2 -g %.8f -c %.8f -e %.8f -b 1', exp(suggest(1)), exp(suggest(2)), 0.001 );
model = svmtrain ( c, z, params );

i=0;
xr=[ min(z(:,1)):0.01:max(z(:,1)) ]*1.05;
yr=[ min(z(:,2)):0.01:max(z(:,2)) ]*1.05;
acc = zeros(size(xr,2),size(yr,2));   

for x=xr
    i=i+1;
    test = repmat(yr',1,2);
    test(:,1)=x;
    [label ac prob] = svmpredict( 0.*test(:,1), test, model, '-b 1');
    acc(i,:) = prob(:,1)*2-1;
end;

figure;
surf(xr,yr,acc','Linestyle','none'); view(2); axis tight; grid off; colormap hot; hold all; 

for i=1:size(z,1)
    if c(i) > 0
        plot3(z(i,1),z(i,2),2,'o','MarkerFaceColor','w','MarkerSize',5,'MarkerEdgeColor','k'); hold all;
    else
        plot3(z(i,1),z(i,2),2,'o','MarkerFaceColor','k','MarkerSize',5,'MarkerEdgeColor','w'); hold all;
    end;
end;


fprintf('\n\nModel achieves %.2f%% accuracy with %d support vectors from %d data points (%.2f)\n', ...
    best_acc, best_sv, size(z,1), best_sv / size(z,1) );
fprintf('  Cost  = exp(%.2f) = %f\n', suggest(2), exp(suggest(2)));
fprintf('  Gamma = exp(%.2f) = %f\n\n', suggest(1), exp(suggest(1)));
