% this is to analyse the results from 2nd experiment

clear;
clc;
tic
np=100;
ns=48;
COWA=zeros(3,3);            % theoretical Vs actual decision
COWA1=zeros(3,3);           % theoretical Vs actual decision in Treatment 1
COWA2=zeros(3,3);           % theoretical Vs actual decision in Treatment 2
COMA=zeros(6,12);           % theoretical Vs actual M
COMA1=zeros(6,12);          % theoretical Vs actual M in Treatment 1
COMA2=zeros(6,10);          % theoretical Vs actual M in Treatment 2
noofthrees=0;
theorinc=zeros(ns,np);            % theoretical income - entire analysis
subjinc=zeros(ns,np);            % actual income - entire subjects
maxregret=zeros(ns,np);          % theoretical minimax regret - entire subjects
subjregret=zeros(ns,np);        % actual regret - entire subjects
counter=0;
for sn=1:1:ns
    inputfile = strcat('input data/input',num2str(sn),'.csv');
    inputdata = dlmread(inputfile);
    resultsfile = strcat('data from experiment/subject',num2str(sn),'input',num2str(sn),'.csv');
    resultsdata = dlmread(resultsfile);
    colzeros=zeros(np,1);
    resultsdata=[resultsdata,colzeros];
    data=zeros(np,100);
    N=inputdata(:,1);
    L0=inputdata(:,2);
    U0=inputdata(:,3);
    k=inputdata(:,4);
    K=inputdata(:,5);
    D=inputdata(:,6);
    x(:,1)=resultsdata(:,2);
    x(:,2)=resultsdata(:,3);
    x(:,3)=resultsdata(:,4);
    x(:,4)=resultsdata(:,5);
    x(:,5)=resultsdata(:,6);
    dec(sn,:)=0;
    theorasplev=zeros(np,5);                        % theoretical aspiration level
    for pn=91:1:np
        U1=U0(pn);
        L1=L0(pn);
        
        % Manski's solution
        M(sn,pn)=floor(log((U1-L1)/k(pn))/log(2));     % theoretical M
        % No deliberation
        if ((k(pn)>=(U1-L1)/2) && (K(pn)>=U1-L1))
            dec(sn,pn)=1;
            theorinc(sn,pn)=min(x(pn,:));
            maxregret(sn,pn)=U1-L1;         % theoretical regret on ND
            theorM(sn,pn)=0;                % M round(s) in ND
        end
        % Optimising
        if ((K(pn)< U1-L1) && (K(pn)<=k(pn)*M(sn,pn)+(U1-L1)/(2^M(sn,pn))))
            dec(sn,pn)=3;
            theorinc(sn,pn)=max(x(pn,:))-K(pn);
            maxregret(sn,pn)=K(pn);         % theoretical regret on Optimising
            theorM(sn,pn)=0;                % M round(s) in Optimising
        end
        % Satisficing - doing it M times and then costlessly
        if ((k(pn)<(U1-L1)/2) && (K(pn)>=k(pn)*M(sn,pn)+(U1-L1)/(2^M(sn,pn))))
            dec(sn,pn)=2;
            lvgeal(sn,pn)=min(x(pn,:));
            maxregret(sn,pn)=k(pn)*M(sn,pn)+(U1-L1)/2^M(sn,pn);     % theoretical regret on Satisficing
            L(1)=L1;
            U(1)=U1;
            for m=1:1:M(sn,pn)
                t(pn,m)=(L(m)+U(m))/2;
                theorasplev(pn,m)=t(pn,m);
                S0=zeros(0,0);
                S1=zeros(0,0);
                for n=1:1:N(pn)
                    if x(pn,n)<t(pn,m)
                        S0=[S0,x(pn,n)];
                    end
                    if x(pn,n)>t(pn,m)
                        S1=[S1,x(pn,n)];
                    end
                end
                
                % calculating lvgeal
                if isempty(S1)==1
                    L(m+1)=L(m);
                    U(m+1)=t(pn,m);
                end
                if isempty(S1)==0
                    lvgeal(sn,pn)=max(lvgeal(sn,pn),min(S1));
                    L(m+1)=t(pn,m);
                    U(m+1)=U(m);
                end
                if m==M(sn,pn)
                    theorinc(sn,pn)=lvgeal(sn,pn);
                end
            end
            theorinc(sn,pn)=theorinc(sn,pn)-M(sn,pn)*k(pn);         % calculating theoretical income
            theorM(sn,pn)=floor(log((U1-L1)/k(pn))/log(2));         % M round(s) in Satisficing
        end
        
        % calculating subjects' income
        j=N(pn)+2;
        while resultsdata(pn,j)~=0
            data(pn,j-N(pn)-1)=resultsdata(pn,j);
            if data(pn,j-N(pn)-1)==-3000
                noofthrees=noofthrees+1;
            end
            j=j+1;
        end
        
        % actual subjects' income
        subjinc(sn,pn)=resultsdata(pn,j-1);         
        if resultsdata(pn,j-1)==-3000
            subjinc(sn,pn)=resultsdata(pn,j);
        end
        if resultsdata(pn,j-1)==-1000
            subjinc(sn,pn)=resultsdata(pn,j);
        end
        
        % Taking the actual M for each subject
        Mact(sn,pn)=sum(data(pn,:)==-2000)-sum(data(pn,:)==999);
        
        % Comparing theoretical Vs actual M
        for row=1:1:6
            for col=1:1:12
                if ((theorM(sn,pn)==row-1) && (Mact(sn,pn)==col-1))
                    COMA(row,col)=COMA(row,col)+1;
                end
            end
        end
        
        % Reading the actual aspiration level
        data2=data;
        subjasplev=[];
        for a=1:size(data,1)
            XX=[];
            for b=1:2:size(data,2)
                if ((data(a,b))==-2000) && (data(a,b+1)~=999)
                    XX=[XX,data(a,b+1)];
                elseif (data(a,b)~=-2000)
                    XX=[XX,0];
                end
            end
            X=zeros(1,size(data,2));
            for idx=1:size(X, 1)
                for jdx=1:size(XX,2)
                    X(idx,jdx)=XX(idx,jdx);
                end
            end
            subjasplev=[subjasplev; X];
            sal=subjasplev(:,1:11);                 % actual aspiration level
        end
        
        % Considered U and L every time subjects do satisficing
        if sal(pn,1)==0         % you need to put Lsub(pn,1)=L1 and Usub(pn,1)=U1 if you want to have L and U for all problems
            Lsub(pn,1:11)=0;
            Usub(pn,1:11)=0;
            tal(pn,:)=0;
        elseif sal(pn,1)~=0
            Lsub(pn,1)=L1;
            Usub(pn,1)=U1;
            counter=counter+1;
            
            actasp(counter)=sal(pn,1);  % if sal(pn,1)<=100
            Lbus(counter)=Lsub(pn,1);
            Ubus(counter)=Usub(pn,1);   % end
            
            for z=2:1:Mact(sn,pn)
                counter=counter+1;
                
                actasp(counter)=sal(pn,z);  % if sal(pn,z)<=100 - end
                
                Empt0=zeros(0,0);
                Empt1=zeros(0,0);
                for g=1:1:N(pn)
                    if x(pn,g)<=sal(pn,z-1)
                        Empt0=[Empt0,x(pn,g)];
                    end
                    if x(pn,g)>sal(pn,z-1)
                        Empt1=[Empt1,x(pn,g)];
                    end
                end
                if isempty(Empt1)==1
                    Lsub(pn,z)=Lsub(pn,z-1);
                    Usub(pn,z)=sal(pn,z-1);
                end
                if isempty(Empt1)==0
                    Lsub(pn,z)=sal(pn,z-1);
                    Usub(pn,z)=Usub(pn,z-1);
                end
                
                Lbus(counter)=Lsub(pn,z);   % if ((Lsub(pn,z)<=100) && (Usub(pn,z)<=100))
                Ubus(counter)=Usub(pn,z);   % end
                
            end
            tal=(Lsub+Usub)/2;              % theoretical aspiration level when subjects do satisficing
            tals=(Lbus+Ubus)/2;             % theoretical aspiration level when subjects do satisficing - in a vector
        end
                 
        % Reading the actual decision of the subjects
        act(sn,pn)=0;
        for jj=1:1:j-N(pn)-2
            if ((data(pn,jj)==-2000) && (data(pn,jj)~=999))
                act(sn,pn)=2;
            end
        end
        for jj=1:1:j-N(pn)-2
            if data(pn,jj)==-3000
                act(sn,pn)=3;
            end
        end
        for jj=1:1:j-N(pn)-2
            if ((data(pn,jj)==-1000) && (act(sn,pn)==0))
                act(sn,pn)=1;
            end
        end
        
        % start calculating ex-ante regret for subject
        for jj=1:1:j-N(pn)-2        
            if ((dec(sn,pn)==1) && (act(sn,pn)==1))
                subjregret(sn,pn)=U1-L1;
            end
            if ((dec(sn,pn)==2) && (act(sn,pn)==2))
                subjregret(sn,pn)=k(pn)*M(sn,pn)+(U1-L1)/2^M(sn,pn);
            end
            if ((dec(sn,pn)==3) && (act(sn,pn)==3))
                subjregret(sn,pn)=K(pn);
            end
            if ((dec(sn,pn)==1) && (act(sn,pn)==2))
                subjregret(sn,pn)=U1-L1+k(pn)*M(sn,pn)+(U1-L1)/2^M(sn,pn);
            end
            if ((dec(sn,pn)==1) && (act(sn,pn)==3))
                subjregret(sn,pn)=U1-L1+K(pn);
            end
            if ((dec(sn,pn)==2) && (act(sn,pn)==1))
                subjregret(sn,pn)=k(pn)*M(sn,pn)+((U1-L1)/2^M(sn,pn))+(U1-L1);
            end
            if ((dec(sn,pn)==2) && (act(sn,pn)==3))
                subjregret(sn,pn)=k(pn)*M(sn,pn)+((U1-L1)/2^M(sn,pn))+K(pn);
            end
            if ((dec(sn,pn)==3) && (act(sn,pn)==1))
                subjregret(sn,pn)=K(pn)+(U1-L1);
            end
            if ((dec(sn,pn)==3) && (act(sn,pn)==2))
                subjregret(sn,pn)=K(pn)+k(pn)*M(sn,pn)+(U1-L1)/2^M(sn,pn);
            end
        end
        
        % comparing theoretical Vs actual decision
        for row=1:1:3           
            for col=1:1:3
                if ((dec(sn,pn)==row) && (act(sn,pn)==col))
                    COWA(row,col)=COWA(row,col)+1;
                end
            end
        end
        tavinc(sn)=mean(theorinc(sn,:));            % theoretical average income
        savinc(sn)=mean(subjinc(sn,:));             % actual average income
        tavregret(sn)=mean(maxregret(sn,:));        % theoretical actual regret
        savregret(sn)=mean(subjregret(sn,:));       % actual average regret

    end
    
    % Scatters for theoretical income and actual subjects' income
