function U04_test_CV_krr

% Generate data.
n = 20;
x = linspace(-1, 1, n);
y = [ 1.153741e+00, 8.145612e-01, 4.369920e-01, 4.227247e-01, 2.699726e-01, 2.347085e-01, 1.136709e-01, 4.134774e-02, -4.843545e-02, -3.683275e-03, -1.416303e-01, 8.616478e-02, -6.309822e-02, 6.957640e-02, 2.097653e-01, 3.599886e-01, 4.604808e-01, 7.970857e-01, 9.627512e-01, 1.062644e+00 ];

% Plot data. 
clf; hold on;
plot(x,y,'bo');

% Select kernel width sigma using 10x10-CV using squared loss. 
[M,opt_param] = cv(x,y,@(tr_x,tr_y,sigma) krr(tr_x,tr_y,'gaussian',sigma), ...
        { 'sigma', logspace(0,1,10) }, [], [], @loss_squared);

% Compute and plot predictions.
xx = linspace(-1,1,200);
yy = apply(M,xx);
plot(xx,yy,'r--');

% Plot truth. 
plot(xx,f(xx),'b--');

legend('Data', 'Predictions', 'Truth');
title(sprintf('Kernel width selected by CV: %f', opt_param.sigma));

% Squared loss.
function sqloss = loss_squared(X,y_true,y_pred)
sqloss = y_true-y_pred;
sqloss = sum(sqloss(:).^2);

% True regression function.
function y = f(x) 
y = x.^2;

% Apply function.
function y = apply(M,X)
y = feval(M.applyfunc, M, X);

