function sheet04

% get the data
[X, Y] = twomoon(1000);

% get a subset of the data
figure(1)
NS = 10;
[XS, YS] = subset(NS, X, Y);
plot2ddata(X, Y);
hold on
plot(XS(:, 1), XS(:, 2), 'kx', 'MarkerSize', 10, 'LineWidth', 3);
hold off
title('data and example points')

% train KRR
figure(2);
K = rbfkern(1, XS, XS);
alpha = inv(K + 1e-6*eye(NS))*YS;

YH = rbfkern(1, X, XS) * alpha;

plot2ddata(X, YH);
hold on;
plot(XS(:,1), XS(:, 2), 'kx', 'MarkerSize', 10, 'LineWidth', 3);
hold off

title('learned only using sample points')

% train with KRR + Graph Laplacian regularization
figure(3)
alpha = trainGLKRR(1, 0.001, 0.1, 1, XS, YS, X);
YH = rbfkern(1, X, X) * alpha;
plot2ddata(X, YH);
hold on;
plot(XS(:,1), XS(:, 2), 'kx', 'MarkerSize', 10, 'LineWidth', 3);
hold off

title('learned with graph Laplacian regularizer')

function K = rbfkern(w, X, Y)
N = size(X, 1);
M = size(Y, 1);
XX = sum(X.*X, 2);
YY = sum(Y.*Y, 2);
D = repmat(XX, 1, M) + repmat(YY', N, 1) - 2 * X * Y';
K = exp(-D/(2*w));

function plot2ddata(X, Y)
P = (sign(Y) == 1);
N = (sign(Y) == -1);
plot(X(P, 1), X(P, 2), 'r+', X(N, 1), X(N, 2), 'bo');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% your solution below
%

% 1. Generate a two-moon data set with N points and some noise. Make sure
% that classes are not linearly seprable.
function [X, Y] = twomoon(N)
% ...

% 2. Choose random subset of size N from a data set X, Y.
function [X, Y] = subset(N, X, Y)
% ...

% 3. Compute the graph-Laplacian for Gaussian weights with width w
function L = graphLaplacian(w, X)
% ...

% 4. Train KRR with Graph-Laplacian regularization on the data
%
% Parameters:
%    w - width for the rbf kernel
%    g - width for the graph-Laplacian
%    lambda - regularization constant for general smoothness
%    tau - regularization constant for GL-regularizer
%    XS, YS - data set with labels
%    X - unlabeled data
function alpha = trainGLKRR(w, g, lambda, tau, XS, YS, X)
% ...