%     scatter(theorinc(sn,:),subjinc(sn,:),'x', 'R');
%     refline(1,0);
%     title(['Subject ',num2str(sn),', Input file ',num2str(sn)]);
%     xlabel('Theoretical Payoff'); ylabel('Actual Payoff');
%     saveas(gcf,strcat('scatters payoffs\Actual v Theory (Payoffs), Subject',num2str(sn),'input files',num2str(sn),'.jpeg'));
    
    % Scatters for theoretical minmax regret and actual subjects' regret
%     scatter(maxregret(sn,:),subjregret(sn,:),'x','R');
%     refline(1,0);
%     title(['Subject ',num2str(sn),', Input file ',num2str(sn)]);
%     xlabel('Theoretical Regret'); ylabel('Actual Regret');
%     saveas(gcf,strcat('scatters regrets\Actual v Theory (Regrets), Subject', num2str(sn),'input files',num2str(sn),'.jpeg'));

    % Fitting actual (sal) Vs theoretical aspiration level (tal) - whenever subjects do satisficing
%     [xData, yData] = prepareCurveData(tal, sal);        
%     ft = fittype( 'poly1' );
%     excludedPoints = (xData < 1) | (xData > 100) | (yData < 1) | (yData > 100);
%     opts = fitoptions( 'Method', 'LinearLeastSquares' ); opts.Normalize = 'on'; opts.Exclude = excludedPoints;
%     [fitresult, gof] = fit(xData, yData,ft,opts);
%     figure( 'Name', 'untitled fit 1' );
%     h = plot(fitresult,xData,yData,excludedPoints);
%     xlim([1,100]); ylim([1,100]);
%     legend(h, 'TAL vs. AAL', 'Excluded Points', 'Fitted Data','Location', 'SouthEast'); refline(1,0);
%     title(['Subject ',num2str(sn),', Input file ',num2str(sn)]);
%     xlabel ('Theoretical Aspiration Level'); ylabel ('Actual Aspiration Level'); grid ('on');
%     saveas(gcf,strcat('scatters asplev\Actual v Theory (AspLev)Subject',num2str(sn),'input files',num2str(sn),'.jpeg'));
    
    % Fitting of subjects' aspiration level (sal) and actual L (Lsub) and U (Usub) for each subject
