

% define required variables first %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
kValue = 16; % your neighborhood value, i.e. the k value

% kernelType = ['uniform']; % kernel type
kernelType = ['triangular']; % kernel type
% kernelType = ['epanechnikov']; % kernel type
% kernelType = ['quartic']; % kernel type
% kernelType = ['triweight']; % kernel type
% kernelType = ['gaussian']; % kernel type
% kernelType = ['cosine']; % kernel type

tempDataset = dataset;

% choose a random test instance
myRowIndex = randi([1 size(tempDataset, 1)], 1, 1);
testInstance = tempDataset(myRowIndex, :);
testEffort = testInstance(:,size(testInstance, 2));

% delete the test instance
tempDataset(myRowIndex, :) = [];

% get the prediction and closest neighbors' effort values in ascending order of their
% distance to our test instance
[prediction, mre, sortedEffortValues] = nnkForKernel(testInstance,tempDataset,kValue);

% calculate the badwidth
% bandwidth used here is acc. to Scott's rule
h = sqrt(5)*std(tempDataset(:,size(tempDataset,2)))*(size(tempDataset,1)^(-0.2));

% now calculate and store the probability value for each instance in the
% neighborhood of our test instance
probValues = -1 .* ones(size(sortedEffortValues,1),1);
% sortedEffortValues = (sortedEffortValues - min(sortedEffortValues)) / (max(sortedEffortValues) -min(sortedEffortValues))

for i = 1: size(sortedEffortValues,1)
    effToEstimate = sortedEffortValues(i);
    probValues(i) = myKernelEstimator(kernelType, effToEstimate, testEffort, tempDataset(:,size(tempDataset,2)), h);
end

plot(probValues);

% now convert prob values into weights
weights = (probValues - min(probValues)) ./ (max(probValues) - min(probValues));











% figure;
% hold on;
% % plot(myRow(:,17),'bo');
% plot(1:kValue,sort(sortedEffortValues),'r.');
% plot(ceil(kValue/2),myRow(:,size(myRow,2)),'b*');
% hold off;

% below is the code if you want to use matlab's kernel estimate, but it
% limits you A LOT!!
% 
% L = min(sortedEffortValues) -1;
% U = max(sortedEffortValues) + 1;
% 
% figure;
% ksdensity(sortedEffortValues,'kernel','epanechnikov', 'width', sqrt(5)*std(sortedEffortValues)*(size(sortedEffortValues,1)^(-0.2)), 'function', 'pdf');
% ksdensity(sortedEffortValues,'kernel','gaussian', 'width', sqrt(5)*std(sortedEffortValues)*(size(sortedEffortValues,1)^(-0.2)), 'function', 'pdf');
