Skip to content Skip to sidebar Skip to footer

Why Does "scipy.optimize.minimize" Gives Me Such A Bad Fit?

I have a function y(x,z) with two variables x, z and 6 coefficients a,b,c,d,e,f. I have the data for x ,z and let's say for testing purpose the data of the coefficients. With those

Solution 1:

Looking at res.fun, which is in your case around 1e-5 the fit is actually quite good.

Most likely you found a local mininum of your objective function. To better understand this behaviour, try the code below. This will generate different results for different startpoints. As you will see, you are minimizing, just not to the global minimum. To optimize globally, you have to use other approaches/methods. You could also increase the criteria for when to stop the optimization. Or use a hybrid approach and start at different initial points, solve the local minimization and take the best value.

for i in range(10):
    pars0 = np.random.rand(6) * 1
    res = minimize(resid, pars0, args=(x,z,y), method='cobyla',options={'maxiter': 5000000})
    print("a = %f , b = %f, c = %f, d = %f, e = %f, f = %f" % (res.x[0], res.x[1], res.x[2], res.x[3], res.x[4], res.x[5]))
    print(res.fun)

Try an inital point close to the solution you are seeking. This will most probably yield the global result. If you don't know the vague location of your solution, you may have to use a hybrid/global approach to the minimization.

For example, the initial point:

pars0 = np.array([1,0.5,0.25,1,0.5,0.25]) + np.random.rand(6)*0.01

yields a quite fitting solution.

Post a Comment for "Why Does "scipy.optimize.minimize" Gives Me Such A Bad Fit?"