1

I'm attempting to fit a function using Scipy's Orthogonal distance regression (odr) package and I keep getting the following error:

"RuntimeWarning: invalid value encountered in power"

this happened when I would use scipy's curve_fit function but I could always safely ignore the warning. But now it seems this is causing a numerical error that halts the fitting. I have based my code off of the example I found here:

python scipy.odrpack.odr example (with sample input / output)?

Here is my code:

import numpy as np
import scipy.odr.odrpack as odrpack


def divergence(x,xDiv):
    return  ( 1 - (x/xDiv) )**( -2.4 )


xValues = np.linspace(.25,.37,12)
yValues = np.array([  6.94970607,   9.12475506,  10.65969954,  12.30241672,
        14.44154148,  16.00261267,  19.98693664,  25.93076421,
        30.89483997,  35.27106466,  50.81645983,  68.06009144])


xErrors = .0005*np.ones(len(xValues))
yErrors = np.array([ 0.31905094,  0.37956865,  0.24837562,  0.68320078,  1.25915789,
        1.40241088,  0.33305157,  1.37165251,  0.32658393,  0.52253429,
        1.04506858,  1.30633573])




wcModel = odrpack.Model(divergence)

mydata = odrpack.RealData(xValues, yValues, sx=xErrors, sy=yErrors)

myodr = odrpack.ODR(mydata, wcModel, beta0=[.8])


myoutput = myodr.run()
myoutput.pprint()

From looking at previous questions about this error I found here:

NumPy, RuntimeWarning: invalid value encountered in power

I suspected that the problem is that I'm raising a negatuve value to a power of a fractional value. But what I'm raising to the power -2.4 (1-x/xDiv) isn't negative (at least around the initial guess of xDiv=.8). But when I try to make my y-values of complex type I get a new error:

"ValueError: y could not be made into a suitable array"

from the line with the command

myoutput = myodr.run().

The only examples I can find that use this odr package are fitting to polynomials so I suspect that might be the problem?

toti08
  • 2,448
  • 5
  • 24
  • 36
  • I still get the same warning even if I replace -2.4 by 2.4. So I think negative power is not the issue. – Sheldore Sep 19 '18 at 17:40
  • Ah I miss typed, the conclusion of the link that I posted was "Numpy does not seem to allow fractional powers of negative numbers", so any fractional power negative are a problem. But what I'm exponentiating isn't negative (at least not around the intitial guess of xDiv=.8, so I'm confused. – Michael van der Naald Sep 19 '18 at 17:46

0 Answers0