#1275 Fit reporting incorrect errors and reduced chisquare

closed-fixed
nobody
None
5
2015-03-23
2013-08-20
Kyle
No

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;
}

Discussion

  • Kyle

    Kyle - 2013-08-20
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,4 @@
    -I am running the current CVS version of gnuplot in Kubuntu version 13.04.
    +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.
    
     
  • Ethan Merritt

    Ethan Merritt - 2013-08-21
    • status: open --> pending-accepted
     
  • Ethan Merritt

    Ethan Merritt - 2013-08-21

    Thank you for the bug report and the analysis.
    You had me seriously worried for a bit, because the specific lines of code that you point to have been there for 25 years. While an unnoticed 25-year-old bug is conceivable, it seemed like there might be a very large number of unhappy people if turned out the code had been wrong all this time.

    But no, the code in those lines is not incorrect per se, just dangerous in that it corrupts the value of chisq after use. That danger bit us in April 2013, when the subsequent call to show_results() was introduced and the corrupted chisq passed to it. So I agree with your fix, and fortunately the error has not made it into any released version.

     
  • Ethan Merritt

    Ethan Merritt - 2013-08-21
    • status: pending-accepted --> closed-fixed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks