2

I would like to use scipy's ODR to fit a curve to a set of variables with variances. In this case, I am fitting a linear function with a set Y axis crossing point (e.g. a*x+100). Due to my inability to find an estimator (I asked about that here), I am using scipy.optimize curve_fit to estimate the initial a value. Now, the function works perfectly without standard deviation, but when I add it, the output makes completely no sense (the curve is far above all of the points). What could be the case of such behaviour? Thanks! The code is attached here:

import scipy.odr as SODR
from scipy.optimize import curve_fit

def fun(kier, arg):
    '''
    Function to fit
    :param kier: list of parameters
    :param arg: argument of the function fun
    :return y: value of function in x
    '''

    y =kier[0]*arg +100 #+kier[1]
    return y

zx       = [30.120348566300354, 36.218214083626386, 52.86998374096616]
zy       = [83.47033171149137, 129.10207165602722, 85.59465198231146]
dx       = [2.537935346025827, 4.918719773247683, 2.5477221183398977]
dy       = [3.3729431749276837, 5.33696690247701, 2.0937213187876]
sx       = [6.605581618516947, 8.221194790372632, 22.980577676739113]
sy       = [1.0936584882351936, 0.7749999999999986, 20.915359045447914]
dx_total = [9.143516964542775, 13.139914563620316, 25.52829979507901]
dy_total = [4.466601663162877, 6.1119669024770085, 23.009080364235516]

# curve fitting
popt, pcov = curve_fit(fun, zx, zy)
danesd     = SODR.RealData(x=zx, y=zy, sx=dx_total, sy=dy_total)
model      = SODR.Model(fun)
onbig      = SODR.ODR(danesd, model, beta0=[popt[0]])
outputbig  = onbig.run()
biga=outputbig.beta[0]
print(biga)

daned   = SODR.RealData(x=zx,y=zy,sx=dx,sy=dy)
on      = SODR.ODR(daned, model, beta0=[popt[0]])
outputd = on.run()
normala = outputd.beta[0]
print(normala)

The outputs are:

30.926925885047254 (this is the output with standard deviation)  
-0.25132703671513873 (this is without standard deviation) 

This makes no sense, as shown here: This is the plot showing the estimation from curve fit (no error), and fits without and with standard deviation

Also, I'd be happy to get any feedback whether my code is clear and the formatting of this question. I am still very new here.

Community
  • 1
  • 1
PKK
  • 31
  • 4
  • 1
    yep...seems strange.... did you consider https://www.scipy.org/scipylib/bug-report.html ??? – mikuszefski May 12 '20 at 06:28
  • Thanks DrBwts! Looks much better! – PKK May 13 '20 at 13:07
  • I have submitted a bug report to the scipy Github. They might be able to help me there, thanks! The question remains still unanswered. – PKK May 13 '20 at 16:13
  • 1
    True... you might want to look [here](https://doi.org/10.1088/0957-0233/18/11/025), [here](https://stackoverflow.com/a/45185859/803359), or even [here](https://stackoverflow.com/a/46904786/803359). – mikuszefski May 14 '20 at 06:16
  • Hey! I am still looking for a solution. I will be trying to use @mikuszefski method in the meantime. Thanks! – PKK Oct 08 '20 at 17:45

0 Answers0