4

I have a basic question about which optimization function I should use. I have:

  1. A model of my system implemented as a python function. This function takes a one dimensional array and outputs a one dimensional array, and also takes a handful of parameters (usually less than 10).

  2. Noisy sample output from that function with unknown input parameters.

  3. Lower and upper bounds on each of the parameters.

What I want:

An optimization routine that allows me to throw all of the above in and get best-fit function parameters out.

I would like to treat the function as a black box, so no assumptions on it being continuous.

I had some luck with the scipy.optimize.curve_fit function, however it seemed very susceptible to deviations in the initial guess. curve_fit also didn't let me provide bounds on the input, which I think would greatly simplify the problem. Here is the code I used with curve_fit:

import numpy as np
from scipy import optimize

def model(x, c, d):
    res = np.sqrt((d/2.0)**2 - (x-c)**2) + (d/2.0)
    res[np.isnan(res)] = 0
    return res

x = np.arange(-1000, 1000, 200)
y = model(x, 0, 250)
y_noise = y + np.random.rand(len(y)) * 20 - 10

popt, pcov = optimize.curve_fit(model, x, y_noise, (50, 200))

The above code returned (50, 200) as the optimized parameters, which is wrong. If I change the initial guess from (50, 200) to (49, 200) then the optimized parameters it returns are (1.84747802, 247.18634951), which is close enough to the truth of (0, 250)

Researching online seems to indicate that either RBF or least squares could help me. However I am having trouble posing my problem as something to be "minimized".

An example of calling my model above with the correct function would be greatly appreciated, as would general pointers to more reading that might help me.

Thanks in advance.

jminardi
  • 936
  • 1
  • 11
  • 23
  • 1
    This is not a problem in `curve_fit` but with numerical optimizations techniques in general. When optimizing non-convex functions you often end in suboptimal local extrema. There's no magical formula to avoid that. – cel Oct 01 '15 at 15:15
  • 2
    As @cel says, there is no magic bullet. You may wish to look at what your function looks like. Some times there are 'good' starting guesses that, while far from the real values, start the function down the 'easy' path to the correct answer. As a self-plug, you might benefit from my answer here: http://stackoverflow.com/a/28195068/3255247 – Jon Custer Oct 01 '15 at 19:30

0 Answers0