% This script is used to calculate the pearson linear correlation
%coefficient and root mean sqaured error after regression

%get the objective scores computed by the IQA metric and the subjective
%scores provided by the dataset

% close all;

mos = x;
y = score;

for i=1:size(y,2)
objectiveValues = y(:,i);

%initialize the parameters used by the nonlinear fitting function
% The following beta were used in VIS, TIP 2014

% Live beta
% beta = [ max(mos), min(mos), mean(objectiveValues), 0.1, 0.1 ];
% 
% CSIQ beta
% beta = [ 5, min(mos), mean(objectiveValues), 0.1, 10 ] ; 

% TID2008
% beta = [ 10, 0, mean(objectiveValues), 1, 0.1]; % Used in 3DTV-CON

% TID2013
% beta = [ 10, 0, mean(objectiveValues), 0.1, 0.1 ];

%LOGS
% beta = [ 1, 0, mean(objectiveValues), 1, 0.1]; % New function for IRRCyN/IVC

beta = [1, 0.5, 2, 0.1];

% %plot objective-subjective score pairs
% figure;
% p = plot(objectiveValues,mos,'o');
% set(p,'Color','blue','LineWidth',3);

%% fitting a curve using the data
[bayta ehat,J] = nlinfit(objectiveValues,mos,@logistic3,beta);
%given a ssim value, predict the correspoing mos (ypre) using the fitted curve
[ypre junk] = nlpredci(@logistic3,objectiveValues,bayta,ehat,J);

RMSError(i) = sqrt(sum((ypre - mos).^2) / length(mos)); %root meas squared error
    PLCC(i) = corr(mos, ypre, 'type','Pearson');        %pearson linear coefficient
   SROCC(i) = corr(mos, ypre, 'type','Spearman');      %pearson linear coefficient
   KROCC(i) = corr(mos, ypre, 'type','Kendall');       %pearson linear coefficient
     MAE(i) = meanAbsError(mos,ypre); 

%plot objective-subjective score pairs
figure;
p = plot(ypre,mos,'*');
set(p,'Color','blue','LineWidth',2);% ,'MarkerFaceColor', 'blue');
hold on;

p1 = plot([0:0.1:1], [0:0.1:1], '--');
set(p1,'Color','black','LineWidth',1);
% legend('Test dataset ','Curve fitted with logistic function', 'Location','SouthWest');
xlabel('Obj');
ylabel('DMOS');
hold off;
res = [PLCC(i) SROCC(i) KROCC(i) RMSError(i) MAE(i) ]
end

% res = [PLCC  SROCC KROCC RMSError MAE ]
