function sheet10

% generate some data
sheet10_data

% plot data
plot(X(Y == 1, 1), X(Y == 1, 2), 'r+', ...
X(Y == -1, 1), X(Y == -1, 2), 'bo');

% learn an SVM
width = 0.01;
C = 1e5;

K = rbfkern(width, X);
svm = trainSVM(K, Y, C);
Yh = predictSVM(K, svm);

% predict the solution
N = 50;
[MX, MY] = meshgrid(linspace(0, 1, N), linspace(0, 1, N));
XP = [reshape(MX, N*N, 1), reshape(MY, N*N, 1)];
KP = rbfkern(width, XP, X);
YP = predictSVM(KP, svm);
F = reshape(YP, N, N);
hold on
contour(MX, MY, F, [-1, 0, 1]);
% comment out next line for octave
surf(MX, MY, F, 'FaceAlpha', 0.2); shading interp; caxis([-2 2])
hold off
grid
colormap([linspace(1, 0, 100)', linspace(1, 0, 100)', ones(100, 1), ;
0 0 0;
ones(100, 1), linspace(0, 1, 100)', linspace(0, 1, 100)'])

title(sprintf('Error rate: %.2f%%', mean(sign(Yh) ~= Y) * 100));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Your solution below

% 3a. train a support vector machine using the built-in generic quadratic
% optimizer (quadprog for matlab, qp for octave).
% Input:
% K - kernel matrix (n x n) of training data
% Y - labels (1 x n)
% C - regularization constant
% Output:
% svm.alpha - learned alphas
% svm.b - learned b
% svm.y - training Ys
function svm = trainSVM(K, Y, C)
% ...

% 3b. Predict the labels given the kernel matrix built from the
% test/training data points, and the svm structure returned by trainSVM.
% Input:
% K: - kernel matrix (m x n) of test/training data
% svm: - svm structure
% Output:
% Yk - predicted labels (1 x m)
function Yh = predictSVM(K, svm)
% ...

% 3c. Compute the rbf kernel. If Y is missing, assume X = Y. Do not use
% for loops to compute the pairwise distances!
% Input:
% w: - kernel width
% X: - Input vectors (d x n)
% Y: - Input vectors (d x m)
% Output:
% K: - Kernel matrix (n x m)
function K = rbfkern(w, X, Y)
% ...