OxIonics / ionics_fits

Small python fitting library with an emphasis on Atomic Molecular and Optical Physics
Apache License 2.0
0 stars 0 forks source link

chi2 broken? #49

Closed hartytp closed 1 year ago

hartytp commented 1 year ago

For a reasonable dataset (see below) the chi2 is coming out as 0. Probably just doing something daft here as this code hasn't been used in anger.

Relevant code is https://github.com/OxIonics/ionics_fits/blob/4d62cc59f02cc23dfc35732b8efd94b0799bfc7d/ionics_fits/normal.py#L88

image

data is:

y_fit = [13316.89984055, 11955.4940811,  10594.08832166,  9232.68256222, 7871.27680278,  6509.87104334,  6620.99186415, 7982.39762359, 9343.80338303, 10705.20914247]

y [14400., 11933.33333333, 11133.33333333,  7600., 7200., 8000., 6600., 7933.33333333, 9466.66666667, 10666.66666667]

self.sigma = [0.06804138, 0.06194022, 0.059828,   0.04943092, 0.04811252, 0.05071505, 0.04606423, 0.0505033,  0.0551684,  0.0585607]

chi_2 =  2490244335.486552
p = 0.0

Casting an eye over that data, the value of self.sigma looks way too low here. Probably an error in the scaling code.

cc @saking2

hartytp commented 1 year ago

yep, I rekon the issue is that we should be setting self.sigma here https://github.com/OxIonics/ionics_fits/blob/4d62cc59f02cc23dfc35732b8efd94b0799bfc7d/ionics_fits/common.py#L439 rather than later on after the value has been scaled.

hartytp commented 1 year ago

Doing that gives me something more sensible

y_fit = [21160.09391833, 19039.49397351, 16918.89402869, 14798.29408387, 12677.69413905, 10557.09419423,  8436.49424941, 6315.89430459, 7070.83706396,  9191.43700878]

y = [21066.66666667, 20066.66666667, 18800., 14733.33333333, 11333.33333333, 9000., 8333.33333333, 7533.33333333, 7400.          8800.]

sigma = [1185.09258898, 1156.62343819, 1119.52370825, 991.07124982, 869.22698736, 774.59666924, 745.3559925, 708.67638752, 702.37691686, 765.94168621]
chi_2 = 13.50659585681905
p = 0.06068599499611331

That's more reasonable but the significance still feels a bit low. @saking2 would you mind casting an eye over the expression above and seeing if you can spot anything obviously wrong?