I am running the current CVS version of gnuplot (4.7) in Kubuntu version 13.04.

Myself and a co-worker were getting different results for the error and chisquared for the same data set when fitting to a simple constant function, however the fit result was correct. After checking the chisquared by hand, I found that gnuplot was wrong and the co-worker's proprietary package was reporting the correct value. I then scoured through the fit.c code and found the cause.

Gnuplot was correctly reporting the chisq in this section of the fit output:

iter chisq delta/lim lambda tau_bottle

0 7.2507863316e+01 0.00e+00 7.27e-01 8.845934e+02

1 8.3875598263e+00 -7.64e+15 7.27e-02 8.804305e+02

2 6.5555584540e+00 -2.79e+14 7.27e-03 8.795996e+02

3 6.5555511552e+00 -1.11e+09 7.27e-04 8.795980e+02

4 6.5555511552e+00 -5.42e-01 7.27e-05 8.795980e+02

iter chisq delta/lim lambda tau_bottle

It is also evaluating the correct reduced chisquared in the code near line 820. However, the following section of code (near line 870) was redefining the chisq as it scaled the fit errors and this redefined chisquared value is used again later in a show_results call where the reduced chisquared is re-evaluated.

if ((fit_errorscaling) || (columns < 3)) {
/* scale parameter errors based on chisq */
chisq = sqrt(chisq / (num_data - num_params));
for (i = 0; i < num_params; i++)
dpar[i] *= chisq;
}

In other words, the reduced chisquare that it is reporting is actually sqrt(sqrt(chisq/ndf)/ndf), rather than sqrt(chisq/ndf).

I replaced it with the following, and gnuplot is now in agreement with the other result and the correct chisquared, reduced chisquared, and errors.

if ((fit_errorscaling) || (columns < 3)) {
/* scale parameter errors based on chisq */
double tchisq = sqrt(chisq / (num_data - num_params));
for (i = 0; i < num_params && tchisq > 1; i++)
dpar[i] *= tchisq;
}