SERA Toolbox1 and Toolbox2 standalone versions

This commit is contained in:
2019-07-05 10:31:31 +02:00
parent 6f5c52a565
commit 758751a7b0
71 changed files with 54733 additions and 3 deletions

View File

@@ -0,0 +1,10 @@
Welcome to the Magnitude Complexity Toolbox!
VERSION_1 is a Interactive Standalone mode.
VERSION_2 is a Function Standalone mode.
please refer to the "READ_ME******.docx" file in each directory for further
instructions on the step-by-step implementation of the applications as well
as for data requirements and general tips.
ENJOY!

View File

@@ -0,0 +1,355 @@
% 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

View File

@@ -0,0 +1,880 @@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
0.9
1.2
2.2
2.9
2.5
2.0
2.1
2.0
1.8
2.0
2.1
2.0
1.5
1.5
2.7
1.8
2.6
1.4
1.9
2.6
1.5
2.1
1.6
2.2
2.0
1.5
2.1
1.8
1.9
2.0
1.8
0.9
2.0
4.6
3.6
1.6
1.4
1.3
2.0
2.9
1.3
2.3
1.7
1.5
3.1
1.8
1.4
1.7
2.9
1.9
1.2
3.0
1.7
2.5
1.3
4.2
1.4
1.6
2.2
2.2
1.5
1.6
1.7
1.7
1.3
2.1
3.1
2.6
1.6
1.5
1.7
1.0
1.0
1.9
1.4
1.2
0.9
2.2
1.6
1.4
2.2
2.2
1.2
1.5
1.2
1.9
1.3
2.1
0.9
1.2
1.3
1.5
1.5
2.4
2.4
2.3
1.9
2.0
2.2
0.8
1.8
1.9
1.1
1.2
1.5
3.4
1.6
1.5
1.4
0.9
1.7
1.5

View File

@@ -0,0 +1,22 @@
function ADTestMag_Plot(M,mmin,P,EPS,MTdistribution)
clf
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);legend('p-value','0.05 line','Location','best');
if strcmp(MTdistribution,'exp');title('AD Test for Exponential Distribution','FontSize',16)
elseif strcmp(MTdistribution,'weibul');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);
title('Event Counts','FontSize',16)
cd Outputs_ADTestMag\;saveas(gcf,'ADTestMag_output.jpg');cd ../
end

View File

@@ -0,0 +1,266 @@
% FUNCTION: ADTestMag
% VERSION: [Wrapper Standalone Version] V2.8
% COMPATIBLE with Matlab version 2017b or later
% TOOLBOX: "Magnitude Complexity Toolbox" within SERA Project
% DOCUMENT: "READ_ME_App_2A_v2_Description_ADTestMag.docx"
% --------------------------------------------------------------------------------------------------------
% Test performed for the distribution of a given dataset (time-series)
% the distributions to be tested are Exponential and Weibull
% ------------------------------------------------------------------------------------------------------
% OVERVIEW: This Application is a Matlab function which performs the
% Anderson-Darling (AD) test for testing the null hypothesis whether a given
% set of magnitudes, follows the Exponential or Weibull distribution. Please
% check also to the "ADTestMag_wrapper" and "ADTestMag_Plot" scripts
% for a specific application (scenario) and plotting results.
% -----------------------------------------------------------------------------------------------------
% AUTHORS: K. Leptokaropoulos, and P. Urban
% last updated: 01/2019, within SERA PROJECT, EU Horizon 2020 R&I
% programme under grant agreement No.730900
% CURRENT VERSION: v2.8 **** [Wrapper Standalone Version]
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PLEASE refer to the accompanying document:
% "READ_ME_App_2A_v2_Description_ADTestMag.docx"
% for description of the Application and its requirements.
% ------------------------------------------------------------------------------------------------------
% DESCRIPTION:
% This function performs the Anderson-Darling test (e.g. Anderson & Darling,
% 1954; Marsaglia & Marsaglia, 2004) for testing the Null Hypothesis, H0, that
% a given set of magnitudes, follows the exponential or the weibull distribution
% This is accomplished as a function on minimum magnitude, therefore, multiple
% results are produced (iteration process). The corresponing p-values for the H0
% is the main output of the program. Before applying the AD test, the magnitude
% values are randomized within their round-off interval, following the formula
% introduced by Lasocki and Papadimitriou, 2006.
% -----------------------------------------------------------------------------------------------------
% ITERATION PROCESS:
% The AD test is perform with an iteration process for a variety of magnitude
% ranges. First the funstion is executed for magnitudes M, with Mmin<M<M1
% where M1 is the higher Magnitude in the selected Catalog. Then, the process
% is repeated for Mmin+EPS<M<M1, then for Mmin+2*EPS<M<M1, etc till
% Mmax<M<M1. Please see INPUT section for reference to the symbols.
% -----------------------------------------------------------------------------------------------------
% INPUT: The function takes as input any matlab vector (input parameter "M").
% The input data can be uploaded by the use of "ADTestMag_wrapper"
% script, from an ASCII file (e.g. *.txt). Such file should contain a vector
% (raw or column) of the Data that the User wishes to process. The User
% is afterwards requested to enter values for some additional parameters.
% Input Parameters Overview:
% --- M: Time-Series (e.g. Magnitude) vector, read i.e. from an ASCII
% file.
% --- 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)
% --- MTdistribution: The User is requested to define the selected
% parameter distribution for which the null hypothesis is to be
% tested. Possible values: 'exp' for 'Exponential' and 'weibul' for
% 'Weibull' distribution.
% --- Mmin: Corresponds to the minumum input vector value (e.g.
% magnitude) for which the AD test is performed (cut-off value).
% For the magnitude case, it is recommended (yet, not restricted)
% to be equal to the catalog completeness threshold, Mc (if known).
% --- Mmax: Corresponds to the maximum input vector value (e.g.
% magnitude) to be considered as minimum thresohold for the
% AD test to be performed. The maximum value is the one for
% which the number of events with M>=Mmax is greater than
% 4 (i. e. the minimum possible sample for ADTestMag function
% exectution). The function automatically finds this value when the
% User sets a higher one.
% --- trials: Number of randomization realizations (trials) perfomed
% in order to diminish the influence of magnitude randomization
% on the resulting p-value. Recommended value: 100.
% ------------------------------------------------------------------------------------------------------
% OUTPUT:
% --- Output Report with parameters used and results
% ('Output_ADTestMag.txt file')
% --- Output Parameters:
% * P - The average of the p-values obtained by the defined
% number of trials performed (also exists within the
% output structure 'pval')
% * S - The corresponding standard deviation of the
% p-values obtained by the defined number of trials
% * pval - Structure with the vectors of p-values obtained by the
% defined number of trials for each minimum magnitude.
% The structure also contais parameters 'P' and 'mmin'.
% [NOTE: histograms of such p-values indicate that their
% distribution is not normal, neither even symmetric
% for a large number of cases]
% * mmin - vector with the minimum magnitudes to which
% the aforementioned output parameters correspond
% (also exists within the output structure 'pval')
% * NN - vector with number of events corresponding to
% each mmin value.
% * bval - b-value corresponding to each set defined by the
% aforementioned mmin values
% --- Output Figures:
% ____ [after running the auxiliary "ADTestMag_Plot" script] _____
% - Figure with average p-value (P parameter) as a function of
% mmin, together with a histogram of the events count per
% magnitude bin. 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.
% -- Anderson T. W., and D. A. Darling, (1954), "A test of goodness of fit",
% J. Amer. Stat. Assoc., 49, 765-769.
% -- Marsaglia, G., and J. Marsaglia (2004), "Evaluating the Anderson-Darling
% distribution", J. Stat. Soft., 9, 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.
% ----------------------------------------------------------------------------------
function [pval,mmin,NN,P,S,bval]=ADTestMag_V2_8(M,EPS,MTdistribution,Mmin,Mmax,trials)
mkdir Outputs_ADTestMag
% Constrain mangitude limits within data range
if Mmin<min(M);Mmin=min(M);end
if Mmax>max(M);Mmax=max(M);end
% INPUT PARAMETERS ARE SPECIFIED AS FUNCTION ARGUMENTS
M=round(M/EPS)*EPS;
Mmin=floor(Mmin/EPS)*EPS;MMax=ceil(max(M)/EPS)*EPS;
Rp=round(-log10(EPS));
mags=Mmin:EPS:MMax;mags=round(mags/EPS)*EPS;
N=find(abs(mags-Mmax)<EPS/2);
% 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 magnitudes
for j=1:N
mmin(j)=Mmin+(j-1)*EPS;%mmin=round(mmin/EPS)*EPS;
m=M(M>=mmin(j)-EPS/2); % numel(m)
if numel(m)<4;warning(['insufficient events number for M>',num2str(mmin(j)-EPS)]);
N=j-1;mmin=mmin(1:N);Merr=1;break
else
Merr=0;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
end
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
% creating results for the report
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,Merr)
%saveas(gcf,'Exponentiality_Output.jpg')
%print(gcf,'Exponentiality_Output.jpeg','-djpeg','-r300')
cd ../
end
%% ******************************************************************
% *************************** FUNCTIONS ***************************
% ****-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-****
%% --------------------------------------------------------------------------------------
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,Merr)
% ---- 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
if Merr==1;fprintf(fid,['insufficient events number for M>',num2str(RES(j,1))]);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