%     [xData, yData, zData] = prepareSurfaceData(Usub, Lsub, sal);
%     ft = fittype('poly11');
%     [fitresult, gof] = fit([xData, yData], zData, ft, 'Normalize', 'on');
%     figure( 'Name', 'untitled fit 1' );
%     h = plot( fitresult, [xData, yData], zData );
%     legend(h, 'untitled fit 1', 'sal vs. Usub, Lsub', 'Location', 'NorthEast');
%     title(['Subject ',num2str(sn),', Input file ',num2str(sn)]);
%     xlabel ('Usub'); ylabel ('Lsub'); zlabel ('sal');
%     grid ('on'); view(21.3, -19.6);
%     saveas(gcf,strcat('regress asplev\SAL L U (Fit) Subject',num2str(sn),'input files',num2str(sn),'.jpeg'));

    % Scatter for vector (aggregate) actual aspiration level (actasp) and actual L (Lbus) and U (Ubus) - whenever subjects do satisficing
%     scatter3(Lbus,Ubus,actasp,'x','R');
%     refline(1,0);
%     title(['Actual Aspiration Level and the Considered L and U (3D)']);
%     xlabel('L'); ylabel('U'); zlabel('Actual Aspiration Level');
%     saveas(gcf,strcat('vector asplev\Actual-Considered L and U-3D','.jpeg'));       % 3D version
%     scatter(Lbus,Ubus,actasp,'x','R');
%     refline(1,0);
%     title(['Actual Aspiration Level and the Considered L and U (2D)']);
%     xlabel('L'); ylabel('U'); zlabel('Actual Aspiration Level');
%     saveas(gcf,strcat('vector asplev\Actual-Considered L and U-2D','.jpeg'));       % 2D version
    
    % Scatter for vector actual aspiration level (actasp) and theoretical aspiration level (tals) - whenever subjects do satisficing
