% FUNCTION:      ADTestMag
% VERSION:        [Interactive Hybrid Standalone Version] V1.8
% COMPATIBLE with Matlab version 2017b or later
% TOOLBOX: "Magnitude Complexity Toolbox" within SERA Project
% DOCUMENT: "READ_ME_App_2A_v1_Description_ADTestMag.docx"
% -----------------------------------------------------------------------------------------------------------------------
%% EXAMPLE TO RUN:
% x=exprnd(log10(exp(1)),1000,1);
% [pval mmin NN P S bval]=ADTestMag_V1_8(x,0.1,'exp',0.5,2.0,200) % for Interactivity OFF 
% [pval mmin NN P S bval ]=ADTestMag_V1_8(x);        % for Interactivity ON
%% ------------------------------------------------------------------------------------------------------------------------
% Test for Exponential/Weibull distribution of a time-series (e.g. Magnitudes)
% -----------------------------------------------------------------------------------------------------------------------
% INPUT DATA:THE CURRENT VERSION USES AS INPUT A MAGNITUDE VECTOR 
% (Appropriate for standalone use - function mode)
% -----------------------------------------------------------------------------------------------------------------------
% OVERVIEW: THE FUNCTION performs the Anderson-Darling test for testing whether 
% a given set of observations (e.g. magnitudes), follows the exponential, or the 
% Weibull distribution 
% -----------------------------------------------------------------------------------------------------------------------
% AUTHORS: K. Leptokaropoulos, and P. Urban  
% last updated: 03/2019, within SERA PROJECT, EU Horizon 2020 R&I
% programme under grant agreement No.730900
% CURRENT VERSION: v1.8        ****     [INTERACTIVE HYBRID STANDALONE VERSION!!]
% ----- THIS IS a dual-mode version: If all input arguments are set, then the
% ----- Application operates as a function. However, if only the input vector 
% ----- is introduced, the application switch to interactive mode.
%% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PLEASE refer to the accompanying document:
%                                                              "READ_ME_App_2A_v1_Description_ADTestMag.docx" 
% for description of the Application and its requirements.
%% -----------------------------------------------------------------------------------------------------------------------
% DESCRIPTION:
% THE FUNCTION performs the Anderson-Darling test for testing the Null Hypothesis, H0,
% that a given dataset (e.g. magnitudes), has been drawn from the exponential or Weibull distribution 
%(e.g. Marsaglia and % Marsaglia, 2004). This is performed as a function on minimum magnitude, such that
% multiple results are produced. The significance of the the H0 (p-value)  is the main 
% output of the program. Before applying the AD test the magnitudes are randomized 
% within their round-off interval, by the formula introduced by Lasocki & Papadimitriou,
% 2006.
% % -----------------------------------------------------------------------------------------------------------------------
%
%CALLING SEQUENCE
%      [pval mmin NN P S bval ]=ADTestMag_V1_8(vector,EPS,MTdistribution,Mmin,Mmax,trials)
%
% INPUT PARAMETERS: 
%               -- 'vector': A sample data (e.g. Magnitude) Vector. The program 
%                              takes as input any matlab vector. The input data can 
%                              be uploaded by the User from an ASCII file  (e. g. the 
%                              file "test_vector.txt" file, located within the directory
%                              "Sample_Data").Such file should contain a vector (row 
%                              or column) of the Data the User wishes to process. The 
%                              User is afterwards requested to enter parameters values: 
%               
%               - EPS: Round-off interval, i.e. minimum non-zero difference of the 
%                        input data. It also corresponds to step for calculations (AD 
%                        test iterations process). Default value is calculated from the 
%                        given dataset. It is recommended to use 0.1 (or 0.01) for 
%                        magnitude vectors
%                - MTdistribution: Distribution to be tested. Possible values: 
%                                       'exp' for the Exponential, 'weibul' for the Weibull distribution 
%               - Mmin: Corresponds to the minumum  'vector' value for which 
%                           the AD test is performed (cut-off value). If 'vector' consists of magnitudes 
%                          it is recommended (yet, not restricted) to set Mmin equal to the 
%                           catalog completeness threshold, Mc (if known).
%                - Mmax: The test is carried out succesively for 'vector' values >= [Mmin, Mmin+EPS, Mmin+2EPS,..,Mmax] 
%                             The default MMax is the 'vector' value for which the number of 
%                             'vector' values >=Mmax is greater than 4 (minimum possible 
%                             sample for AD test function execution)
%                - trials: Number of randomization realizations (trials) perfomed in 
%                           order to diminish the influence of randomization on the 
%                           resulting p-value. Default is 100. 

