(* ::Package:: *)

Needs["ErrorBarPlots`"]


(* ::Section:: *)
(*Data import*)


gsraw=Import[NotebookDirectory[]<>"data/graphdata_ccm_initialtris.m"];
(* gsraw=SortBy[gsraw,{#[[1,1]]&,#[[1,2]]&}]; (* Sort by n and then by tau. The {} forces a *stable* sort because otherwise Mathematica sorts also on triangle count and other things. *) *)


gdata=gsraw;
(* Data format: *)
(* gdata[[ tau index , datatype index ]] *)
(* datatype index:
1: {n,tau}
2: {uniform triangle samples}
3: {CCMdu initial triangle samples} <-- CCMdu: get new highest degree vertex every time
4: {CCMd initial triangle samples} <-- CCMd: finish vertex completely
*)


(* ::Section:: *)
(*Greedy configuration model*)


(* ::Subsection:: *)
(*Distribution of initial #triangles for CCMd(u) compared to uniform triangle distribution*)


getHistogram[run_]:=Histogram[{run[[2]],run[[3]],run[[4]]},Automatic,"Probability",
ChartLegends->Placed[{"Uniform","CCMdu","CCMd"},Bottom],
ImageSize->250,
Frame->True,
FrameLabel->{"Triangles","Probability"},
PlotLabel->("n = "<>ToString[run[[1,1]]]<>", \[Tau] = "<>ToString[run[[1,2]]])
];
histograms=Map[getHistogram,gdata]


(* ::Subsubsection:: *)
(*Exporting plots*)


getHistogram[run_,bins_,plotrange_,tickDelta_,textpos_,bottomLegends_,bottomLabel_]:=Histogram[{run[[2]],run[[3]],run[[4]]},bins,"Probability",
ImageSize->250,
AspectRatio->4/14,
PlotRange->plotrange,
Frame->True,
FrameLabel->{If[bottomLabel,"Triangles",None],"Probability"},
FrameTicks->{{{#,NumberForm[#,{2,2}]}&/@Range[0,0.30,tickDelta],Automatic},{Automatic,Automatic}},
Epilog->Text["n = "<>ToString[run[[1,1]]]<>", \[Tau] = "<>ToString[run[[1,2]]],textpos],
ChartLegends->If[bottomLegends,Placed[{"Uniform","CCMdu","CCMd"},Top],None]
];
{h1,h2,h3}={
getHistogram[gdata[[1]],Automatic,{0,0.30} ,0.1 ,Scaled[{0.82,0.86}],True ,False],
getHistogram[gdata[[2]],      {5},{0,0.15} ,0.05,Scaled[{0.82,0.86}],False,False],
getHistogram[gdata[[3]],Automatic,Automatic,0.05,Scaled[{0.82,0.86}],False,True]
};
plotgrid1=Column[{h1,h2,h3}]


Export[NotebookDirectory[]<>"plots/ccm_initialtris.pdf",plotgrid1]


(* ::Section:: *)
(*CCMu rates only*)


gsraw2=Import[NotebookDirectory[]<>"data/graphdata_ccm_constructionrate.m"];
(* gsraw=SortBy[gsraw,{#[[1,1]]&,#[[1,2]]&}]; (* Sort by n and then by tau. The {} forces a *stable* sort because otherwise Mathematica sorts also on triangle count and other things. *) *)


gdata2=GatherBy[gsraw2,{#[[1,2]]&,#[[1,1]]&}];
(* Data format: *)
(* gdata[[ tau index, n index, run index , datatype index ]] *)
(* datatype index:
1: {n,tau}
3: CCMdu construction rate <-- CCMdu: get new highest degree vertex every time
4: CCMd  construction rate <-- CCMd: finish vertex completely
*)


successrates=gdata2[[All,All,All,2]]; (* New datafile *)
legends=Map["\[Tau] = "<>ToString[#[[1,1,2]]]<>" ; avg = "<>ToString[NumberForm[N[Mean[#[[All,2]]]],{4,4}]]&,gdata2,{2}];


successrates=Map[Clip[#,{0,0.99999}]&,successrates,{3}];

datasets={successrates[[1,1]], successrates[[2,1]], successrates[[3,1]]};
selectedLegends={legends[[1,1]],legends[[2,1]],legends[[3,1]]};

plot1=Histogram[datasets,{-0.025,1.025,0.05},"Probability",
PlotRange->{{0,1},{0,1}},
ChartStyle->Directive[FaceForm[Opacity[0.8]],EdgeForm[Thickness[0.001]]],
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n(distribution over sampled degree sequences)","Probability"},
ChartLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"]

histogramlist = Map[Last[HistogramList[#,{-0.05,1.05,0.1},"Probability"]]&,datasets];
histogramlist = Transpose[histogramlist];
(* labels=ConstantArray["",Ceiling[1/0.05]];
labels[[2;;-1;;2]]=Map[ToString,Range[0.1,1,0.1]]; *)
labels=Map[ToString,Range[0,1,0.1]];
plot2=BarChart[histogramlist,
PlotRange->{All,{0,1}},
BarSpacing->{None,Medium},
ChartLabels->{labels,None},
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n(distribution over sampled degree sequences)","Probability"},
ChartLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"]

plot3=SmoothHistogram[datasets,{0.01,"Gaussian"},"PDF",
PlotRange->{{0,1},All},
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n(distribution over sampled degree sequences)","PDF"},
PlotLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"]

(*
Table[
SmoothHistogram[datasets,{0.01,weightKernel},"PDF",
PlotRange->{{0,1},All},
(*ChartStyle\[Rule]Directive[FaceForm[Opacity[0.5]],EdgeForm[Thickness[0.007]]],*)
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n over sampled degree sequences","CDF"},
PlotLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"],{weightKernel,{"Biweight","Gaussian","Rectangular","Cosine","SemiCircle"}}]
*)


Export[NotebookDirectory[]<>"plots/ccm_construction_successrates.pdf",plot2]