%     scatter(tals(tals~=0),actasp(actasp~=0),'x','R');        % Putting (tals>100) and (actasp>100) instead of (~=0) leads to unmatched X and Y vectors
%     refline(1,0);
%     xlim([1,100]); ylim([1,100]);
%     legend('TAL vs. AAL', 'Ref. Line','Location', 'SouthEast');
%     title(['Actual Vs Theoretical Aspiration Level - Treatment 2']);
%     xlabel ('Theoretical Aspiration Level'); ylabel('Actual Aspiration Level');
%     saveas(gcf,strcat('vector asplev\ActualVsTheoretical','.jpeg'));
    
    % t-test actual Vs theoretical aspiration level - decision, p-val, conf. int., test info
%     [ttest,ptest,citest,stattest]=ttest2(actasp(actasp>100),tals(tals>100));
    
%     fistest=table(dec(sn,pn),Mact(sn,pn));
%     [fish,p]=fishertest(fistest);

end
% theoravinc=mean(tavinc);        % average theoretical income - entire subjects
% subjavinc=mean(savinc);         % average actual income - entire subjects
% theoravregret=mean(tavregret);  % average theoretical minmax regret - entire subjects
% subjavregret=mean(savregret);   % average actual regret - entire subjects

% Analysis for Treatment 1
% for sn1=1:1:24
%     % average theoretical and actual income in Treatment 1 - for each
%     % subject
%     tavinc1(sn1)=mean(theorinc(sn1,:));
%     savinc1(sn1)=mean(subjinc(sn1,:));
%     % average theoretical and actual regret in Treatment 1 - for each
%     % subject
%     tavregret1(sn1)=mean(maxregret(sn1,:));
%     savregret1(sn1)=mean(subjregret(sn1,:));
%     
%     for pn1=1:1:np
%         % comparison of theoretical Vs actual M in Treatment 1
%         for row1=1:1:6
%             for col1=1:1:12
%                 if((theorM(sn1,pn1)==row1-1) && (Mact(sn1,pn1)==col1-1))
%                     COMA1(row1,col1)=COMA1(row1,col1)+1;                
%                 end
%             end
%         end
%         % comparison of theoretical Vs actual decision in Treatment 1
%         for row1=1:1:3
%             for col1=1:1:3
%                 if ((dec(sn1,pn1)==row1 && act(sn1,pn1)==col1));
%                     COWA1(row1,col1)=COWA1(row1,col1)+1;                
%                 end
%             end
%         end
%     end
% end
% theoravinc1=mean(tavinc1);          % average theoretical income - Treatment 1
% subjavinc1=mean(savinc1);           % average actual income - Treatment 1
% theoravregret1=mean(tavregret1);    % average theoretical minmax regret - Treatment 1
% subjavregret1=mean(savregret1);     % average actual regret - Treatment 1

