File:Fitting and extrapolation.gif

Summary

Description
English: A lot of different models can be a good fit for your data. That by itself doesn't mean your model is good. And extrapolating from your fit is easily a recipe for disaster.
Date
Source https://twitter.com/j_bertolotti/status/1234528010809810944
Author Jacopo Bertolotti
Permission
(Reusing this file)
https://twitter.com/j_bertolotti/status/1030470604418428929

Mathematica 12.0 code

\[Sigma] = 15;
data = Table[{j, j^2 + RandomVariate[NormalDistribution[0, \[Sigma]]]}, {j, 5, 20}];
dim = Dimensions[data][[1]]
ed = Table[{data[[j, 1]], data[[j, 2]] \[PlusMinus] \[Sigma]}, {j, 1, dim}];
expfit = FindFit[data, a*E^(b x) + c, {a, b, c}, x];
powfit = FindFit[data, a*x^b + c, {a, b, c}, x];
sigmoidalfit = FindFit[data, a*Erf[b*x + c] + a, {{a, 200}, {b, 0.15}, {c, -2}}, x];
p0 = Table[
   Show[
    Plot[x^2, {x, 0, j}, PlotStyle -> {Thick, Gray, Dashed}], PlotRange -> {{0, 25}, {0, 500}}, AxesOrigin -> {0, 0}, Ticks -> None, AxesLabel -> {"x", "y(x)"}, LabelStyle -> {Black, Bold, Medium}, Epilog -> {Text[Style["Ground truth", Bold, FontSize -> 14], Scaled[{0.5, 0.9}]]}], {j, 0.1, 25, 0.4}];
p1 = Table[
   Show[
    Plot[x^2, {x, 0, 100}, PlotStyle -> {Thick, Gray, Dashed}], ListPlot[ed[[1 ;; j]], PlotStyle -> {Thick, Black, PointSize[0.02]}], PlotRange -> {{0, 25}, {0, 500}}, AxesOrigin -> {0, 0}, Ticks -> None, AxesLabel -> {"x", "y(x)"}, LabelStyle -> {Black, Bold, Medium}, Epilog -> {Text[Style["Data Points", Bold, FontSize -> 14], Scaled[{0.5, 0.9}]]}], {j, 1, 16, 1}];
p2 = Table[
   Show[
    Plot[x^2, {x, 0, 100}, PlotStyle -> {Thick, Gray, Dashed}], ListPlot[ed, PlotStyle -> {Thick, Black, PointSize[0.02]}], Plot[(a*E^(b x) + c) /. expfit, {x, 0, j}, PlotStyle -> {Thick, Purple}], PlotRange -> {{0, 25}, {0, 500}}, AxesOrigin -> {0, 0}, Ticks -> None, AxesLabel -> {"x", "y(x)"}, LabelStyle -> {Black, Bold, Medium}, Epilog -> {Text[Style["Exponential fit", Bold, FontSize -> 14], Scaled[{0.5, 0.9}]]}], {j, 0.1, 25, 0.4}];
p3 = Table[
   Show[
    Plot[x^2, {x, 0, 100}, PlotStyle -> {Thick, Gray, Dashed}], ListPlot[ed, PlotStyle -> {Thick, Black, PointSize[0.02]}], Plot[(a*E^(b x) + c) /. expfit, {x, 0, 100}, PlotStyle -> {Thick, Purple}],
    Plot[(a*x^b + c) /. powfit, {x, 0, j}, PlotStyle -> {Thick, Orange}], PlotRange -> {{0, 25}, {0, 500}}, AxesOrigin -> {0, 0}, Ticks -> None, AxesLabel -> {"x", "y(x)"}, LabelStyle -> {Black, Bold, Medium}, 
    Epilog -> {Text[Style["Polynomial fit", Bold, FontSize -> 14], Scaled[{0.5, 0.9}]]}], {j, 0.1, 25, 0.4}];
p4 = Table[
   Show[
    Plot[x^2, {x, 0, 100}, PlotStyle -> {Thick, Gray, Dashed}], ListPlot[ed, PlotStyle -> {Thick, Black, PointSize[0.02]}], Plot[(a*E^(b x) + c) /. expfit, {x, 0, 100}, PlotStyle -> {Thick, Purple}], Plot[(a*x^b + c) /. powfit, {x, 0, 100}, PlotStyle -> {Thick, Orange}], Plot[(a*Erf[b*x + c] + a) /. sigmoidalfit, {x, 0, j}, PlotStyle -> {Thick, Cyan}], PlotRange -> {{0, 25}, {0, 500}}, AxesOrigin -> {0, 0}, Ticks -> None, AxesLabel -> {"x", "y(x)"}, LabelStyle -> {Black, Bold, Medium}, Epilog -> {Text[Style["Sigmoidal fit", Bold, FontSize -> 14], Scaled[{0.5, 0.9}]]}], {j, 0.1, 25, 0.4}];
p5 = Table[
   Show[
    Plot[x^2, {x, 0, 100}, PlotStyle -> {Thick, Gray, Dashed}], ListPlot[ed, PlotStyle -> {Thick, Black, PointSize[0.02]}], Plot[(a*E^(b x) + c) /. expfit, {x, 0, 100}, PlotStyle -> {Thick, Purple}], Plot[(a*x^b + c) /. powfit, {x, 0, 100}, PlotStyle -> {Thick, Orange}], Plot[(a*Erf[b*x + c] + a) /. sigmoidalfit, {x, 0, 250}, PlotRange -> All, PlotStyle -> {Thick, Cyan}], PlotRange -> {{0, j*25}, {0, j*500}}, AxesOrigin -> {0, 0}, Ticks -> None, AxesLabel -> {"x", "y(x)"}, LabelStyle -> {Black, Bold, Medium}, Epilog -> {Text[Style["Extrapolations", Bold, FontSize -> 14], Scaled[{0.5, 0.9}]]}], {j, 1, 7, 0.1}];
ListAnimate[Join[p0, p1, p2, p3, p4, p5]]

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Category:CC-Zero#Fitting%20and%20extrapolation.gif
Category:Self-published work Category:Animated GIF files Category:Least squares Category:Extrapolation Category:Images with Mathematica source code
Category:Animated GIF files Category:CC-Zero Category:Extrapolation Category:Images with Mathematica source code Category:Least squares Category:Self-published work