View File

@@ -0,0 +1,39 @@
% This is a Wrapper Script for Performing the Anderson-Darling Test for testing
% the hypothesis that a given time-series (e.g. magnitude) follows the exponential
% or weibull distribution. The Matlab function "ADTestMag_v2_*.m" is executed for
% this purpose.The description of the function can be found in the comments within
% 'ADTestMag_V2_*.m' code. Here, the input data and parameters (i.e. arguments
% of the Function) are defined by the User. Please modify parameters in the script
% accordingly, the lines that can be modified are followed by a comment "- PLEASE SET".
% PLEASE REFER ALSO TO APPLCATION DOCUMENTATION:
% "READ_ME_App_2B_v2_Description_MM_MB.docx"
clc;clear
% STEP 1. DATA Upload:
cd Sample_Data % PLEASE Specify data directory path
vector=dlmread('test_vector.txt'); % PLEASE SET data (magnitude) vector input file
cd ../
% STEP 2. Minimum Vector Value Selection:
Mmin=0.0; % PLEASE SET
% STEP 3. Maximum Vector Value Selection:
Mmax=5.0; % PLEASE SET
% STEP 4. Parameter Round-off Interval:
EPS=0.1; % PLEASE SET
% STEP 5. Define number of Trials:
trials=100; % PLEASE SET (interger)
% STEP 6. Distribution Selection:
MTdistribution='exp'; % PLEASE SET ('exp' or 'weibul')
% STEP 7. RUN Function ['ADTestMag']
[pval, mmin, NN ,P ,S, bval]=ADTestMag_V2_8(vector,EPS,MTdistribution,Mmin,Mmax,trials);
% STEP 8. Optional: plotting results %PLEASE Comment next line to deactivate plotting
ADTestMag_Plot(vector,mmin,P,EPS,MTdistribution)

View File

@@ -0,0 +1,880 @@
1.7
0.8
1.8
1.5
1.7
0.8
1.7
2.1
0.9
1.3
1.1
1.9
2.3
1.7
1.9
1.4
1.5
1.6
1.5
2.7
2.9
1.3
1.2
0.9
2.7
1.4
1.2
1.4
1.7
1.5
1.4
1.5
2.4
2.7
1.7
2.1
1.0
1.7
2.3
1.3
1.7
1.2
1.8
1.5
1.7
1.1
1.6
1.3
1.1
1.3
1.2
1.1
2.1
0.9
1.5
1.5
0.9
2.1
1.5
2.0
3.4
2.7
2.1
1.8
2.1
1.4
2.6
0.8
1.5
4.1
1.7
2.0
2.1
1.7
2.1
2.0
1.7
2.2
1.7
2.1
2.2
1.5
3.6
2.0
1.5
1.5
2.0
1.8
1.3
1.5
2.8
1.3
1.5
1.9
1.5
2.4
1.6
0.5
1.8
1.4
1.6
1.5
2.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1
1.2
1.5
3.4
1.6
1.5
1.4
0.9
1.7
1.5

View File

