Parameter b from Gutenberg - Richter Law

(* asd[[1]] - partition; asd[[2]] - Centroids; asd[[3]] - Covariance matrices *)
SetDirectory[NotebookDirectory[]];
asd=mapa4=ReadList["C:/Rudolf/AB-STRUKA/1_RADOVI/B1_CLUSTERS/B1_PACO/Paco-2023/nb-IP3/MAPart4.txt"];
First[asd[[1,1]]]
Length[asd]

Number of data  with unknown depths

Nr=Table[0,{i,4}];
Do[
Do[
If[asd[[1,j,s,3,9]]==0,Nr[[j]]=Nr[[j]]+1];
,{s,Length[asd[[1,j]]]}]
,{j,4}]
Print["Broj nula-podataka po klasterima: ",Nr]
Total[Nr]

GR by clusters

(* data *)
jj=3;data=asd[[1,jj,All,3]]; 
Print["Cluster ", jj," - Length[data] = ",m=Length[data]];
data=SortBy[data,First];
Print["First: : ",First[data]]
Print["Last: ",Last[data]]
Print["Range of magnitudes: ",{Min[data[[All,10]]],Max[data[[All,10]]]}]

				(* Data set with with known and unknown depths *)
pod3=pod4=pod5=pod6=0;
Do[
If[data[[i,10]]<  4,pod3=pod3+1,
If[data[[i,10]]<5,pod4=pod4+1,pod5=pod5+1]]
,{i,Length[data]}];
Print["pod3=",pod3,"; pod4=",pod4,"; pod5=",pod5,"; podAll= ",pod3+pod4+pod5]
								(* Kontrola *)
Nr=0;
Do[
If[6>data[[i,10]]≥5,Nr=Nr+1]
,{i,Length[data]}]
Print["Broj potresa 6 > Mg >= 5: ",Nr,"\nBroj potresa Mg>=6: ",pod5-Nr]

							(* Data for G-R Law *)
w=Table[data[[i,10]],{i,m}]; 
Magini=Sort[w];
Mag=Table[Magini[[i]],{i,m}];  
Pod={{Mag[[1]],m}};
Do[
If[Mag[[i+1]]>Mag[[i]],Pod=Append[Pod,{Mag[[i+1]],m-(i+1)}]]
,{i,m-1}]; Pod=Drop[Pod,-1];
broj=Length[Pod];
slpod=ListPlot[Pod,PlotStyle->{PointSize[.02]},PlotRange->All,ImageSize->Small];
Print["Nr.data = ",m,"; Length[Pod] = ",broj]
Print["Pod: ",Pod]

								(* Calculation *)
