%loads y_time and y_samples and path structure and computes GP

function [gps]=gp_test(x,y,group)

%close all; %clear all;

x = x';
y = y';
n = length(x);
z = linspace(0, 10, 50)';

% --------------------------------------------------
% covfunc = @covSEiso; 
% covfunc = @covNNone;
% covfunc = @covPeriodic;
%covfunc = @covRQard;
%covfunc = {@covMaterniso, 1}; 
%inffunc = @infExact;

gps.meanfunc = {@meanLinear};
%gps.covfunc = @covNNone;
gps.covfunc = {@covMaterniso, 3};
gps.likfunc = @likGauss; 
gps.inffunc = @infExact;
gps.hyp.cov = [0;0]; 
gps.hyp.lik = log(0.1);
gps.hyp.mean = 0;




%find best hyperparameters values (training or fitting)
gps.hyp = minimize(gps.hyp, @gp, -10, ...
    gps.inffunc, gps.meanfunc, gps.covfunc, gps.likfunc, x, y);
% for i = 1:n
%     nlml = gp(hyp, inffunc, meanfunc, covfunc, likfunc, group(i).x, group(i).y)
% end

nlml = gp(gps.hyp, gps.inffunc, gps.meanfunc, gps.covfunc, gps.likfunc, x, y)

%prediction
% [m s2 fm fs2] = gp(hyp, inffunc, meanfunc, covfunc, likfunc, x, y, z);
[m s2] = gp(gps.hyp, gps.inffunc, gps.meanfunc, gps.covfunc, gps.likfunc, x, y, z);


draw(m,s2,z,x,y,group);

% --------------------------------------------------
% --------------------------------------------------

function draw(m,s2,z,x,y,group)

figure;
set(gca, 'FontSize', 12), grid on, hold all;
xlabel('input, x'), ylabel('output, y');

f = [m+2*sqrt(s2); flipdim(m-2*sqrt(s2),1)];
fill([z; flipdim(z,1)], f, [7 7 7]/8)
plot(z, m, 'LineWidth', 2); 
plot(x, y, '+', 'MarkerSize', 12);

for i=1:length(group)
    plot(group(i).time_normalized,group(i).y);
end
