Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

python - Why does setting loose bounds to scipy.optimize.curve_fit ruin my fit?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...