Print[Style["Results by clusters -- LAD method (21) -- CLUSTER: ",18,Blue],jj]
Clear[a,b];min=NMinimize[Sum[Abs[Pod[[i,2]]-10^(a-b Pod[[i,1]])],{i,Length[Pod]}],{a,b}];
a1=a/.min[[2]]; b1=b/.min[[2]];
Print["a = ",a1,",  b = ",b1,";  Sum of ",Length[Pod]," LAD = ",min[[1]]]
slf=Plot[10^(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
Show[slpod,slf, GridLines->Automatic]
Print[Style["Linearizirana slika",15,Blue]]
slphi=Plot[(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
PodLog=Table[{Pod[[i,1]],Log[10,Pod[[i,2]]]},{i,Length[Pod]}];
slLog=ListPlot[PodLog,PlotStyle->{PointSize[.02]},PlotRange->All,ImageSize->Small];
Show[slLog,slphi, GridLines->Automatic]
										(*  *)
Print[Style["Results by clusters -- LAD method (22) -- CLUSTER: ",18,Blue],jj]
Clear[a,b];min=NMinimize[Sum[Abs[Log[10,Pod[[i,2]]]-(a-b Pod[[i,1]])],{i,Length[Pod]}],{a,b}];
a1=a/.min[[2]]; b1=b/.min[[2]];
Print["a = ",a1,",  b = ",b1,";  Sum of ",Length[Pod]," LAD = ",min[[1]]]
slf=Plot[10^(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
Show[slpod,slf, GridLines->Automatic]
Print[Style["Linearized image",15,Blue]]
slphi=Plot[(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
PodLog=Table[{Pod[[i,1]],Log[10,Pod[[i,2]]]},{i,Length[Pod]}];
slLog=ListPlot[PodLog,PlotStyle->{PointSize[.02]},PlotRange->All,ImageSize->Small];
Show[slLog,slphi, GridLines->Automatic]
										(*  *)
Print[Style["Results by clusters according to Marquez-Ramirez2015 (6) -- CLUSTER: ",18,Blue],jj]
Print[Style["Parametar a naknadno se računa kao the best LS value",14,Blue]]
Mp=Mean[data[[All,10]]];
b1=Log[10,E]/((Mp-(Pod[[1,1]]-0.1/2)));
Print[ "Mean(all magnitudes) = ",Mp,";   b = ",b1]
a1=Mean[Table[Log[10,Pod[[i,2]]]+b1 Pod[[i,1]],{i,Length[Pod]}]];
min=Sum[Abs[Pod[[i,2]]-10^(a1-b1 Pod[[i,1]])],{i,Length[Pod]}];
Print["{a, b}: ",{a1, b1},";   Sum of ",Length[Pod]," LAD = ",min]
slf=Plot[10^(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
Show[slpod,slf,GridLines->Automatic,ImageSize->Small]
Print[Style["Linearized image",15,Blue]]
slphi=Plot[(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
PodLog=Table[{Pod[[i,1]],Log[10,Pod[[i,2]]]},{i,Length[Pod]}];
slLog=ListPlot[PodLog,PlotStyle->{PointSize[.02]},PlotRange->All,ImageSize->Small];
Show[slLog,slphi, GridLines->Automatic]

Results for b:  Shi (43)

Mp=Mean[Pod[[All,1]]];
Mp=Mean[data[[All,10]]];
b1=Log[10,E]/(Mp-Pod[[1,1]]);(* Shi (43) *)
(* Za b1=0.9 je dobro poklapanje *)
a1=Mean[Table[Log[10,Pod[[i,2]]]+b1 Pod[[i,1]],{i,Length[Pod]}]];
Print["Mean = ",Mp,";  {a, b}: ",{a1, b1}]
slf=Plot[10^(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
Show[slpod,slf,ImageSize->Small]

Results for b: Maquez-Ramirez2015 (6)

Mp=Mean[Pod[[All,1]]];
Mp=Mean[data[[All,10]]];
b1=Log[10,E]/((Mp-(Pod[[1,1]]-0.1/2)));(* Maquez (6) *)
(* Za b1=0.9 je dobro poklapanje *)  
a1=Mean[Table[Log[10,Pod[[i,2]]]+b1 Pod[[i,1]],{i,Length[Pod]}]];
Print["Mean = ",Mp,";  {a, b}: ",{a1, b1}]
slf=Plot[10^(a1-x b1),{x,Pod[[1,1]],Last[Pod][[1]]},PlotStyle->{Red,Thickness[.005]},PlotRange->{0,Pod[[1,2]]}];
Show[slpod,slf,ImageSize->Small]

Results for b - by all references

Mp=Mean[Pod[[All,1]]];
Mp=Mean[data[[All,10]]];
b=Log[10,E]/(Mp-Pod[[1,1]]);(* Shi (43) *)
Print["Mean = ",Mp, ";   b: Shi (43): ",b]
b=Log[10,E]/(Log[10](Mp-Pod[[1,1]]));(* Marzocchi (2.2) *)
Print["Mean = ",Mp,";   b: Marzocchi (2.2): ",b]
b=Log[10,E]/((Mp-Pod[[1,1]])); (* Maquez (4) *)
Print["Mean = ",Mp,";   b: Maquez (4): ",b]
n=Length[Pod[[All,1]]];
b=n/Sum[Log[Pod[[i,1]]/Pod[[1,1]]],{i,n}]; (* Kagan (22) *)
Print["Mean = ",Mp,";   b: Kagan (22): ",b]