Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelResult.fit() does not use provided params #960

Closed
Trayder opened this issue Jul 2, 2024 · 1 comment · Fixed by #961
Closed

ModelResult.fit() does not use provided params #960

Trayder opened this issue Jul 2, 2024 · 1 comment · Fixed by #961

Comments

@Trayder
Copy link

Trayder commented Jul 2, 2024

First Time Issue Code

Yes, I read the instructions and I am sure this is a GitHub Issue.

Description

When using ModelResult.fit() the provided params are not being used as the initial values.
In the below example, result_leastsq.params shows that result_brute.params was used as the initial values rather than the params provided.
result_leastsq.init_params does report the correct values.

A Minimal, Complete, and Verifiable example
import numpy as np
import lmfit
import copy

x = np.linspace(-10, 10, 100)
y = 3 * x**2 - 2 * x + 1 + np.random.normal(scale=10, size=x.size)

def quadratic(x, a, b, c):
    return a * x**2 + b * x + c

model = lmfit.Model(quadratic)
params = model.make_params(a=dict(value=1, min=-10, max=10),
                           b=dict(value=0, min=-10, max=10),
                           c=dict(value=0, min=-10, max=10),)
result_brute = model.fit(y, params, x=x, method='brute')
result_leastsq = copy.deepcopy(result_brute)
result_leastsq.fit(method='leastsq',params=result_brute.candidates[49].params)
Version information

Python: 3.11.9 (main, Apr 19 2024, 16:48:06) [GCC 11.2.0]

lmfit: 1.3.1, scipy: 1.14.0, numpy: 2.0.0,asteval: 1.0.0, uncertainties: 3.2.1

@newville
Copy link
Member

newville commented Jul 2, 2024

@Trayder I think I see the problem here... or at least "a problem". ModelResult.fit() to re-fit is not using the passed in parameters uniformly as initial values. I think that is probably what you are reporting, and will try to push a fix to that soon.

To be clear, I think that is the problem you were reporting, but you were not specific. In the future and for any such reports, explicitly state what you expected to get and what you actually got. For lmfit, including a fit report is highly recommended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants