% before everything, do a clean-up of the workspace
clear;clc;

% define dataset names for different within-company like features
cocomoProjType = {'cocomo81e','cocomo81o','cocomo81s'};
nasaCenterType = {'nasa93_center_1','nasa93_center_2','nasa93_center_5'};
desharnaisLangType = {'desharnaisL1','desharnaisL2','desharnaisL3'};
chinaResource = {'chinaResource1','chinaResource2','chinaResource3','chinaResource4'};
finnishAppArea = {'finnishAppArea2','finnishAppArea7','finnishAppArea10'};
finnishAppType = {'finnishAppType1','finnishAppType2345'};
kemererHardware = {'kemererHardware1','kemererHardware23456'};
maxwellAppType = {'maxwellAppType1','maxwellAppType2','maxwellAppType3'};
maxwellHardware = {'maxwellHardware2','maxwellHardware3','maxwellHardware5'};
maxwellSource = {'maxwellSource1','maxwellSource2'};
% combine above datasets into a single variable
allMyDatasets = {...
    %     'chinaResource'...
    %     ,'finnishAppArea',...
    'cocomoProjType',...
    'nasaCenterType',...
    'desharnaisLangType',...
    'finnishAppType',...
    'kemererHardware',...
    'maxwellAppType',...
    'maxwellHardware',...
    'maxwellSource'...
    };

% open and close file for current dataset
% below is the file for number of instances
resultsFile = fopen('resultsFile.tex','w' );
fclose(resultsFile);
% below is for all win,tie,loss and actual mar, mmre, mdmre, pred25
% combined
winTieLossFileAll = fopen('winTieLossFileAll.tex','w' );
fclose(winTieLossFileAll);
% below is the file for MMRE win, tie, loss values
winTieLossFileMMRE = fopen('winTieLossFileMMRE.tex','w' );
fclose(winTieLossFileMMRE);
% below is the file for MdMRE win, tie, loss values
winTieLossFileMdMRE = fopen('winTieLossFileMdMRE.tex','w' );
fclose(winTieLossFileMdMRE);
% below is the file for Pred(25) win, tie, loss values
winTieLossFilePred25 = fopen('winTieLossFilePred25.tex','w' );
fclose(winTieLossFilePred25);
% below is the file for MAR win, tie, loss values
winTieLossFileMAR = fopen('winTieLossFileMAR.tex','w' );
fclose(winTieLossFileMAR);

% define variables that will store cc and wc selection percentages
wcPercentages2 = [];
ccPercentages2 = [];
wcPercentages4 = [];
ccPercentages4 = [];
wcPercentagesBest = [];
ccPercentagesBest = [];

% define variables to keep track of win,tie,losses
myFixSubsetSize = 21;
% for mmre
winMmrek2 = zeros(21,1)-1; tieMmrek2 = zeros(21,1)-1; lossMmrek2 = zeros(21,1)-1;
winMmrek4 = zeros(21,1)-1; tieMmrek4 = zeros(21,1)-1; lossMmrek4 = zeros(21,1)-1;
winMmrekBest = zeros(21,1)-1; tieMmrekBest = zeros(21,1)-1; lossMmrekBest = zeros(21,1)-1;
% for mdmre
winMdmrek2 = zeros(21,1)-1; tieMdmrek2 = zeros(21,1)-1; lossMdmrek2 = zeros(21,1)-1;
winMdmrek4 = zeros(21,1)-1; tieMdmrek4 = zeros(21,1)-1; lossMdmrek4 = zeros(21,1)-1;
winMdmrekBest = zeros(21,1)-1; tieMdmrekBest = zeros(21,1)-1; lossMdmrekBest = zeros(21,1)-1;
% for pred25
winPred25k2 = zeros(21,1)-1; tiePred25k2 = zeros(21,1)-1; lossPred25k2 = zeros(21,1)-1;
winPred25k4 = zeros(21,1)-1; tiePred25k4 = zeros(21,1)-1; lossPred25k4 = zeros(21,1)-1;
winPred25kBest = zeros(21,1)-1; tiePred25kBest = zeros(21,1)-1; lossPred25kBest = zeros(21,1)-1;
% for pred25
winArk2 = zeros(21,1)-1; tieArk2 = zeros(21,1)-1; lossArk2 = zeros(21,1)-1;
winArk4 = zeros(21,1)-1; tieArk4 = zeros(21,1)-1; lossArk4 = zeros(21,1)-1;
winArkBest = zeros(21,1)-1; tieArkBest = zeros(21,1)-1; lossArkBest = zeros(21,1)-1;

% start doing cross-company and within company experiments for
% all the datasets and their subsets
myIterator = 0; % this will iterate through win,tie,loss values of subsets
    
for datasetCounter = 1:size(allMyDatasets,2)
    myIterator = myIterator + 1;
    
    % load subsets of the dataset
    currentDatasetName = (allMyDatasets(datasetCounter));
    % get subset size
    eval(['subsetSize = size(' char(currentDatasetName) ',2);']);
    
    % do for each subset
    for subsetCounter = 1:subsetSize
        % define the within company and cross company datasets
        subsetIndices = [1:subsetSize];             % get indices
        wcIndex = subsetIndices(subsetCounter);     % define one as wc
        % subsetIndices(subsetCounter) = [];          % delete wc from subsets
        ccIndex = subsetIndices;                    % assing cc indices
        eval(['wcFileName = char(' char(currentDatasetName) '(' num2str(subsetCounter) '));']);
        wcDataset = csvread([wcFileName '.csv']);   % read the wc dataset
%         wcDataset = myNormalizer(wcDataset);
        ccDataset = [];                             % define a var. for cc dataset
        ccDatasetSizes = zeros(1,size(ccIndex,2));
        for tmp1 = 1:size(ccIndex,2)                % append cc datasets in that for loop
            eval(['tmpName = [' char(currentDatasetName) '(' num2str(ccIndex(tmp1)) ')];']);
            tmpDataset = csvread([char(tmpName) '.csv']);
            ccDatasetSizes(tmp1) = size(tmpDataset,1);
%             tmpDataset = myNormalizer(tmpDataset);
            ccDataset = [ccDataset;tmpDataset];
            % also define a variable to keep track of how many instances
            % are selected from each CC center
            eval([char(tmpName) 'Instances2K = -1 * ones(20,size(wcDataset,1));']);
            eval([char(tmpName) 'Instances4K = -1 * ones(20,size(wcDataset,1));']);
            eval([char(tmpName) 'InstancesBestK = -1 * ones(20,size(wcDataset,1));']);
        end
        
        % define the variables to keep track of.. well pretty much
        % everything
        % variables related to how many instances are selected from
        % different centers
        predictionZoneSize2CC = -1 * ones(20,size(wcDataset,1));
        predictionZoneSize4CC = -1 * ones(20,size(wcDataset,1));
        predictionZoneSizeBestCC = -1 * ones(20,size(wcDataset,1));
        
        % below variables keep track of stuff related to wc experiments
        pred2WC = -1 * ones(20,size(wcDataset,1));
        pred4WC = -1 * ones(20,size(wcDataset,1));
        predBestWC = -1 * ones(20,size(wcDataset,1));
        predictionZone2WC = -1 * ones(20,size(wcDataset,1));
        predictionZone4WC = -1 * ones(20,size(wcDataset,1));
        predictionZoneBestWC = -1 * ones(20,size(wcDataset,1));
        
        pred2CC = -1 * ones(20,size(wcDataset,1));
        pred4CC = -1 * ones(20,size(wcDataset,1));
        predBestCC = -1 * ones(20,size(wcDataset,1));
        predictionZone2CC = -1 * ones(20,size(wcDataset,1));
        predictionZone4CC = -1 * ones(20,size(wcDataset,1));
        predictionZoneBestCC = -1 * ones(20,size(wcDataset,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(wcDataset);
        end
        
        % randomize dataset
        wcDataset = randomizeDataset(wcDataset);
        ccDataset = randomizeDataset(ccDataset);
        % take log of the randomized dataset. log does not apply indep.
        % variable
%         wcDataset(:,1:(size(wcDataset,2)-1)) = log(wcDataset(:,1:(size(wcDataset,2)-1)));
%         ccDataset(:,1:(size(ccDataset,2)-1)) = log(ccDataset(:,1:(size(ccDataset,2)-1)));
        
        for runCounter = 1:20  % do WC and CC experiments 20 times
            
            % for each row in the within company dataset do the following
            actualEffortValues = wcDataset(:,size(wcDataset,2));
            i = 0;
            while i < size(wcDataset,1)
                % increment i by 1
                i = i + 1;
                
                % pick up the row
                myRow = wcDataset(i,:);
                % wcTrain is the within-company dataset minus the selected row
                wcTrain = wcDataset;
                wcTrain(i,:) = [];
                
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%%%%%%%%%%%%%% below are predictions for within-company%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % now start predictions
                % the one below -treeK- is our guy to defend
                [pred2WC(runCounter,i), mre, predictionZone2WC] = nnk(myRow,wcTrain,2);
                [pred4WC(runCounter,i), mre, predictionZone4WC] = nnk(myRow,wcTrain,4);
                [predBestWC(runCounter,i), mre, predictionZoneBestWC] = nnk(myRow,wcTrain,myBestK);
                
            end
            
            i = 0;
            while i < size(wcDataset,1)
                % increment i by 1
                i = i + 1;
                
                % pick up the row
                myRow = wcDataset(i,:);
                % wcTrain is the within-company dataset minus the selected row
                wcTrain = wcDataset;
                wcTrain(i,:) = [];
                % ccTrain is the cross-company train dataset and it is used in
                % an as-is manner
                ccTrain = ccDataset;
                
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%%%%%%%%%%%%%% below are predictions for cross-company%%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                % now start predictions
                [pred2CC(runCounter,i), mre, predictionZone2CC] = nnk(myRow,ccTrain,2);
                [pred4CC(runCounter,i), mre, predictionZone4CC] = nnk(myRow,ccTrain,4);
                [predBestCC(runCounter,i), mre, predictionZoneBestCC] = nnk(myRow,ccTrain,myBestK);
                
                % below part needs to be dynamic
                % now get statistics from the prediction zone
                % below is for k=2
                predictionZoneSize2CC(runCounter,i) = size(predictionZone2CC,1);
                for tmp1 = 1:size(ccIndex,2)
                    eval(['tmpName = [' char(currentDatasetName) '(' num2str(ccIndex(tmp1)) ')];']);
                    tmpDataset = csvread([char(tmpName) '.csv']);
%                     tmpDataset(:,1:(size(tmpDataset,2)-1)) = log(tmpDataset(:,1:(size(tmpDataset,2)-1)));
                    [howManySelected] = findMyClassStatistics(predictionZone2CC, tmpDataset);
                    if howManySelected < 0
                        howManySelected
                    end
                    
                    % also define a variable to keep track of how many instances
                    % are selected from each CC center
                    eval([char(tmpName) 'Instances2K(runCounter,' num2str(i) ') = howManySelected;']);
                end
                % below is for k=4
                predictionZoneSize4CC(runCounter,i) = size(predictionZone4CC,1);
                for tmp1 = 1:size(ccIndex,2)
                    eval(['tmpName = [' char(currentDatasetName) '(' num2str(ccIndex(tmp1)) ')];']);
                    tmpDataset = csvread([char(tmpName) '.csv']);
%                     tmpDataset(:,1:(size(tmpDataset,2)-1)) = log(tmpDataset(:,1:(size(tmpDataset,2)-1)));
                    [howManySelected] = findMyClassStatistics(predictionZone4CC, tmpDataset);
                    if howManySelected < 0
                        howManySelected
                    end
                    % also define a variable to keep track of how many instances
                    % are selected from each CC center
                    eval([char(tmpName) 'Instances4K(runCounter,' num2str(i) ') = howManySelected;']);
                end
                % below is for k=best
                predictionZoneSizeBestCC(runCounter,i) = size(predictionZoneBestCC,1);
                for tmp1 = 1:size(ccIndex,2)
                    eval(['tmpName = [' char(currentDatasetName) '(' num2str(ccIndex(tmp1)) ')];']);
                    tmpDataset = csvread([char(tmpName) '.csv']);
%                     tmpDataset(:,1:(size(tmpDataset,2)-1)) = log(tmpDataset(:,1:(size(tmpDataset,2)-1)));
                    [howManySelected] = findMyClassStatistics(predictionZoneBestCC, tmpDataset);
                    if howManySelected < 0
                        howManySelected
                    end
                    % also define a variable to keep track of how many instances
                    % are selected from each CC center
                    eval([char(tmpName) 'InstancesBestK(runCounter,' num2str(i) ') = howManySelected;']);
                end
                
            end
            runCounter
        end
        
        
        % now that we have done WC and CC experiments for one WC subset
        % we need to derive the win,tie,loss statistics out of them
        % below are these values to compare CC vs WC via kNN with k=2
        [winMmrek2(myIterator) tieMmrek2(myIterator) lossMmrek2(myIterator) medMmrek2] = winTieLossCalculatorMMRE(actualEffortValues',pred2CC,pred2WC);
        [winMdmrek2(myIterator) tieMdmrek2(myIterator) lossMdmrek2(myIterator) medMdmrek2] = winTieLossCalculatorMdmre(actualEffortValues',pred2CC,pred2WC);
        [winPred25k2(myIterator) tiePred25k2(myIterator) lossPred25k2(myIterator) medPred25k2] = winTieLossCalculatorPred25(actualEffortValues',pred2WC,pred2CC);
        [winArk2(myIterator) tieArk2(myIterator) lossArk2(myIterator) medArk2] = winTieLossCalculatorAr(actualEffortValues',pred2WC,pred2CC);
        % below are these values to compare CC vs WC via kNN with k=4
        [winMmrek4(myIterator) tieMmrek4(myIterator) lossMmrek4(myIterator) medMmrek4] = winTieLossCalculatorMMRE(actualEffortValues',pred4CC,pred4WC);
        [winMdmrek4(myIterator) tieMdmrek4(myIterator) lossMdmrek4(myIterator) medMdmrek4] = winTieLossCalculatorMdmre(actualEffortValues',pred4WC,pred4CC);
        [winPred25k4(myIterator) tiePred25k4(myIterator) lossPred25k4(myIterator) medPred25k4] = winTieLossCalculatorPred25(actualEffortValues',pred4WC,pred4CC);
        [winArk4(myIterator) tieArk4(myIterator) lossArk4(myIterator) medArk4] = winTieLossCalculatorAr(actualEffortValues',pred4WC,pred4CC);
        % below are these values to compare CC vs WC via kNN with k=best
        [winMmrekBest(myIterator) tieMmrekBest(myIterator) lossMmrekBest(myIterator) medMmrekBest] = winTieLossCalculatorMMRE(actualEffortValues',predBestWC,predBestCC);
        [winMdmrekBest(myIterator) tieMdmrekBest(myIterator) lossMdmrekBest(myIterator) medMdmrekBest] = winTieLossCalculatorMdmre(actualEffortValues',predBestWC,predBestCC);
        [winPred25kBest(myIterator) tiePred25kBest(myIterator) lossPred25kBest(myIterator) medPred25kBest] = winTieLossCalculatorPred25(actualEffortValues',predBestWC,predBestCC);
        [winArkBest(myIterator) tieArkBest(myIterator) lossArkBest(myIterator) medArkBest] = winTieLossCalculatorAr(actualEffortValues',predBestWC,predBestCC);
        
        
        % now that we are done with the derivation of all the stuff about
        % the subset, we can start writing these things into a file
        resultsFile = fopen('resultsFile.tex','a' );
%         winTieLossFileAll = fopen('winTieLossFileAll.tex','a' );
%         winTieLossFileMMRE = fopen('winTieLossFileMMRE.tex','a' );
%         winTieLossFileMdMRE = fopen('winTieLossFileMdMRE.tex','a' );
%         winTieLossFilePred25 = fopen('winTieLossFilePred25.tex','a' );
%         winTieLossFileMAR = fopen('winTieLossFileMAR.tex','a' );
        
%         fprintf(winTieLossFileAll, [char(wcFileName) ' & ']);
%         fprintf(winTieLossFileMMRE, [char(wcFileName) ' & ']);
%         fprintf(winTieLossFileMdMRE, [char(wcFileName) ' & ']);
%         fprintf(winTieLossFilePred25, [char(wcFileName) ' & ']);
%         fprintf(winTieLossFileMAR, [char(wcFileName) ' & ']);
       

        %write results for k=2
        fprintf(resultsFile, ['\\textbf{2K:S' num2str(subsetCounter) ': }' char(wcFileName) ' (' num2str(size(wcDataset,1)) ') & ']);
        %%%%% now write the avg. instance sizes of k=2
        fprintf(resultsFile, [ num2str(mean(mean(predictionZoneSize2CC')),'%6.1f') ]);
        for tmp1 = 1:size(ccIndex,2)
            eval(['tmpName = [' char(currentDatasetName) '(' num2str(ccIndex(tmp1)) ')];']);
            eval(['avgSubsetSize = mean(mean(' char(tmpName) 'Instances2K''));']);
            myTmpPercentage = (avgSubsetSize*100)/ccDatasetSizes(tmp1);
            % store the wc percentages and cc percentages for k =2
            if subsetCounter == tmp1
                wcPercentages2 = [wcPercentages2;myTmpPercentage];
            else
                ccPercentages2 = [ccPercentages2;myTmpPercentage];
            end
            fprintf(resultsFile, [' & ' num2str(avgSubsetSize,'%6.1f') ' (' num2str(myTmpPercentage,'%6.1f') ')' ]);
        end
        fprintf(resultsFile, '\\\\ \n');
        fprintf(resultsFile, ['\\textbf{4K:S' num2str(subsetCounter) ': }' char(wcFileName) ' (' num2str(size(wcDataset,1)) ') & ']);
        %%%%% now write the avg. instance sizes of k=4
        fprintf(resultsFile, [ num2str(mean(mean(predictionZoneSize4CC')),'%6.1f') ]);
        for tmp1 = 1:size(ccIndex,2)
            eval(['tmpName = [' char(currentDatasetName) '(' num2str(ccIndex(tmp1)) ')];']);
            eval(['avgSubsetSize = mean(mean(' char(tmpName) 'Instances4K''));']);
            myTmpPercentage = (avgSubsetSize*100)/ccDatasetSizes(tmp1);
            % store the wc percentages and cc percentages for k =4
            if subsetCounter == tmp1
                wcPercentages4 = [wcPercentages4;myTmpPercentage];
            else
                ccPercentages4 = [ccPercentages4;myTmpPercentage];
            end
            fprintf(resultsFile, [' & ' num2str(avgSubsetSize,'%6.1f') ' (' num2str(myTmpPercentage,'%6.1f') ')' ]);
        end
        fprintf(resultsFile, '\\\\ \n');
        fprintf(resultsFile, ['\\textbf{bestK:S' num2str(subsetCounter) ': }' char(wcFileName) ' (' num2str(size(wcDataset,1)) ') & ']);
        %%%%% now write the avg. instance sizes of k=best
        fprintf(resultsFile, [ num2str(mean(mean(predictionZoneSizeBestCC')),'%6.1f') ]);
        for tmp1 = 1:size(ccIndex,2)
            eval(['tmpName = [' char(currentDatasetName) '(' num2str(ccIndex(tmp1)) ')];']);
            eval(['avgSubsetSize = mean(mean(' char(tmpName) 'InstancesBestK''));']);
            myTmpPercentage = (avgSubsetSize*100)/ccDatasetSizes(tmp1);
            % store the wc percentages and cc percentages for k = best
            if subsetCounter == tmp1
                wcPercentagesBest = [wcPercentagesBest;myTmpPercentage];
            else
                ccPercentagesBest = [ccPercentagesBest;myTmpPercentage];
            end
            fprintf(resultsFile, [' & ' num2str(avgSubsetSize,'%6.1f') ' (' num2str(myTmpPercentage,'%6.1f') ')' ]);
        end
        fprintf(resultsFile, '\\\\ \n');
        
%         % write win tie loss values of within company w.r.t. MAR
%         fprintf(winTieLossFileMAR, [num2str(winAr(1)) '&' num2str(tieAr(1)) '&' num2str(lossAr(1)) '&   ']);
%         fprintf(winTieLossFileMAR, [num2str(medAr(1),'%6.1f') '&' num2str(medAr(2),'%6.1f')]);
%         fprintf(winTieLossFileMAR, ' \\\\ \n');
%         % write win tie loss values of within company w.r.t. MMRE
%         fprintf(winTieLossFileMMRE, [num2str(winMmre(1)) '&' num2str(tieMmre(1)) '&' num2str(lossMmre(1)) '&   ']);
%         fprintf(winTieLossFileMMRE, [num2str(medMmre(1),'%6.1f') '&' num2str(medMmre(2),'%6.1f')]);
%         fprintf(winTieLossFileMMRE, ' \\\\ \n');
%         % write win tie loss values of within company w.r.t. MdMRE
%         fprintf(winTieLossFileMdMRE, [num2str(winMdmre(1)) '&' num2str(tieMdmre(1)) '&' num2str(lossMdmre(1)) '&   ']);
%         fprintf(winTieLossFileMdMRE, [num2str(medMmre(1),'%6.1f') '&' num2str(medMdmre(2),'%6.1f')]);
%         fprintf(winTieLossFileMdMRE, ' \\\\ \n');
%         % write win tie loss values of within company w.r.t. Pred25
%         fprintf(winTieLossFilePred25, [num2str(winPred25(1)) '&' num2str(tiePred25(1)) '&' num2str(lossPred25(1)) '&   ']);
%         fprintf(winTieLossFilePred25, [num2str(medPred25(1),'%6.1f') '&' num2str(medPred25(2),'%6.1f')]);
%         fprintf(winTieLossFilePred25, ' \\\\ \n');
%         
%         % now write all above statistics into winTieLossAll file
%         fprintf(winTieLossFileAll, [num2str(winAr(1)) '&' num2str(tieAr(1)) '&' num2str(lossAr(1)) '& ']);
%         fprintf(winTieLossFileAll, [num2str(medAr(1),'%6.1e') '&' num2str(medAr(2),'%6.1e') '&   ']);
%         fprintf(winTieLossFileAll, [num2str(winMmre(1)) '&' num2str(tieMmre(1)) '&' num2str(lossMmre(1)) '& ']);
%         fprintf(winTieLossFileAll, [num2str(medMmre(1),'%6.1f') '&' num2str(medMmre(2),'%6.1f') '&   ']);
%         fprintf(winTieLossFileAll, [num2str(winMdmre(1)) '&' num2str(tieMdmre(1)) '&' num2str(lossMdmre(1)) '& ']);
%         fprintf(winTieLossFileAll, [num2str(medMdmre(1),'%6.1f') '&' num2str(medMdmre(2),'%6.1f') '&   ']);
%         fprintf(winTieLossFileAll, [num2str(winPred25(1)) '&' num2str(tiePred25(1)) '&' num2str(lossPred25(1)) '& ']);
%         fprintf(winTieLossFileAll, [num2str(medPred25(1),'%6.1f') '&' num2str(medPred25(2),'%6.1f')]);
%         fprintf(winTieLossFileAll, ' \\\\ \n');
    end
    % place a straight line at the end of each dataset
    fprintf(resultsFile, ' \\hline \n');
%     fprintf(winTieLossFileAll, ' \\hline \n');
%     fprintf(winTieLossFileMAR, ' \\hline \n');
%     fprintf(winTieLossFileMMRE, ' \\hline \n');
%     fprintf(winTieLossFileMdMRE, ' \\hline \n');
%     fprintf(winTieLossFilePred25, ' \\hline \n');
    % finally close the file
    fclose(resultsFile);
%     fclose(winTieLossFileAll);
%     fclose(winTieLossFileMMRE);
%     fclose(winTieLossFileMdMRE);
%     fclose(winTieLossFilePred25);
%     fclose(winTieLossFileMAR);
end

mmreToWrite = [(winMmrek2-lossMmrek2)  (winMmrek4 - lossMmrek4) (winMmrekBest - lossMmrekBest)];
mdmreToWrite = [(winMdmrek2 - lossMdmrek2) (winMdmrek4 - lossMdmrek4) (winMdmrekBest - lossMdmrekBest)];
pred25ToWrite = [(winPred25k2 - lossPred25k2) (winPred25k4 - lossPred25k4) (winPred25kBest - lossPred25kBest)];
marToWrite = [(winArk2 - lossArk2) (winArk4 - lossArk4) (winArkBest - lossArkBest)];
allToWrite = [marToWrite  mmreToWrite mdmreToWrite pred25ToWrite];