% Analysis for Treatment 2
% for sn2=25:1:48
%     % average theoretical and actual income in Treatment 2 - for each
%     % subject
%     tavinc2(sn2)=mean(theorinc(sn2,:));
%     savinc2(sn2)=mean(subjinc(sn2,:));
%     % average theoretical and actual regret in Treatment 2 - for each
%     % subject
%     tavregret2(sn2)=mean(maxregret(sn2,:));
%     savregret2(sn2)=mean(subjregret(sn2,:));
%     
%     for pn2=1:1:np
%         % comparison of theoretical Vs actual M in Treatment 2
%         for row2=1:1:6
%             for col2=1:1:10
%                 if ((theorM(sn2,pn2)==row2-1) && (Mact(sn2,pn2)==col2-1))
%                     COMA2(row2,col2)=COMA2(row2,col2)+1;
%                 end
%             end
%         end
%         % comparison of theoretical Vs actual decision in Treatment 2
%         for row2=1:1:3
%             for col2=1:1:3
%                 if ((dec(sn2,pn2)==row2 && act(sn2,pn2)==col2));
%                     COWA2(row2,col2)=COWA2(row2,col2)+1;
%                 end
%             end
%         end
%     end
% end
% theoravinc2=mean(tavinc2(1,25:48));          % average theoretical income - Treatment 2
% subjavinc2=mean(savinc2(1,25:48));           % average actual income - Treatment 2
% theoravregret2=mean(tavregret2(1,25:48));    % average theoretical minmax regret - Treatment 2
% subjavregret2=mean(savregret2(1,25:48));     % average actual regret - Treatment 2

toc