function [y_predict] = kregression(x_train,y_train, x_predict, h)
%REGRESSION construct regression estimate by using the Nadaraya Watson
%kernel estimate
%   We are using as a kernel function the Gaussian kernel with bandwidth h,
%   which is choosen data dependent by "Splitting the Sample"

SizeOfX = size(x_train,1);
n=size(x_predict,1);

y_predict=zeros(n,1);
W=zeros(SizeOfX,1);
K=0;

%Variante ohne Vektorisieren des Ganzen
for i=1:n
    
    %compute the weights of y_i with the Gaussian Kernel Function:
    
    for j=1:SizeOfX
        K=K+ exp(-sum((x_predict(i,:)-x_train(j,:)).^2)/(2*h^2));
    end

    if (K==0)
        y_predict(i)=0;
        continue
    else
        for j=1:SizeOfX
            W(j)=exp(-sum((x_predict(i,:)-x_train(j,:)).^2)/(2*h^2))/K;
        end
    end
    y_predict(i)=sum(W.*y_train);
end

end