%  The User may also use the calling sequence [pval mmin NN P S bval ]=ADTestMag_V1_8(vector)
%               In such a case they are requested interactivelly to provide the rest of input
%               parameters.
% -----------------------------------------------------------------------------------------------------------------------
% OUTPUT:
%
%                 - Output Parameters:
%                        * pval - Structure with 3 fields: 
%                                   p - vector of test p-values obtained in successive trials,
%                                   mmin - Minimum value of 'vector' used in the test,
%                                   P - the average of p-values 
%                          [NOTE: histograms of such p-values indicate that
%                          their distribution is not normal, neither even symmetric 
%                          for a large number of cases]
%
%                        * P - vector of the average of the p-values obtained when testing for 
%                                 the consecutive mmin values
%                        * S - vector of the standard deviation of the
%                                 p-values obtained when testing for the consecutive mmin values
%                        * mmin - vector with minimum values of 'vector' used in the consecutive tests
%                        * NN - vector with the number of 'vector' values >= each mmin value.
%                        * bval - the Gutenberg-Richter b-value for 'vector' values >= each mmin value. 
%                               This parameter has a meaning only when 'vector' consists of magnitudes. 
%
%                  - Output:
%                       RES - a five column matrix. Col 1 - mmin values,
%                           Col 2 - NN values, Col 3 - b values, Col 4 - P
%                           values, Col 5 - S values
%                       mmin, NN, P, S as defined above
%
%                 - Output Figures: 
%                        Figure with the average p-value (P parameter) as a function of
%                        mmin, together with histogram of events count in bin=EPS. 
%                       The 0.05 significance level is plotted as well
% -----------------------------------------------------------------------------------------------------------------------
% REFERENCES:
% -- Lasocki S. and E. E,  Papadimitriou (2006), "Magnitude distribution 
% complexity revealed in seismicity from Greece", J. Geophys. Res., 
% 111, B11309, doi:10.1029/2005JB003794.
% -- Marsaglia, G., and J. Marsaglia (2004), "Evaluating the Anderson-Darling
% distribution", J. Stat. Soft., 1-5.
% -----------------------------------------------------------------------------------------------------------------------
% LICENSE 
%     This is free software: you can redistribute it and/or modify it under 
%     the terms of the GNU General Public License as published by the Free 
%     Software Foundation, either version 3 of the License, or 
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
%     See the GNU General Public License for more details.
% -----------------------------------------------------------------------------------------------------------------------
%% EXAMPLE TO RUN:
% x=exprnd(log10(exp(1)),1000,1);
% [pval mmin NN P S bval]=ADTestMag_V1_8(x,0.1,'exp',0.5,2.0,200) % for Interactivity OFF 
% [pval mmin NN P S bval ]=ADTestMag_V1_8(x);        % for Interactivity ON
%% ------------------------------------------------------------------------------------------------------------------------


function [pval, mmin, NN, P, S, bval ]=ADTestMag_V1_8(vector,EPS,MTdistribution,Mmin,Mmax,trials)
mkdir Outputs_ADTestMag
if nargin==1 
%DEFINE INPUT PARAMETERS MANUALLY: NARGIN==1
  % Select Mc and filter parameters for M>=Mc 
 [MM,Mmin,EPS,MTdistribution]=FiltMcVector(vector);
 if Mmin<min(MM);Mmin=min(MM);end


% ROUND MAGNITUDES to the selected EPS
M=round(MM/EPS)*EPS;


Mmin=floor(Mmin/EPS)*EPS;MMax=ceil(max(M)/EPS)*EPS;
Rp=round(-log10(EPS));

trials=dialog1('number of trials',{'100'})
% define number of classes for calculation, 
% up to the M where a minimum of N=5 occurs
mags=Mmin:EPS:MMax;mags=round(mags/EPS)*EPS;

     %%Alternative way to estimate counts of events
hi=histc(M,mags-eps/2); % Check the rounding
H=flipud(cumsum(flipud(hi)));

N1=numel(H(H>=4));

