function [loss, win, tie] = myMIBRE(actuals,predictions,algorithms)

% IBRE: inverted balanced relative error
% abs(actual-predicted) divided by actual or predicted
% whichever of actual and predicted is greater will be the divident

% define a variable to keep the MBRE results for each algorithm
IBREResults = -1 * ones(size(algorithms,1),size(actuals,1));

% process each algorithm's results
for i = 1:size(algorithms,1)
    predictions(i,find(predictions(i,:) == 0)) = 1; % if there is any prediction of
    % zero effort, replace it with 1
    for j = 1: size(actuals,1)
        absoluteDifference = abs(actuals(j) - predictions(i,j));
        if actuals(j) > predictions(i,j)
            IBREResults(i,j) = absoluteDifference / actuals(j);
        else
            IBREResults(i,j) = absoluteDifference / predictions(i,j);
        end
    end
end


% define win tie loss variables
win = zeros(size(algorithms,1),1);
tie = zeros(size(algorithms,1),1);
loss = zeros(size(algorithms,1),1);

% start calculating win tie loss values
for i = 1:size(algorithms,1)
    for j = (i+1):size(algorithms,1)
        if i ~= j   % if i and j are different
            eval(['[P,H] = ranksum(IBREResults(',num2str(i),',:),IBREResults(',num2str(j),',:));']);
            if H == 0   % if they are the same
                eval(['tie(',num2str(i),') = tie(',num2str(i),') +1;']);
                eval(['tie(',num2str(j),') = tie(',num2str(j),') +1;']);
            else
                % calculate mean values
                eval(['mibrei = mean(IBREResults(',num2str(i),',:));']);
                eval(['mibrej = mean(IBREResults(',num2str(j),',:));']);
                if mibrei < mibrej
                    eval(['win(',num2str(i),') = win(',num2str(i),') +1;']);
                    eval(['loss(',num2str(j),') = loss(',num2str(j),') +1;']);
                else
                    eval(['win(',num2str(j),') = win(',num2str(j),') +1;']);
                    eval(['loss(',num2str(i),') = loss(',num2str(i),') +1;']);
                end
            end
        end
    end
end

% % and now write the loss values to the file
% fid=fopen('RUN_RESULTS.txt','a' );
% for i = 1:size(algorithms,1)
%     fprintf(fid, [num2str(loss(i)) ',']);
% end
% fprintf(fid, ['\n']);    % append a new line at the end of the line
% % close file
% fclose(fid);

end