How to properly calculate Chi Square
Dear Forum,
I have two cases where I am trying to calculate goodness of a fit (chi square).
Case1: I have my data points and the fit function si f(x)=1/(x+b)^c where b and c are fit parameters. In first case, I do 'Curve Fitting' and in 'Data Options' i use 'None' for weighting. As you see in image attached, the first point of the fit is not exactly on first data point and Chi Square is small (0.16337),
Case2: Again I have the same data points and fit function, but this time in 'Data Options' I select error bars for each data point. In this case first points of fit and data points coincide, but the Chi Square is very Large (388.21).
I'm curious how does igor calculate chi square?, and why in one case is very different than the other?
Thank you very much for your help and comments.
John will likely add something but have a look at:
DisplayHelptopic "Overview of Curve Fitting"
You can manually calculate ChiSquare using something like
MatrixOP/O chisquare = sum(magsqr((fit_yy - yy) * rec(sdev_yy)))
I believe if you do not specify an uncertainty of yy, it is taken to be 1. If the uncertainty of yy is given the numeric values of chisquare can be drastically different.
August 4, 2020 at 03:49 am - Permalink
As already noted by ChrLie, the ChiSquare value takes the error or standard deviation into account for the calculation. Naturally, if you provide an error yourself instead of the default value your result will be very different. By the way, your 'model' does not seem to fit your data very well. This makes the ChiSquare a bit meaningless either way, since obviously your 'hypothesis' is far off from the start.
August 4, 2020 at 04:12 am - Permalink
Armoh-
Both ChrLie and chozo are correct. When Igor computes chi-square during a fit, the equation is this:
chi-square = sum_over_i( ((y_hat_i - y_i)/sigma_i)^2 )
y_hat_i is the model value for data point i
y_i is data point i
sigma_i is either 1.0 or your weighting value in standard deviation terms
So, as you can see here, if you provide weighting, the chi-square can be quite different from what it is if you don't. Note also that in both cases, there is no division by N or degrees of freedom. That is a bit different from some other applications like Excel. It is pretty easy to compute that yourself :)
When the uncertainties for the coefficients are computed, Igor takes into account whether you provided weighting values. If you do, a rule of thumb is that chi-square should be about the number of data points. If it is much larger than that, then you are underestimating the uncertainty or your model doesn't fit very well.
August 4, 2020 at 10:56 am - Permalink