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]]

Length[as[[-31]]]

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

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]

Show[Plot[{Sin[2π x],as[[6]].polyBasis[Length[as[[6]]]]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

Show[Plot[{Sin[2π x],as[[100]].polyBasis[Length[as[[100]]]]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

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

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))]}]]

Show[Plot[{Sin[2π x]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

Show[Plot[{Sin[2π x],Last[as].edgeBasis[Length[Last[as]]]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

Show[Plot[{Sin[2π x],as[[6]].edgeBasis[Length[as[[6]]]]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

Show[Plot[{Sin[2π x],as[[100]].edgeBasis[Length[as[[100]]]]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

(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];

Show[Plot[{Sin[2π x],as2.polyBasis[Length[as2]]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

Show[Plot[{Sin[2π x],as3.polyBasis[Length[as3]]},{x,0,1},PlotPoints->100],ListPlot[testdata,PlotStyle->Black]]

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

testdata2=Partition[rawdat2[[1]],2];
as2=rawdat2[[2]];

Show[Plot[{Sin[2π x],as2.edgeBasis[Length[as2]]},{x,0,1},PlotPoints->100],ListPlot[testdata2,PlotStyle->Black]]