function sinc_test
% SINC_TEST: simple example program for doing a polynomial regression on 1D
% data generated by a noisy sinc function.
%
%
% (C) Copyright 2005, Matthias Franz (2005-03-01)


% constants
max_order = 8;		% maximum polynomial order 	
ptype = 'ihp';		% kernel type ('ihp' or 'ap')
method = 'gpp';		% model selection method ('llh', 'gpp', 'loo')
n_iter = 20;		% number of iterations
n_data = 90;		% number of data points
ntr = 10;			% number of training data
sigma_0 = 0.05;		% noise variance

% generate data from sinc function
x = 2.0*rand(n_data, 1) - 1.0;
y = zeros(n_data, 1);
for i=1:n_data                                         
    if x(i) ~= 0
        y(i) = sin(8*x(i))/x(i);
    else
        y(i) = 1;
    end
end
y = y + sqrt(sigma_0)*randn(n_data, 1);  % add noise

% divide into training set ...
x0 = x(1:ntr,:);
y0 = y(1:ntr);

% ... and test set
x1 = x(ntr+1:end,:);
y1 = y(ntr+1:end);

% true function (for plotting purposes)
xt = (1:101)';
xt = (xt/101 - 0.5)*2;
yt = zeros(101, 1);
for i=1:101                                         
    if xt(i) ~= 0
        yt(i) = sin(8*xt(i))/xt(i);
    else
        yt(i) = 1;
    end
end

% regression
hp0 = [log(0.6); log(sqrt(0.001))]; % initial guess for hyperparameters
erg = preg(n_iter, hp0, x0, y0, x1, y1, ptype, method, 1:max_order);
[mu, s2] = gpP_pred(erg, xt); % predict on interval [-1,1]
disp('best hyperparameters:')
disp(exp(erg.hp)')

% plot prediction
plot_predict(xt, mu, s2, x0, y0, exp(2*erg.hp(2)))
hold on 
plot(xt, yt, 'y')
hold off