I currently want to fit data with errors in x and y and im using the scipy.odr package to get my results. Im just wondering about the correct use of errors sx and sy. So here is an example.
Lets assume im measuring a voltage V for different currents I. So I have measured 1V with an error of +- 0.1V and so on. So if I'm assuming no error in current measurement I could use spipy.curve_fit as follows. Absolute_sigma is set True because my absolute error of +- 0.1V.
So I'm getting:
from scipy.optimize import curve_fit
y=[1,2.5,3,4]
x=[1,2,3,4]
yerr=[0.1,0.1,0.1,0.1]
def func(x, a, b):
return a*x+b
popt, pcov = curve_fit(func, x, y,sigma=yerr,absolute_sigma=True)
print(popt)
print(np.sqrt(np.diag(pcov)))
[0.95 0.25]
[0.04472136 0.12247449]
In a second step I want to use the odr-package with errors in both, current and voltage. According to the documentation it should be used as follows: sx and sy are the errors for my measurement data. So I should assume to get similar results to curve_fit if I'm using a very small error for sx.
from scipy.odr import *
x_err = [0.0000000001]*x.__len__()
y_err = yerr
def linear_func(p, x):
m, c = p
return m*x+c
linear_model = Model(linear_func)
data = RealData(x, y, sx=x_err, sy=y_err)
odr = ODR(data, linear_model, beta0=[0.4, 0.4])
out = odr.run()
out.pprint()
Beta: [0.94999996 0.24999994]
Beta Std Error: [0.13228754 0.36228459]
But as you can see, the result is different from the curve_fit above with absolute_sigma=True.
Using the same data with curve_fit and absolute_sigma=False leads to the same results as the ODR-Fit .
popt, pcov = curve_fit(func, x, y,sigma=yerr,absolute_sigma=False)
print(popt)
print(np.sqrt(np.diag(pcov)))
[0.95 0.25]
[0.13228756 0.36228442]
So I guess the ODR-Fit is not really taking care of my absolute errors as curve_fit and absolute_sigma=True does. Is there any way to do that or am I missing something?