1

I'm trying to use Scipy's ODR to fit various different curves to data. These curves have to be given as an ODR Model, which is defined by a function. This function has two arguments: p and x. p is a list of the parameters that will be optimised. Example:

def f(p, x):
  m, c = p
  return m*x + c

model = Model(f)
data = RealData(xdata, ydata)
odr_setup = ODR(data, model, beta0=[0,0], partol=0.001)
odr_result = odr_setup.run()

My problem: the length of p depends on the function I am fitting. If the length of beta0 and p are not the same, it gives a ValueError. To get round this, I have constructed some nested try-except statements to get the matching length. Is there a more elegant way of achieving the same thing?

Ideally I would like something like ODR(data, model, ***beta0=[0]*n_params***, partol=0.001). How would I find n_params? Or is there a better way?

Thanks!

Alex_vT
  • 21
  • 2
  • Can you be more clear on how n_params depends on p? It isn't just ```len(p)``` if that is the case ```beta=[0] * len(p)``` – dosas Oct 21 '20 at 12:17
  • Basically I am looking for len(p), yes. The problem is that I don't know how to access p from just having the function. There are different functions with different lengths of p being passed around. As far as I know, the only way to tell the length of p would be to go inside the function and see p get unpacked... how? – Alex_vT Oct 21 '20 at 12:57

0 Answers0