% define datasets of experiment
myDatasets = {
    'cocomo81o','cocomo81s','cocomo81','cocomo81e',...
    'desharnaisL1',...
    'desharnais','desharnaisL2',...
    'desharnaisL3',...
    'nasa93','nasa93_center_1','nasa93_center_2','nasa93_center_5',...
    'cocomo81','nasa93','desharnais',...
    'sdr','albrecht',...
    'finnish','kemerer','maxwell','miyazaki94','telecom1',...
%     'china'...
    };
% define kernel types of the experiment
% myKernels = {'uniform','triangular','epanechnikov','gaussian'};
myKernels = {'mendes'};

datasetSize = size(myDatasets,2);
for datasetCounter = 1:datasetSize
    
    eval(['load ',char(myDatasets(datasetCounter)),'.csv;']);
    eval(['dataset = ',char(myDatasets(datasetCounter)),';']);
    eval(['clear ',char(myDatasets(datasetCounter)),';']);
    
    
    actuals = -1 * ones(20,size(dataset,1));
    
    pred3 = -1 * ones(20,size(dataset,1));
    mre3 = -1 * ones(20,size(dataset,1));
    ar3 = -1 * ones(20,size(dataset,1));
    pred3Kernel = -1 * ones(20,size(dataset,1));
    mre3Kernel = -1 * ones(20,size(dataset,1));
    ar3Kernel = -1 * ones(20,size(dataset,1));
    
    pred5 = -1 * ones(20,size(dataset,1));
    mre5 = -1 * ones(20,size(dataset,1));
    ar5 = -1 * ones(20,size(dataset,1));
    pred5Kernel = -1 * ones(20,size(dataset,1));
    mre5Kernel = -1 * ones(20,size(dataset,1));
    ar5Kernel = -1 * ones(20,size(dataset,1));
    
    pred7 = -1 * ones(20,size(dataset,1));
    mre7 = -1 * ones(20,size(dataset,1));
    ar7 = -1 * ones(20,size(dataset,1));
    pred7Kernel = -1 * ones(20,size(dataset,1));
    mre7Kernel = -1 * ones(20,size(dataset,1));
    ar7Kernel = -1 * ones(20,size(dataset,1));
    
    pred9 = -1 * ones(20,size(dataset,1));
    mre9 = -1 * ones(20,size(dataset,1));
    ar9 = -1 * ones(20,size(dataset,1));
    pred9Kernel = -1 * ones(20,size(dataset,1));
    mre9Kernel = -1 * ones(20,size(dataset,1));
    ar9Kernel = -1 * ones(20,size(dataset,1));
    
    predBestK = -1 * ones(20,size(dataset,1));
    mreBestK = -1 * ones(20,size(dataset,1));
    arBestK = -1 * ones(20,size(dataset,1));
    predBestKKernel = -1 * ones(20,size(dataset,1));
    mreBestKKernel = -1 * ones(20,size(dataset,1));
    arBestKKernel = -1 * ones(20,size(dataset,1));
    
    % a dummy to keep track of whether the best k value was found
    bestKFound = 0;
    % find the best k value for the train data
    if bestKFound == 0
        myBestK = bestk(dataset);
    end
    
    kernelSize = size(myKernels,2);
    for kernelCounter = 1:kernelSize
        kernelType  = char(myKernels(kernelCounter));
        kernelWinTieLossValues = [];
%         for hIndicator = 1:5 % indicates which bandwidth index to use
          for hIndicator = 1:1 % uncomment this line only for irwm/mendes kernel
            % repeat 20 times
            for counter = 1:20
                % randomize dataset
                dataset = randomizeDataset(dataset);
                % for each row in the randomized dataset do the following
                i = 0;
                while i < size(dataset,1)
                    % increment i by 1
                    i = i + 1;
                    
                    % pick up the row
                    myRow = dataset(i,:);
                    
                    % record it into actuals
                    actuals(counter,i) = myRow(1,size(myRow,2));                   
                    
                    % train is the dataset minus the selected row
                    train = dataset;
                    train(i,:) = [];
                    
                    % now start predictions
                    [pred3(counter,i), mre3(counter,i), sortedEffortValues,ar3(counter,i)] = nnkForKernel(myRow,train,3);
                    [pred3Kernel(counter,i), mre3Kernel(counter,i),ar3Kernel(counter,i)] = myKernelEstimator(myRow, train, kernelType, sortedEffortValues, hIndicator);
                    
                    [pred5(counter,i), mre5(counter,i), sortedEffortValues,ar5(counter,i)] = nnkForKernel(myRow,train,5);
                    [pred5Kernel(counter,i), mre5Kernel(counter,i),ar5Kernel(counter,i)] = myKernelEstimator(myRow, train, kernelType, sortedEffortValues, hIndicator);
                    
                    [pred7(counter,i), mre7(counter,i), sortedEffortValues,ar7(counter,i)] = nnkForKernel(myRow,train,7);
                    [pred7Kernel(counter,i), mre7Kernel(counter,i),ar7Kernel(counter,i)] = myKernelEstimator(myRow, train, kernelType, sortedEffortValues, hIndicator);
                    
                    [pred9(counter,i), mre9(counter,i), sortedEffortValues,ar9(counter,i)] = nnkForKernel(myRow,train,9);
                    [pred9Kernel(counter,i), mre9Kernel(counter,i),ar9Kernel(counter,i)] = myKernelEstimator(myRow, train, kernelType, sortedEffortValues, hIndicator);
                    
                    [predBestK(counter,i), mreBestK(counter,i), sortedEffortValues,arBestK(counter,i)] = nnkForKernel(myRow,train,myBestK);
                    [predBestKKernel(counter,i), mreBestKKernel(counter,i),arBestKKernel(counter,i)] = myKernelEstimator(myRow, train, kernelType, sortedEffortValues, hIndicator);
                    
                end
            end
            
            [winMdMRE,tieMdMRE,lossMdMRE] = kernelWinTieLossCalulatorMdMRE(mre3,mre5,mre7,mre9,mreBestK,mre3Kernel,mre5Kernel,mre7Kernel,mre9Kernel,mreBestKKernel);
            [winMAR,tieMAR,lossMAR] = kernelWinTieLossCalulatorMAR(ar3,ar5,ar7,ar9,arBestK,ar3Kernel,ar5Kernel,ar7Kernel,ar9Kernel,arBestKKernel);
            [winPred25,tiePred25,lossPred25] = kernelWinTieLossCalulatorPred25(mre3,mre5,mre7,mre9,mreBestK,mre3Kernel,mre5Kernel,mre7Kernel,mre9Kernel,mreBestKKernel);
            myTemp1 = [winMdMRE,tieMdMRE,lossMdMRE];
            myTemp2 = [winMAR,tieMAR,lossMAR];
            myTemp3 = [winPred25,tiePred25,lossPred25];
            myTempToAppend = [myTemp1;myTemp2;myTemp3];
            kernelWinTieLossValues = [kernelWinTieLossValues,myTempToAppend];
            
        end
        [char(myDatasets(datasetCounter)) kernelType]
%       xlswrite(char(myDatasets(datasetCounter)),kernelWinTieLossValues,kernelType, 'C3');
        
    end
    xlswrite('irwmResults',kernelWinTieLossValues,kernelType, ['C' num2str((datasetCounter-1)*30+1)]);
end