% This is for the July 2019 sessions.
% version 9 goes back to one tremble and assumes risk-neutrality
% version 8 has two trembles
% versions 6 and 7 use fmincon instead of fminunc.
% Version 5 takes away the tremble everywhere except at the acceptance
% decision.
%Version 4 uses the tidied up master progam Estimation 5 - which just
%passes the raw data.
% I realise that the loglik can operate directly on the raw data file, so this version 5 
% cuts out all the crap in version 4.
tic
clear
clc
npr=81;
nsj=39;
mineps=8;
maxeps=22;
readdata=1;  % this asks the program to read the raw data. If the raw data is
% already saved in the file 'data' in the drive 'data', put this equal to
% 0.
if readdata==1
    data=xlsread('data from sessions in july 2019/all data.xlsx');
    disp("data read in")
    nl=size(data,1);
end
xall=zeros(nsj,2);
lb=[1.0,0.0];
ub=[50.0,0.5];
llMAX=zeros(nsj,1);
for sjn=1:1:nsj
    disp("new subject")
    sjn
    %the parameters to be estimated are p (precison) and
    %t (tremble1) 
    llfun=@(pars) LogLikelihood9(pars,sjn,data,mineps,maxeps);
    pars0=[10.0,0.05];
    x=fmincon(llfun,pars0,[],[],[],[],lb,ub);
    llMAX(sjn)=-LogLikelihood9(x,sjn,data,mineps,maxeps);
    x(1)=1+abs(x(1));
    xall(sjn,:)=x;
    disp("estimated subject")
    sjn
end
xlswrite('output/xall9july2019.xls',xall);
xlswrite('output/llall9july2019.xls',llMAX);
toc
