I am trying to fit an equation of 6 fitparams to my 2D dataset. Since 6 is quite a lot, I would like to include some limits and a good initial guess to help the algorithm out.
However, just adding a single bound somehow causes my fit to collapse to its first guess: none of the fitparameters change at all. Here's what my code looks like:
p0 = (A0, a0, b0, L0, Lp0, x00, xR0) # b0 -> 7.1e-13
bounds = (
# A, a, b, L, Lp, x0 , xR
(-np.inf, -np.inf, -np.inf, -np.inf, -np.inf, -np.inf, -np.inf),
( np.inf, np.inf, 1, np.inf, np.inf, +np.inf, np.inf)
)
popt, _ = curve_fit(f, x, y, p0=p0, bounds=bounds, maxfev=30000)
Here I show my attempt at fixing the upper-bound value of b. The result is that popt remains at p0.
Setting the upper bound back to np.inf completely fixes the problem, and I get a nice fit.
What am I doing wrong here? I would really still like to add bounds, even if I get a nice fit for my testdata.
So far I have tried making sure that p0 falls within the bounds, and that the bounds are chosen so broad that it shouldn't actually affect the fitting procedure.
The fit-function I am using is not the simplest, but I will place it here anyway:
def f(x, A, a, b, L, Lp, x0, xR):
r = x - x0
regime1 = np.where(x < x0)
regime2 = np.where(x <= xR)
y = a*r + b
y[regime1] += A * r[regime1]**2
y[regime2] -= WLC(r[regime2], L, Lp)
return y
Here WLC is the worm-like chain function
I'm hoping anyone can help me out here :)
Thank you
question from:
https://stackoverflow.com/questions/65831097/why-does-setting-loose-bounds-to-scipy-optimize-curve-fit-ruin-my-fit