function [y_predict] = smoothingParameter(x_train,y_train, x_predict)
% for choosing the smoothing parameter datedependent
    %we split the sample D_n into a learning and a testing sample. We use
    %the learning sample to compute the regression estimate and the testing
    %sample for choosing the best bandwidth h, which minimizes the
    %empirical L2 risk
    
n=size(x_train,1);
nl= ceil(n/2);

[x_learning] = x_train(1:nl,:);
[y_learning] = y_train(1:nl,:);
[x_testing] = x_train((nl+1):n,:);
[y_testing] = y_train((nl+1):n,:);

nt=size(x_testing,1);


[y_predict_t] =zeros(nt,1);
[y_h]= zeros(21,2);
s=1;
%eigentlich müsste k= -size(x_train,1) : size(x_train,1), aber alles
%außerhalb des Intervalls bringt keine wesentlichen Veränderungen mit sich
for k=(-10):10
    %set bandwidth
    hi=2^k;
    
    %compute regression estimate with learning sample, and use X-values
    %of testing sample for the empirical error
    y_predict_t(:,:)= kr(x_learning,y_learning,x_testing,hi);
    
    %compute the empirical error mit Y-values of the testing sample, save
    %the result and the corresponding bandwidth in a 2-dim. matrice
    y_h(s,1)= (1/(nt))* sum((y_predict_t - y_testing).^2); 
    y_h(s,2)=hi;
    
    %raise the counter +1
    s=s+1;
end

%determine the minimal empirical L2 risk and remember the position
[c,i]=min(y_h(:,1));
%bandwidth is the value on the position i in the second column
h= y_h(i,2);
%now do regression estimate again with the predicted x-values
y_predict=kr(x_learning,y_learning,x_predict,h);  




end
