0

I'm trying to fit a set of points with error bars using the scipy.optimize function curve_fit. The file I use to read the input is something like

y           x           dy_1             dy_2
0.64        45.1        6.65E-004        1.20E-002
0.72        38.3        1.81E-004        3.93E-002
0.62        46.3        1.20E-004        6.65E-002
0.71        39.1        3.93E-004        6.65E-002
0.76        33.2        6.65E-004        1.81E-002
0.69        39.4        5.86E-003        0.58
0.76        33.5        6.65E-004        6.65E-002
0.79        28.5        1.27E-002        1.27

where dy_1 and dy_2 are two different uncertainties for y. The code I'm using to fit the data looks like

#!/usr/bin/env python
import numpy as np
from scipy.optimize import curve_fit
from scipy import stats
import inspect

## READ INPUT ##
array_fit=np.loadtxt(file.dat)
xdata=array_fit[1]
ydata=array_fit[0]
delta_y=array_fit[2] #or [3]

## FIT ##
print 'Fit with f(x)=ax+b'
def fit(x,a,b): 
     return a*x+b

popt,pcov=curve_fit(fit,xdata,ydata,p0=(0.0,0.0),sigma=delta_y) 
a=popt[0]; err_a=np.sqrt(pcov[0,0])
b=popt[1]; err_b=np.sqrt(pcov[1,1])
perr = np.sqrt(np.diag(pcov))
print 'a=',a,"+/-",err_a,' (',abs(err_a/a*100.0),'% )'
print 'b=',b,"+/-",err_b,' (',abs(err_b/b*100.0),'% )'

The output not including the error bars reads

a= -0.00964422694853 +/- 0.000497582672356  ( 5.15938369153 % )
b= 1.07794220116 +/- 0.0190839336114  ( 1.77040416368 % )

And I expected the deviation using the tiny error bars to be very small. My problem is not only that the difference between outputs is big but also that regardless if I use the tiny error bars or the big error bars I obtain the same output:

a= -0.0115247039688 +/- 0.000341896188408  ( 2.96663748877 % )
b= 1.15636325135 +/- 0.0148255830134  ( 1.28208700823 % )

It seems that my program is insensible to the dimension of the errors. Is that normal? Did I make some mistakes?

I asked a very similar question in

Wrong fit with error bars in Gnuplot

using gnuplot where I got the same results and the same problem. There also some plots are shown.

Matteo Lucca
  • 21
  • 1
  • 5
  • Do you scale all errors equally? Because then, yes, the fit is insensible to variation in the errors: the relative weights (set by the errors) is the same whether your errors would be e.g. 10, 10, 100, 200, or if they are 1, 1, 10, 20. –  Sep 09 '17 at 10:25
  • Looking at dy_1 and dy_2, they are *nearly* scaled equally (by a factor of 100), so I expect the fit to be similar, but not exactly the same. And your values of `a` and `b` show this (have you made a figure that shows the two fits in the same graph?). Hence I don't understand why you say the results are the same. –  Sep 09 '17 at 10:27
  • That a and b should remain the same is clear to me and it is good so. My question was more related to the uncertainties of a and b in the output. I thought that by adding tiny error bars, a and b would have remained nearly the same but the errors would have increased. And the bigger the error bars the bigger the errors in a and b. The problem is that the errors are always the same. – Matteo Lucca Sep 09 '17 at 10:38
  • Your input (data) errors are *weights*: they don't "scale" `a` and `b`. The uncertainties in `a` and `b` are the accuracy of the fit, they don't represent what the input errors are. –  Sep 09 '17 at 10:41
  • Compare to the weighted mean and its weighted standard deviation; for the latter, the errors again acts as weights and "fall out" of the equation, due to a division by the sum of the weights (you can look up the formula and do a very simple calculation yourself: once with small errors, once where all errors are a factor 1000 or so larger). For the weighted mean, this is the error in the mean: it is *not* about the spread and uncertainty in the input values. –  Sep 09 '17 at 10:50

0 Answers0