Mmax=dialog1(['Maximum value for calculations ',num2str(Mmin),'\leqM\leq', num2str(mags(N1)),')'],{num2str(mags(N1))})
N=find(abs(mags-Mmax)<EPS/2);

tic;
elseif nargin>1 
% INPUT PARAMETERS ARE SPECIFIED AS FUNCTION ARGUMENTS
M=round(vector/EPS)*EPS;
Mmin=floor(Mmin/EPS)*EPS;MMax=ceil(max(M)/EPS)*EPS;
Rp=round(-log10(EPS));if Mmax>max(M);Mmax=max(M);end
mags=Mmin:EPS:MMax;mags=round(mags/EPS)*EPS;
N1=find(abs(mags-Mmax)<EPS/2);

hi=histc(M,mags-eps/2); % Check the rounding
H=flipud(cumsum(flipud(hi)));
N2=numel(H(H>=4));

N=min(N1,N2);
end

% magnitude distribution to be tested
if strcmp(MTdistribution,'exp')==1;MTdist='Exponential';
elseif strcmp(MTdistribution,'weibul')==1;MTdist='Weibull';
end

% Anderson-Darling Test for exponentiality
% Loop for different maggnitudes
for j=1:N

mmin(j)=Mmin+(j-1)*EPS;%mmin=round(mmin/EPS)*EPS;
m=M(M>=mmin(j)-EPS/2);    
    cou=0;
for i=1:trials
[beta]=beta_AK_UT_Mbin(mmin(j),m,Rp);
[m_corr]=korekta(m,mmin(j),EPS,beta);

Mag=m_corr-min(m)+EPS/2;

[h1, p1]=adtest(Mag,'Distribution',MTdistribution);
h(i)=h1;p(i,j)=p1;

if p1<=0.0005+eps;cou=cou+1;else, cou=0;end
if cou==2;p(i:trials,j)=0.0005;break;end

end
%j

NN(j)=numel(m);bval(j)=beta/log(10);sb(j)=bval(j)/sqrt(NN(j));
P(j)=mean(p(:,j));
S(j)=std(p(:,j));  % p-values are not normally distributed!!
clear b
end

%mean(p),std(p)
%subplot(2,1,1);hist(h);xlim([-1 2]);subplot(2,1,2);histogram(p,0:0.05:1.0);
%hold on;plot([mean(p) mean(p)],[0 200],'r--','LineWidth',2);

%PLOTTING
EPS2=0.1;
xa=min(M):EPS2:max(M);xa1=[min(mmin) max(mmin)];

subplot(2,1,1);plot(mmin,P,'ro-','MarkerSize',12,'LineWidth',1,'MarkerFaceColor',[0.33 0.66 0.99]);
hold on;plot([min(M)-EPS max(M)],[0.05 0.05],'k--','Linewidth',2);xlim([min(M)-EPS max(M)]);
ylabel('p-value','FontSize',16);
if strcmp(MTdistribution,'exp')==1;title('AD Test for Exponentiality','FontSize',16);
elseif strcmp(MTdistribution,'weibul')==1;title('AD Test for Weibull Distribution','FontSize',16);
end  
subplot(2,1,2);histogram(M,numel(xa),'FaceColor',[0.7 0.7 0.8]);set(gca,'YScale','log');hold on
xlim([min(M)-EPS max(M)]);xlabel('Data','FontSize',16);ylabel('N','FontSize',16);


for j=1:N
pval(j).p=p(:,j);
pval(j).mmin=mmin(j);
pval(j).P=P(j);
end

for i=1:length(P);
    if P(i)<0.05;h_decision{i,1}='rejected';
    else h_decision{i,1}='not_rejected';
    end
end
%h_decision=h_decision';

RES=[mmin' NN' bval' P' S']
%SAVE OUTPUTS
cd Outputs_ADTestMag
SaveOuts(EPS,Mmin,Mmax,trials,RES,h_decision,MTdist)
%saveas(gcf,'Exponentiality_Output.jpg')
print(gcf,'ADTestMag_Output.jpeg','-djpeg','-r300')
cd ../

end

%% ******************************************************************
% *************************** FUNCTIONS ***************************
% ****-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-****
%% Select Mc and filter parameters for M>=Mc  
% [OK!!!!!!!!!!!!!!]
function [Cmag,Mc,EPS,MTdist]=FiltMcVector(vector)
Cmag=vector;

sm=sort(Cmag); sm1=sm(2:length(sm))-sm(1:length(sm)-1); EPS1=min(sm1(sm1>0));clc
if EPS1<0.01;warning('Data round-off interval is too small, please consider setting a higher value (e.g. 0.1)');end
EPS=dialog1('Data round-off interval',{num2str(EPS1)});

ar=min(Cmag):0.1:max(Cmag);
fig_Mc=figure;histogram(Cmag,length(ar));set(gca,'YScale','log')
title('Please Select Data Cutoff','FontSize',14);xlabel('M','FontSize',14),ylabel('Log_1_0N','FontSize',14)
% Select events above Mc
[Mc,N]=ginput(1);Mc=floor(Mc/EPS)*EPS,close(fig_Mc);

%Cmag=Cmag(Cmag>=Mc);

 % select magnitude distribution to be tested
[MTd,ok]=listdlg('PromptString','Select field:',...
    'ListString',{'Exponential','Weibul'},'SelectionMode','single')
if MTd==1;MTdist='exp';else; MTdist='weibul';end 

end
%% --------------------------------------------------------------------------------------
function [ou]=dialog1(name,defaultanswer)

prompt=['\fontsize{12} Please enter ',name, ':'];
prompt={prompt};
numlines=1; opts.Interpreter='tex';
 ou=inputdlg(prompt,'Parameter Setting',numlines,defaultanswer,opts);ou=str2double(ou{1});

end
%% --------------------------------------------------------------------------------------
function [beta]=beta_AK_UT_Mbin(Mmin,m,Rp)
%
%   m - magnitude vector
%   Mmin - completeness magitude threshold
%   beta - beta value. b(G-R)=beta/log(10)
%   Rp - Rounding precision, (1 - one decimal, 2 - two decimals, etc)
beta=1/(mean(m)-(Mmin-0.5*10^(-Rp)));
end

%% --------------------------------------------------------------------------------------
% Magnitude randomization
%
function [m_corr]=korekta(m,Mmin,EPS,beta)
%
%   m - magnitude vector
%   Mmin - completeness magitude threshold
%   beta - beta value. b(G-R)=beta/log(10)
% EPS - magnitude round-off interval
%
% m_corr - randomized magnitude vector
%
F1=1-exp(-beta*(m-Mmin-0.5*EPS));
F2=1-exp(-beta*(m-Mmin+0.5*EPS));
u=rand(size(m));
w=u.*(F2-F1)+F1;
m_corr=Mmin-log(1-w)./beta;
end
%% --------------------------------------------------------------------------------------------------------
% --------------------------------------- SAVE OUTPUTS in the report file ---------------------------------------
% Save Outputs
function SaveOuts(EPS,Mmin,Mmax,trials,RES,h_decision,MTdist)
% ---- Save *.txt file with Parameters Report ----
%cd Outputs/
fid=fopen('REPORT_ADTestMag.txt','w');
fprintf(fid,['PARAMETERS & RESULTS from DISTRIBUTION TESTING (created on ', datestr(now),')\n']);
fprintf(fid,'------------------------------------------------------------------------------------\n');
fprintf(fid,['<Round-off interval              >: ', num2str(EPS),'\n']);
fprintf(fid,['<Data Distribution tested        >: ', MTdist,'\n']);
fprintf(fid,['<Data Range for Analysis         >: ', num2str(Mmin), ' to ', num2str(Mmax),'\n']);
fprintf(fid,['<Number of randomization trials  >: ', num2str(trials),'\n']);
fprintf(fid,'------------------------------------------------------------------------------------\n');

fprintf(fid,['Mmin    N    b-value   mean(p)    std(p)    Decision for H0 \n']);
fprintf(fid,['                       from ',num2str(trials),' trials    (0.05 significance)\n']);
for j=1:size(RES,1);
fprintf(fid,['%5.2f %6d  %5.3f    %5.4f     %5.3f      %s  \n'],RES(j,:),h_decision{j});
end
fclose(fid);

%saveas()

% Save Output Structure
%  prompt={'\fontsize{12} Please enter output file name:'};
%  name='Extract Output Structure';
%     numlines=1;
%     defaultanswer={'Tdata.mat'};
%   opts.Interpreter='tex';
%   answer=inputdlg(prompt,name,numlines,defaultanswer,opts);
% save(char(answer),'Tdata')
% cd ../

end




