File:Chi squared.gif
Summary
| Description |
English: If you repeat your set of measurements (e.g. measure y as a function of x) several time, you will get different values each time, and thus a different fit to your model each time, and thus a different χ² each time (and we can calculate how χ² is distributed) |
| Date | |
| Source | https://twitter.com/j_bertolotti/status/1464276671469694978 |
| Author | Jacopo Bertolotti |
| Permission (Reusing this file) |
https://twitter.com/j_bertolotti/status/1030470604418428929 |
Mathematica 12.0 code
\[Sigma] = 1.5; npoints = 15; nparameters = 4; \[Chi]list = {};
frames1 = Table[
data = Table[{i, 0.05*i (i - 5) (i - 12) + 1 + RandomVariate[NormalDistribution[0, \[Sigma]]]}, {i, 1, npoints}];
fit = Fit[data, Evaluate@Table[x^j, {j, 0, nparameters - 1}], x ];
\[Chi]2 = Chop@(Total@Table[(Chop@Evaluate[fit /. {x -> j}] - data[[j, 2]])^2, {j, 1, npoints}]/(\[Sigma]^2));
AppendTo[\[Chi]list, \[Chi]2];
Grid[{{
Show[
ListPlot[Around[#, \[Sigma]] & /@ data[[All, 2]] , PlotStyle -> Black, AxesLabel -> {"x", "y"}, LabelStyle -> {Bold, Black}, PlotLabel -> "f(x)=" Evaluate@Sum[Subscript[a, j] x^j, {j, 0, nparameters - 1}] , PlotRange -> {-10, 30}], Plot[fit, {x, 0, npoints}, PlotStyle -> {Red}, PlotRange -> All], ImageSize -> Medium]
,
Plot[PDF[ChiSquareDistribution[npoints - nparameters], x], {x, -0.1, (npoints - nparameters) + 5 Sqrt[2 (npoints - nparameters)]}, PlotRange -> All, PlotStyle -> {Black, Opacity[0]}, Prolog -> {Pink,
Table[Line[{{\[Chi]list[[j]], -5}, {\[Chi]list[[j]], 100}}], {j, 1, Dimensions[\[Chi]list][[1]]}], Red, Line[{{\[Chi]2, -5}, {\[Chi]2, 100}}]}, AxesLabel -> {"\!\(\*SuperscriptBox[\(\[Chi]\), \(2\)]\)", "P(\!\(\*SuperscriptBox[\(\[Chi]\), \(2\)]\))"}, LabelStyle -> {Bold, Black}, PlotLabel -> "\!\(\*SuperscriptBox[\(\[Chi]\), \(2\)]\)=\!\(\*SubscriptBox[\(\[Sum]\), \(j\)]\)\!\(\*FractionBox[SuperscriptBox[\((\*SubscriptBox[\(y\), \(j\)] - fit)\), \(2\)], SuperscriptBox[\(\[Sigma]\), \(2\)]]\)", ImageSize -> Medium]
}}]
, {75}];
frames2 = Table[
Grid[{{
Show[
ListPlot[Around[#, \[Sigma]] & /@ data[[All, 2]] , PlotStyle -> Black, AxesLabel -> {"x", "y"}, LabelStyle -> {Bold, Black}, PlotLabel -> "f(x)=" Evaluate@Sum[Subscript[a, j] x^j, {j, 0, nparameters - 1}] , PlotRange -> {-10, 30}], Plot[fit, {x, 0, npoints}, PlotStyle -> {Red}, PlotRange -> All], ImageSize -> Medium]
,
Plot[PDF[ChiSquareDistribution[npoints - nparameters], x], {x, -0.1, (npoints - nparameters) + 5 Sqrt[2 (npoints - nparameters)]}, PlotRange -> All, PlotStyle -> {Black, Opacity[\[Tau]]}, Filling -> Axis, FillingStyle -> {Opacity[\[Tau]/2]}, Prolog -> {Pink, Table[Line[{{\[Chi]list[[j]], -5}, {\[Chi]list[[j]], 100}}], {j, 1, Dimensions[\[Chi]list][[1]]}]}, AxesLabel -> {"\!\(\*SuperscriptBox[\(\[Chi]\), \(2\)]\)", "P(\!\(\*SuperscriptBox[\(\[Chi]\), \(2\)]\))"}, LabelStyle -> {Bold, Black}, PlotLabel -> "\!\(\*SuperscriptBox[\(\[Chi]\), \(2\)]\)=\!\(\*SubscriptBox[\(\[Sum]\), \(j\)])\!\(\*FractionBox[SuperscriptBox[\((\*SubscriptBox[\(y\), \(j\)] - fit)\), \(2\)], SuperscriptBox[\(\[Sigma]\), \(2\)]]\)", ImageSize -> Medium]
}}]
, {\[Tau], 0, 1, 0.1}];
ListAnimate[
Join[frames1, Table[frames1[[-1]], 5], frames2,
Table[frames2[[-1]], 20]]]
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
| 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.
|