polyBasis[m_]:=Map[LegendreP[#,2.0(x-0.5)]&,Range[0,m-1]]

edgeBasis[m_]:=Map[(Abs[x-#/(m-1)])&,Range[0,m-1]]

rawdat=Import["~/doc/professional/presentations/2025/ictp-saifr_colloquium/poly_fits.dat"];

testdata=Partition[rawdat[[1]],2];
as=rawdat[[2;;]];
Length[as]

dataOnly=ListPlot[testdata,PlotStyle->Black,PlotRange->{-1.5,1.5},ImageSize->300]

truthOnly=Plot[{Sin[2π x]},{x,0,1},PlotPoints->100,PlotRange->{-1.5,1.5},ImageSize->300]

dataAndTruth=Show[Plot[{Sin[2π x]},{x,0,1},PlotPoints->100,PlotRange->{-1.5,1.5}],ListPlot[testdata,PlotStyle->Black],ImageSize->300]

basisPlot=Plot[Evaluate[polyBasis[6]],{x,0,1},PlotRange->{-1.5,1.5},ImageSize->300]

underfit=Show[Plot[{as[[2]].polyBasis[Length[as[[2]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

goodfit=Show[Plot[{as[[7]].polyBasis[Length[as[[7]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

overfit=Show[Plot[{as[[40]].polyBasis[Length[as[[40]]]]},{x,-0.01,1.01},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

overparamfit=Show[Plot[{as[[-31]].polyBasis[Length[as[[-31]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

asExtra=Import["~/doc/professional/presentations/2025/ictp-saifr_colloquium/poly_fits_extra.dat"];

overparamfitExtra=Show[Plot[{#.polyBasis[Length[#]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]&/@asExtra

biasVarianceCurve=ListLogPlot[(1/2000)Sum[(((#.polyBasis[Length[#]]-Sin[2π x]))^2),{x,0,1,1/2000}]&/@Join[as[[;;Length[testdata]]]],Joined->True,PlotRange->All,ImageSize->250,AxesLabel->{N,"MSE"},LabelStyle->Black]

gg[6]=Graphics3D[{
InfinitePlane[{{-1.5,0,0},{-1.5,-1.5,-1.5},{0,0,-1.5}}]
}
,Axes->True,AxesOrigin->{0,0,0},AxesStyle->Black,PlotRange->{{-1,1},{-1,1},{-1,1}},ViewPoint->{-1.4659926879216445`,-2.964110116985182`,0.7175769320054625`}]

gg[5]=Graphics3D[{
InfinitePlane[{{-1.5,0,0},{-1.5,-1.5,-1.5},{0,0,-1.5}}],
{Red,PointSize[0.03],Point[{0,0,0}]}
}
,Axes->True,AxesOrigin->{0,0,0},AxesStyle->Black,PlotRange->{{-1,1},{-1,1},{-1,1}},ViewPoint->{-1.4659926879216445`,-2.964110116985182`,0.7175769320054625`}]

gg[4]=Graphics3D[{
InfinitePlane[{{-1.5,0,0},{-1.5,-1.5,-1.5},{0,0,-1.5}}],
{Red,PointSize[0.03],Point[{0,0,0}]},
{Blue,PointSize[0.03],Point[(1/2){-1,1,-1}]},
{Thick,Arrow[{{0,0,0},(1/2){-1,1,-1}}]}
}
,Axes->True,AxesOrigin->{0,0,0},AxesStyle->Black,PlotRange->{{-1,1},{-1,1},{-1,1}},ViewPoint->{-1.4659926879216445`,-2.964110116985182`,0.7175769320054625`}]

gg[3]=Graphics3D[{
InfinitePlane[{{-1.5,0,0},{-1.5,-1.5,-1.5},{0,0,-1.5}}],
{Red,PointSize[0.03],Point[{0,0,0}],Point[{0.7,0.7,0.8}]},
{Blue,PointSize[0.03],Point[(1/2){-1,1,-1}]},
{Thick,Arrow[{{0,0,0},(1/2){-1,1,-1}}]}
}
,Axes->True,AxesOrigin->{0,0,0},AxesStyle->Black,PlotRange->{{-1,1},{-1,1},{-1,1}},ViewPoint->{-1.4659926879216445`,-2.964110116985182`,0.7175769320054625`}]

gg[2]=Graphics3D[{
InfinitePlane[{{-1.5,0,0},{-1.5,-1.5,-1.5},{0,0,-1.5}}],
{Red,PointSize[0.03],Point[{0,0,0}],Point[{0.7,0.7,0.8}]},
{Blue,PointSize[0.03],Point[(1/2){-1,1,-1}],Point[(1/2){-1,1,-1}+1/4{-1,1,2}]},
{Thick,Arrow[{{0,0,0},(1/2){-1,1,-1}}],
Arrow[{{0.7,0.7,0.8},(1/2){-1,1,-1}+1/4{-1,1,2}}]}
}
,Axes->True,AxesOrigin->{0,0,0},AxesStyle->Black,PlotRange->{{-1,1},{-1,1},{-1,1}},ViewPoint->{-1.4659926879216445`,-2.964110116985182`,0.7175769320054625`}]

gg[1]=Graphics3D[{
InfinitePlane[{{-1.5,0,0},{-1.5,-1.5,-1.5},{0,0,-1.5}}],
{Red,PointSize[0.03],Point[{0,0,0}],Point[{0.7,0.7,0.8}]},
{Blue,PointSize[0.03],Point[(1/2){-1,1,-1}],Point[(1/2){-1,1,-1}+1/4{-1,1,2}]},
{Thick,Arrow[{{0,0,0},(1/2){-1,1,-1}}],
Arrow[{{0.7,0.7,0.8},(1/2){-1,1,-1}+1/4{-1,1,2}}]}
}
,Axes->True,AxesOrigin->{0,0,0},AxesStyle->Black,PlotRange->{{-1,1},{-1,1},{-1,1}},Epilog->{Inset[Show[overparamfit,Ticks->None],Scaled[{0.25,0.35}],Automatic,0.25],Inset[Show[overparamfitExtra[[-1]],Ticks->None],Scaled[{0.1,0.75}],Automatic,0.25]},ViewPoint->{-1.4659926879216445`,-2.964110116985182`,0.7175769320054625`}]

biasVarianceCurve2=ListLogPlot[{Length[#],(1/1000)Sum[(((#.polyBasis[Length[#]]-Sin[2π x]))^2),{x,0,1,1/1000}]}&/@Join[as[[;;Length[testdata]]],Reverse@as[[Length[testdata]+1;;]]],Joined->True,PlotRange->All,ImageSize->250,AxesLabel->{N,"MSE"},LabelStyle->Black,Prolog->Line[{{40,Log[4 (10^(-1))]},{40,Log[(10^18)]}}],Epilog->Text["M",{40,Log[1 (10^(-2))]}]]

(1/1000)Sum[(((#.polyBasis[Length[#]]-Sin[2π x]))^2),{x,0,1,1/1000}]&/@{as[[6]],as[[-1]]}

ListPlot[(1/1000)Sum[(((#.edgeBasis[Length[#]]-Sin[2π x]))^2),{x,0,1,1/1000}]&/@Join[as[[2;;Length[testdata]]],Reverse[as[[1+Length[testdata];;]]]],Joined->True,PlotRange->All]

Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_data.pdf",dataOnly];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_truth.pdf",truthOnly];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_data_truth.pdf",dataAndTruth];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_basis_poly.pdf",basisPlot];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_underfit_poly.pdf",underfit];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_goodfit_poly.pdf",goodfit];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_overfit_poly.pdf",overfit];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_overparamfit_poly.pdf",overparamfit];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_bias-variance_poly.pdf",biasVarianceCurve];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_bias-variance2_poly.pdf",biasVarianceCurve2];

MapIndexed[Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_overparamfit_poly_"<>ToString[#2[[1]]]<>".pdf",#1]&,overparamfitExtra];

Map[Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_gradient_"<>ToString[#]<>".pdf",gg[7-#]]&,Range[6]]

asAbs=Import["~/doc/professional/presentations/2025/ictp-saifr_colloquium/test.dat"];

Length[asAbs]

basisPlotAbs=Plot[Evaluate[20 edgeBasis[10]],{x,0,1},PlotRange->{-1.5,1.5},ImageSize->300]

underfitAbs=Show[Plot[{asAbs[[2]].edgeBasis[Length[asAbs[[2]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

goodfitAbs=Show[Plot[{asAbs[[7]].edgeBasis[Length[asAbs[[7]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

overfitAbs=Show[Plot[{asAbs[[40]].edgeBasis[Length[asAbs[[40]]]]},{x,-0.01,1.01},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

overparamfitAbs=Show[Plot[{asAbs[[-39]].edgeBasis[Length[asAbs[[-39]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdata,PlotStyle->Black]]

biasVarianceCurveAbs=ListPlot[{Length[#],(1/2000)Sum[(((#.edgeBasis[Length[#]]-Sin[2π x]))^2),{x,0,1,1/2000}]}&/@Join[asAbs[[;;Length[testdata]]],Most@Reverse@asAbs[[Length[testdata]+1;;]]],Joined->True,PlotRange->All,ImageSize->250,AxesLabel->{N,"MSE"},LabelStyle->Black,Prolog->Line[{{40,0},{40,0.17}}],Epilog->Text["M",{40,0.2}]]

Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_basis_abs.pdf",basisPlotAbs];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_underfit_abs.pdf",underfitAbs];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_goodfit_abs.pdf",goodfitAbs];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_overfit_abs.pdf",overfitAbs];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_overparamfit_abs.pdf",overparamfitAbs];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_bias-variance_abs.pdf",biasVarianceCurveAbs];

testdat2=Partition[First@Import["~/doc/professional/presentations/2025/ictp-saifr_colloquium/test2.dat"],2];

asAbs2=Rest@Import["~/doc/professional/presentations/2025/ictp-saifr_colloquium/test2.dat"];

Length[asAbs2]

dataOnlyAbs2=ListPlot[testdat2,PlotStyle->Black,PlotRange->{-1.5,1.5},ImageSize->300]

underfitAbs2=Show[Plot[{asAbs2[[4]].edgeBasis[Length[asAbs2[[4]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdat2,PlotStyle->Black]]

goodfitAbs2=Show[Plot[{asAbs2[[6]].edgeBasis[Length[asAbs2[[6]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdat2,PlotStyle->Black]]

overfitAbs2=Show[Plot[{asAbs2[[10]].edgeBasis[Length[asAbs2[[10]]]]},{x,-0.01,1.01},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdat2,PlotStyle->Black]]

overparamfitAbs2=Show[Plot[{asAbs2[[15]].edgeBasis[Length[asAbs2[[15]]]]},{x,0,1},PlotRange->{-1.5,1.5},ImageSize->200],ListPlot[testdat2,PlotStyle->Black]]

biasVarianceCurveAbs2=ListLogPlot[{Length[#],(1/2000)Sum[(((#.edgeBasis[Length[#]]-Cos[2π x]))^2),{x,0,1,1/2000}]}&/@Join[asAbs2[[;;Length[testdat2]]],Most@Reverse@asAbs2[[Length[testdat2]+1;;]]],Joined->True,PlotRange->All,ImageSize->250,AxesLabel->{N,"MSE"},LabelStyle->Black,Prolog->Line[{{10,Log[6 (10^(-5))]},{10,Log[(10^(-1))]}}],Epilog->Text["M",{10,Log[2 (10^(-1))]}]]

Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_data_abs2.pdf",dataOnlyAbs2];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_underfit_abs2.pdf",underfitAbs2];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_goodfit_abs2.pdf",goodfitAbs2];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_overfit_abs2.pdf",overfitAbs2];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_overparamfit_abs2.pdf",overparamfitAbs2];
Export["~/doc/professional/presentations/2025/ictp-saifr_colloquium/figs/fit_bias-variance_abs2.pdf",biasVarianceCurveAbs2];