@@ -0,0 +1,701 @@
% FUNCTION: MM_MB
% 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_2B_v1_Description_MM_MB.docx"
%--------------------------------------------------------------------------------------------------------------
%% EXAMPLE TO RUN:
% x=exprnd(log10(exp(1)),1000,1);
% [n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps,gau,gau_b,poch,poch2,zer1,zer2,x1,x2]...
% =MM_MB_V1_8(x,0.2,500,500,0.01,0.001,'Efron','Silverman') % for Interactivity OFF
% [n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps,gau,gau_b,poch,poch2,zer1,zer2,x1,x2]...
% =MM_MB_V1_8(x); % for Interactivity ON
%% ----------------------------------------------------------------------------------------------------------
% Test performed for Magnitude Distribution of a given dataset checking
% whether PDF demonstrates multi-modes/ multi-bumps
% --------------------------------------------------------------------------------------------------------
% INPUT: THE CURRENT VERSION USES AS INPUT ANY MAGNITUDE VECTOR
% (Appropriate for standalone use - function mode)
% --------------------------------------------------------------------------------------------------------
% OVERVIEW: This Application is a Matlab function which performs testing
% of hypotheses of 1) multimodality and 2) existence of multi-bumps in a given
% magnitude 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_2B_v1_Description_MM_MB.docx"
% for a description of the Application and its requirements.
% --------------------------------------------------------------------------------------------------------
% DESCRIPTION:
% The function studies the magniutude distribution complexity by means of the
% Mulitmodality Test (Silverman, 1986; Efron and Tibshirani, 1993). Two null
% hypotheses (H0s) are tested:
% H01 - multimodality: The magnitude PDF is unimodal
% H02 - multi-bump: The magnitude PDF has one bump to the right of the
% mode.
% A mode is a local maximum of probability density and a bump is an interval
% [a,b] such that the probability density is concave over [a,b] but not over any
% larger interval (Silverman, 1986). The importance of modes and bumps relies
% on the fact that multiple occurrences of these features in a PDF indicate, for
% most standard densities, a mixing of components (e.g. Cox, 1966).
% --------------------------------------------------------------------------------------------------------
% INPUT: If the User Provides only the magnitude vector as input, then the
% function enables a GUI allowing the User to interactivelly set the rest
% of the input parameters by the User. Otherwise the User has to define
% the 7 input parameters in addition to the input magnitude vector.
% PLEASE REFER TO "MM_MB.doc" FOR A COMPREHENSIVE
% PARAMETER DESCRIPTION
% Input Parameters Overview:
% --- M: A Time-Series (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", located in the "Sample_Data"). Such file
% should contain a vector (raw or column) of the Data the
% User wishes to process. The User is afterwards requested
% to enter parameters values:
% --- Mc: Corresponds to the catalog completeness threshold.
% --- m: number of points to divide magnitude sample, default 100,
% recommended 100-1000.
% % INPUTS for MODE and BUMP HUNTING
% - n_boot: Number of bootstrap iterations for both MM and MB
% (default: 100)
% - delta_h: Smoothing parameter step for successive trials in defining
% the critical h for MM and MB process (h-critical accuracy)
% (default: 0.001)
% - h : initial value of the smoothing factor to apply in defining the
% critical h for the MM testing process (default:0.01)
% - MMmeth: Method for multimodality Testing. Possible values:
% 'Efron', 'Silverman'
% - MBmeth: Method for multibumps Testing. Possible values:
% 'Silverman', 'Efron'
% NOTE: The initial value for MB process is taken equal to the
% critical value defined in the MM process minus its
% accuracy (i.e. hcrit_Modes-delta_h, see OUTPUT below)
% ---------------------------------------------------------------------------------------------------------
% OUTPUT:
% - Output Report with data and parameters used
% ('Output_MM_MB.txt file')
% - Output Parameters:
% % DOUBLES:
% * n - Number of observations used
% * bval- b-value of the G-R law
% * Rmodes - The estimated significance of null hypothesis (H01)
% * hcrit_modes - estimated critical smoothness parameter for MM test
% * Rbumps - The estimated significance of null hypothesis (H02)
% * hcrit_bumps - estimated critical smoothness parameter for MB test
% * zer1 - point where 1st derivative is zero (extremum)
% * zer2 - point(s) where 2nd derivative is zero (inflection points)
% [NOTE: zer2 can be a vector]
% % VECTORS:
% * gau: Data vector PDF estimated for h critical from Multimodality
% * gau_b: Data vector PDF estimated for h critical from Multibumps
% * poch: 1st derivative of Data vector PDF for h critical from Multibumps
% * poch2: 2nd derivative of Data vector PDF for h critical from Multibumps
% * x1,x2: minimum and maximum Data points after randomization
% (parameters needed for plotting)
% - Output Figures:
%
% ---------------------------------------------------------------------------------------------------------
% REFERENCES:
% -- Cox, D. R., (1966), Notes on the analysis of mixed frequency distributions.
% Br. J. Math. Stat. Psychol. 19, 39-47, doi.org/10.1111/j.2044-8317.1966.tb00353.x.
% -- Efron, B., and Tibshirani R.J. (1993) An Introduction to the Bootstrap,
% CRC Press, Boca Raton, Fla.
% -- Lasocki S. and E. E, Papadimitriou (2006), "Magnitude distribution
% complexity revealed in seismicity from Greece", J. Geophys. Res.,
% 111, B11309, doi:10.1029/2005JB003794.
% -- Silverman, B. W., (1986), Density estimation for statistics and data analysis,
% CRC press, Boa Raton, Fla.
% ---------------------------------------------------------------------------------------------------------
% 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);
% [n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps,gau,gau_b,poch,poch2,zer1,zer2,x1,x2]...
% =MM_MB_V1_8(x,0.2,500,500,0.01,0.001,'Efron','Silverman') % for Interactivity OFF
% [n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps,gau,gau_b,poch,poch2,zer1,zer2,x1,x2]...
% =MM_MB_V1_8(x); % for Interactivity ON
%% ----------------------------------------------------------------------------------------------------------
function [n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps,gau,gau_b,poch,poch2,zer1,zer2,x1,x2]...
=MM_MB_V1_8(M,Mc,m,n_boot,h,delta_h,MMmeth,MBmeth)
mkdir Outputs_MM_MB
% define round-off interval and select data above Mc
sm=sort(M);dm=sm(2:length(sm))-sm(1:length(sm)-1);
dm=dm(dm>0);eps_M=min(dm);
%% --------------------------------------- INTERACTIVE MODE ----------------------------------------
if nargin==1
[Mi,Mmin]=FiltMcVector(M,eps_M);
M=Mi(Mi>=Mmin-eps_M/2);
m=dialog1('number of points to divide sample',{'100'});
Rp=round(-log10(eps_M)); %% check the RP parameter
[beta]=beta_AK_UT_Mbin (Mmin,M,Rp) ;
% Randomize magnitude
m_corr(:,1) = korekta(M,Mmin,eps_M,beta);
% SETTING PARAMETERS
n=length(m_corr);
x1=floor(min(m_corr)*10)/10; %nieco mniej ni<EFBFBD> min(m) zaokr<EFBFBD>glone do jednej dziesi<EFBFBD>tej
x2=ceil(max(m_corr)*10)/10; %nieco wi<EFBFBD>cej ni<EFBFBD> max(m) zaokr<EFBFBD>glone do jednej dziesi<EFBFBD>tej
% MULTIMODALITY TESTING
% Give number of bootstrap attempts and h values
[n_boot,delta_h,h]=dialog2('Multimodality',{'100','0.001','0.01'});
n_boot1=n_boot;
%delta_h=0.1;%input('czynnik wygladzajacy '); %0.1
%h=0.01; %0.001
[hcrit,gau]=critical_smoothing(m_corr,n,delta_h,x1,x2,m,h);
hcrit;
% Select Method of Multimodality testing 'Silverman' or 'Efron'
clc;
disp('Please Select Method for Multimodality Testing: ');
disp('(1 - for Efron, 2 - for Silverman)');
MM=input(' ');
% ---------testing plot-----------
plot(1:m,gau);title('Data density for critical smoothing factor (h_c_r_i_t)');
disp('press any key ...')
pause
close all
% ---------testing plot-----------
% Run Multimodality testing
switch MM
case 1
[R] = test_multimodality_e (m_corr,n,x1,x2,m,hcrit,n_boot1);
case 2
[R] = test_multimodality (m_corr,n,x1,x2,m,hcrit,n_boot1);
end
% ------------------------------------------------------------------------
% [zer1]=zera_1st(m_corr,n,x1,x2,m,hcrit-delta_h);
% ------------------------------------------------------------------------
% BUMP HUNTING
% Give number of bootstrap attempts and h values
[n_boot,delta_h,h]=dialog2('Multibumps',{'100','0.001',num2str(hcrit)});
n_boot2=n_boot;
%delta_h=0.1;
[hcrit_bumps,gau_b,poch,poch2]=critical_smoothing_bumps(m_corr,n,delta_h,x1,x2,m,hcrit-delta_h);
hcrit_bumps;
% Select Method for testing the bump size 'Silverman' or 'Efron'
disp(' ');disp(' ');disp(' ');
disp('Please Select Method for Bump Testing: ');
disp('(1 - for Silverman, 2 - for Efron)');
BM=input(' ');
% Run Bump hunting
switch BM
case 1
[Rb] = bump_hunt (m_corr,n,x1,x2,m,hcrit_bumps,n_boot2);
case 2
[Rb] = bump_hunt_e (m_corr,n,x1,x2,m,hcrit_bumps,n_boot2);
end
%% -------------------------------------- FUNCTION MODE ----------------------------------------
elseif nargin>1
% INPUT PARAMETERS ARE SPECIFIED AS FUNCTION ARGUMENTS
MM=M;Mmin=Mc;M=M(M>=Mmin-eps_M/2);
m;n_boot;h;delta_h;MMmeth;MBmeth;
Rp=round(-log10(eps_M)); %% check the RP parameter
[beta]=beta_AK_UT_Mbin (Mmin,M,Rp); % beta value
% Randomize magnitude
m_corr(:,1) = korekta(M,Mmin,eps_M,beta);
% SETTING PARAMETERS
n=length(m_corr);
x1=floor(min(m_corr)*10)/10; %nieco mniej ni<EFBFBD> min(m) zaokr<EFBFBD>glone do jednej dziesi<EFBFBD>tej
x2=ceil(max(m_corr)*10)/10; %nieco wi<EFBFBD>cej ni<EFBFBD> max(m) zaokr<EFBFBD>glone do jednej dziesi<EFBFBD>tej
% MULTIMODALITY TESTING
[hcrit,gau]=critical_smoothing(m_corr,n,delta_h,x1,x2,m,h);
switch MMmeth
case 'Efron'
[R] = test_multimodality_e (m_corr,n,x1,x2,m,hcrit,n_boot);
case 'Silverman'
[R] = test_multimodality (m_corr,n,x1,x2,m,hcrit,n_boot);
end
% BUMP HUNTING
[hcrit_bumps,gau_b,poch,poch2]=critical_smoothing_bumps(m_corr,n,delta_h,x1,x2,m,hcrit-delta_h);
switch MBmeth
case 'Silverman'
[Rb] = bump_hunt (m_corr,n,x1,x2,m,hcrit_bumps,n_boot);
case 'Efron'
[Rb] = bump_hunt_e (m_corr,n,x1,x2,m,hcrit_bumps,n_boot);
end
end
bval=beta/log(10);
% Display the Results:
re=table(n, bval, R,hcrit,Rb,hcrit_bumps)
disp(['Significance of Ho(1) that input data distribution is unimodal is: ', num2str(R)])
disp(['Significance of Ho(2) that input data distribution has no more '])
disp(['than one bumps to the right of the global maximum is: ', num2str(Rb)])
[zer1]=zera_1st(m_corr,n,x1,x2,m,hcrit_bumps-delta_h); % use the same h in both cases???
[zer2]=zera_2nd(m_corr,n,x1,x2,m,hcrit_bumps-delta_h);
%% -------------------------- PLOTTING AND SAVING RESULTS ----------------------------
% ----- plotting magnitude density and its derivatives ------
xp=linspace(x1,x2,m);
%st=range([x1 x2])/(m-1);xp=x1:st:x2;
figure('rend','painters','pos',[800 100 600 900]);hold on
subplot(3,1,1);
plot(xp,gau_b,'LineWidth',2);hold on;
for i=1:length(zer1)
Le1=plot([zer1(i) zer1(i)],[0 10],'k--','LineWidth',1);
end
for i=1:length(zer2)
Le2=plot([zer2(i) zer2(i)],[0 10],'r--','LineWidth',1);
end
xlim([min(M)-0.1 max(M)+0.1]);ylim([0 max(gau_b)+0.01])
title(['Data density for h_c_r_i_t ';' (critical smoothing factor) '],'FontSize',14);%axis square
legend([Le1,Le2],{'extremum','Inflection Point(s)'});ylabel('PDF','FontSize',14)
subplot(3,1,2);
plot(xp,poch,'LineWidth',2);ylabel('1^s^t derivative','FontSize',14);hold on;%axis square;
plot([xp(1) xp(length(xp))],[0 0],'--','LineWidth',1)
for i=1:length(zer1)
plot([zer1(i) zer1(i)],[min(poch)-0.01 max(poch)+0.01],'k--','LineWidth',1)
end
for i=1:length(zer2)
plot([zer2(i) zer2(i)],[min(poch)-0.01 max(poch)+0.01],'r--','LineWidth',1)
end
xlim([min(M)-0.1 max(M)+0.1]);ylim([min(poch)-0.01 max(poch)+0.01])
subplot(3,1,3);
plot(xp,poch2,'LineWidth',2);ylabel('2^n^d derivative','FontSize',14);%axis square
hold on; plot([xp(1) xp(length(xp))],[0 0],'--','LineWidth',1)
for i=1:length(zer1)
plot([zer1(i) zer1(i)],[min(poch2)-0.01 max(poch2)+0.01],'k--','LineWidth',1)
end
for i=1:length(zer2)
plot([zer2(i) zer2(i)],[min(poch2)-0.01 max(poch2)+0.01],'r--','LineWidth',1)
end
xlim([min(M)-0.1 max(M)+0.1]);ylim([min(poch2)-0.01 max(poch2)+0.01])
xlabel('Data','FontSize',14);
% Save Outputs
cd Outputs_MM_MB
Rmodes=R;Rbumps=Rb;hcrit_modes=hcrit;
SaveOuts(eps_M,Mmin,m,n_boot,n_boot,n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps)
saveas(gcf,'Multimodality_Output.jpg')
cd ../
%% ******************************************************************
% *************************** FUNCTIONS ***************************
% ****-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-****
%% Select Mc and filter parameters for M>=Mc
function [Cmag,Mc]=FiltMcVector(magnitude_vector,EPS1)
Cmag=magnitude_vector;
%sm=sort(Cmag); sm1=sm(2:length(sm))-sm(1:length(sm)-1); EPS1=min(sm1(sm1>0));
%EPS=dialog1('magnitude 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/EPS1)*EPS1,close(fig_Mc);
%Cmag=Cmag(Cmag>=Mc);
end
%% --------------------------------------------------------------------------------------
%% --------------------------------------------------------------------------------------
% finds a field defined by a certain (string) name
function [id] = findfield( catalog,field )
id=0;
j=1;
while j <= size(catalog,2) && id==0
if (strcmp(catalog(j).field,field)==1)
id=j;
end
j=j+1;
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=str2num(ou{1});
end
%% --------------------------------------------------------------------------------------
function [ou1, ou2, ou3]=dialog2(titl,defaultanswer)
prompt={'\fontsize{12} Please enter number of bootstrap iterations:','\fontsize{12} Please enter \Deltah (h_c_r_i_t accuracy):','\fontsize{12} Please enter h (smoothing factor):'};
name=['Parameters for ',titl, ':'];
numlines=1;
opts.Interpreter='tex';
answer=inputdlg(prompt,name,numlines,defaultanswer,opts);
ou1=answer(1);ou2=answer(2);ou3=answer(3);
ou1=str2num(ou1{1});ou2=str2num(ou2{1});ou3=str2num(ou3{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
%% --------------------------------------------------------------------------------------
% Finding critical smoothing parameter value for which the non parametric
% PDF demonstrates one mode in the selected interval
%
function [hcrit,gau]=critical_smoothing(mm,n,delta_h,x1,x2,m,h)
%
x=sort(mm);
p=2.0;
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,m);
%h=input('Initial smoothing factor ');
while p>1
for i=1:m,
poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./h).^2));
end
p=0;
for i=2:m,
if poch(i-1)*poch(i)<0
p=p+1;
end
end
h=h+delta_h;
end
hcrit=h-delta_h;
for i=1:m,
gau(i)=sum(exp(-0.5*((y(i)-x)./hcrit).^2))/c/n/hcrit;
end
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
%
function [R]=test_multimodality(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_wielom=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
z=x(ind)+h_crit.*eps;
z=sort(z);
poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch(i-1)*poch(i)<0
p=p+1;
end
end
if p>1
no_wielom=no_wielom+1;
end
end
R=no_wielom/mm;
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
% EFRON
%
function [R]=test_multimodality_e(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
s2=sqrt(1+h_crit^2/var(x));
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_wielom=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
me=mean(x(ind));
z=me+(x(ind)-me+h_crit.*eps)./s2;
z=sort(z);
poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch(i-1)*poch(i)<0
p=p+1;
end
end
if p>1
no_wielom=no_wielom+1;
end
end
R=no_wielom/mm;
end
%% --------------------------------------------------------------------------------------
% Finding critical smoothing parameter value for which the non parametric
% PDF demonstrates one bump in the selected interval
%
function [hcrit,gau,poch,poch2]=critical_smoothing_bumps(mm,n,delta_h,x1,x2,m,h)
%
x=sort(mm);
p=2.0;
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,m);
%h=input('Initial smoothing factor ');
while p>1
for i=1:m,
% poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./h).^2));
poch2(i)=sum((((y(i)-x)./h).^2-1).*exp(-0.5*((y(i)-x)./h).^2));
end
p=0;
for i=2:m,
if poch2(i-1)*poch2(i)<0
p=p+1;
end
end
h=h+delta_h;
end
hcrit=h-delta_h;
for i=1:m,
gau(i)=sum(exp(-0.5*((y(i)-x)/hcrit).^2))/c/n/hcrit;
poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./hcrit).^2));
poch2(i)=sum((((y(i)-x)./hcrit).^2-1).*exp(-0.5*((y(i)-x)./hcrit).^2));
end
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
%
function [R]=bump_hunt(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_bump=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
z=x(ind)+h_crit.*eps;
z=sort(z);
% poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
poch2(1)=sum((((y(1)-z)./h_crit).^2-1).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
% poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
poch2(i)=sum((((y(i)-z)./h_crit).^2-1).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch2(i-1)*poch2(i)<0
p=p+1;
end
end
if p>1
no_bump=no_bump+1;
end
end
R=no_bump/mm;
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
% EFRON
%
function [R]=bump_hunt_e(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
s2=sqrt(1+h_crit^2/var(x));
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_bump=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
me=mean(x(ind));
z=me+(x(ind)-me+h_crit.*eps)./s2;
z=sort(z);
% poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
poch2(1)=sum((((y(1)-z)./h_crit).^2-1).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
% poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
poch2(i)=sum((((y(i)-z)./h_crit).^2-1).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch2(i-1)*poch2(i)<0
p=p+1;
end
end
if p>1
no_bump=no_bump+1;
end
end
R=no_bump/mm;
end
%% --------------------------------------------------------------------------------------
% Determination of PDF first derivative zeros estimates in the selected interval
%
function [zer1]=zera_1st(mm,n,x1,x2,m,h)
%
x=sort(mm);
c=sqrt(2*pi);
y=linspace(x1,x2,m);
for i=1:m,
poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./h).^2));
end
p=0;
for i=2:m,
if poch(i-1)*poch(i)<0
p=p+1;
zer1(p)=(y(i)+y(i-1))/2; %K 23JAN2019
end
end
end
%% --------------------------------------------------------------------------------------
% Determination of PDF second derivative zeros in the selected interval
%
function [zer2]=zera_2nd(mm,n,x1,x2,m,h)
%
x=sort(mm);
c=sqrt(2*pi);
y=linspace(x1,x2,m);
for i=1:m,
poch2(i)=sum((((y(i)-x)./h).^2-1).*exp(-0.5*((y(i)-x)./h).^2));
end
p1=0;
for i=2:m,
if poch2(i-1)*poch2(i)<0
p1=p1+1;
zer2(p1)=(y(i)+y(i-1))/2; %K 23JAN2019
end
end
end
%% --------------------------------------------------------------------------------------------------------
% --------------------------------------- SAVE OUTPUTS in the report file ---------------------------------------
% Save Outputs
function SaveOuts(EPS,Mmin,m,n_boot1,n_boot2,n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps)
% ---- Save *.txt file with Parameters Report ----
%cd Outputs/
fid=fopen('REPORT_Multimodality.txt','w');
fprintf(fid,['RESULTS from MULTIMODALITY/MULTIBUMP TESTING (created on ', datestr(now),')\n']);
fprintf(fid,'------------------------------------------------------------------------------\n');
fprintf(fid,['<Round-off interval >: ', num2str(EPS),'\n']);
fprintf(fid,['<Number of points to divide the sample >: ', num2str(m),'\n']);
fprintf(fid,['<Completeness Threshold >: ', num2str(Mmin),'\n']);
fprintf(fid,['<Number of events used >: ', num2str(n),'\n']);
fprintf(fid,['<Gutenberg-Richter b-value >: ', num2str(bval,'%5.3f'),'\n']);
fprintf(fid,['<Number of bootstrap iterations (multimodality) >: ', num2str(n_boot1),'\n']);
fprintf(fid,['<Number of bootstrap iterations (multibumps) >: ', num2str(n_boot2),'\n']);
fprintf(fid,'------------------------------------------------------------------------------\n');
fprintf(fid,['<Critical Smoothing Parameter,h (for modes) >: ', num2str(hcrit_modes,'%6.4f'),'\n']);
fprintf(fid,['<Critical Smoothing Parameter,h (for bumps) >: ', num2str(hcrit_bumps,'%6.4f'),'\n']);
fprintf(fid,['<Significance of H01 that input data PDF is unimodal >: ', num2str(Rmodes,'%4.3f'),'\n']);
fprintf(fid,['<Significance of H02 that input data PDF has no more \n']);
fprintf(fid,[' than one bump to the right of the global maximum >: ', num2str(Rbumps,'%4.3f'),'\n']);
fclose(fid);
end
end

View File

@@ -0,0 +1,880 @@
1.7
0.8
1.8
1.5
1.7
0.8
1.7
2.1
0.9
1.3
1.1
1.9
2.3
1.7
1.9
1.4
1.5
1.6
1.5
2.7
2.9
1.3
1.2
0.9
2.7
1.4
1.2
1.4
1.7
1.5
1.4
1.5
2.4
2.7
1.7
2.1
1.0
1.7
2.3
1.3
1.7
1.2
1.8
1.5
1.7
1.1
1.6
1.3
1.1
1.3
1.2
1.1
2.1
0.9
1.5
1.5
0.9
2.1
1.5
2.0
3.4
2.7
2.1
1.8
2.1
1.4
2.6
0.8
1.5
4.1
1.7
2.0
2.1
1.7
2.1
2.0
1.7
2.2
1.7
2.1
2.2
1.5
3.6
2.0
1.5
1.5
2.0
1.8
1.3
1.5
2.8
1.3
1.5
1.9
1.5
2.4
1.6
0.5
1.8
1.4
1.6
1.5
2.2
1.4
1.5
1.9
2.5
3.2
2.9
1.0
1.4
1.8
2.0
2.2
1.4
1.6
1.8
2.0
1.3
2.8
1.4
1.3
1.1
1.8
1.6
1.1
0.8
1.4
1.0
1.7
1.3
1.5
1.7
3.2
1.9
1.6
2.1
1.6
1.7
1.9
1.6
2.6
1.2
2.3
2.1
2.1
1.8
1.4
1.1
1.9
3.3
1.4
1.6
1.8
1.7
2.4
1.6
1.7
2.2
2.9
2.7
1.3
2.2
1.4
1.9
1.6
1.4
2.0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
1.7
2.3
1.8
1.5
2.7
2.3
3.0
2.4
2.4
2.9
1.7
1.5
1.0
2.5
1.7
1.9
2.0
1.8
1.4
2.1
1.6
2.3
1.7
2.3
2.7
1.8
1.4
1.6
1.7
1.2
2.5
1.5
1.9
1.4
1.9
1.5
1.9
1.4
1.7
1.5
1.5
1.6
2.1
1.7
2.3
1.0
1.5
1.5
1.4
0.9
2.8
1.6
2.1
1.8
1.7
2.3
1.8
2.0
1.3
2.1
2.0
0.5
1.2
1.2
2.2
2.2
0.8
1.2
1.8
1.0
1.9
2.0
1.7
1.9
2.5
1.1
2.2
1.1
1.4
1.4
1.7
2.1
1.4
2.0
1.9
1.7
2.5
1.2
0.9
1.2
2.2
2.9
2.5
2.0
2.1
2.0
1.8
2.0
2.1
2.0
1.5
1.5
2.7
1.8
2.6
1.4
1.9
2.6
1.5
2.1
1.6
2.2
2.0
1.5
2.1
1.8
1.9
2.0
1.8
0.9
2.0
4.6
3.6
1.6
1.4
1.3
2.0
2.9
1.3
2.3
1.7
1.5
3.1
1.8
1.4
1.7
2.9
1.9
1.2
3.0
1.7
2.5
1.3
4.2
1.4
1.6
2.2
2.2
1.5
1.6
1.7
1.7
1.3
2.1
3.1
2.6
1.6
1.5
1.7
1.0
1.0
1.9
1.4
1.2
0.9
2.2
1.6
1.4
2.2
2.2
1.2
1.5
1.2
1.9
1.3
2.1
0.9
1.2
1.3
1.5
1.5
2.4
2.4
2.3
1.9
2.0
2.2
0.8
1.8
1.9
1.1
1.2
1.5
3.4
1.6
1.5
1.4
0.9
1.7
1.5

View File

@@ -0,0 +1,45 @@
function MM_MB_Plot(M,Mc,x1,x2,m,zer1,zer2,gau_b,poch,poch2)
M=M(M>=Mc);
% ----- plotting magnitude density and its derivatives ------
xp=linspace(x1,x2,m)';
%st=range([x1 x2])/(m-1);xp=x1:st:x2;
figure('rend','painters','pos',[800 100 600 900]);hold on
subplot(3,1,1);
plot(xp,gau_b,'LineWidth',2);hold on;
for i=1:length(zer1)
Le1=plot([zer1(i) zer1(i)],[0 10],'k--','LineWidth',1);
end
for i=1:length(zer2)
Le2=plot([zer2(i) zer2(i)],[0 10],'r--','LineWidth',1);
end
xlim([min(M)-0.1 max(M)+0.1]);ylim([0 max(gau_b)+0.01])
title(['Data density for h_c_r_i_t ';' (critical smoothing factor) '],'FontSize',14);%axis square
legend([Le1,Le2],{'extremum','Inflection Point(s)'});ylabel('PDF','FontSize',14)
subplot(3,1,2);
plot(xp,poch,'LineWidth',2);ylabel('1^s^t derivative','FontSize',14);hold on;%axis square;
plot([xp(1) xp(length(xp))],[0 0],'--','LineWidth',1)
for i=1:length(zer1)
plot([zer1(i) zer1(i)],[min(poch)-0.01 max(poch)+0.01],'k--','LineWidth',1)
end
for i=1:length(zer2)
plot([zer2(i) zer2(i)],[min(poch)-0.01 max(poch)+0.01],'r--','LineWidth',1)
end
xlim([min(M)-0.1 max(M)+0.1]);ylim([min(poch)-0.01 max(poch)+0.01])
subplot(3,1,3);
plot(xp,poch2,'LineWidth',2);ylabel('2^n^d derivative','FontSize',14);%axis square
hold on; plot([xp(1) xp(length(xp))],[0 0],'--','LineWidth',1)
for i=1:length(zer1)
plot([zer1(i) zer1(i)],[min(poch2)-0.01 max(poch2)+0.01],'k--','LineWidth',1)
end
for i=1:length(zer2)
plot([zer2(i) zer2(i)],[min(poch2)-0.01 max(poch2)+0.01],'r--','LineWidth',1)
end
xlim([min(M)-0.1 max(M)+0.1]);ylim([min(poch2)-0.01 max(poch2)+0.01])
xlabel('Data','FontSize',14);
cd Outputs_MM_MB\;saveas(gcf,'Mulitimodality_output.jpg');cd ../
end

View File

@@ -0,0 +1,489 @@
% FUNCTION: MM_MB
% VERSION: [Wrapper Standalone Version] V2.8
% COMPATIBLE with Matlab version 2017b or later
% TOOLBOX: "Magnitude Complexity Toolbox" within SERA Project
% DOCUMENT: "READ_ME_App_2B_v2_Description_MM_MB.docx"
% --------------------------------------------------------------------------------------------------------
% Test performed for the Distribution of a given dataset (time-series)
% checking whether PDF demonstrates multi-modes/ multi-bumps
% --------------------------------------------------------------------------------------------------------
% OVERVIEW: This Application is a Matlab function which performs testing
% of hypotheses of 1) multimodality and 2) existence of multi-bumps in a given
% magnitude distribution. Please check also the accompanying auxiliary scripts
% 'MM_MB_wrapper' and 'MM_MB_Plot' for a specific application (scenario)
% and plotting results.
% --------------------------------------------------------------------------------------------------------
% 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: v2.8 **** [Wrapper Standalone Version]
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% PLEASE refer to the accompanying document:
% "READ_ME_App_2B_v2_Description_MM_MB.docx"
% for description of the Application and its requirements.
% --------------------------------------------------------------------------------------------------------
% DESCRIPTION:
% The function studies the magniutude distribution complexity by means of the
% Mulitmodality Test (Silverman, 1986; Efron and Tibshirani, 1993). Two null
% hypotheses (H0s) are tested:
% H01 - multimodality: The magnitude PDF is unimodal
% H02 - multi-bump: The magnitude PDF has one bump to the right of the
% mode.
% A mode is a local maximum of probability density and a bump is an interval
% [a,b] such that the probability density is concave over [a,b] but not over any
% larger interval (Silverman, 1986). The importance of modes and bumps relies
% on the fact that multiple occurrences of these features in a PDF indicate, for
% most standard densities, a mixing of components (e.g. Cox, 1966).
% --------------------------------------------------------------------------------------------------------
% INPUT: The function takes as input any matlab vector (input parameter "M").
% The input data can be uploaded by the use of "ADTestMag_wrapper"
% script, from an ASCII file (e.g. *.txt). Such file should contain a vector
% (raw or column) of the Data that the User wishes to process. The User
% is afterwards requested to enter values for some additional parameters.
% Input Parameters Overview:
% --- M: Time-Series (e.g. Magnitude) vector, read i.e. from an ASCII
% file.
% --- Mc: Corresponds to the input data completeness threshold.
% --- m: number of points to divide the data sample, default 100,
% recommended 100-1000.
% % INPUTS for MODE and BUMP HUNTING
% - n_boot: Number of bootstrap iterations for both MM and MB
% (default: 100)
% - delta_h: Smoothing parameter step for successive trials in defining
% the critical h for MM and MB process (h-critical accuracy)
% (default: 0.001)
% - h : initial value of the smoothing factor to apply in defining the
% critical h for the MM testing process (default:0.01)
% - MMmeth: Method for multimodality Testing. Possible values:
% 'Efron', 'Silverman'
% - MBmeth: Method for multibumps Testing. Possible values:
% 'Silverman', 'Efron'
% NOTE: The initial value for MB process is taken equal to the
% critical value defined in the MM process minus its
% accuracy (i.e. hcrit_Modes-delta_h, see OUTPUT below)
% ---------------------------------------------------------------------------------------------------------
% OUTPUT:
% - Output Report with data and parameters used
% ('Output_MM_MB.txt file')
% - Output Parameters:
% % DOUBLES:
% * n - Number of observations used
% * bval- b-value of the G-R law
% * Rmodes - The estimated significance of null hypothesis (H01)
% * hcrit_modes - estimated critical smoothness parameter for MM test
% * Rbumps - The estimated significance of null hypothesis (H02)
% * hcrit_bumps - estimated critical smoothness parameter for MB test
% * zer1 - point where 1st derivative is zero (extremum)
% * zer2 - point(s) where 2nd derivative is zero (inflection points)
% [NOTE: zer2 can be a vector]
% % VECTORS:
% * gau: Data vector PDF estimated for h critical from Multimodality
% * gau_b: Data vector PDF estimated for h critical from Multibumps
% * poch: 1st derivative of Data vector PDF for h critical from Multibumps
% * poch2: 2nd derivative of Data vector PDF for h critical from Multibumps
% * x1,x2: minimum and maximum data points after randomization
% (parameters needed for plotting)
% - Output Figures: Please refer to the Auxiliary Function: "MM_MB_Plot"
%
% ---------------------------------------------------------------------------------------------------------
% REFERENCES:
% -- Cox, D. R., (1966), Notes on the analysis of mixed frequency distributions.
% Br. J. Math. Stat. Psychol. 19, 39-47, doi.org/10.1111/j.2044-8317.1966.tb00353.x.
% -- Efron, B., and Tibshirani R.J. (1993) An Introduction to the Bootstrap,
% CRC Press, Boca Raton, Fla.
% -- Lasocki S. and E. E, Papadimitriou (2006), "Magnitude distribution
% complexity revealed in seismicity from Greece", J. Geophys. Res.,
% 111, B11309, doi:10.1029/2005JB003794.
% -- Silverman, B. W., (1986), Density estimation for statistics and data analysis,
% CRC press, Boa Raton, Fla.
% ---------------------------------------------------------------------------------------------------------
% 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.
% ---------------------------------------------------------------------------------------------------------
function [n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps,gau,gau_b,poch,poch2,zer1,zer2,x1,x2]...
=MM_MB_V2_8(M,Mc,m,n_boot,h,delta_h,MMmeth,MBmeth)
tic
mkdir Outputs_MM_MB
% define round-off interval and select data above Mc
sm=sort(M);dm=sm(2:length(sm))-sm(1:length(sm)-1);
dm=dm(dm>0);eps=min(dm);
M=M(M>=Mc);
% calculate beta
Rp=round(-log10(eps)); %% check the RP parameter
[beta]=beta_AK_UT_Mbin (Mc,M,Rp);
% Randomize magnitude
m_corr(:,1) = korekta(M,Mc,eps,beta);
% SETTING PARAMETERS
n=length(m_corr);
x1=min(m_corr)-0.001; %a bit lower than Mmin
x2=max(m_corr)+0.001; %a bit higher than Mmax
% MULTIMODALITY TESTING
[hcrit_modes,gau]=critical_smoothing(m_corr,n,delta_h,x1,x2,m,h);
% Run Multimodality testing
switch MMmeth
case 'Efron'
[Rmodes] = test_multimodality_e (m_corr,n,x1,x2,m,hcrit_modes,n_boot);
case 'Silverman'
[Rmodes] = test_multimodality (m_corr,n,x1,x2,m,hcrit_modes,n_boot);
end
% ------------------------------------------------------------------------
% [zer1]=zera_1st(m_corr,n,x1,x2,m,hcrit-delta_h);
% ------------------------------------------------------------------------
% BUMP HUNTING
[hcrit_bumps,gau_b,poch,poch2]=critical_smoothing_bumps(m_corr,n,delta_h,x1,x2,m,hcrit_modes-delta_h);
% Run Bump hunting
switch MBmeth
case 'Silverman'
[Rbumps] = bump_hunt (m_corr,n,x1,x2,m,hcrit_bumps,n_boot);
case 'Efron'
[Rbumps] = bump_hunt_e (m_corr,n,x1,x2,m,hcrit_bumps,n_boot);
end
bval=beta/log(10);
% Display and Save the Results:
re=table(n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps)
[zer1]=zera_1st(m_corr,n,x1,x2,m,hcrit_bumps-delta_h); % use the same h in both cases
[zer2]=zera_2nd(m_corr,n,x1,x2,m,hcrit_bumps-delta_h);
toc
% Save Outputs
cd Outputs_MM_MB\
SaveOuts(eps,Mc,m,n_boot,n_boot,n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps)
cd ../
end
% ----- plotting magnitude density and its derivatives ------
% ****************************************************
%% ******************************************************************
% *************************** FUNCTIONS ***************************
% ****-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-****
%% --------------------------------------------------------------------------------------
%% --------------------------------------------------------------------------------------
%
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
%% --------------------------------------------------------------------------------------
% Finding critical smoothing parameter value for which the non parametric
% PDF demonstrates one mode in the selected interval
%
function [hcrit,gau]=critical_smoothing(mm,n,delta_h,x1,x2,m,h)
%
x=sort(mm);
p=2.0;
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,m);
%h=input('Initial smoothing factor ');
while p>1
for i=1:m,
poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./h).^2));
end
p=0;
for i=2:m,
if poch(i-1)*poch(i)<0
p=p+1;
end
end
h=h+delta_h;
end
hcrit=h-delta_h;
for i=1:m,
gau(i)=sum(exp(-0.5*((y(i)-x)./hcrit).^2))/c/n/hcrit;
end
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
%
function [R]=test_multimodality(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_wielom=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
z=x(ind)+h_crit.*eps;
z=sort(z);
poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch(i-1)*poch(i)<0
p=p+1;
end
end
if p>1
no_wielom=no_wielom+1;
end
end
R=no_wielom/mm;
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
% EFRON
%
function [R]=test_multimodality_e(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
s2=sqrt(1+h_crit^2/var(x));
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_wielom=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
me=mean(x(ind));
z=me+(x(ind)-me+h_crit.*eps)./s2;
z=sort(z);
poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch(i-1)*poch(i)<0
p=p+1;
end
end
if p>1
no_wielom=no_wielom+1;
end
end
R=no_wielom/mm;
end
%% --------------------------------------------------------------------------------------
% Finding critical smoothing parameter value for which the non parametric
% PDF demonstrates one bump in the selected interval
%
function [hcrit,gau,poch,poch2]=critical_smoothing_bumps(mm,n,delta_h,x1,x2,m,h)
%
x=sort(mm);
p=2.0;
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,m);
%h=input('Initial smoothing factor ');
while p>1
for i=1:m,
% poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./h).^2));
poch2(i)=sum((((y(i)-x)./h).^2-1).*exp(-0.5*((y(i)-x)./h).^2));
end
p=0;
for i=2:m,
if poch2(i-1)*poch2(i)<0
p=p+1;
end
end
h=h+delta_h;
end
hcrit=h-delta_h;
for i=1:m,
gau(i)=sum(exp(-0.5*((y(i)-x)/hcrit).^2))/c/n/hcrit;
poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./hcrit).^2));
poch2(i)=sum((((y(i)-x)./hcrit).^2-1).*exp(-0.5*((y(i)-x)./hcrit).^2));
end
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
%
function [R]=bump_hunt(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_bump=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
z=x(ind)+h_crit.*eps;
z=sort(z);
% poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
poch2(1)=sum((((y(1)-z)./h_crit).^2-1).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
% poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
poch2(i)=sum((((y(i)-z)./h_crit).^2-1).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch2(i-1)*poch2(i)<0
p=p+1;
end
end
if p>1
no_bump=no_bump+1;
end
end
R=no_bump/mm;
end
%% --------------------------------------------------------------------------------------
% "SMOOTHED BOOTSTRAP"
% EFRON
%
function [R]=bump_hunt_e(m,n,x1,x2,n_point,h_crit,mm)
%
x=sort(m);
s2=sqrt(1+h_crit^2/var(x));
c=sqrt(2*pi);
%x1=input('Lower limit ');
%x2=input('Upper limit ');
%m=input('No. of points ');
y=linspace(x1,x2,n_point);
%h_crit=input('Critical smoothing factor ');
%mm=input('No of trials ');
no_bump=0;
for j=1:mm,
ind=unidrnd(n,n,1);
eps=normrnd(0,1,n,1);
me=mean(x(ind));
z=me+(x(ind)-me+h_crit.*eps)./s2;
z=sort(z);
% poch(1)=-sum((y(1)-z).*exp(-0.5*((y(1)-z)./h_crit).^2));
poch2(1)=sum((((y(1)-z)./h_crit).^2-1).*exp(-0.5*((y(1)-z)./h_crit).^2));
p=0;
for i=2:n_point,
% poch(i)=-sum((y(i)-z).*exp(-0.5*((y(i)-z)./h_crit).^2));
poch2(i)=sum((((y(i)-z)./h_crit).^2-1).*exp(-0.5*((y(i)-z)./h_crit).^2));
if poch2(i-1)*poch2(i)<0
p=p+1;
end
end
if p>1
no_bump=no_bump+1;
end
end
R=no_bump/mm;
end
%% --------------------------------------------------------------------------------------
% Determination of PDF first derivative zeros estimates in the selected interval
%
function [zer1]=zera_1st(mm,n,x1,x2,m,h)
%
x=sort(mm);
c=sqrt(2*pi);
y=linspace(x1,x2,m);
for i=1:m,
poch(i)=-sum((y(i)-x).*exp(-0.5*((y(i)-x)./h).^2));
end
p=0;
for i=2:m,
if poch(i-1)*poch(i)<0
p=p+1;
zer1(p)=(y(i)+y(i-1))/2; %K 23JAN2019
end
end
end
%% --------------------------------------------------------------------------------------
% Determination of PDF second derivative zeros estimates in the selected interval
%
function [zer2]=zera_2nd(mm,n,x1,x2,m,h)
%
x=sort(mm);
c=sqrt(2*pi);
y=linspace(x1,x2,m);
for i=1:m,
poch2(i)=sum((((y(i)-x)./h).^2-1).*exp(-0.5*((y(i)-x)./h).^2));
end
p1=0;
for i=2:m,
if poch2(i-1)*poch2(i)<0
p1=p1+1;
zer2(p1)=(y(i)+y(i-1))/2; %K 23JAN2019
end
end
end
%% --------------------------------------------------------------------------------------------------------
% --------------------------------------- SAVE OUTPUTS in the report file ---------------------------------------
% Save Outputs
function SaveOuts(EPS,Mmin,m,n_boot1,n_boot2,n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps)
% ---- Save *.txt file with Parameters Report ----
%cd Outputs/
fid=fopen('REPORT_Multimodality.txt','w');
fprintf(fid,['RESULTS from MULTIMODALITY/MULTIBUMP TESTING (created on ', datestr(now),')\n']);
fprintf(fid,'------------------------------------------------------------------------------\n');
fprintf(fid,['<Data Round-off interval >: ', num2str(EPS),'\n']);
fprintf(fid,['<Number of points to divide the sample >: ', num2str(m),'\n']);
fprintf(fid,['<Completeness Threshold >: ', num2str(Mmin),'\n']);
fprintf(fid,['<Number of events used >: ', num2str(n),'\n']);
fprintf(fid,['<Gutenberg-Richter b-value >: ', num2str(bval,'%5.3f'),'\n']);
fprintf(fid,['<Number of bootstrap iterations (multimodality) >: ', num2str(n_boot1),'\n']);
fprintf(fid,['<Number of bootstrap iterations (multibumps) >: ', num2str(n_boot2),'\n']);
fprintf(fid,'------------------------------------------------------------------------------\n');
fprintf(fid,['<Critical Smoothing Parameter,h (for modes) >: ', num2str(hcrit_modes,'%6.4f'),'\n']);
fprintf(fid,['<Critical Smoothing Parameter,h (for bumps) >: ', num2str(hcrit_bumps,'%6.4f'),'\n']);
fprintf(fid,['<p-value of H01 that input data PDF is unimodal >: ', num2str(Rmodes,'%4.3f'),'\n']);
fprintf(fid,['<p-value of H02 that input data PDF has no more than \n']);
fprintf(fid,[' one bump to the right of the global maximum >: ', num2str(Rbumps,'%4.3f'),'\n']);
fclose(fid);
end

View File

@@ -0,0 +1,46 @@
% This is a Wrapper Script for performing a test for the existence of multi-modes
% and multi-bumps in a given (magnitude) time-series. The function MM_MB is
% executed for this purpose. The description of the fuction can be found in
% comments within "MM_MB_V2_*.m" code. Here, the input data and the
% parameters (i.e. the function's arguments) are defined by the User. Please
% modify the parameters in the script accordingly,the lines that can be modified
% are followed by a comment "- PLEASE SET".
% PLEASE REFER ALSO TO APPLCATION DOCUMENTATION:
% "READ_ME_App_2B_v2_Description_MM_MB.docx"
clc;clear;
% STEP 1. DATA Upload:
cd Sample_Data % PLEASE Specify data directory path
M=dlmread('test_vector.txt'); % PLEASE SET data (magnitude) vector input file
cd ../
% STEP 2. Completeness Threshold Selection:
Mc=1.5; % PLEASE SET
% STEP 3. Number of points:
m=100; % PLEASE SET
% STEP 4. Bootstrap Iterations:
n_boot=100; % PLEASE SET
% STEP 5. Initial h:
h=0.01; % PLEASE SET
% STEP 6. h step (dh):
delta_h=0.001; % PLEASE SET
% STEP 7. Method for MM testing:
MMmeth='Efron'; % PLEASE SET
% STEP 8. Method for MB testing:
MBmeth='Silverman'; % PLEASE SET
% STEP 9. RUN Function ['MM_MB']
[n,bval,Rmodes,hcrit_modes,Rbumps,hcrit_bumps,gau,gau_b,poch,poch2,zer1,zer2,x1,x2]=...
MM_MB_V2_8(M,Mc,m,n_boot,h,delta_h,MMmeth,MBmeth);
% STEP 10. Optional, Plotting % PLEASE Comment the next line to deactivate visualization
MM_MB_Plot(M,Mc,x1,x2,m,zer1,zer2,gau_b,poch,poch2);

View File

@@ -0,0 +1,880 @@
1.7
0.8
1.8
1.5
1.7
0.8
1.7
2.1
0.9
1.3
1.1
1.9
2.3
1.7
1.9
1.4
1.5
1.6
1.5
2.7
2.9
1.3
1.2
0.9
2.7
1.4
1.2
1.4
1.7
1.5
1.4
1.5
2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